diff options
-rw-r--r-- | lib/nickserver/nicknym/source.rb | 20 | ||||
-rw-r--r-- | test/remote/nicknym_source_test.rb | 25 | ||||
-rw-r--r-- | test/unit/nicknym/source_test.rb | 5 |
3 files changed, 41 insertions, 9 deletions
diff --git a/lib/nickserver/nicknym/source.rb b/lib/nickserver/nicknym/source.rb index 0776a36..45daeaf 100644 --- a/lib/nickserver/nicknym/source.rb +++ b/lib/nickserver/nicknym/source.rb @@ -6,20 +6,28 @@ module Nickserver class Source < Nickserver::Source def available_for?(domain) - status, _body = get "#{domain}/provider.json" - status == 200 + status, body = adapter.get "https://#{domain}/provider.json" + status == 200 && provider_with_mx?(body) end def query(email) - status, body = get "nicknym.#{email.domain}", address: email.to_s + status, body = nicknym_get 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) + def nicknym_get(domain, query = {}) + url = "https://nicknym.#{domain}:#{Config.port}" + adapter.get(url, query: query) + end + + def provider_with_mx?(provider_json) + provider = JSON.parse provider_json + services = provider['services'] || [] + services.include?('mx') + rescue JSON::ParserError + return false end end end diff --git a/test/remote/nicknym_source_test.rb b/test/remote/nicknym_source_test.rb index e516895..2be7251 100644 --- a/test/remote/nicknym_source_test.rb +++ b/test/remote/nicknym_source_test.rb @@ -1,5 +1,6 @@ require 'test_helper' require 'nickserver/nicknym/source' +require 'nickserver/email_address' class RemoteNicknymSourceTest < Minitest::Test @@ -13,8 +14,23 @@ class RemoteNicknymSourceTest < Minitest::Test super end - def test_truth + def test_availablility_check assert source.available_for? 'mail.bitmask.net' + refute source.available_for? 'dl.bitmask.net' # not a provider + refute source.available_for? 'demo.bitmask.net' # provider without mx + end + + def test_successful_query + response = source.query(email_with_key) + json = JSON.parse response.content + assert_equal 200, response.status + assert_equal email_with_key.to_s, json["address"] + refute_empty json["openpgp"] + end + + def test_not_found + response = source.query(email_without_key) + assert_equal 404, response.status end protected @@ -23,5 +39,12 @@ class RemoteNicknymSourceTest < Minitest::Test @source ||= Nickserver::Nicknym::Source.new end + def email_with_key + Nickserver::EmailAddress.new('test@mail.bitmask.net') + end + + def email_without_key + Nickserver::EmailAddress.new('pleaseneverusethisemailweuseittotest@mail.bitmask.net') + end end diff --git a/test/unit/nicknym/source_test.rb b/test/unit/nicknym/source_test.rb index 41f5ac0..76337d4 100644 --- a/test/unit/nicknym/source_test.rb +++ b/test/unit/nicknym/source_test.rb @@ -9,7 +9,7 @@ class NicknymSourceTest < Minitest::Test end def test_available_for_domain - adapter.expect :get, [200, 'dummy body'], + adapter.expect :get, [200, '{"services": ["mx"]}'], ['https://leap_powered.tld/provider.json'] assert source.available_for?('leap_powered.tld') adapter.verify @@ -24,7 +24,8 @@ class NicknymSourceTest < Minitest::Test def test_successful_query adapter.expect :get, [200, 'dummy body'], - ['https://nicknym.leap_powered.tld', address: email_stub.to_s] + ['https://nicknym.leap_powered.tld:6425', + {query: {address: email_stub.to_s}}] response = source.query(email_stub) assert_equal 200, response.status assert_equal 'dummy body', response.content |