summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-07-02 12:03:46 +0200
committerAzul <azul@riseup.net>2016-07-02 12:03:46 +0200
commitfb2d7e6f8f1fceefbc8964d34369a867eb8f25bb (patch)
tree672ea8bbf03876d65c90ecb1ed152424e74939e1
parent8a664a39bc3dd77a9c53fa5931f81c2b2b8b7295 (diff)
refactor: replace blocks/yields with returns
This became possible because we now use celluloid. Celluloid handles asynchronity without the need for callbacks or blocks.
-rw-r--r--lib/nickserver/adapters/celluloid_http.rb2
-rw-r--r--lib/nickserver/couch_db/source.rb5
-rw-r--r--lib/nickserver/hkp/client.rb6
-rw-r--r--lib/nickserver/hkp/source.rb35
-rw-r--r--lib/nickserver/request_handler.rb5
-rw-r--r--test/helpers/test_adapter.rb10
-rw-r--r--test/integration/couch_db/source_test.rb8
-rw-r--r--test/integration/nickserver_test.rb7
-rw-r--r--test/unit/adapters/celluloid_http_test.rb12
-rw-r--r--test/unit/couch_db/source_unit_test.rb5
-rw-r--r--test/unit/hkp/client_test.rb53
11 files changed, 96 insertions, 52 deletions
diff --git a/lib/nickserver/adapters/celluloid_http.rb b/lib/nickserver/adapters/celluloid_http.rb
index 1adfaca..d326b8a 100644
--- a/lib/nickserver/adapters/celluloid_http.rb
+++ b/lib/nickserver/adapters/celluloid_http.rb
@@ -13,7 +13,7 @@ module Nickserver::Adapters
params: options[:query],
ssl_context: ctx,
ssl_socket_class: Celluloid::IO::SSLSocket
- yield response.code, response.to_s
+ return response.code, response.to_s
end
def ctx
diff --git a/lib/nickserver/couch_db/source.rb b/lib/nickserver/couch_db/source.rb
index b30fdfc..7c3ad95 100644
--- a/lib/nickserver/couch_db/source.rb
+++ b/lib/nickserver/couch_db/source.rb
@@ -11,9 +11,8 @@ module Nickserver::CouchDB
VIEW = '/_design/Identity/_view/pgp_key_by_email'
def query(nick)
- adapter.get url, query: query_for(nick) do |status, body|
- yield Response.new(nick, status: status, body: body)
- end
+ status, body = adapter.get url, query: query_for(nick)
+ Response.new(nick, status: status, body: body)
end
protected
diff --git a/lib/nickserver/hkp/client.rb b/lib/nickserver/hkp/client.rb
index 6bd239d..d9a9b48 100644
--- a/lib/nickserver/hkp/client.rb
+++ b/lib/nickserver/hkp/client.rb
@@ -21,7 +21,7 @@ module Nickserver; module Hkp
# used to fetch an array of KeyInfo objects that match the given email
#
def get_key_infos_by_email(email, &block)
- get op: 'vindex', search: email, fingerprint: 'on', &block
+ get op: 'vindex', search: email, fingerprint: 'on'
end
#
@@ -35,10 +35,10 @@ module Nickserver; module Hkp
attr_reader :adapter
- def get(query, &block)
+ def get(query)
# in practice, exact=on seems to have no effect
query = {exact: 'on', options: 'mr'}.merge query
- adapter.get Config.hkp_url, query: query, &block
+ adapter.get Config.hkp_url, query: query
end
end
end; end
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
index 9c79546..0d79856 100644
--- a/lib/nickserver/hkp/source.rb
+++ b/lib/nickserver/hkp/source.rb
@@ -14,22 +14,20 @@ require "nickserver/hkp/key_info"
module Nickserver; module Hkp
class Source < Nickserver::Source
- def query(nick, &block)
- 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
+ def query(nick)
+ status, response = search(nick)
+ if status == 200
+ best = pick_best_key(response)
+ get_key_by_fingerprint(nick, best.keyid)
+ else
+ Nickserver::Response.new(status, response)
end
end
- def search(nick, &block)
- client.get_key_infos_by_email(nick) do |status, response|
- parser = ParseKeyInfo.new status, response
- yield parser.status_for(nick), parser.response_for(nick)
- end
+ def search(nick)
+ status, response = client.get_key_infos_by_email(nick)
+ parser = ParseKeyInfo.new status, response
+ return parser.status_for(nick), parser.response_for(nick)
end
protected
@@ -45,12 +43,11 @@ module Nickserver; module Hkp
end
def get_key_by_fingerprint(nick, fingerprint)
- client.get_key_by_fingerprint fingerprint do |status, response|
- if status == 200
- yield Response.new nick, response
- else
- yield Nickserver::Response.new status, "HKP Request failed"
- end
+ status, response = client.get_key_by_fingerprint fingerprint
+ if status == 200
+ Response.new nick, response
+ else
+ Nickserver::Response.new status, "HKP Request failed"
end
end
diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb
index f6954f1..295d3c0 100644
--- a/lib/nickserver/request_handler.rb
+++ b/lib/nickserver/request_handler.rb
@@ -40,9 +40,8 @@ module Nickserver
else
source = Nickserver::Hkp::Source.new(adapter)
end
- source.query(uid) do |response|
- send_response response.status, response.content
- end
+ response = source.query(uid)
+ send_response response.status, response.content
end
#
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/nickserver_test.rb b/test/integration/nickserver_test.rb
index d179d7e..710c3a1 100644
--- a/test/integration/nickserver_test.rb
+++ b/test/integration/nickserver_test.rb
@@ -18,7 +18,12 @@ class NickserverTest < Minitest::Test
def setup
super
- Celluloid.shutdown; Celluloid.boot
+ Celluloid.boot
+ end
+
+ def teardown
+ Celluloid.shutdown
+ super
end
def test_GET_served_via_SKS
diff --git a/test/unit/adapters/celluloid_http_test.rb b/test/unit/adapters/celluloid_http_test.rb
index 87e7f55..4381b8f 100644
--- a/test/unit/adapters/celluloid_http_test.rb
+++ b/test/unit/adapters/celluloid_http_test.rb
@@ -18,18 +18,16 @@ class Nickserver::Adapters::CelluloidHttpTest < Minitest::Test
stub_http_request(:get, url)
.with(query: {key: :value})
.to_return status: 200, body: 'body'
- adapter.get(url, query: {key: :value}) do |status, body|
- assert_equal 200, status
- assert_equal 'body', body
- end
+ 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
- adapter.get url do |status, _body|
- assert_equal 404, status
- end
+ status, _body = adapter.get url
+ assert_equal 404, status
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/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