validate NodeInfo json against schema in ConnectionTester
This commit is contained in:
parent
da1a02892d
commit
2a0e87957f
2 changed files with 21 additions and 3 deletions
|
|
@ -129,6 +129,8 @@ class ConnectionTester
|
|||
nd_resp = http.get(find_nodeinfo_url(ni_resp.body))
|
||||
find_software_version(nd_resp.body)
|
||||
end
|
||||
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError => e
|
||||
raise NodeInfoFailure, "#{e.class}: #{e.message}"
|
||||
rescue Faraday::ResourceNotFound, JSON::JSONError => e
|
||||
raise NodeInfoFailure, e.message[0..255].encode(Encoding.default_external, undef: :replace)
|
||||
rescue StandardError => e
|
||||
|
|
@ -190,6 +192,7 @@ class ConnectionTester
|
|||
# walk the JSON document, find the version string
|
||||
def find_software_version(body)
|
||||
info = JSON.parse(body)
|
||||
JSON::Validator.validate!(NodeInfo.schema("1.0"), info)
|
||||
sw = info.fetch("software")
|
||||
@result.software_version = "#{sw.fetch('name')} #{sw.fetch('version')}"
|
||||
end
|
||||
|
|
|
|||
|
|
@ -113,13 +113,21 @@ describe ConnectionTester do
|
|||
end
|
||||
|
||||
describe "#nodeinfo" do
|
||||
let(:ni_wellknown) { {links: [{rel: ConnectionTester::NODEINFO_SCHEMA, href: "/nodeinfo"}]} }
|
||||
|
||||
it "reads the version from the nodeinfo document" do
|
||||
ni_wellknown = {links: [{rel: ConnectionTester::NODEINFO_SCHEMA, href: "/nodeinfo"}]}
|
||||
ni_document = {software: {name: "diaspora", version: "a.b.c.d"}}
|
||||
ni_document = NodeInfo.build do |doc|
|
||||
doc.version = "1.0"
|
||||
doc.open_registrations = true
|
||||
doc.protocols.inbound << "diaspora"
|
||||
doc.protocols.outbound << "diaspora"
|
||||
doc.software.name = "diaspora"
|
||||
doc.software.version = "a.b.c.d"
|
||||
end
|
||||
|
||||
stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}")
|
||||
.to_return(status: 200, body: JSON.generate(ni_wellknown))
|
||||
stub_request(:get, "#{url}/nodeinfo").to_return(status: 200, body: JSON.generate(ni_document))
|
||||
stub_request(:get, "#{url}/nodeinfo").to_return(status: 200, body: JSON.generate(ni_document.as_json))
|
||||
|
||||
tester.nodeinfo
|
||||
expect(result.software_version).to eq("diaspora a.b.c.d")
|
||||
|
|
@ -136,5 +144,12 @@ describe ConnectionTester do
|
|||
.to_return(status: 200, body: '{"json"::::"malformed"}')
|
||||
expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure)
|
||||
end
|
||||
|
||||
it "handles a invalid nodeinfo document gracefully" do
|
||||
stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}")
|
||||
.to_return(status: 200, body: JSON.generate(ni_wellknown))
|
||||
stub_request(:get, "#{url}/nodeinfo").to_return(status: 200, body: '{"software": "invalid nodeinfo"}')
|
||||
expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue