diff options
author | Azul <azul@riseup.net> | 2016-09-12 11:42:02 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-09-12 11:42:02 +0200 |
commit | ce09dd9c21d79e2260f9436e41f9970b42935b27 (patch) | |
tree | f620f8bf3d7386d8a5a48b1feb0a18c58269c128 | |
parent | f18d698d09afda894a568de79c5770704a0c9de7 (diff) |
[wip] nicknym source query implemented
Also changed Nickserver::Response to not include the status code.
This may be okay for error responses but in most cases we want to
have a parsable message and not some status code prepended to it.
-rw-r--r-- | lib/nickserver/dispatcher.rb | 8 | ||||
-rw-r--r-- | lib/nickserver/error_response.rb | 7 | ||||
-rw-r--r-- | lib/nickserver/nicknym/source.rb | 14 | ||||
-rw-r--r-- | lib/nickserver/response.rb | 8 | ||||
-rw-r--r-- | test/integration/dispatcher_test.rb | 6 | ||||
-rw-r--r-- | test/integration/nicknym_test.rb | 11 | ||||
-rw-r--r-- | test/support/client_integration_test.rb | 13 | ||||
-rw-r--r-- | test/unit/error_response_test.rb | 12 | ||||
-rw-r--r-- | test/unit/nicknym/source_test.rb | 14 | ||||
-rw-r--r-- | test/unit/response_test.rb | 7 |
10 files changed, 85 insertions, 15 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb index 9a031ab..7a584e5 100644 --- a/lib/nickserver/dispatcher.rb +++ b/lib/nickserver/dispatcher.rb @@ -29,7 +29,7 @@ module Nickserver def respond_to(params, headers) request = Nickserver::Request.new params, headers response = handle request - send_response response.status, response.content + send_response response end protected @@ -51,12 +51,12 @@ module Nickserver RequestHandlers::LocalEmailHandler, RequestHandlers::HkpEmailHandler, RequestHandlers::FingerprintHandler, - Proc.new { Nickserver::Response.new(404, "Not Found\n") } + Proc.new { Nickserver::Response.new(404, "404 Not Found\n") } ] end - def send_response(status = 200, content = '') - responder.respond status, content + def send_response(response) + responder.respond response.status, response.content end attr_reader :responder diff --git a/lib/nickserver/error_response.rb b/lib/nickserver/error_response.rb index 1065e4e..9d53630 100644 --- a/lib/nickserver/error_response.rb +++ b/lib/nickserver/error_response.rb @@ -7,5 +7,12 @@ module Nickserver @message = message + "\n" end + def content + "#{status} #{message}" + end + + protected + + attr_reader :message end end diff --git a/lib/nickserver/nicknym/source.rb b/lib/nickserver/nicknym/source.rb index 1be0c2a..3957cdd 100644 --- a/lib/nickserver/nicknym/source.rb +++ b/lib/nickserver/nicknym/source.rb @@ -1,14 +1,26 @@ require 'nickserver/source' +require 'nickserver/response' module Nickserver module Nicknym class Source < Nickserver::Source def available_for?(domain) - status, _body = adapter.get "https://#{domain}/provider.json" + status, _body = get "#{domain}/provider.json" status == 200 end + def query(email) + status, body = get "nicknym.#{email.domain}", address: email.to_s + return Nickserver::Response.new(status, body) + end + + protected + + def get(*args) + args[0] = "https://#{args.first}" + adapter.get *args + end end end end diff --git a/lib/nickserver/response.rb b/lib/nickserver/response.rb index c55d359..372da36 100644 --- a/lib/nickserver/response.rb +++ b/lib/nickserver/response.rb @@ -1,15 +1,15 @@ module Nickserver class Response - attr_reader :status, :message + attr_reader :status, :body - def initialize(status, message) + def initialize(status, body) @status = status - @message = message + @body = body end def content - "#{status} #{message}" + body end end end diff --git a/test/integration/dispatcher_test.rb b/test/integration/dispatcher_test.rb index 4ec823b..60ad9d7 100644 --- a/test/integration/dispatcher_test.rb +++ b/test/integration/dispatcher_test.rb @@ -38,10 +38,10 @@ class Nickserver::DispatcherTest < Minitest::Test handle fingerprint: ['E36E738D69173C13D709E44F2F455E2824D18DDF'] source = Minitest::Mock.new source.expect :get_key_by_fingerprint, - Nickserver::Response.new(200, "fake fingerprint"), + Nickserver::Response.new(200, "fake key response"), ['E36E738D69173C13D709E44F2F455E2824D18DDF'] Nickserver::Hkp::Source.stub :new, source do - assert_response status: 200, content: "200 fake fingerprint" + assert_response status: 200, content: "fake key response" end end @@ -62,7 +62,7 @@ class Nickserver::DispatcherTest < Minitest::Test 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" + assert_response status: 200, content: "fake content" end end diff --git a/test/integration/nicknym_test.rb b/test/integration/nicknym_test.rb new file mode 100644 index 0000000..bfcd7e1 --- /dev/null +++ b/test/integration/nicknym_test.rb @@ -0,0 +1,11 @@ +require 'test_helper' +require 'support/client_integration_test' +require 'nickserver/nicknym/source' + +class NicknymClientTest < ClientIntegrationTest + + def test_truth + assert true + end + +end diff --git a/test/support/client_integration_test.rb b/test/support/client_integration_test.rb new file mode 100644 index 0000000..b05db73 --- /dev/null +++ b/test/support/client_integration_test.rb @@ -0,0 +1,13 @@ +class ClientIntegrationTest + + def setup + super + Celluloid.boot + end + + def teardown + Celluloid.shutdown + super + end + +end diff --git a/test/unit/error_response_test.rb b/test/unit/error_response_test.rb new file mode 100644 index 0000000..7242b38 --- /dev/null +++ b/test/unit/error_response_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' +require 'nickserver/error_response' + +class ErrorResponseTest < Minitest::Test + + def test_content + response = Nickserver::ErrorResponse.new "Not a valid address" + assert_equal "500 Not a valid address\n", response.content + assert_equal 500, response.status + end + +end diff --git a/test/unit/nicknym/source_test.rb b/test/unit/nicknym/source_test.rb index 586dc0e..41f5ac0 100644 --- a/test/unit/nicknym/source_test.rb +++ b/test/unit/nicknym/source_test.rb @@ -1,5 +1,6 @@ require 'test_helper' require 'nickserver/nicknym/source' +require 'nickserver/email_address' class NicknymSourceTest < Minitest::Test @@ -21,6 +22,15 @@ class NicknymSourceTest < Minitest::Test adapter.verify end + def test_successful_query + adapter.expect :get, [200, 'dummy body'], + ['https://nicknym.leap_powered.tld', address: email_stub.to_s] + response = source.query(email_stub) + assert_equal 200, response.status + assert_equal 'dummy body', response.content + adapter.verify + end + protected def source @@ -30,4 +40,8 @@ class NicknymSourceTest < Minitest::Test def adapter @adapter ||= Minitest::Mock.new end + + def email_stub + @email_stub ||= Nickserver::EmailAddress.new 'test@leap_powered.tld' + end end diff --git a/test/unit/response_test.rb b/test/unit/response_test.rb index 8a53066..ac7a3a8 100644 --- a/test/unit/response_test.rb +++ b/test/unit/response_test.rb @@ -3,9 +3,10 @@ require 'nickserver/response' class ResponseTest < Minitest::Test - def test_content - response = Nickserver::Response.new 500, "Not a valid address" - assert_equal "500 Not a valid address", response.content + def test_ok_response + response = Nickserver::Response.new 200, "content" + assert_equal "content", response.content + assert_equal 200, response.status end end |