diff options
-rw-r--r-- | lib/nickserver/dispatcher.rb | 4 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/email_handler.rb | 40 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/invalid_email_handler.rb | 15 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/local_email_handler.rb | 32 | ||||
-rw-r--r-- | test/integration/dispatcher_test.rb (renamed from test/unit/dispatcher_test.rb) | 0 | ||||
-rw-r--r-- | test/unit/request_handlers/local_email_handler_test.rb | 65 |
6 files changed, 120 insertions, 36 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb index 833a2ad..8bcfd05 100644 --- a/lib/nickserver/dispatcher.rb +++ b/lib/nickserver/dispatcher.rb @@ -14,6 +14,8 @@ # require 'nickserver/request' +require 'nickserver/request_handlers/invalid_email_handler' +require 'nickserver/request_handlers/local_email_handler' require 'nickserver/request_handlers/email_handler' require 'nickserver/request_handlers/fingerprint_handler' @@ -45,6 +47,8 @@ module Nickserver def handler_chain [ + RequestHandlers::InvalidEmailHandler.new, + RequestHandlers::LocalEmailHandler.new, RequestHandlers::EmailHandler.new, RequestHandlers::FingerprintHandler.new, Proc.new { Nickserver::Response.new(404, "Not Found\n") } diff --git a/lib/nickserver/request_handlers/email_handler.rb b/lib/nickserver/request_handlers/email_handler.rb index b163b27..96fcaf3 100644 --- a/lib/nickserver/request_handlers/email_handler.rb +++ b/lib/nickserver/request_handlers/email_handler.rb @@ -1,7 +1,5 @@ require 'nickserver/email_address' -require 'nickserver/error_response' require 'nickserver/hkp/source' -require 'nickserver/couch_db/source' module Nickserver module RequestHandlers @@ -9,46 +7,16 @@ module Nickserver def call(request) return unless request.email - handle_request(request) - end - - protected - - def handle_request(request) email = EmailAddress.new(request.email) - if email.invalid? - ErrorResponse.new("Not a valid address") - else - send_key(email, request) - end - end - - def send_key(email, request) - if local_address?(email, request) - source = Nickserver::CouchDB::Source.new - else - source = Nickserver::Hkp::Source.new - end source.query(email) - rescue MissingHostHeader - ErrorResponse.new("HTTP request must include a Host header.") end - # - # Return true if the user address is for a user of this service provider. - # e.g. if the provider is example.org, then alice@example.org returns true. - # - # If 'domain' is not configured, we rely on the Host header of the HTTP request. - # - def local_address?(email, request) - domain = Config.domain || request.domain - raise MissingHostHeader if domain == '' - email.domain? domain - end - end + protected + def source + Nickserver::Hkp::Source.new + end - class MissingHostHeader < StandardError end end end diff --git a/lib/nickserver/request_handlers/invalid_email_handler.rb b/lib/nickserver/request_handlers/invalid_email_handler.rb new file mode 100644 index 0000000..eaf8156 --- /dev/null +++ b/lib/nickserver/request_handlers/invalid_email_handler.rb @@ -0,0 +1,15 @@ +require 'nickserver/email_address' +require 'nickserver/error_response' + +module Nickserver + module RequestHandlers + class InvalidEmailHandler + def call(request) + return unless request.email + email = EmailAddress.new(request.email) + ErrorResponse.new("Not a valid address") if email.invalid? + end + + end + end +end diff --git a/lib/nickserver/request_handlers/local_email_handler.rb b/lib/nickserver/request_handlers/local_email_handler.rb new file mode 100644 index 0000000..1f2abc2 --- /dev/null +++ b/lib/nickserver/request_handlers/local_email_handler.rb @@ -0,0 +1,32 @@ +require 'nickserver/email_address' +require 'nickserver/error_response' +require 'nickserver/couch_db/source' + +module Nickserver + module RequestHandlers + class LocalEmailHandler + + def call(request) + return nil unless request.email + domain = Config.domain || request.domain + return missing_domain_response if domain.nil? || domain == '' + email = EmailAddress.new(request.email) + return nil unless email.domain?(domain) + source.query email + end + + protected + + attr_reader :domain + + def source + Nickserver::CouchDB::Source.new + end + + def missing_domain_response + ErrorResponse.new "HTTP request must include a Host header." + end + + end + end +end diff --git a/test/unit/dispatcher_test.rb b/test/integration/dispatcher_test.rb index 60d252b..60d252b 100644 --- a/test/unit/dispatcher_test.rb +++ b/test/integration/dispatcher_test.rb 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 |