Skip to content

Commit

Permalink
specs: default context, shared groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Feb 17, 2024
1 parent 1a541a8 commit 4f091cd
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions spec/support/shared.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
RSpec.shared_context "default endpoint context" do
let(:bearer_token) { "Bearer token" }

let(:arguments) do
args = {}
args[:connected] = connected if defined?(connected)
args[:basic] = basic if defined?(basic)
args[:body] = body if defined?(body)
args.merge!(params) if defined?(params)
args.compact!

args
end

let(:scalingo_client) do
scalingo = Scalingo::Client.new
scalingo.authenticate_with(bearer_token: bearer_token) if bearer_token
scalingo
end

let(:api_path) { URI.parse("http://localhost") }

let(:api_client) { described_class.module_parent.new(api_path.to_s, scalingo: scalingo_client) }
let(:instance) { described_class.new(api_client) }

subject do
response # need to be defined in the including context
WebMock # returning this lets us use the one-liner `have_requested` syntax
end
end

RSpec.shared_examples "requires authentication" do
context "when the client is not authenticated" do
let(:bearer_token) { nil }

it "raises an exception" do
expect { subject }.to raise_error(Scalingo::Error::Unauthenticated)
end
end
end

RSpec.shared_examples "requires some params" do |*keys|
keys.each do |key|
context "when #{key} is missing" do
before { params.delete(key) }

it "raises an exception" do
expect { subject }.to raise_error(ArgumentError)
end
end
end
end

0 comments on commit 4f091cd

Please sign in to comment.