summaryrefslogtreecommitdiff
path: root/test/unit/request_handlers/local_email_handler_test.rb
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2016-08-29 10:19:22 +0000
committerazul <azul@riseup.net>2016-08-29 10:19:22 +0000
commit6e2d31e3f7c515f65d92533bcdb035438461a00c (patch)
tree4efa7445db3a0521a14d75e626d64f85434a3ea5 /test/unit/request_handlers/local_email_handler_test.rb
parentc134e0940a44ba3fb3f0f8ee86faa8053a9e0b44 (diff)
parent0784391a21b75ca52892e992a614b0f927ade00e (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/request_handlers/local_email_handler_test.rb')
-rw-r--r--test/unit/request_handlers/local_email_handler_test.rb65
1 files changed, 65 insertions, 0 deletions
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