summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/nickserver/hkp/fetch_key.rb29
-rw-r--r--lib/nickserver/hkp/source.rb16
-rw-r--r--test/integration/hkp_test.rb33
-rw-r--r--test/unit/adapters/em_http_test.rb25
4 files changed, 68 insertions, 35 deletions
diff --git a/lib/nickserver/hkp/fetch_key.rb b/lib/nickserver/hkp/fetch_key.rb
index fe00473..10c4926 100644
--- a/lib/nickserver/hkp/fetch_key.rb
+++ b/lib/nickserver/hkp/fetch_key.rb
@@ -1,4 +1,6 @@
require 'em-http'
+require 'nickserver/response'
+require 'nickserver/hkp/response'
#
# Fetch keys via HKP
@@ -8,35 +10,36 @@ require 'em-http'
module Nickserver; module Hkp
class FetchKey
- include EM::Deferrable
- def get(uid)
- FetchKeyInfo.new.search(uid).callback {|key_info_list|
+ def initialize(adapter)
+ @adapter = adapter
+ end
+
+ def get(nick, &block)
+ FetchKeyInfo.new.search(nick).callback {|key_info_list|
best = pick_best_key(key_info_list)
- get_key_by_fingerprint(best.keyid) {|key|
- self.succeed key
- }
+ get_key_by_fingerprint(nick, best.keyid, &block)
}.errback {|status, msg|
- self.fail status, msg
+ yield Nickserver::Response.new(status, msg)
}
- self
end
#
# fetches ascii armored OpenPGP public key from the keyserver
#
- def get_key_by_fingerprint(key_id)
+ def get_key_by_fingerprint(nick, key_id)
params = {op: 'get', search: "0x" + key_id, exact: 'on', options: 'mr'}
http = EventMachine::HttpRequest.new(Config.hkp_url).get(query: params)
http.callback {
- if http.response_header.status != 200
- self.fail http.response_header.status, "HKP Request failed"
+ status = http.response_header.status
+ if status != 200
+ yield Nickserver::Response.new status, "HKP Request failed"
else
- yield http.response
+ yield Response.new nick, http.response
end
}
http.errback {
- self.fail 500, http.error
+ yield Nickserver::Response.new 500, http.error
}
end
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
index 79333f0..f06a931 100644
--- a/lib/nickserver/hkp/source.rb
+++ b/lib/nickserver/hkp/source.rb
@@ -1,20 +1,20 @@
-require 'nickserver/hkp/response'
module Nickserver::Hkp
class Source
def initialize(adapter)
+ @adapter = adapter
end
- def query(nick)
- fetcher.get(nick).callback {|key|
- yield Response.new(nick, key)
- }.errback {|status, msg|
- yield Nickserver::Response.new(status, msg)
- }
+ def query(nick, &block)
+ fetcher.get(nick, &block)
end
+ protected
+
+ attr_reader :adapter
+
def fetcher
- Nickserver::Hkp::FetchKey.new
+ Nickserver::Hkp::FetchKey.new(adapter)
end
end
end
diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb
index 0e77253..6ef52fe 100644
--- a/test/integration/hkp_test.rb
+++ b/test/integration/hkp_test.rb
@@ -29,17 +29,13 @@ class HkpTest < Minitest::Test
def test_key_info_not_found
uid = 'leaping_lemur@leap.se'
stub_sks_vindex_reponse(uid, status: 404)
- test_em_errback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |error|
- assert_equal 404, error
- end
+ assert_response_status_for_uid uid, 404
end
def test_no_matching_key_found
uid = 'leaping_lemur@leap.se'
stub_sks_vindex_reponse(uid, status: 200)
- test_em_errback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |error|
- assert_equal 404, error
- end
+ assert_response_status_for_uid uid, 404
end
def test_fetch_key
@@ -48,8 +44,8 @@ class HkpTest < Minitest::Test
stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result))
stub_sks_get_reponse(key_id, body: file_content(:leap_public_key))
- test_em_callback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |key_text|
- assert_equal file_content(:leap_public_key), key_text
+ assert_response_for_uid(uid) do |response|
+ assert_equal file_content(:leap_public_key), response.body
end
end
@@ -60,18 +56,14 @@ class HkpTest < Minitest::Test
stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result))
stub_sks_get_reponse(key_id, status: 404)
- test_em_errback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |error|
- assert_equal 404, error
- end
+ assert_response_status_for_uid uid, 404
end
def test_fetch_key_too_short
uid = 'chiiph@leap.se'
stub_sks_vindex_reponse(uid, body: file_content(:short_key_vindex_result))
- test_em_errback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |error|
- assert_equal 500, error
- end
+ assert_response_status_for_uid uid, 500
end
#
@@ -110,6 +102,19 @@ class HkpTest < Minitest::Test
protected
+ def assert_response_status_for_uid(uid, status)
+ assert_response_for_uid(uid) do |response|
+ assert_equal status, response.status
+ end
+ end
+
+ def assert_response_for_uid(uid, &block)
+ EM.run do
+ Nickserver::Hkp::FetchKey.new(nil).get(uid, &block)
+ EM.stop
+ end
+ end
+
#
# Takes a code snippet that returns a Deferrable, and yields the callback result.
# Assertion fails if errback is called instead of callback.
diff --git a/test/unit/adapters/em_http_test.rb b/test/unit/adapters/em_http_test.rb
new file mode 100644
index 0000000..659ff1b
--- /dev/null
+++ b/test/unit/adapters/em_http_test.rb
@@ -0,0 +1,25 @@
+require 'test_helper'
+require 'nickserver/adapters/em_http'
+
+class Nickserver::Adapters::EmHttpTest < Minitest::Test
+
+ def test_successful_request
+ url = 'http://url.to'
+ stub_http_request(:get, url)
+ .with(query: {key: :value})
+ .to_return status: 200, body: 'body'
+ EM.run do
+ adapter.get(url, query: {key: :value}) do |status, body|
+ assert_equal 200, status
+ assert_equal 'body', body
+ EM.stop
+ end
+ end
+ end
+
+ protected
+
+ def adapter
+ Nickserver::Adapters::EmHttp.new
+ end
+end