diff options
author | Azul <azul@riseup.net> | 2016-06-11 15:19:50 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-06-14 10:36:58 +0200 |
commit | f567ed80427d43019ceb1aaf77d4bc6c01e62729 (patch) | |
tree | 8698f379be2d0706e19b72134019a0aa044f132d | |
parent | 92c86fc4e1e6dcb86793992e69dfd0608c118c9a (diff) |
use adapter for FetchKeyInfo
-rw-r--r-- | lib/nickserver/hkp/fetch_key_info.rb | 28 | ||||
-rw-r--r-- | lib/nickserver/hkp/parse_key_info.rb | 22 | ||||
-rw-r--r-- | lib/nickserver/hkp/source.rb | 14 | ||||
-rw-r--r-- | test/helpers/test_adapter.rb | 10 | ||||
-rw-r--r-- | test/integration/couch_db/source_test.rb | 12 | ||||
-rw-r--r-- | test/integration/hkp_test.rb | 59 |
6 files changed, 62 insertions, 83 deletions
diff --git a/lib/nickserver/hkp/fetch_key_info.rb b/lib/nickserver/hkp/fetch_key_info.rb index b7481d0..b23af15 100644 --- a/lib/nickserver/hkp/fetch_key_info.rb +++ b/lib/nickserver/hkp/fetch_key_info.rb @@ -1,30 +1,26 @@ -require 'em-http' - # # used to fetch an array of KeyInfo objects that match the given uid. # module Nickserver; module Hkp class FetchKeyInfo - include EM::Deferrable - def search(uid) + def initialize(adapter) + @adapter = adapter + end + + def search(uid, &block) # in practice, exact=on seems to have no effect params = {op: 'vindex', search: uid, exact: 'on', options: 'mr', fingerprint: 'on'} - EventMachine::HttpRequest.new(Config.hkp_url).get(query: params).callback {|http| - parser = ParseKeyInfo.new http.response_header, http.response - keys = parser.keys(uid) - if keys.any? - self.succeed keys - else - self.fail parser.status(uid), parser.msg(uid) - end - }.errback {|http| - self.fail 500, http.error - } - self + adapter.get(Config.hkp_url, query: params) do |status, response| + parser = ParseKeyInfo.new status, response + yield parser.status_for(uid), parser.response_for(uid) + end end + protected + attr_reader :adapter + end end; end diff --git a/lib/nickserver/hkp/parse_key_info.rb b/lib/nickserver/hkp/parse_key_info.rb index d8abe4f..9d59d6b 100644 --- a/lib/nickserver/hkp/parse_key_info.rb +++ b/lib/nickserver/hkp/parse_key_info.rb @@ -12,18 +12,26 @@ module Nickserver; module Hkp # which the output of sks does. MATCH_PUB_KEY = /(^pub:.+?\n(^uid:.+?\n)+)/m - # header -- header of the hkp response + # status -- http status of the hkp response # vindex_result -- raw output from a vindex hkp query (machine readable) - def initialize(header, vindex_result) - @header = header + def initialize(status, vindex_result) + @status = status @vindex_result = vindex_result end - def status(uid) + def status_for(uid) if hkp_ok? && keys(uid).empty? error_status(uid) else - header.status + status + end + end + + def response_for(uid) + if keys(uid).any? + keys(uid) + else + msg(uid) end end @@ -41,7 +49,7 @@ module Nickserver; module Hkp protected - attr_reader :header + attr_reader :status attr_reader :vindex_result def error_status(uid) @@ -78,7 +86,7 @@ module Nickserver; module Hkp end def hkp_ok? - header.status == 200 + status == 200 end def error_message(uid, key, err) diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb index 48a63d8..1af8ac9 100644 --- a/lib/nickserver/hkp/source.rb +++ b/lib/nickserver/hkp/source.rb @@ -14,12 +14,14 @@ module Nickserver; module Hkp end def query(nick, &block) - FetchKeyInfo.new.search(nick).callback {|key_info_list| - best = pick_best_key(key_info_list) - get_key_by_fingerprint(nick, best.keyid, &block) - }.errback {|status, msg| - yield Nickserver::Response.new(status, msg) - } + FetchKeyInfo.new(adapter).search(nick) do |status, response| + if status == 200 + best = pick_best_key(response) + get_key_by_fingerprint(nick, best.keyid, &block) + else + yield Nickserver::Response.new(status, response) + end + end end protected diff --git a/test/helpers/test_adapter.rb b/test/helpers/test_adapter.rb new file mode 100644 index 0000000..46d4713 --- /dev/null +++ b/test/helpers/test_adapter.rb @@ -0,0 +1,10 @@ +class TestAdapter + def initialize(status, content) + @status = status + @content = content + end + + def get(url, opts) + yield @status, @content + end +end diff --git a/test/integration/couch_db/source_test.rb b/test/integration/couch_db/source_test.rb index 9e319f4..21e3642 100644 --- a/test/integration/couch_db/source_test.rb +++ b/test/integration/couch_db/source_test.rb @@ -1,22 +1,12 @@ require 'test_helper' require 'file_content' +require 'helpers/test_adapter' require 'nickserver/couch_db/source' module Nickserver::CouchDB class SourceTest < Minitest::Test include FileContent - class TestAdapter - def initialize(status, content) - @status = status - @content = content - end - - def get(url, opts) - yield @status, @content - end - end - def test_couch_query_and_response adapter = TestAdapter.new 200, file_content(:blue_couchdb_result) source = Source.new adapter diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb index 3988fa5..2afd2c0 100644 --- a/test/integration/hkp_test.rb +++ b/test/integration/hkp_test.rb @@ -47,7 +47,8 @@ class HkpTest < Minitest::Test stub_sks_get_reponse(key_id, body: file_content(:leap_public_key)) assert_response_for_uid(uid) do |response| - assert_equal file_content(:leap_public_key), response.body + content = JSON.parse response.content + assert_equal file_content(:leap_public_key), content['openpgp'] end end @@ -76,7 +77,7 @@ class HkpTest < Minitest::Test def test_key_info_real_network real_network do uid = 'elijah@riseup.net' - test_em_callback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |keys| + assert_key_info_for_uid uid do |keys| assert_equal 1, keys.size assert keys.first.keyid =~ /00440025$/ end @@ -93,7 +94,7 @@ class HkpTest < Minitest::Test #stub_config(:hkp_ca_file, file_path('autistici-ca.pem')) do assert File.exist?(Nickserver::Config.hkp_ca_file) uid = 'elijah@riseup.net' - test_em_callback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |keys| + assert_key_info_for_uid uid do |keys| assert_equal 1, keys.size assert keys.first.keyid =~ /00440025$/ end @@ -112,58 +113,30 @@ class HkpTest < Minitest::Test def assert_response_for_uid(uid, &block) EM.run do - Nickserver::Hkp::Source.new(Nickserver::Adapters::EmHttp.new).query(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. - # - # This method takes care of the calls to EM.run and EM.stop. It works kind of like EM.run_block, - # except I couldn't get run_block to work with multiple nested HTTP requests. - # - def test_em_callback(code, &block) - EM.run do - deferrable = instance_eval(code) - deferrable.callback {|response| - EM.stop + Nickserver::Hkp::Source.new(adapter).query uid do |response| yield response - return - } - deferrable.errback {|response, msg| EM.stop - puts caller.join("\n") - flunk "Expecting callback, but errback invoked with response: #{response} #{msg}\n\n#{caller.join("\n")}" - } + end end - assert false, 'should not get here' end - # - # like test_em_callback, except value yielded is the result of errback, and - # we raise an exception if errback was not called. - # - def test_em_errback(code, &block) + def assert_key_info_for_uid(uid, &block) EM.run do - deferrable = instance_eval(code) - deferrable.callback {|response| - EM.stop - flunk "Expecting errback, but callback invoked with response: #{response}" - } - deferrable.errback {|response| + Nickserver::Hkp::FetchKeyInfo.new(adapter).search uid do |status, keys| + assert_equal 200, status + yield keys EM.stop - yield response - return - } + end end - assert false, 'should not get here' + end + + def adapter + Nickserver::Adapters::EmHttp.new end def fetch_key_info(body_source, uid, &block) stub_sks_vindex_reponse(uid, body: file_content(body_source)) - test_em_callback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'", &block + assert_key_info_for_uid(uid, &block) end end |