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 /test | |
parent | 92c86fc4e1e6dcb86793992e69dfd0608c118c9a (diff) |
use adapter for FetchKeyInfo
Diffstat (limited to 'test')
-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 |
3 files changed, 27 insertions, 54 deletions
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 |