summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/helpers/test_adapter.rb10
-rw-r--r--test/integration/couch_db/source_test.rb8
-rw-r--r--test/integration/hkp_test.rb37
-rw-r--r--test/integration/nickserver_test.rb95
-rw-r--r--test/test_helper.rb15
-rw-r--r--test/unit/adapters/celluloid_http_test.rb39
-rw-r--r--test/unit/adapters/em_http_test.rb25
-rw-r--r--test/unit/couch_db/source_unit_test.rb5
-rw-r--r--test/unit/email_address_test.rb22
-rw-r--r--test/unit/hkp/client_test.rb53
-rw-r--r--test/unit/nickname_test.rb28
-rw-r--r--test/unit/request_handler_test.rb8
12 files changed, 196 insertions, 149 deletions
diff --git a/test/helpers/test_adapter.rb b/test/helpers/test_adapter.rb
deleted file mode 100644
index 46d4713..0000000
--- a/test/helpers/test_adapter.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-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 21e3642..cb1153e 100644
--- a/test/integration/couch_db/source_test.rb
+++ b/test/integration/couch_db/source_test.rb
@@ -1,6 +1,5 @@
require 'test_helper'
require 'file_content'
-require 'helpers/test_adapter'
require 'nickserver/couch_db/source'
module Nickserver::CouchDB
@@ -8,12 +7,17 @@ module Nickserver::CouchDB
include FileContent
def test_couch_query_and_response
- adapter = TestAdapter.new 200, file_content(:blue_couchdb_result)
+ adapter = adapter_returns 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
+
+ def adapter_returns(*return_values)
+ adapter = Minitest::Mock.new
+ adapter.expect :get, return_values, [String, Hash]
+ end
end
end
diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb
index a824a3f..09673cf 100644
--- a/test/integration/hkp_test.rb
+++ b/test/integration/hkp_test.rb
@@ -1,9 +1,20 @@
require 'test_helper'
require 'nickserver/hkp/source'
-require 'nickserver/adapters/em_http'
+require 'nickserver/adapters/celluloid_http'
class HkpTest < Minitest::Test
+ def setup
+ super
+ Celluloid.boot
+ end
+
+ def teardown
+ Celluloid.shutdown
+ super
+ end
+
+
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'
@@ -89,9 +100,9 @@ class HkpTest < Minitest::Test
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
+ config.stub(:hkp_url, hkp_url) do
+ config.stub(:hkp_ca_file, ca_file) do
+ #config.stub(:hkp_ca_file, file_path('autistici-ca.pem')) do
assert File.exist?(Nickserver::Config.hkp_ca_file)
uid = 'elijah@riseup.net'
assert_key_info_for_uid uid do |keys|
@@ -112,26 +123,20 @@ class HkpTest < Minitest::Test
end
def assert_response_for_uid(uid, &block)
- EM.run do
- Nickserver::Hkp::Source.new(adapter).query uid do |response|
- yield response
- EM.stop
- end
+ Nickserver::Hkp::Source.new(adapter).query uid do |response|
+ yield response
end
end
def assert_key_info_for_uid(uid, &block)
- EM.run do
- Nickserver::Hkp::Source.new(adapter).search uid do |status, keys|
- assert_equal 200, status
- yield keys
- EM.stop
- end
+ Nickserver::Hkp::Source.new(adapter).search uid do |status, keys|
+ assert_equal 200, status
+ yield keys
end
end
def adapter
- Nickserver::Adapters::EmHttp.new
+ Nickserver::Adapters::CelluloidHttp.new
end
def fetch_key_info(body_source, uid, &block)
diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb
index b4ff4da..710c3a1 100644
--- a/test/integration/nickserver_test.rb
+++ b/test/integration/nickserver_test.rb
@@ -4,18 +4,28 @@ require 'json'
#
# Some important notes to understanding these tests:
#
-# (1) Requests to localhost always bypass HTTP stub.
+# (1) Requests to 127.0.0.1 always bypass HTTP stub.
#
-# (2) All requests to nickserver are to localhost.
+# (2) All requests to nickserver are to 127.0.0.1.
#
# (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).
+# default (127.0.0.1) to a dummy value (notlocalhost).
#
class NickserverTest < Minitest::Test
+ def setup
+ super
+ Celluloid.boot
+ end
+
+ def teardown
+ Celluloid.shutdown
+ super
+ end
+
def test_GET_served_via_SKS
uid = 'cloudadmin@leap.se'
key_id = 'E818C478D3141282F7590D29D041EB11B1647490'
@@ -24,9 +34,8 @@ class NickserverTest < Minitest::Test
start do
params = {query: {"address" => uid}}
- get(params) do |http|
- assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"]
- stop
+ get(params) do |response|
+ assert_equal file_content(:leap_public_key), JSON.parse(response.to_s)["openpgp"]
end
end
end
@@ -39,9 +48,8 @@ class NickserverTest < Minitest::Test
start do
params = {body: {"address" => uid}}
- post(params) do |http|
- assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"]
- stop
+ post(params) do |response|
+ assert_equal file_content(:leap_public_key), JSON.parse(response.to_s)["openpgp"]
end
end
end
@@ -51,10 +59,9 @@ class NickserverTest < Minitest::Test
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
+ params = {query: {"address" => uid}, head: {"Host" => domain}}
+ get(params) do |response|
+ assert_equal 404, response.code
end
end
end
@@ -66,9 +73,8 @@ class NickserverTest < Minitest::Test
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
+ get(params) do |response|
+ assert_equal 404, response.code
end
end
end
@@ -79,10 +85,9 @@ class NickserverTest < Minitest::Test
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
+ params = {query: {"address" => uid}, head: {"Host" => domain}}
+ get(params) do |response|
+ assert_equal file_content(:blue_nickserver_result), response.to_s
end
end
end
@@ -90,9 +95,8 @@ class NickserverTest < Minitest::Test
def test_GET_empty
start do
- get({}) do |http|
- assert_equal "404 Not Found\n", http.response
- stop
+ get({}) do |response|
+ assert_equal "404 Not Found\n", response.to_s
end
end
end
@@ -103,53 +107,36 @@ class NickserverTest < Minitest::Test
# 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'
+ server = Nickserver::ReelServer.new '127.0.0.1', config.port
+ yield server
+ ensure
+ server.terminate if server && server.alive?
end
#
# http GET requests to nickserver
#
- def get(params, &block)
- request(:get, params, &block)
+ def get(options = {}, &block)
+ request(:get, params: options[:query], head: options[:head], &block)
end
#
# http POST requests to nickserver
#
- def post(params, &block)
- request(:post, params, &block)
+ def post(options, &block)
+ request(:post, params: options[:body], head: options[:head], &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
+ # this works because http requests to 127.0.0.1 are not stubbed, but requests to other domains are.
#
- def stop
- EM.stop
+ def request(method, options = {})
+ response = HTTP.
+ headers(options.delete(:head)).
+ request method, "http://127.0.0.1:#{config.port}/", options
+ yield response
end
end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index afdd3f9..1ed2a98 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -7,6 +7,7 @@ require 'minitest/autorun'
silence_warnings do
require 'webmock/minitest'
end
+require 'celluloid/test'
require 'nickserver'
require 'minitest/pride'
require 'minitest/hell'
@@ -48,26 +49,22 @@ class Minitest::Test
def stub_sks_get_reponse(key_id, opts = {})
options = {status: 200, body: ""}.merge(opts)
- stub_http_request(:get, Nickserver::Config.hkp_url).with(
+ stub_http_request(:get, config.hkp_url).with(
query: {op: 'get', search: "0x"+key_id, exact: 'on', options: 'mr'}
).to_return(options)
end
def stub_couch_response(uid, opts = {})
# can't stub localhost, so set couch_host to anything else
- Nickserver::Config.stub :couch_host, 'notlocalhost' do
+ 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::Config.couch_url)}.*#{query}/).to_return(options)
+ stub_http_request(:get, /#{Regexp.escape(config.couch_url)}.*#{query}/).to_return(options)
yield
end
end
- #
- # temporarily stubs the config property for the duration of the given block
- #
- def stub_config(property, value, &block)
- Nickserver::Config.stub(property, value, &block)
+ def config
+ Nickserver::Config
end
-
end
diff --git a/test/unit/adapters/celluloid_http_test.rb b/test/unit/adapters/celluloid_http_test.rb
new file mode 100644
index 0000000..4381b8f
--- /dev/null
+++ b/test/unit/adapters/celluloid_http_test.rb
@@ -0,0 +1,39 @@
+require 'test_helper'
+require 'nickserver/adapters/celluloid_http'
+
+class Nickserver::Adapters::CelluloidHttpTest < Minitest::Test
+
+ def setup
+ super
+ Celluloid.boot
+ end
+
+ def teardown
+ Celluloid.shutdown
+ super
+ end
+
+ def test_successful_request
+ url = 'http://url.to'
+ stub_http_request(:get, url)
+ .with(query: {key: :value})
+ .to_return status: 200, body: 'body'
+ status, body = adapter.get(url, query: {key: :value})
+ assert_equal 200, status
+ assert_equal 'body', body
+ end
+
+ def test_https_for_hkp
+ url = Nickserver::Config.hkp_url
+ real_network do
+ status, _body = adapter.get url
+ assert_equal 404, status
+ end
+ end
+
+ protected
+
+ def adapter
+ @adapter ||= Nickserver::Adapters::CelluloidHttp.new
+ end
+end
diff --git a/test/unit/adapters/em_http_test.rb b/test/unit/adapters/em_http_test.rb
deleted file mode 100644
index 659ff1b..0000000
--- a/test/unit/adapters/em_http_test.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-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
diff --git a/test/unit/couch_db/source_unit_test.rb b/test/unit/couch_db/source_unit_test.rb
index 19ea9bc..fd07808 100644
--- a/test/unit/couch_db/source_unit_test.rb
+++ b/test/unit/couch_db/source_unit_test.rb
@@ -7,10 +7,9 @@ module Nickserver::CouchDB
def test_query
address = "nick@domain.tl"
adapter = Minitest::Mock.new
- adapter.expect :get, nil,
+ adapter.expect :get, [200, nil],
[String, {query: { reduce: "false", key: "\"#{address}\"" }}]
- query = Source.new(adapter)
- query.query address
+ Source.new(adapter).query address
adapter.verify
end
end
diff --git a/test/unit/email_address_test.rb b/test/unit/email_address_test.rb
new file mode 100644
index 0000000..6d57a8c
--- /dev/null
+++ b/test/unit/email_address_test.rb
@@ -0,0 +1,22 @@
+require 'test_helper'
+require 'nickserver/email_address'
+
+class EmailAddressTest < Minitest::Test
+
+ def test_domain
+ nick = Nickserver::EmailAddress.new 'nick@test.me'
+ assert nick.domain?('test.me')
+ assert !nick.domain?('est.me')
+ end
+
+ def test_valid
+ nick = Nickserver::EmailAddress.new 'nick@remote.domain'
+ assert nick.valid?
+ end
+
+ def test_invalid
+ nick = Nickserver::EmailAddress.new 'asdf'
+ assert nick.invalid?
+ end
+
+end
diff --git a/test/unit/hkp/client_test.rb b/test/unit/hkp/client_test.rb
new file mode 100644
index 0000000..9784d0a
--- /dev/null
+++ b/test/unit/hkp/client_test.rb
@@ -0,0 +1,53 @@
+require 'test_helper'
+require 'nickserver/hkp/client'
+
+module Nickserver::Hkp
+ class ClientTest < Minitest::Test
+
+ def test_get_key_infos_by_email
+ adapter_expects_query op: "vindex",
+ search: email,
+ options: "mr",
+ fingerprint: "on",
+ exact: "on"
+ client.get_key_infos_by_email(email)
+ @adapter.verify
+ end
+
+ def test_key_by_fingerprint
+ adapter_expects_query op: "get",
+ search: "0x#{fingerprint}",
+ options: "mr",
+ exact: "on"
+ client.get_key_by_fingerprint(fingerprint)
+ @adapter.verify
+ end
+
+ def client
+ @client ||= Client.new @adapter
+ end
+
+ def adapter_expects_query(query = {})
+ adapter_expects Nickserver::Config.hkp_url, query: query
+ end
+
+ def adapter_expects(*args)
+ @adapter = Minitest::Mock.new
+ @adapter.expect :get, dummy_response,
+ args
+ end
+
+ def email
+ 'dummy_email'
+ end
+
+ def fingerprint
+ 'dummy_fingerprint'
+ end
+
+ def dummy_response
+ [200, 'dummy_response']
+ end
+
+ end
+end
diff --git a/test/unit/nickname_test.rb b/test/unit/nickname_test.rb
deleted file mode 100644
index 8681545..0000000
--- a/test/unit/nickname_test.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-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/request_handler_test.rb b/test/unit/request_handler_test.rb
index c9d316f..ef656f9 100644
--- a/test/unit/request_handler_test.rb
+++ b/test/unit/request_handler_test.rb
@@ -13,6 +13,11 @@ class Nickserver::RequestHandlerTest < Minitest::Test
assert_response status: 500, content: "500 Not a valid address\n"
end
+ def test_missing_domain
+ handle address: ['valid@email.tld']
+ assert_response status: 500, content: "500 HTTP request must include a Host header.\n"
+ end
+
protected
def handle(params = {}, headers = {})
@@ -21,8 +26,7 @@ class Nickserver::RequestHandlerTest < Minitest::Test
end
def assert_response(args)
- args[:content_type] ||= 'text/plain'
- responder.expect :send_response, nil, [args]
+ responder.expect :respond, nil, [args[:status], args[:content]]
handler.respond_to @params, @headers
responder.verify
end