diff options
author | azul <azul@riseup.net> | 2016-08-29 10:19:22 +0000 |
---|---|---|
committer | azul <azul@riseup.net> | 2016-08-29 10:19:22 +0000 |
commit | 6e2d31e3f7c515f65d92533bcdb035438461a00c (patch) | |
tree | 4efa7445db3a0521a14d75e626d64f85434a3ea5 /test/unit | |
parent | c134e0940a44ba3fb3f0f8ee86faa8053a9e0b44 (diff) | |
parent | 0784391a21b75ca52892e992a614b0f927ade00e (diff) |
Merge branch 'refactor/request-handling' into 'master'
refactor: restructure the way we handle requests to make it more consistent.
Requests are handled at a lot of different ways in different styles right now.
Let's make this more consistent and flexible to add email lookup at other leap providers.
See merge request !2
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/invalid_source_test.rb | 16 | ||||
-rw-r--r-- | test/unit/lookup_test.rb | 23 | ||||
-rw-r--r-- | test/unit/request_handler_test.rb | 76 | ||||
-rw-r--r-- | test/unit/request_handlers/local_email_handler_test.rb | 65 | ||||
-rw-r--r-- | test/unit/request_test.rb | 44 |
5 files changed, 109 insertions, 115 deletions
diff --git a/test/unit/invalid_source_test.rb b/test/unit/invalid_source_test.rb deleted file mode 100644 index 37a38fc..0000000 --- a/test/unit/invalid_source_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'test_helper' -require 'nickserver/invalid_source' - -class Nickserver::InvalidSourceTest < Minitest::Test - - def test_query - adapter.query(nil) do |status, content| - assert_equal 500, status - assert_equal "Not a valid address", content - end - end - - def adapter - Nickserver::InvalidSource.new - end -end diff --git a/test/unit/lookup_test.rb b/test/unit/lookup_test.rb deleted file mode 100644 index ac827e0..0000000 --- a/test/unit/lookup_test.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'test_helper' -require 'minitest/mock' -require 'nickserver/lookup' - -class TestLookup < Nickserver::Lookup - - def query - yield 200, 'yeah' - end - -end - -class LookupTest < Minitest::Test - - def test_responding - responder = Minitest::Mock.new - responder.expect :send_response, nil, - [{status: 200, content: 'yeah'}] - lookup = TestLookup.new nil - lookup.respond_with responder - responder.verify - end -end diff --git a/test/unit/request_handler_test.rb b/test/unit/request_handler_test.rb deleted file mode 100644 index 6c7a036..0000000 --- a/test/unit/request_handler_test.rb +++ /dev/null @@ -1,76 +0,0 @@ -require 'test_helper' -require 'nickserver/request_handler' - -class Nickserver::RequestHandlerTest < Minitest::Test - - def test_empty_query - handle - assert_response status: 404, content: "404 Not Found\n" - end - - def test_invalid_query - handle address: ['asdf'] - assert_response status: 500, content: "500 Not a valid address\n" - end - - def test_missing_domain - handle address: ['valid@email.tld'] - assert_response status: 500, content: "500 HTTP request must include a Host header.\n" - end - - def test_email_from_hkp - handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" } - 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: "200 fake content" - end - end - - def test_fingerprint_to_short - handle fingerprint: ['44F2F455E28'] - assert_response status: 500, content: "500 Fingerprint invalid: 44F2F455E28\n" - end - - def test_fingerprint_is_not_hex - handle fingerprint: ['X36E738D69173C13Z709E44F2F455E2824D18DDX'] - assert_response status: 500, - content: "500 Fingerprint invalid: X36E738D69173C13Z709E44F2F455E2824D18DDX\n" - end - - def test_get_key_with_fingerprint_from_hkp - handle fingerprint: ['E36E738D69173C13D709E44F2F455E2824D18DDF'] - source = Minitest::Mock.new - source.expect :get_key_by_fingerprint, - Nickserver::Response.new(200, "fake fingerprint"), - ['E36E738D69173C13D709E44F2F455E2824D18DDF'] - Nickserver::Hkp::Source.stub :new, source do - assert_response status: 200, content: "200 fake fingerprint" - end - end - - protected - - def handle(params = {}) - @headers = params.delete(:headers) || {} - @params = Hash[ params.map{ |k,v| [k.to_s, v] } ] - end - - def assert_response(args) - responder.expect :respond, nil, [args[:status], args[:content]] - handler.respond_to @params, @headers - responder.verify - end - - def handler - Nickserver::RequestHandler.new responder, adapter - end - - def responder - @responder ||= Minitest::Mock.new - end - - def adapter - @adapter ||= Minitest::Mock.new - end -end diff --git a/test/unit/request_handlers/local_email_handler_test.rb b/test/unit/request_handlers/local_email_handler_test.rb new file mode 100644 index 0000000..8f303ec --- /dev/null +++ b/test/unit/request_handlers/local_email_handler_test.rb @@ -0,0 +1,65 @@ +require 'test_helper' +require 'nickserver/request_handlers/local_email_handler' + +class LocalEmailHandlerTest < MiniTest::Test + + def test_no_email + assert_refuses + end + + def test_remote_email + assert_refuses email: 'me@remote.tld', domain: 'local.tld' + end + + def test_local_email + assert_handles email: 'me@local.tld', domain: 'local.tld' + end + + def test_missing_host_header + Nickserver::Config.stub :domain, nil do + assert_responds_with_error "HTTP request must include a Host header.", + email: 'me@local.tld' + end + end + + protected + + def handler + Nickserver::RequestHandlers::LocalEmailHandler.new + end + + def source + source = Minitest::Mock.new + source.expect :query, + 'response', + [Nickserver::EmailAddress] + source + end + + def assert_handles(opts) + Nickserver::CouchDB::Source.stub :new, source do + assert_equal 'response', handle(request(opts)) + end + end + + def assert_responds_with_error(msg, opts) + response = handle(request(opts)) + assert_equal 500, response.status + assert_equal "500 #{msg}\n", response.content + end + + def assert_refuses(opts = {}) + assert_nil handle(request(opts)) + end + + def handle(request) + handler.call(request) + end + + def request(opts = {}) + params = {'address' => [opts[:email]]} + headers = {'Host' => opts[:domain]} + Nickserver::Request.new params, headers + end + +end diff --git a/test/unit/request_test.rb b/test/unit/request_test.rb new file mode 100644 index 0000000..698a275 --- /dev/null +++ b/test/unit/request_test.rb @@ -0,0 +1,44 @@ +require 'test_helper' +require 'nickserver/request' + +class Nickserver::RequestTest < Minitest::Test + + def test_email + request = request_with_params address: fake_email + assert_equal fake_email, request.email + end + + def test_blank_email + request = request_with_params + assert_equal nil, request.email + end + + def test_fingerprint + request = request_with_params fingerprint: fake_fingerprint + assert_equal fake_fingerprint, request.fingerprint + end + + def test_domain + request = Nickserver::Request.new Hash.new, + 'Host' => ' nicknym.my.domain.tld:123' + assert_equal 'my.domain.tld', request.domain + end + + protected + + # params are encoded with strings as keys and arrays with the + # given value(s) + def request_with_params(params = {}) + params = params.collect{|k,v| [k.to_s, Array(v)]}.to_h + Nickserver::Request.new params, {} + end + + def fake_email + 'test@domain.tld' + end + + def fake_fingerprint + 'F' * 40 + end + +end |