[wip] nicknym source query implemented
authorAzul <azul@riseup.net>
Mon, 12 Sep 2016 09:42:02 +0000 (11:42 +0200)
committerAzul <azul@riseup.net>
Mon, 12 Sep 2016 09:42:02 +0000 (11:42 +0200)
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.

lib/nickserver/dispatcher.rb
lib/nickserver/error_response.rb
lib/nickserver/nicknym/source.rb
lib/nickserver/response.rb
test/integration/dispatcher_test.rb
test/integration/nicknym_test.rb [new file with mode: 0644]
test/support/client_integration_test.rb [new file with mode: 0644]
test/unit/error_response_test.rb [new file with mode: 0644]
test/unit/nicknym/source_test.rb
test/unit/response_test.rb

index 9a031ab..7a584e5 100644 (file)
@@ -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
index 1065e4e..9d53630 100644 (file)
@@ -7,5 +7,12 @@ module Nickserver
       @message = message + "\n"
     end
 
+    def content
+      "#{status} #{message}"
+    end
+
+    protected
+
+    attr_reader :message
   end
 end
index 1be0c2a..3957cdd 100644 (file)
@@ -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
index c55d359..372da36 100644 (file)
@@ -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
index 4ec823b..60ad9d7 100644 (file)
@@ -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 (file)
index 0000000..bfcd7e1
--- /dev/null
@@ -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 (file)
index 0000000..b05db73
--- /dev/null
@@ -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 (file)
index 0000000..7242b38
--- /dev/null
@@ -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
index 586dc0e..41f5ac0 100644 (file)
@@ -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
index 8a53066..ac7a3a8 100644 (file)
@@ -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