Commit 52306323 authored by Laurent Arnoud's avatar Laurent Arnoud

Add api uri helper for specs

With this patch we are now using configured api host and port instead of
`localhost`. This allow to use a container/vm for the API without
changing the config for the tests
With the stub_request...to_rack the `allow_localhost` option on WebMock
does not seems used
parent 524732d4
......@@ -8,8 +8,8 @@ feature "User views a list" do
list = create(:list, email: email, subscriptions: [subscription1])
stub_request(
:get,
"https://localhost:4443/keys/59C71FB38AEE22E091C78259D06350440F759BD3.json?list_id=#{list.id}"
).to_return(status: 200, body: File.read(File.join(fixture_path, "key.json")))
api_uri_with_path("/keys/59C71FB38AEE22E091C78259D06350440F759BD3.json?list_id=#{list.id}")
).to_return(status: 200, body: read_fixture('key.json'))
sign_in(account)
visit(list_subscriptions_path(list))
......@@ -25,7 +25,7 @@ feature "User views a list" do
list = create(:list, email: email, subscriptions: [subscription1])
stub_request(
:get,
"https://localhost:4443/keys/59C71FB38AEE22E091C78259D06350440F759BD3.json?list_id=#{list.id}"
api_uri_with_path("/keys/59C71FB38AEE22E091C78259D06350440F759BD3.json?list_id=#{list.id}")
).to_return(status: 404, body: "[]")
sign_in(account)
......
......@@ -6,11 +6,11 @@ feature "User edits a subscription" do
admin = create(:subscription, admin: true, email: 'admin@example.org')
subscription = create(:subscription, email: account.email)
_list = create(:list, email: "list1@example.org", subscriptions: [admin, subscription])
stub_request(:get, "https://localhost:4443/subscriptions.json?admin=true&email=subscription1@example.org").
stub_request(:get, api_uri_with_path("/subscriptions.json?admin=true&email=subscription1@example.org")).
to_return(status: 200, body: "[]")
stub_request(:get, "https://localhost:4443/subscriptions.json?email=subscription1@example.org&list_id=1").
stub_request(:get, api_uri_with_path("/subscriptions.json?email=subscription1@example.org&list_id=1")).
to_return(status: 200, body: json_object_as_array('subscription1.json'))
stub_request(:get, "https://localhost:4443/subscriptions.json?email=subscription1@example.org").
stub_request(:get, api_uri_with_path("/subscriptions.json?email=subscription1@example.org")).
to_return(body: json_object_as_array('subscription1.json'))
sign_in(account)
......
......@@ -7,11 +7,11 @@ feature "User views a subscription" do
subscription2 = create(:subscription, email: 'subscription2@example.org')
admin = create(:subscription, admin: true, email: 'admin@example.org')
_list = create(:list, email: "list1@example.org", subscriptions: [admin, subscription1])
stub_request(:get, "https://localhost:4443/subscriptions.json?admin=true&email=subscription1@example.org").
stub_request(:get, api_uri_with_path("/subscriptions.json?admin=true&email=subscription1@example.org")).
to_return(status: 200, body: "[]")
stub_request(:get, "https://localhost:4443/subscriptions.json?email=subscription1@example.org&list_id=1").
stub_request(:get, api_uri_with_path("/subscriptions.json?email=subscription1@example.org&list_id=1")).
to_return(status: 200, body: json_object_as_array('subscription1.json'))
stub_request(:get, "https://localhost:4443/subscriptions.json?email=subscription1@example.org").
stub_request(:get, api_uri_with_path("/subscriptions.json?email=subscription1@example.org")).
to_return(body: json_object_as_array('subscription1.json'))
sign_in(account)
......
......@@ -5,11 +5,11 @@
describe "#key" do
it "returns the the key with the subscriptions fingerprint" do
stub_request(:post, "https://localhost:4443/subscriptions.json").
stub_request(:post, api_uri_with_path("/subscriptions.json")).
with(body: "{\"email\":\"user1@example.org\",\"fingerprint\":\"129A74AD5317457F9E502844A39C61B32003A8D8\",\"admin\":false,\"delivery_enabled\":true,\"list_id\":1}").
to_return(status: 200, body: File.read(fixture_path + "/subscription1.json"))
stub_request(:get, "https://localhost:4443/keys/129A74AD5317457F129A74AD5317457F.json?list_id=1").
to_return(status: 200, body: File.read(fixture_path + "/key.json"))
to_return(status: 200, body: read_fixture("subscription1.json"))
stub_request(:get, api_uri_with_path("/keys/129A74AD5317457F129A74AD5317457F.json?list_id=1")).
to_return(status: 200, body: read_fixture("key.json"))
subscription = create(:subscription, email: "user1@example.org")
......@@ -23,11 +23,11 @@
it " returns the account with the subscriptions email" do
account = create(:account, email: "subscription1@example.org")
stub_request(:post, "https://localhost:4443/subscriptions.json").
stub_request(:post, api_uri_with_path("/subscriptions.json")).
with(body: "{\"email\":\"subscription1@example.org\",\"fingerprint\":\"129A74AD5317457F9E502844A39C61B32003A8D8\",\"admin\":false,\"delivery_enabled\":true,\"list_id\":1}").
to_return(status: 200, body: File.read(fixture_path + "/subscription1.json"))
stub_request(:get, "https://localhost:4443/keys/129A74AD5317457F129A74AD5317457F.json?list_id=1").
to_return(status: 200, body: File.read(fixture_path + "/key.json"))
to_return(status: 200, body: read_fixture("subscription1.json"))
stub_request(:get, api_uri_with_path("/keys/129A74AD5317457F129A74AD5317457F.json?list_id=1")).
to_return(status: 200, body: read_fixture("key.json"))
subscription = create(:subscription, email: "subscription1@example.org")
......
......@@ -21,7 +21,7 @@ RSpec.configure do |config|
config.filter_rails_from_backtrace!
config.before(:each) do
stub_request(:any, /localhost:4443/).to_rack(FakeSchleuderApiDaemon)
stub_request(:any, /#{Conf.api.host}:#{Conf.api.port}/).to_rack(FakeSchleuderApiDaemon)
end
# Add FactoryBots syntax methods
......
......@@ -11,9 +11,17 @@ RSpec.configure do |config|
config.shared_context_metadata_behavior = :apply_to_host_groups
end
def api_uri_with_path(path)
URI.join(Conf.api_uri, path)
end
def read_fixture(file)
File.read(File.join(fixture_path, file))
end
def json_object_as_array(file)
[JSON.parse(File.read(File.join(fixture_path, file)))].to_json
[JSON.parse(read_fixture(file))].to_json
end
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
WebMock.disable_net_connect!
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment