summaryrefslogtreecommitdiff
path: root/test/integration/dispatcher_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/integration/dispatcher_test.rb')
-rw-r--r--test/integration/dispatcher_test.rb120
1 files changed, 92 insertions, 28 deletions
diff --git a/test/integration/dispatcher_test.rb b/test/integration/dispatcher_test.rb
index 60ad9d7..4f13e6b 100644
--- a/test/integration/dispatcher_test.rb
+++ b/test/integration/dispatcher_test.rb
@@ -1,48 +1,80 @@
require 'test_helper'
require 'nickserver/dispatcher'
+#
+# Test integration between the Dispatcher and the RequestHandlers
+#
+# Starting from a given request we test the interaction between the dispatcher
+# and the different RequestHandlers. There's a lot of combinations possible
+# and we only test a couple of them to ensure the parts work together well.
+#
+# This does not test the server. We stub and mock the sources. The nickserver
+# integration test covers these as well.
+#
+
class Nickserver::DispatcherTest < Minitest::Test
def test_empty_query
handle
- assert_response status: 404, content: "404 Not Found\n"
+ assert_response not_found
end
def test_invalid_query
handle address: ['asdf']
- assert_response status: 500, content: "500 Not a valid address\n"
+ assert_response error('Not a valid address')
+ end
+
+ def test_fingerprint_to_short
+ handle fingerprint: ['44F2F455E28']
+ assert_response error("Fingerprint invalid: 44F2F455E28")
+ end
+
+ def test_fingerprint_is_not_hex
+ handle fingerprint: ['X36E738D69173C13Z709E44F2F455E2824D18DDX']
+ assert_response error("Fingerprint invalid: X36E738D69173C13Z709E44F2F455E2824D18DDX")
end
def test_missing_domain
handle address: ['valid@email.tld']
- assert_response_from_hkp
+ stub_nicknym_not_available
+ hkp_source.expect :query, success, [Nickserver::EmailAddress]
+ assert_response success
end
- def test_email_from_hkp
+ def test_email_via_hkp
handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" }
- assert_response_from_hkp
+ stub_nicknym_not_available
+ hkp_source.expect :query, success, [Nickserver::EmailAddress]
+ assert_response success
end
- def test_fingerprint_to_short
- handle fingerprint: ['44F2F455E28']
- assert_response status: 500, content: "500 Fingerprint invalid: 44F2F455E28\n"
+ def test_email_via_hkp_nicknym_unreachable
+ handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" }
+ stub_nicknym_raises
+ hkp_source.expect :query, success, [Nickserver::EmailAddress]
+ assert_response success
end
- def test_fingerprint_is_not_hex
- handle fingerprint: ['X36E738D69173C13Z709E44F2F455E2824D18DDX']
- assert_response status: 500,
- content: "500 Fingerprint invalid: X36E738D69173C13Z709E44F2F455E2824D18DDX\n"
+ def test_email_via_hkp_nicknym_unreachable
+ handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" }
+ stub_nicknym_raises
+ hkp_source.expect :query, nil, [Nickserver::EmailAddress]
+ assert_response response(status: 502, content: "HTTP::ConnectionError")
+ end
+
+ def test_email_via_nicknym
+ handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" }
+ nicknym_source.expect :available_for?, true, [String]
+ nicknym_source.expect :query, success, [Nickserver::EmailAddress]
+ assert_response success
end
- def test_get_key_with_fingerprint_from_hkp
+ def test_get_key_with_fingerprint
handle fingerprint: ['E36E738D69173C13D709E44F2F455E2824D18DDF']
- source = Minitest::Mock.new
- source.expect :get_key_by_fingerprint,
- Nickserver::Response.new(200, "fake key response"),
+ stub_nicknym_not_available
+ hkp_source.expect :get_key_by_fingerprint, success,
['E36E738D69173C13D709E44F2F455E2824D18DDF']
- Nickserver::Hkp::Source.stub :new, source do
- assert_response status: 200, content: "fake key response"
- end
+ assert_response success
end
protected
@@ -52,20 +84,52 @@ class Nickserver::DispatcherTest < Minitest::Test
@params = Hash[ params.map{ |k,v| [k.to_s, v] } ]
end
- def assert_response(args)
- responder.expect :respond, nil, [args[:status], args[:content]]
- dispatcher.respond_to @params, @headers
- responder.verify
+ def assert_response(response)
+ Nickserver::Nicknym::Source.stub :new, nicknym_source do
+ Nickserver::Hkp::Source.stub :new, hkp_source do
+ responder.expect :respond, nil, [response.status, response.content]
+ dispatcher.respond_to @params, @headers
+ responder.verify
+ end
+ end
end
- def assert_response_from_hkp
- source = Minitest::Mock.new
- source.expect :query, Nickserver::Response.new(200, "fake content"), [Nickserver::EmailAddress]
- Nickserver::Hkp::Source.stub :new, source do
- assert_response status: 200, content: "fake content"
+ def hkp_source
+ @hkp_source ||= Minitest::Mock.new
+ end
+
+ def stub_nicknym_not_available
+ def nicknym_source.available_for?(*_args)
+ false
end
end
+ def stub_nicknym_raises
+ def nicknym_source.available_for?(*_args)
+ raise HTTP::ConnectionError
+ end
+ end
+
+ def nicknym_source
+ @nicknym_source ||= Minitest::Mock.new
+ end
+
+ def success
+ response status: 200, content: "fake content"
+ end
+
+ def not_found
+ response status: 404, content: "404 Not Found\n"
+ end
+
+ def error(msg)
+ response status: 500, content: "500 #{msg}\n"
+ end
+
+ def response(options)
+ Nickserver::Response.new(options[:status], options[:content])
+ end
+
def dispatcher
Nickserver::Dispatcher.new responder
end