From 3d84da5fbca59b72149e08f4ffae203771169995 Mon Sep 17 00:00:00 2001 From: Kevin Soltysiak Date: Sat, 17 Feb 2024 14:14:38 +0100 Subject: [PATCH] specs: rewrite auth specs --- spec/scalingo/auth/keys_spec.rb | 61 ++++++--------- spec/scalingo/auth/scm_integrations_spec.rb | 61 ++++++--------- spec/scalingo/auth/tokens_spec.rb | 82 ++++++++------------- spec/scalingo/auth/two_factor_auth_spec.rb | 71 +++++------------- spec/scalingo/auth/user_spec.rb | 34 ++++----- 5 files changed, 113 insertions(+), 196 deletions(-) diff --git a/spec/scalingo/auth/keys_spec.rb b/spec/scalingo/auth/keys_spec.rb index 2c9415b..ff4ccc2 100644 --- a/spec/scalingo/auth/keys_spec.rb +++ b/spec/scalingo/auth/keys_spec.rb @@ -1,58 +1,43 @@ require "spec_helper" -RSpec.describe Scalingo::Auth::Keys do - describe_method "all" do - let(:stub_pattern) { "all-200" } +RSpec.describe Scalingo::Auth::Keys, type: :endpoint do + describe "all" do + subject(:response) { instance.all(**arguments) } - it_behaves_like "a collection response" - it_behaves_like "a non-paginated collection" + include_examples "requires authentication" + + it { is_expected.to have_requested(:get, api_path.merge("/keys")) } end - describe_method "create" do - context "success" do - let(:body) { meta[:create][:valid] } - let(:stub_pattern) { "create-201" } + describe "create" do + subject(:response) { instance.create(**arguments) } - it_behaves_like "a singular object response", 201 - end + let(:body) { {field: "value"} } - context "unprocessable" do - let(:body) { meta[:create][:invalid] } - let(:stub_pattern) { "create-422" } + include_examples "requires authentication" - it_behaves_like "an unprocessable request" - end + it { is_expected.to have_requested(:post, api_path.merge("/keys")).with(body: {key: body}) } end - describe_method "show" do - context "success" do - let(:params) { {id: meta[:id]} } - let(:stub_pattern) { "show-200" } + describe "show" do + subject(:response) { instance.show(**arguments) } - it_behaves_like "a singular object response" - end + let(:params) { {id: "key-id"} } - context "not found" do - let(:params) { {id: meta[:not_found_id]} } - let(:stub_pattern) { "show-404" } + include_examples "requires authentication" + include_examples "requires some params", :id - it_behaves_like "a not found response" - end + it { is_expected.to have_requested(:get, api_path.merge("/keys/key-id")) } end - describe_method "destroy" do - context "success" do - let(:params) { {id: meta[:id]} } - let(:stub_pattern) { "destroy-204" } + describe "destroy" do + subject(:response) { instance.destroy(**arguments) } - it_behaves_like "an empty response" - end + let(:params) { {id: "key-id"} } - context "not found" do - let(:params) { {id: meta[:not_found_id]} } - let(:stub_pattern) { "destroy-404" } + include_examples "requires authentication" + include_examples "requires some params", :id - it_behaves_like "a not found response" - end + it { is_expected.to have_requested(:delete, api_path.merge("/keys/key-id")) } end end diff --git a/spec/scalingo/auth/scm_integrations_spec.rb b/spec/scalingo/auth/scm_integrations_spec.rb index 39a562c..e915786 100644 --- a/spec/scalingo/auth/scm_integrations_spec.rb +++ b/spec/scalingo/auth/scm_integrations_spec.rb @@ -1,58 +1,43 @@ require "spec_helper" -RSpec.describe Scalingo::Auth::ScmIntegrations do - describe_method "all" do - let(:stub_pattern) { "all-200" } +RSpec.describe Scalingo::Auth::ScmIntegrations, type: :endpoint do + describe "all" do + subject(:response) { instance.all(**arguments) } - it_behaves_like "a collection response" - it_behaves_like "a non-paginated collection" + include_examples "requires authentication" + + it { is_expected.to have_requested(:get, api_path.merge("/scm_integrations")) } end - describe_method "create" do - context "success" do - let(:body) { meta[:create][:valid] } - let(:stub_pattern) { "create-201" } + describe "create" do + subject(:response) { instance.create(**arguments) } - it_behaves_like "a singular object response", 201 - end + let(:body) { {field: "value"} } - context "failure" do - let(:body) { meta[:create][:invalid] } - let(:stub_pattern) { "create-422" } + include_examples "requires authentication" - it_behaves_like "an unprocessable request" - end + it { is_expected.to have_requested(:post, api_path.merge("/scm_integrations")).with(body: {scm_integration: body}) } end - describe_method "show" do - context "success" do - let(:params) { {id: meta[:id]} } - let(:stub_pattern) { "show-200" } + describe "show" do + subject(:response) { instance.show(**arguments) } - it_behaves_like "a singular object response" - end + let(:params) { {id: "scm-integration-id"} } - context "not found" do - let(:params) { {id: meta[:not_found_id]} } - let(:stub_pattern) { "show-404" } + include_examples "requires authentication" + include_examples "requires some params", :id - it_behaves_like "a not found response" - end + it { is_expected.to have_requested(:get, api_path.merge("/scm_integrations/scm-integration-id")) } end - describe_method "destroy" do - context "success" do - let(:params) { {id: meta[:id]} } - let(:stub_pattern) { "destroy-204" } + describe "destroy" do + subject(:response) { instance.destroy(**arguments) } - it_behaves_like "an empty response" - end + let(:params) { {id: "scm-integration-id"} } - context "not found" do - let(:params) { {id: meta[:not_found_id]} } - let(:stub_pattern) { "destroy-404" } + include_examples "requires authentication" + include_examples "requires some params", :id - it_behaves_like "a not found response" - end + it { is_expected.to have_requested(:delete, api_path.merge("/scm_integrations/scm-integration-id")) } end end diff --git a/spec/scalingo/auth/tokens_spec.rb b/spec/scalingo/auth/tokens_spec.rb index c8af105..9be9ec7 100644 --- a/spec/scalingo/auth/tokens_spec.rb +++ b/spec/scalingo/auth/tokens_spec.rb @@ -1,74 +1,54 @@ require "spec_helper" -RSpec.describe Scalingo::Auth::Tokens do - describe_method "exchange" do - subject { auth_guest.tokens } - - context "with a valid token" do - let(:basic) { {password: meta[:exchange][:valid]} } - let(:stub_pattern) { "exchange-200" } - - it "is successful" do - expect(response).to be_success - expect(response.body).to be_present - end - end +RSpec.describe Scalingo::Auth::Tokens, type: :endpoint do + describe "all" do + subject(:response) { instance.all(**arguments) } - context "with an invalid token" do - let(:basic) { {password: meta[:exchange][:invalid]} } - let(:stub_pattern) { "exchange-401" } + include_examples "requires authentication" - it "is rejected with an valid token" do - expect(response.status).to eq 401 - end - end + it { is_expected.to have_requested(:get, api_path.merge("/tokens")) } end - describe_method "all" do - let(:stub_pattern) { "all-200" } + describe "create" do + subject(:response) { instance.create(**arguments) } + + let(:body) { {field: "value"} } - it_behaves_like "a collection response" - it_behaves_like "a non-paginated collection" + include_examples "requires authentication" + + it { is_expected.to have_requested(:post, api_path.merge("/tokens")).with(body: {token: body}) } end - describe_method "create" do - context "success" do - let(:body) { meta[:create][:valid] } - let(:stub_pattern) { "create-201" } + describe "exchange" do + subject(:response) { instance.exchange(**arguments) } - it_behaves_like "a singular object response", 201 + let(:basic) { {user: nil, password: "yo"} } + let(:expected_headers) do + {"Authorization" => Faraday::Utils.basic_header_from(nil, "yo")} end + + it { is_expected.to have_requested(:post, api_path.merge("/tokens/exchange")).with(headers: expected_headers) } end - describe_method "renew" do - context "success" do - let(:params) { {id: meta[:id]} } - let(:stub_pattern) { "renew-200" } + describe "renew" do + subject(:response) { instance.renew(**arguments) } - it_behaves_like "a singular object response" - end + let(:params) { {id: "token-id"} } - context "not found" do - let(:params) { {id: meta[:not_found_id]} } - let(:stub_pattern) { "renew-404" } + include_examples "requires authentication" + include_examples "requires some params", :id - it_behaves_like "a not found response" - end + it { is_expected.to have_requested(:patch, api_path.merge("/tokens/token-id/renew")) } end - describe_method "destroy" do - context "success" do - let(:params) { {id: meta[:id]} } - let(:stub_pattern) { "destroy-204" } + describe "destroy" do + subject(:response) { instance.destroy(**arguments) } - it_behaves_like "an empty response" - end + let(:params) { {id: "token-id"} } - context "not found" do - let(:params) { {id: meta[:not_found_id]} } - let(:stub_pattern) { "destroy-404" } + include_examples "requires authentication" + include_examples "requires some params", :id - it_behaves_like "a not found response" - end + it { is_expected.to have_requested(:delete, api_path.merge("/tokens/token-id")) } end end diff --git a/spec/scalingo/auth/two_factor_auth_spec.rb b/spec/scalingo/auth/two_factor_auth_spec.rb index 8e39f94..b70c0ba 100644 --- a/spec/scalingo/auth/two_factor_auth_spec.rb +++ b/spec/scalingo/auth/two_factor_auth_spec.rb @@ -1,70 +1,39 @@ require "spec_helper" -RSpec.describe Scalingo::Auth::TwoFactorAuth do - describe_method "status" do - let(:stub_pattern) { "status" } +RSpec.describe Scalingo::Auth::TwoFactorAuth, type: :endpoint do + describe "status" do + subject(:response) { instance.status(**arguments) } - it_behaves_like "a singular object response" - end - - describe_method "initiate" do - context "success" do - let(:body) { meta[:initiate][:valid] } - let(:stub_pattern) { "initiate-success" } + include_examples "requires authentication" - it_behaves_like "a singular object response", 201 - end + it { is_expected.to have_requested(:get, api_path.merge("/client/tfa")) } + end - context "wrong provider" do - let(:body) { meta[:initiate][:invalid] } - let(:stub_pattern) { "initiate-wrong-provider" } + describe "initiate" do + subject(:response) { instance.initiate(**arguments) } - it_behaves_like "a client error" - end + let(:body) { {provider: "value"} } - context "already enabled" do - let(:body) { meta[:initiate][:valid] } - let(:stub_pattern) { "initiate-already-enabled" } + include_examples "requires authentication" - it_behaves_like "a client error" - end + it { is_expected.to have_requested(:post, api_path.merge("/client/tfa")).with(body: {tfa: body}) } end - describe_method "validate" do - context "success" do - let(:body) { meta[:validate][:valid] } - let(:stub_pattern) { "validate-success" } - let(:expected_keys) { %i[codes user] } + describe "validate" do + subject(:response) { instance.validate(**arguments) } - it_behaves_like "a singular object response", 201 - end + let(:body) { {attempt: "value"} } - context "wrong provider" do - let(:body) { meta[:validate][:invalid] } - let(:stub_pattern) { "validate-wrong" } + include_examples "requires authentication" - it_behaves_like "a client error" - end - - context "already enabled" do - let(:body) { meta[:validate][:invalid] } - let(:stub_pattern) { "validate-not-initiated" } - - it_behaves_like "a client error" - end + it { is_expected.to have_requested(:post, api_path.merge("/client/tfa/validate")).with(body: {tfa: body}) } end - describe_method "disable" do - context "success" do - let(:stub_pattern) { "disable-success" } - - it_behaves_like "a singular object response" - end + describe "disable" do + subject(:response) { instance.disable(**arguments) } - context "not enabled" do - let(:stub_pattern) { "disable-not-initiated" } + include_examples "requires authentication" - it_behaves_like "a client error" - end + it { is_expected.to have_requested(:delete, api_path.merge("/client/tfa")) } end end diff --git a/spec/scalingo/auth/user_spec.rb b/spec/scalingo/auth/user_spec.rb index a7db45e..dd779a1 100644 --- a/spec/scalingo/auth/user_spec.rb +++ b/spec/scalingo/auth/user_spec.rb @@ -1,31 +1,29 @@ require "spec_helper" -RSpec.describe Scalingo::Auth::User do - describe_method "self" do - let(:stub_pattern) { "self" } +RSpec.describe Scalingo::Auth::User, type: :endpoint do + describe "self" do + subject(:response) { instance.self(**arguments) } - it_behaves_like "a singular object response" + include_examples "requires authentication" + + it { is_expected.to have_requested(:get, api_path.merge("/users/self")) } end - describe_method "update" do - context "success" do - let(:body) { meta[:update][:valid] } - let(:stub_pattern) { "update-200" } + describe "update" do + subject(:response) { instance.update(**arguments) } - it_behaves_like "a singular object response" - end + let(:body) { {field: "value"} } - context "unprocessable" do - let(:body) { meta[:update][:invalid] } - let(:stub_pattern) { "update-422" } + include_examples "requires authentication" - it_behaves_like "an unprocessable request" - end + it { is_expected.to have_requested(:put, api_path.merge("/users/account")).with(body: {user: body}) } end - describe_method "stop_free_trial" do - let(:stub_pattern) { "stop-free-trial" } + describe "stop free trial" do + subject(:response) { instance.stop_free_trial(**arguments) } + + include_examples "requires authentication" - it_behaves_like "a successful response" + it { is_expected.to have_requested(:post, api_path.merge("/users/stop_free_trial")) } end end