Skip to content

Commit

Permalink
specs: rewrite regional specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Feb 17, 2024
1 parent b1671a6 commit 67cbfa1
Show file tree
Hide file tree
Showing 17 changed files with 561 additions and 823 deletions.
2 changes: 1 addition & 1 deletion lib/scalingo/regional/collaborators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Scalingo
class Regional::Collaborators < API::Endpoint
get :for, "apps/{app_id}/collaborators"
get :accept, "apps/collaboration", root_key: "token"
get :accept, "apps/collaboration?token={token}"
post :invite, "apps/{app_id}/collaborators", root_key: "collaborator"
delete :destroy, "apps/{app_id}/collaborators/{id}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/regional/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class Regional::Environment < API::Endpoint
patch :update, "apps/{app_id}/variables/{id}", root_key: :variable
put :bulk_update, "apps/{app_id}/variables", root_key: :variables
delete :destroy, "apps/{app_id}/variables/{id}"
delete :bulk_destroy, "apps/{app_id}/variables", root_key: :variable_ids
delete :bulk_destroy, "apps/{app_id}/variables", root_key: :variable_ids, params_as_body: true
end
end
3 changes: 2 additions & 1 deletion lib/scalingo/regional/logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def get(url, **params, &block)

## Helper method to avoid having to manually chain two operations
def for(**params, &block)
logs_response = scalingo.apps.logs_url(**params)
params[:id] = params.delete(:app_id) if params[:app_id].present?
logs_response = client.apps.logs_url(**params)

return logs_response unless logs_response.success?

Expand Down
185 changes: 53 additions & 132 deletions spec/scalingo/regional/addons_spec.rb
Original file line number Diff line number Diff line change
@@ -1,175 +1,96 @@
require "spec_helper"

RSpec.describe Scalingo::Regional::Addons do
describe_method "categories" do
context "guest" do
let(:params) { {connected: false} }
let(:expected_count) { 2 }
let(:stub_pattern) { "categories-guest" }

it_behaves_like "a collection response"
it_behaves_like "a non-paginated collection"
end

context "logged" do
let(:params) { {connected: true} }
let(:expected_count) { 2 }
let(:stub_pattern) { "categories-logged" }

it_behaves_like "a collection response"
it_behaves_like "a non-paginated collection"
end
end

describe_method "providers" do
context "guest" do
let(:params) { {connected: false} }
let(:expected_count) { 9 }
let(:stub_pattern) { "providers-guest" }

it_behaves_like "a collection response"
it_behaves_like "a non-paginated collection"
end
RSpec.describe Scalingo::Regional::Addons, type: :endpoint do
let(:app_id) { "my-app-id" }

context "logged" do
let(:params) { {connected: true} }
let(:expected_count) { 11 }
let(:stub_pattern) { "providers-logged" }
describe "providers" do
subject(:response) { instance.providers(**arguments) }

it_behaves_like "a collection response"
it_behaves_like "a non-paginated collection"
end
it { is_expected.to have_requested(:get, api_path.merge("/addon_providers")) }
end

describe_method "provision" do
context "success" do
let(:params) { meta.slice(:app_id) }
let(:body) { meta[:provision][:valid] }
let(:stub_pattern) { "provision-201" }
let(:expected_keys) { %i[addon message] }
describe "categories" do
subject(:response) { instance.categories(**arguments) }

it_behaves_like "a singular object response", 201
end
it { is_expected.to have_requested(:get, api_path.merge("/addon_categories")) }
end

context "failure" do
let(:params) { meta.slice(:app_id) }
let(:body) { meta[:provision][:invalid] }
let(:stub_pattern) { "provision-400" }
describe "for" do
subject(:response) { instance.for(**arguments) }

it_behaves_like "a client error"
end
end
let(:params) { {app_id: app_id} }

describe_method "for" do
context "success" do
let(:params) { meta.slice(:app_id) }
let(:stub_pattern) { "for-200" }
include_examples "requires authentication"
include_examples "requires some params", :app_id

it_behaves_like "a collection response"
it_behaves_like "a non-paginated collection"
end
it { is_expected.to have_requested(:get, api_path.merge("/apps/my-app-id/addons")) }
end

describe_method "find" do
context "success" do
let(:params) { meta.slice(:app_id, :id) }
let(:stub_pattern) { "find-200" }
describe "provision" do
subject(:response) { instance.provision(**arguments) }

it_behaves_like "a singular object response"
end
let(:params) { {app_id: app_id} }
let(:body) { {field: "value"} }

context "not found" do
let(:params) { meta.slice(:app_id).merge(id: meta[:not_found_id]) }
let(:stub_pattern) { "find-404" }
include_examples "requires authentication"
include_examples "requires some params", :app_id

it_behaves_like "a not found response"
end
it { is_expected.to have_requested(:post, api_path.merge("/apps/my-app-id/addons")).with(body: {addon: body}) }
end

describe_method "sso" do
context "success" do
let(:params) { meta.slice(:app_id, :id) }
let(:stub_pattern) { "sso-200" }
describe "find" do
subject(:response) { instance.find(**arguments) }

it_behaves_like "a singular object response"
end
let(:params) { {app_id: app_id, id: "addon-id"} }

context "not found" do
let(:params) { meta.slice(:app_id).merge(id: meta[:not_found_id]) }
let(:stub_pattern) { "sso-404" }
include_examples "requires authentication"
include_examples "requires some params", :app_id, :id

it_behaves_like "a not found response"
end
it { is_expected.to have_requested(:get, api_path.merge("/apps/my-app-id/addons/addon-id")) }
end

describe_method "token" do
context "success" do
let(:params) { meta.slice(:app_id, :id) }
let(:stub_pattern) { "token-200" }
describe "update" do
subject(:response) { instance.update(**arguments) }

it_behaves_like "a singular object response"
end
let(:params) { {app_id: app_id, id: "addon-id"} }
let(:body) { {field: "value"} }

context "not found" do
let(:params) { meta.slice(:app_id).merge(id: meta[:not_found_id]) }
let(:stub_pattern) { "token-404" }
include_examples "requires authentication"
include_examples "requires some params", :app_id, :id

it_behaves_like "a not found response"
end
it { is_expected.to have_requested(:patch, api_path.merge("/apps/my-app-id/addons/addon-id")).with(body: {addon: body}) }
end

describe_method "authenticate!" do
context "success" do
let(:params) { meta.slice(:app_id, :id) }
let(:stub_pattern) { "token-200" }
describe "sso" do
subject(:response) { instance.sso(**arguments) }

it_behaves_like "a singular object response"
it "authenticates" do
response
expect(scalingo.authenticated_for_database?(meta[:id])).to be true
end
end
let(:params) { {app_id: app_id, id: "addon-id"} }

context "not found" do
let(:params) { meta.slice(:app_id).merge(id: meta[:not_found_id]) }
let(:stub_pattern) { "token-404" }
include_examples "requires authentication"
include_examples "requires some params", :app_id, :id

it_behaves_like "a not found response"
end
it { is_expected.to have_requested(:get, api_path.merge("/apps/my-app-id/addons/addon-id/sso")) }
end

describe_method "update" do
context "success" do
let(:params) { meta.slice(:app_id, :id) }
let(:body) { meta[:update][:valid] }
let(:stub_pattern) { "update-200" }
let(:expected_keys) { %i[addon message] }
describe "token" do
subject(:response) { instance.token(**arguments) }

it_behaves_like "a singular object response"
end
let(:params) { {app_id: app_id, id: "addon-id"} }

context "failure" do
let(:params) { meta.slice(:app_id, :id) }
let(:body) { meta[:update][:invalid] }
let(:stub_pattern) { "update-404" }
include_examples "requires authentication"
include_examples "requires some params", :app_id, :id

it_behaves_like "a not found response"
end
it { is_expected.to have_requested(:post, api_path.merge("/apps/my-app-id/addons/addon-id/token")) }
end

describe_method "destroy" do
context "success" do
let(:params) { meta.slice(:app_id, :id) }
let(:stub_pattern) { "destroy-204" }
describe "destroy" do
subject(:response) { instance.destroy(**arguments) }

it_behaves_like "an empty response"
end
let(:params) { {app_id: app_id, id: "addon-id"} }

context "not found" do
let(:params) { meta.slice(:app_id).merge(id: meta[:not_found_id]) }
let(:stub_pattern) { "destroy-404" }
include_examples "requires authentication"
include_examples "requires some params", :app_id, :id

it_behaves_like "a not found response"
end
it { is_expected.to have_requested(:delete, api_path.merge("/apps/my-app-id/addons/addon-id")) }
end
end
Loading

0 comments on commit 67cbfa1

Please sign in to comment.