From 8a81429f0eb8aa5041d47557d0c5b5359bb959e6 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 25 May 2016 13:13:30 +0200 Subject: copy over all files from rewritten attempt I started a nickserver from scratch to implement the things that are independent of our choice of stack (eventmachine or other). This commit copies them over and tests both things in parallel. --- test/file_content.rb | 11 +++++++++++ test/integration/couch_db/source_test.rb | 29 +++++++++++++++++++++++++++++ test/test_helper.rb | 1 + test/unit/adapters/couch_db.rb | 15 +++++++++++++++ test/unit/adapters/local_test.rb | 5 +++++ test/unit/couch_db/response_test.rb | 30 ++++++++++++++++++++++++++++++ test/unit/couch_db/source_unit_test.rb | 17 +++++++++++++++++ test/unit/hkp/v_index_response_test.rb | 17 +++++++++++++++++ test/unit/hkp_test.rb | 16 ++++++++-------- test/unit/invalid_source_test.rb | 16 ++++++++++++++++ test/unit/lookup_test.rb | 23 +++++++++++++++++++++++ test/unit/nickname_test.rb | 28 ++++++++++++++++++++++++++++ test/unit/response_test.rb | 11 +++++++++++ 13 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 test/file_content.rb create mode 100644 test/integration/couch_db/source_test.rb create mode 100644 test/unit/adapters/couch_db.rb create mode 100644 test/unit/adapters/local_test.rb create mode 100644 test/unit/couch_db/response_test.rb create mode 100644 test/unit/couch_db/source_unit_test.rb create mode 100644 test/unit/hkp/v_index_response_test.rb create mode 100644 test/unit/invalid_source_test.rb create mode 100644 test/unit/lookup_test.rb create mode 100644 test/unit/nickname_test.rb create mode 100644 test/unit/response_test.rb (limited to 'test') diff --git a/test/file_content.rb b/test/file_content.rb new file mode 100644 index 0000000..0d0ac21 --- /dev/null +++ b/test/file_content.rb @@ -0,0 +1,11 @@ +module FileContent + + def file_content(filename) + (@file_contents ||= {})[filename] ||= File.read(file_path(filename)) + end + + def file_path(filename) + "%s/files/%s" % [File.dirname(__FILE__), filename] + end + +end diff --git a/test/integration/couch_db/source_test.rb b/test/integration/couch_db/source_test.rb new file mode 100644 index 0000000..9e319f4 --- /dev/null +++ b/test/integration/couch_db/source_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' +require 'file_content' +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 + source.query 'blue@example.org' do |response| + assert_equal 200, response.status + assert_equal file_content(:blue_nickserver_result), response.content + end + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index d4765bc..842f05f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require 'bundler/setup' require 'minitest/autorun' require 'webmock/minitest' require 'nickserver' +require 'minitest/pride' TESTING = true diff --git a/test/unit/adapters/couch_db.rb b/test/unit/adapters/couch_db.rb new file mode 100644 index 0000000..339e4f4 --- /dev/null +++ b/test/unit/adapters/couch_db.rb @@ -0,0 +1,15 @@ +require 'testhelper' +require 'nickserver/adapters/couch_db' + +class Nickserver::Adapters::CouchDBTest < Minitest::Test + + def test_query_404 + adapter.query(nil) do |status, content| + assert_equal 404, status + end + end + + def adapter + @adapter ||= Nickserver::Adapters::CouchDB.new + end +end diff --git a/test/unit/adapters/local_test.rb b/test/unit/adapters/local_test.rb new file mode 100644 index 0000000..97d394f --- /dev/null +++ b/test/unit/adapters/local_test.rb @@ -0,0 +1,5 @@ +require 'test_helper' +require 'nickserver/adapters/local' + +class Nickserver::Adapters::LocalTest < Minitest::Test +end diff --git a/test/unit/couch_db/response_test.rb b/test/unit/couch_db/response_test.rb new file mode 100644 index 0000000..d44760d --- /dev/null +++ b/test/unit/couch_db/response_test.rb @@ -0,0 +1,30 @@ +require 'test_helper' +require 'file_content' +require 'nickserver/couch_db/response' + +class Nickserver::CouchDB::ResponseTest < Minitest::Test + include FileContent + + def test_404 + response = response_for "bananas@example.org", + status: 404, body: "{}" + assert_equal 404, response.status + end + + def test_200_with_empty_response + response = response_for "stompy@example.org", + status: 200, body: file_content(:empty_couchdb_result) + assert_equal 404, response.status + end + + def test_200_with_success + response = response_for "blue@example.org", + status: 200, body: file_content(:blue_couchdb_result) + assert_equal 200, response.status + assert_equal file_content(:blue_nickserver_result), response.content + end + + def response_for(uid, couch_response = {}) + Nickserver::CouchDB::Response.new uid, couch_response + end +end diff --git a/test/unit/couch_db/source_unit_test.rb b/test/unit/couch_db/source_unit_test.rb new file mode 100644 index 0000000..19ea9bc --- /dev/null +++ b/test/unit/couch_db/source_unit_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' +require 'nickserver/couch_db/source' + +module Nickserver::CouchDB + class SourceUnitTest < Minitest::Test + + def test_query + address = "nick@domain.tl" + adapter = Minitest::Mock.new + adapter.expect :get, nil, + [String, {query: { reduce: "false", key: "\"#{address}\"" }}] + query = Source.new(adapter) + query.query address + adapter.verify + end + end +end diff --git a/test/unit/hkp/v_index_response_test.rb b/test/unit/hkp/v_index_response_test.rb new file mode 100644 index 0000000..d909520 --- /dev/null +++ b/test/unit/hkp/v_index_response_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' +require 'file_content' +require 'nickserver/hkp/v_index_response' + +class Nickserver::Hkp::VIndexResponseTest < Minitest::Test + include FileContent + + def test_leap_public_key + response = response_for 'cloudadmin@leap.se', + body: file_content(:leap_vindex_result) + assert_equal 'E818C478D3141282F7590D29D041EB11B1647490', response.keys.first.keyid + end + + def response_for(uid, hkp_response = {}) + Nickserver::Hkp::VIndexResponse.new uid, hkp_response + end +end diff --git a/test/unit/hkp_test.rb b/test/unit/hkp_test.rb index 0ecde2d..fc3cf97 100644 --- a/test/unit/hkp_test.rb +++ b/test/unit/hkp_test.rb @@ -29,7 +29,7 @@ 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| + test_em_errback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |error| assert_equal 404, error end end @@ -37,7 +37,7 @@ class HkpTest < Minitest::Test 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| + test_em_errback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |error| assert_equal 404, error end end @@ -48,7 +48,7 @@ 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| + test_em_callback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |key_text| assert_equal file_content(:leap_public_key), key_text end end @@ -60,7 +60,7 @@ 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| + test_em_errback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |error| assert_equal 404, error end end @@ -70,7 +70,7 @@ class HkpTest < Minitest::Test key_id = '9A753A6B' stub_sks_vindex_reponse(uid, body: file_content(:short_key_vindex_result)) - test_em_errback "Nickserver::HKP::FetchKey.new.get '#{uid}'" do |error| + test_em_errback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |error| assert_equal 500, error end end @@ -83,7 +83,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| + test_em_callback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |keys| assert_equal 1, keys.size assert keys.first.keyid =~ /00440025$/ end @@ -100,7 +100,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| + test_em_callback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'" do |keys| assert_equal 1, keys.size assert keys.first.keyid =~ /00440025$/ end @@ -157,7 +157,7 @@ class HkpTest < Minitest::Test 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 + test_em_callback "Nickserver::Hkp::FetchKeyInfo.new.search '#{uid}'", &block end end diff --git a/test/unit/invalid_source_test.rb b/test/unit/invalid_source_test.rb new file mode 100644 index 0000000..37a38fc --- /dev/null +++ b/test/unit/invalid_source_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' +require 'nickserver/invalid_source' + +class Nickserver::InvalidSourceTest < Minitest::Test + + def test_query + adapter.query(nil) do |status, content| + assert_equal 500, status + assert_equal "Not a valid address", content + end + end + + def adapter + Nickserver::InvalidSource.new + end +end diff --git a/test/unit/lookup_test.rb b/test/unit/lookup_test.rb new file mode 100644 index 0000000..ac827e0 --- /dev/null +++ b/test/unit/lookup_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' +require 'minitest/mock' +require 'nickserver/lookup' + +class TestLookup < Nickserver::Lookup + + def query + yield 200, 'yeah' + end + +end + +class LookupTest < Minitest::Test + + def test_responding + responder = Minitest::Mock.new + responder.expect :send_response, nil, + [{status: 200, content: 'yeah'}] + lookup = TestLookup.new nil + lookup.respond_with responder + responder.verify + end +end diff --git a/test/unit/nickname_test.rb b/test/unit/nickname_test.rb new file mode 100644 index 0000000..8681545 --- /dev/null +++ b/test/unit/nickname_test.rb @@ -0,0 +1,28 @@ +require 'test_helper' +require 'nickserver/nickname' + +class NicknameTest < Minitest::Test + + def test_local + nick = Nickserver::Nickname.new 'nick@test.me' + assert nick.local? + assert !nick.remote? + end + + def test_remote + nick = Nickserver::Nickname.new 'nick@remote.domain' + assert !nick.local? + assert nick.remote? + end + + def test_valid + nick = Nickserver::Nickname.new 'nick@remote.domain' + assert nick.valid? + end + + def test_invalid + nick = Nickserver::Nickname.new 'asdf' + assert nick.invalid? + end + +end diff --git a/test/unit/response_test.rb b/test/unit/response_test.rb new file mode 100644 index 0000000..8a53066 --- /dev/null +++ b/test/unit/response_test.rb @@ -0,0 +1,11 @@ +require 'test_helper' +require 'nickserver/response' + +class ResponseTest < Minitest::Test + + def test_content + response = Nickserver::Response.new 500, "Not a valid address" + assert_equal "500 Not a valid address", response.content + end + +end -- cgit v1.2.3 From b28d1eec92fb7d1ec05d37ae13df1d4049d57932 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 25 May 2016 14:22:00 +0200 Subject: run all tests by default --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index 842f05f..c9d8fd4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -28,7 +28,7 @@ class Minitest::Test end def real_network - if ENV['REAL_NET'] == 'true' + unless ENV['ONLY_LOCAL'] == 'true' WebMock.allow_net_connect! yield WebMock.disable_net_connect! -- cgit v1.2.3 From c90b23b4b4a4f550d6e2f0ce6dc0cc2b6fc1a2d5 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 25 May 2016 14:55:00 +0200 Subject: move nickserver_test to integration dir it really is more of an integration test --- test/integration/nickserver_test.rb | 155 ++++++++++++++++++++++++++++++++++++ test/unit/nickserver_test.rb | 155 ------------------------------------ 2 files changed, 155 insertions(+), 155 deletions(-) create mode 100644 test/integration/nickserver_test.rb delete mode 100644 test/unit/nickserver_test.rb (limited to 'test') diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb new file mode 100644 index 0000000..b4ff4da --- /dev/null +++ b/test/integration/nickserver_test.rb @@ -0,0 +1,155 @@ +require 'test_helper' +require 'json' + +# +# Some important notes to understanding these tests: +# +# (1) Requests to localhost always bypass HTTP stub. +# +# (2) All requests to nickserver are to localhost. +# +# (3) the "Host" header for requests to nickserver must be set (or Config.domain set) +# +# (4) When stubbing requests to couchdb, the couchdb host is changed from the +# default (localhost) to a dummy value (notlocalhost). +# + +class NickserverTest < Minitest::Test + + def test_GET_served_via_SKS + uid = 'cloudadmin@leap.se' + key_id = 'E818C478D3141282F7590D29D041EB11B1647490' + stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result)) + stub_sks_get_reponse(key_id, body: file_content(:leap_public_key)) + + start do + params = {query: {"address" => uid}} + get(params) do |http| + assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"] + stop + end + end + end + + def test_POST_served_via_SKS + uid = 'cloudadmin@leap.se' + key_id = 'E818C478D3141282F7590D29D041EB11B1647490' + stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result)) + stub_sks_get_reponse(key_id, body: file_content(:leap_public_key)) + + start do + params = {body: {"address" => uid}} + post(params) do |http| + assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"] + stop + end + end + end + + def test_GET_served_via_couch_not_found + domain = "example.org" + uid = "bananas@" + domain + stub_couch_response(uid, status: 404) do + start do + params = {query: {"address" => uid}, head: {host: domain}} + get(params) do |http| + assert_equal 404, http.response_header.status + stop + end + end + end + end + + def test_GET_served_via_couch_empty_results + domain = "example.org" + uid = "stompy@" + domain + stub_couch_response(uid, body: file_content(:empty_couchdb_result)) do + start do + params = {query: {"address" => uid}, head: {host: domain}} + get(params) do |http| + assert_equal 404, http.response_header.status + stop + end + end + end + end + + def test_GET_served_via_couch_success + domain = "example.org" + uid = "blue@" + domain + stub_couch_response(uid, body: file_content(:blue_couchdb_result)) do + start do + params = {query: {"address" => uid}, head: {host: domain}} + get(params) do |http| + assert_equal file_content(:blue_nickserver_result), http.response + stop + end + end + end + end + + def test_GET_empty + start do + get({}) do |http| + assert_equal "404 Not Found\n", http.response + stop + end + end + end + + protected + + # + # start nickserver + # + def start(timeout = 1) + Timeout::timeout(timeout) do + EM.run do + Nickserver::Server.start + EM.epoll + yield + end + end + rescue Timeout::Error + flunk 'EventMachine was not stopped before the timeout expired' + end + + # + # http GET requests to nickserver + # + def get(params, &block) + request(:get, params, &block) + end + + # + # http POST requests to nickserver + # + def post(params, &block) + request(:post, params, &block) + end + + # + # http request to nickserver + # + # this works because http requests to localhost are not stubbed, but requests to other domains are. + # + def request(method, params) + EventMachine::HttpRequest.new("http://localhost:#{Nickserver::Config.port}/").send(method,params).callback {|http| + # p http.response_header.status + # p http.response_header + # p http.response + yield http + }.errback {|http| + flunk(http.error) if http.error + EM.stop + } + end + + # + # stop nickserver + # + def stop + EM.stop + end + +end diff --git a/test/unit/nickserver_test.rb b/test/unit/nickserver_test.rb deleted file mode 100644 index 65ade8c..0000000 --- a/test/unit/nickserver_test.rb +++ /dev/null @@ -1,155 +0,0 @@ -require File.expand_path('test_helper', File.dirname(__FILE__)) -require 'json' - -# -# Some important notes to understanding these tests: -# -# (1) Requests to localhost always bypass HTTP stub. -# -# (2) All requests to nickserver are to localhost. -# -# (3) the "Host" header for requests to nickserver must be set (or Config.domain set) -# -# (4) When stubbing requests to couchdb, the couchdb host is changed from the -# default (localhost) to a dummy value (notlocalhost). -# - -class NickserverTest < Minitest::Test - - def test_GET_served_via_SKS - uid = 'cloudadmin@leap.se' - key_id = 'E818C478D3141282F7590D29D041EB11B1647490' - stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result)) - stub_sks_get_reponse(key_id, body: file_content(:leap_public_key)) - - start do - params = {query: {"address" => uid}} - get(params) do |http| - assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"] - stop - end - end - end - - def test_POST_served_via_SKS - uid = 'cloudadmin@leap.se' - key_id = 'E818C478D3141282F7590D29D041EB11B1647490' - stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result)) - stub_sks_get_reponse(key_id, body: file_content(:leap_public_key)) - - start do - params = {body: {"address" => uid}} - post(params) do |http| - assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"] - stop - end - end - end - - def test_GET_served_via_couch_not_found - domain = "example.org" - uid = "bananas@" + domain - stub_couch_response(uid, status: 404) do - start do - params = {query: {"address" => uid}, head: {host: domain}} - get(params) do |http| - assert_equal 404, http.response_header.status - stop - end - end - end - end - - def test_GET_served_via_couch_empty_results - domain = "example.org" - uid = "stompy@" + domain - stub_couch_response(uid, body: file_content(:empty_couchdb_result)) do - start do - params = {query: {"address" => uid}, head: {host: domain}} - get(params) do |http| - assert_equal 404, http.response_header.status - stop - end - end - end - end - - def test_GET_served_via_couch_success - domain = "example.org" - uid = "blue@" + domain - stub_couch_response(uid, body: file_content(:blue_couchdb_result)) do - start do - params = {query: {"address" => uid}, head: {host: domain}} - get(params) do |http| - assert_equal file_content(:blue_nickserver_result), http.response - stop - end - end - end - end - - def test_GET_empty - start do - get({}) do |http| - assert_equal "404 Not Found\n", http.response - stop - end - end - end - - protected - - # - # start nickserver - # - def start(timeout = 1) - Timeout::timeout(timeout) do - EM.run do - Nickserver::Server.start - EM.epoll - yield - end - end - rescue Timeout::Error - flunk 'EventMachine was not stopped before the timeout expired' - end - - # - # http GET requests to nickserver - # - def get(params, &block) - request(:get, params, &block) - end - - # - # http POST requests to nickserver - # - def post(params, &block) - request(:post, params, &block) - end - - # - # http request to nickserver - # - # this works because http requests to localhost are not stubbed, but requests to other domains are. - # - def request(method, params) - EventMachine::HttpRequest.new("http://localhost:#{Nickserver::Config.port}/").send(method,params).callback {|http| - # p http.response_header.status - # p http.response_header - # p http.response - yield http - }.errback {|http| - flunk(http.error) if http.error - EM.stop - } - end - - # - # stop nickserver - # - def stop - EM.stop - end - -end -- cgit v1.2.3 From aed9ae1743eaf433f192a4602b2c6012ffb02a25 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 25 May 2016 14:59:16 +0200 Subject: fix our own warnings --- test/unit/hkp_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/unit/hkp_test.rb b/test/unit/hkp_test.rb index fc3cf97..c315726 100644 --- a/test/unit/hkp_test.rb +++ b/test/unit/hkp_test.rb @@ -67,7 +67,6 @@ class HkpTest < Minitest::Test def test_fetch_key_too_short uid = 'chiiph@leap.se' - key_id = '9A753A6B' stub_sks_vindex_reponse(uid, body: file_content(:short_key_vindex_result)) test_em_errback "Nickserver::Hkp::FetchKey.new.get '#{uid}'" do |error| -- cgit v1.2.3 From a58ffc4d3e5bb63b7dc6aea93e76be4aa4c4345b Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 25 May 2016 15:01:41 +0200 Subject: silence all warnings of webmock and evma_httpserver --- test/test_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index c9d8fd4..3222daa 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,9 +1,12 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'rubygems' +require 'kernel_ext' require 'bundler/setup' require 'minitest/autorun' -require 'webmock/minitest' +silence_warnings do + require 'webmock/minitest' +end require 'nickserver' require 'minitest/pride' -- cgit v1.2.3 From 712c1d062c08a58ca8772aafcdc39d0281959b4a Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 25 May 2016 15:04:36 +0200 Subject: add minitest/hell to the mix This will randomize test order and parallelize them iirc --- test/test_helper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index 3222daa..68e9694 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,6 +9,7 @@ silence_warnings do end require 'nickserver' require 'minitest/pride' +require 'minitest/hell' TESTING = true -- cgit v1.2.3 From a89e2ba55399e6bddd9e052cf2064ed0056c958a Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 8 Jun 2016 12:44:38 +0200 Subject: refactor: use new couch_db classes from fetch_key This moves all the em_http related stuff into a single adapter. We're also not using callback and errback inside fetch_key or server for couch requests anymore. Changing the interface for hkp to do the same will follow. --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index 68e9694..afdd3f9 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -58,7 +58,7 @@ class Minitest::Test Nickserver::Config.stub :couch_host, 'notlocalhost' do options = {status: 200, body: ""}.merge(opts) query = "\?key=#{"%22#{uid}%22"}&reduce=false" - stub_http_request(:get, /#{Regexp.escape(Nickserver::Couch::FetchKey.couch_url)}.*#{query}/).to_return(options) + stub_http_request(:get, /#{Regexp.escape(Nickserver::Config.couch_url)}.*#{query}/).to_return(options) yield end end -- cgit v1.2.3 From b68b025a6d39516a6b4d04dd7c1786df50466d2c Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 9 Jun 2016 11:41:56 +0200 Subject: move hkp test to integration tests That's what it actually is --- test/integration/hkp_test.rb | 162 +++++++++++++++++++++++++++++++++++++++++++ test/unit/hkp_test.rb | 162 ------------------------------------------- 2 files changed, 162 insertions(+), 162 deletions(-) create mode 100644 test/integration/hkp_test.rb delete mode 100644 test/unit/hkp_test.rb (limited to 'test') diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb new file mode 100644 index 0000000..0e77253 --- /dev/null +++ b/test/integration/hkp_test.rb @@ -0,0 +1,162 @@ +require 'test_helper' + +class HkpTest < Minitest::Test + + def test_key_info_expired + fetch_key_info(:hkp_vindex_result, 'lemur@leap.se') do |keys| + assert_equal 1, keys.length, 'should find a single key' + assert_equal ['lemur@example.org', 'lemur@leap.se'].sort, keys.first.uids.sort, 'should find both uids' + assert_equal '0EE5BE979282D80B9F7540F1CCD2ED94D21739E9', keys.first.keyid + end + end + + def test_key_info_multiple_valid_results + fetch_key_info :hkp_vindex_result, 'gazelle@leap.se' do |keys| + assert_equal 2, keys.length, 'should find two keys' + assert_equal ['gazelle@leap.se'], keys.first.uids + assert_equal '3790027A', keys.first.keyid + assert keys.last.uids.include? 'gazelle@leap.se' + end + end + + def test_key_info_reject_keysize + fetch_key_info :hkp_vindex_result, 'frog@leap.se' do |keys| + assert_equal 1, keys.length, 'should find one key' # because short key gets ignored + assert_equal '00440025', keys.first.keyid + end + end + + 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 + 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 + end + + def test_fetch_key + uid = 'cloudadmin@leap.se' + key_id = 'E818C478D3141282F7590D29D041EB11B1647490' + 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 + end + end + + def test_fetch_key_not_found + uid = 'cloudadmin@leap.se' + key_id = 'E818C478D3141282F7590D29D041EB11B1647490' + + 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 + 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 + end + + # + # real network tests + # remember: must be run with REAL_NET=true + # + + 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_equal 1, keys.size + assert keys.first.keyid =~ /00440025$/ + end + end + end + + def test_tls_validation_with_real_network + hkp_url = 'https://keys.mayfirst.org/pks/lookup' + ca_file = file_path('mayfirst-ca.pem') + + real_network do + stub_config(:hkp_url, hkp_url) do + stub_config(:hkp_ca_file, ca_file) do + #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_equal 1, keys.size + assert keys.first.keyid =~ /00440025$/ + end + end + end + end + end + + protected + + # + # 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 + 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 + 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) + EM.run do + deferrable = instance_eval(code) + deferrable.callback {|response| + EM.stop + flunk "Expecting errback, but callback invoked with response: #{response}" + } + deferrable.errback {|response| + EM.stop + yield response + return + } + end + assert false, 'should not get here' + 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 + end + +end diff --git a/test/unit/hkp_test.rb b/test/unit/hkp_test.rb deleted file mode 100644 index c315726..0000000 --- a/test/unit/hkp_test.rb +++ /dev/null @@ -1,162 +0,0 @@ -require File.expand_path('test_helper', File.dirname(__FILE__)) - -class HkpTest < Minitest::Test - - def test_key_info_expired - fetch_key_info(:hkp_vindex_result, 'lemur@leap.se') do |keys| - assert_equal 1, keys.length, 'should find a single key' - assert_equal ['lemur@example.org', 'lemur@leap.se'].sort, keys.first.uids.sort, 'should find both uids' - assert_equal '0EE5BE979282D80B9F7540F1CCD2ED94D21739E9', keys.first.keyid - end - end - - def test_key_info_multiple_valid_results - fetch_key_info :hkp_vindex_result, 'gazelle@leap.se' do |keys| - assert_equal 2, keys.length, 'should find two keys' - assert_equal ['gazelle@leap.se'], keys.first.uids - assert_equal '3790027A', keys.first.keyid - assert keys.last.uids.include? 'gazelle@leap.se' - end - end - - def test_key_info_reject_keysize - fetch_key_info :hkp_vindex_result, 'frog@leap.se' do |keys| - assert_equal 1, keys.length, 'should find one key' # because short key gets ignored - assert_equal '00440025', keys.first.keyid - end - end - - 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 - 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 - end - - def test_fetch_key - uid = 'cloudadmin@leap.se' - key_id = 'E818C478D3141282F7590D29D041EB11B1647490' - 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 - end - end - - def test_fetch_key_not_found - uid = 'cloudadmin@leap.se' - key_id = 'E818C478D3141282F7590D29D041EB11B1647490' - - 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 - 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 - end - - # - # real network tests - # remember: must be run with REAL_NET=true - # - - 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_equal 1, keys.size - assert keys.first.keyid =~ /00440025$/ - end - end - end - - def test_tls_validation_with_real_network - hkp_url = 'https://keys.mayfirst.org/pks/lookup' - ca_file = file_path('mayfirst-ca.pem') - - real_network do - stub_config(:hkp_url, hkp_url) do - stub_config(:hkp_ca_file, ca_file) do - #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_equal 1, keys.size - assert keys.first.keyid =~ /00440025$/ - end - end - end - end - end - - protected - - # - # 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 - 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 - 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) - EM.run do - deferrable = instance_eval(code) - deferrable.callback {|response| - EM.stop - flunk "Expecting errback, but callback invoked with response: #{response}" - } - deferrable.errback {|response| - EM.stop - yield response - return - } - end - assert false, 'should not get here' - 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 - end - -end -- cgit v1.2.3 From b4075771b2b1f3c688496d18d7a5a5f1db952004 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 9 Jun 2016 13:51:02 +0200 Subject: refactor: remove EM specific stuff from Hkp::FetchKey interface --- test/integration/hkp_test.rb | 33 +++++++++++++++++++-------------- test/unit/adapters/em_http_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 test/unit/adapters/em_http_test.rb (limited to 'test') 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 -- cgit v1.2.3 From 10a57e4f92432ff2b82c4a6bb5027bb3bcbdfab9 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 10 Jun 2016 10:28:09 +0200 Subject: turn Hkp::FetchKey into Hkp::Source The source was really just an empty shell now that we pushed the em specific stuff further down. --- test/integration/hkp_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb index 6ef52fe..0410c4a 100644 --- a/test/integration/hkp_test.rb +++ b/test/integration/hkp_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'nickserver/hkp/source' class HkpTest < Minitest::Test @@ -110,7 +111,7 @@ class HkpTest < Minitest::Test def assert_response_for_uid(uid, &block) EM.run do - Nickserver::Hkp::FetchKey.new(nil).get(uid, &block) + Nickserver::Hkp::Source.new(nil).query(uid, &block) EM.stop end end -- cgit v1.2.3 From 92c86fc4e1e6dcb86793992e69dfd0608c118c9a Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 11 Jun 2016 10:20:06 +0200 Subject: use the adapter not EM in hkp source --- test/integration/hkp_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb index 0410c4a..3988fa5 100644 --- a/test/integration/hkp_test.rb +++ b/test/integration/hkp_test.rb @@ -1,5 +1,6 @@ require 'test_helper' require 'nickserver/hkp/source' +require 'nickserver/adapters/em_http' class HkpTest < Minitest::Test @@ -111,7 +112,7 @@ class HkpTest < Minitest::Test def assert_response_for_uid(uid, &block) EM.run do - Nickserver::Hkp::Source.new(nil).query(uid, &block) + Nickserver::Hkp::Source.new(Nickserver::Adapters::EmHttp.new).query(uid, &block) EM.stop end end -- cgit v1.2.3 From f567ed80427d43019ceb1aaf77d4bc6c01e62729 Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 11 Jun 2016 15:19:50 +0200 Subject: use adapter for FetchKeyInfo --- test/helpers/test_adapter.rb | 10 ++++++ test/integration/couch_db/source_test.rb | 12 +------ test/integration/hkp_test.rb | 59 +++++++++----------------------- 3 files changed, 27 insertions(+), 54 deletions(-) create mode 100644 test/helpers/test_adapter.rb (limited to 'test') 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 -- cgit v1.2.3 From 93258bd6fe6247e7af67f423243eba9808e920ee Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 15 Jun 2016 10:36:25 +0200 Subject: we don't need FetchKeyInfo anymore including in Source --- test/integration/hkp_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb index 2afd2c0..a824a3f 100644 --- a/test/integration/hkp_test.rb +++ b/test/integration/hkp_test.rb @@ -122,7 +122,7 @@ class HkpTest < Minitest::Test def assert_key_info_for_uid(uid, &block) EM.run do - Nickserver::Hkp::FetchKeyInfo.new(adapter).search uid do |status, keys| + Nickserver::Hkp::Source.new(adapter).search uid do |status, keys| assert_equal 200, status yield keys EM.stop -- cgit v1.2.3