summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-08-30 12:38:43 +0200
committerAzul <azul@riseup.net>2016-08-31 12:14:52 +0200
commit625416ac16a1216240fc4c48aa19802ef8a967a1 (patch)
treef3a4a54627fcae49bf896f5f58e60b506f9a55d4
parentb22e23eb9f5a1cb3e37ab2a26b1091183574f4e4 (diff)
wip: key lookup by address via leaps nicknym
I added a RequestHandler that uses Nicknym::Source - but that has not been implemented yet.
-rw-r--r--lib/nickserver/email_address.rb6
-rw-r--r--lib/nickserver/nicknym/source.rb14
-rw-r--r--lib/nickserver/request_handlers/leap_email_handler.rb36
-rw-r--r--test/support/request_handler_test_helper.rb6
-rw-r--r--test/unit/adapters/celluloid_http_test.rb11
-rw-r--r--test/unit/email_address_test.rb1
-rw-r--r--test/unit/nicknym/source_test.rb33
-rw-r--r--test/unit/request_handlers/leap_email_handler_test.rb41
8 files changed, 143 insertions, 5 deletions
diff --git a/lib/nickserver/email_address.rb b/lib/nickserver/email_address.rb
index 2b3f2c2..c5d5df7 100644
--- a/lib/nickserver/email_address.rb
+++ b/lib/nickserver/email_address.rb
@@ -30,7 +30,11 @@ module Nickserver
end
def domain?(domain)
- address.end_with? "@#{domain}"
+ domain == self.domain
+ end
+
+ def domain
+ address.split('@')[1]
end
def to_s
diff --git a/lib/nickserver/nicknym/source.rb b/lib/nickserver/nicknym/source.rb
new file mode 100644
index 0000000..1be0c2a
--- /dev/null
+++ b/lib/nickserver/nicknym/source.rb
@@ -0,0 +1,14 @@
+require 'nickserver/source'
+
+module Nickserver
+ module Nicknym
+ class Source < Nickserver::Source
+
+ def available_for?(domain)
+ status, _body = adapter.get "https://#{domain}/provider.json"
+ status == 200
+ end
+
+ end
+ end
+end
diff --git a/lib/nickserver/request_handlers/leap_email_handler.rb b/lib/nickserver/request_handlers/leap_email_handler.rb
new file mode 100644
index 0000000..548bc8a
--- /dev/null
+++ b/lib/nickserver/request_handlers/leap_email_handler.rb
@@ -0,0 +1,36 @@
+require 'nickserver/email_address'
+require 'nickserver/nicknym/source'
+
+module Nickserver
+ module RequestHandlers
+ class LeapEmailHandler < Base
+
+ def handle
+ source.query(email) if request.email && remote_email? && nicknym_email?
+ end
+
+ protected
+
+ def source
+ @source ||= Nicknym::Source.new
+ end
+
+ def remote_email?
+ !email.domain?(domain)
+ end
+
+ def nicknym_email?
+ source.available_for?(email.domain)
+ end
+
+ def email
+ @email ||= EmailAddress.new(request.email)
+ end
+
+ def domain
+ Config.domain || request.domain
+ end
+
+ end
+ end
+end
diff --git a/test/support/request_handler_test_helper.rb b/test/support/request_handler_test_helper.rb
index 3ca89ba..dd6940d 100644
--- a/test/support/request_handler_test_helper.rb
+++ b/test/support/request_handler_test_helper.rb
@@ -15,9 +15,9 @@ module RequestHandlerTestHelper
end
def source_expecting_query_for(*query_args)
- source = Minitest::Mock.new
- source.expect :query, 'response', query_args
- source
+ @source ||= Minitest::Mock.new
+ @source.expect :query, 'response', query_args
+ @source
end
def assert_responds_with_error(msg, opts)
diff --git a/test/unit/adapters/celluloid_http_test.rb b/test/unit/adapters/celluloid_http_test.rb
index 4381b8f..68b9606 100644
--- a/test/unit/adapters/celluloid_http_test.rb
+++ b/test/unit/adapters/celluloid_http_test.rb
@@ -13,7 +13,16 @@ class Nickserver::Adapters::CelluloidHttpTest < Minitest::Test
super
end
- def test_successful_request
+ def test_request_without_query
+ url = 'http://url.to'
+ stub_http_request(:get, url)
+ .to_return status: 200, body: 'body'
+ status, body = adapter.get url
+ assert_equal 200, status
+ assert_equal 'body', body
+ end
+
+ def test_successful_request_with_query
url = 'http://url.to'
stub_http_request(:get, url)
.with(query: {key: :value})
diff --git a/test/unit/email_address_test.rb b/test/unit/email_address_test.rb
index 6d57a8c..6aecef7 100644
--- a/test/unit/email_address_test.rb
+++ b/test/unit/email_address_test.rb
@@ -5,6 +5,7 @@ class EmailAddressTest < Minitest::Test
def test_domain
nick = Nickserver::EmailAddress.new 'nick@test.me'
+ assert_equal 'test.me', nick.domain
assert nick.domain?('test.me')
assert !nick.domain?('est.me')
end
diff --git a/test/unit/nicknym/source_test.rb b/test/unit/nicknym/source_test.rb
new file mode 100644
index 0000000..586dc0e
--- /dev/null
+++ b/test/unit/nicknym/source_test.rb
@@ -0,0 +1,33 @@
+require 'test_helper'
+require 'nickserver/nicknym/source'
+
+class NicknymSourceTest < Minitest::Test
+
+ def test_initialization
+ assert source
+ end
+
+ def test_available_for_domain
+ adapter.expect :get, [200, 'dummy body'],
+ ['https://leap_powered.tld/provider.json']
+ assert source.available_for?('leap_powered.tld')
+ adapter.verify
+ end
+
+ def test_not_available_for_domain
+ adapter.expect :get, [404, nil],
+ ['https://remote.tld/provider.json']
+ assert !source.available_for?('remote.tld')
+ adapter.verify
+ end
+
+ protected
+
+ def source
+ Nickserver::Nicknym::Source.new(adapter)
+ end
+
+ def adapter
+ @adapter ||= Minitest::Mock.new
+ end
+end
diff --git a/test/unit/request_handlers/leap_email_handler_test.rb b/test/unit/request_handlers/leap_email_handler_test.rb
new file mode 100644
index 0000000..fadb914
--- /dev/null
+++ b/test/unit/request_handlers/leap_email_handler_test.rb
@@ -0,0 +1,41 @@
+require 'test_helper'
+require 'support/request_handler_test_helper'
+require 'nickserver/request_handlers/leap_email_handler'
+
+class LeapEmailHandlerTest < MiniTest::Test
+ include RequestHandlerTestHelper
+
+ def test_no_email
+ assert_refuses
+ end
+
+ def test_local_email
+ assert_refuses email: 'me@local.tld', domain: 'local.tld'
+ end
+
+ def test_remote_email
+ source ||= Minitest::Mock.new
+ source.expect :available_for?, false, ['remote.tld']
+ source_class.stub :new, source do
+ assert_refuses email: 'me@remote.tld', domain: 'local.tld'
+ end
+ end
+
+ def test_nicknym_email
+ @source ||= Minitest::Mock.new
+ @source.expect :available_for?, true, ['nicknym.tld']
+ assert_queries_for Nickserver::EmailAddress do
+ assert_handles email: 'me@nicknym.tld', domain: 'local.tld'
+ end
+ end
+
+ protected
+
+ def handler
+ Nickserver::RequestHandlers::LeapEmailHandler
+ end
+
+ def source_class
+ Nickserver::Nicknym::Source
+ end
+end