Skip to content

Commit

Permalink
specs: rewrite auth specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Feb 17, 2024
1 parent e076087 commit 3d84da5
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 196 deletions.
61 changes: 23 additions & 38 deletions spec/scalingo/auth/keys_spec.rb
Original file line number Diff line number Diff line change
@@ -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
61 changes: 23 additions & 38 deletions spec/scalingo/auth/scm_integrations_spec.rb
Original file line number Diff line number Diff line change
@@ -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
82 changes: 31 additions & 51 deletions spec/scalingo/auth/tokens_spec.rb
Original file line number Diff line number Diff line change
@@ -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
71 changes: 20 additions & 51 deletions spec/scalingo/auth/two_factor_auth_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 3d84da5

Please sign in to comment.