Skip to content

Commit

Permalink
Rubocop-rspec + code coverage (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: aurelien-reeves-scalingo <[email protected]>
  • Loading branch information
ksol and aurelien-reeves-scalingo authored Feb 19, 2024
1 parent e40d3ae commit 61e0c6a
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 63 deletions.
41 changes: 41 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require:
- standard-custom
- standard-performance
- rubocop-performance
- rubocop-rspec

inherit_gem:
standard: config/base.yml
Expand All @@ -18,3 +19,43 @@ AllCops:
- 'tmp/**/*'
- 'vendor/**/*'
- '.git/**/*'

# Line-base counting is not reliable enough
RSpec/ExampleLength:
Enabled: false

# Deprecated cop, will be removed
RSpec/FilePath:
Enabled: false

# Not convinced by how strict this cop is by default
RSpec/MultipleExpectations:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Enabled: false

# Really not convinced by this one
RSpec/NamedSubject:
Enabled: false

# Default is 3, but 4 is used and relevant (class / method / global state / local state)
RSpec/NestedGroups:
Max: 4

# Not always relevant for libs
RSpec/SpecFilePathFormat:
Exclude:
- 'spec/scalingo/faraday/**/*'

# Not sure about this one - requires more research
RSpec/MessageSpies:
Enabled: false

# Not sure about this one - requires more research
RSpec/StubbedMock:
Enabled: false

# Not sure about this one - requires more research
RSpec/SubjectStub:
Enabled: false
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ source "https://rubygems.org"

# Specify your gem's dependencies in scalingo.gemspec
gemspec

gem "simplecov"
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

A ruby wrapper for the Scalingo API

### Migration from v2

This gem is changing its name from `scalingo-ruby-api` to `scalingo`,
and the versioning does **not** reset; the first major version of `scalingo`
will therefore be `3.x.x`.

You can check the version 2 at [the v2 branch of this repository](https://github.com/Scalingo/scalingo-ruby-api/tree/v2)
> [!WARNING]
> `master` is under development and contains yet-to-be-documented breaking changes. The readme is about the stable v3 interface.

## Installation

Expand Down
2 changes: 2 additions & 0 deletions lib/scalingo/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ def self.register_handler!(method_name, klass)
end
end

# :nocov:
def inspect
str = %(<#{self.class}:0x#{object_id.to_s(16)} url:"#{@url}" methods:)

str << self.class.instance_methods(false).to_s
str << ">"
str
end
# :nocov:

## Faraday objects
def headers
Expand Down
2 changes: 2 additions & 0 deletions lib/scalingo/api/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ def request(method, path, body: nil, root_key: nil, connected: true, basic: nil,
end
end

# :nocov:
def inspect
str = %(<#{self.class}:0x#{object_id.to_s(16)} base_url:"#{@client.url}" endpoints:)

str << self.class.instance_methods(false).to_s
str << ">"
str
end
# :nocov:
end
end
end
2 changes: 2 additions & 0 deletions lib/scalingo/bearer_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(value, expires_at: nil, raise_on_expired: false)
@raise_on_expired = raise_on_expired
end

# :nocov:
def inspect
str = "<#{self.class}:0x#{object_id.to_s(16)} "

Expand All @@ -23,6 +24,7 @@ def inspect
str << %(value:"#{value}">)
str
end
# :nocov:

def raise_on_expired?
@raise_on_expired
Expand Down
2 changes: 2 additions & 0 deletions lib/scalingo/core_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(attributes = {})
@config = Configuration.new(attributes, Scalingo.config)
end

# :nocov:
def inspect
str = %(<#{self.class}:0x#{object_id.to_s(16)} version:"#{Scalingo::VERSION}" authenticated:)

Expand All @@ -26,6 +27,7 @@ def inspect
str << ">"
str
end
# :nocov:

## Sub-clients accessors
def auth
Expand Down
24 changes: 11 additions & 13 deletions spec/scalingo/api/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require "spec_helper"

RSpec.describe Scalingo::API::Client do
subject { described_class.new(url, scalingo: scalingo) }

let(:url) { "http://localhost" }
let(:bearer_token) { "bearer-token" }
let(:configuration) { {} }
let(:scalingo) do
scalingo_client = Scalingo::Client.new
scalingo_client = Scalingo::Client.new(configuration)
scalingo_client.authenticate_with(bearer_token: bearer_token) if bearer_token
scalingo_client
end

subject { described_class.new(url, scalingo: scalingo) }

describe "initialize" do
let(:config) { {default_region: :test} }

Expand Down Expand Up @@ -85,11 +86,11 @@
expect(mock).to receive(:new).with(instance).and_return("1st").once

# Not yet loaded...
expect(instance.instance_variable_get(:@handler)).to eq(nil)
expect(instance.instance_variable_get(:@handler)).to be_nil
instance.handler

# Memoized...
expect(instance.instance_variable_get(:@handler)).not_to eq(nil)
expect(instance.instance_variable_get(:@handler)).not_to be_nil

# More calls won't try to perform more instanciations
instance.handler
Expand All @@ -98,10 +99,7 @@
end

describe "headers" do
before do
expect(scalingo.config).to receive(:user_agent).and_return(user_agent).once
end

let(:configuration) { {user_agent: user_agent} }
let(:user_agent) { "user agent" }
let(:extra_hash) { {"X-Other" => "other"} }
let(:extra_block) {
Expand Down Expand Up @@ -229,8 +227,8 @@
end

describe "connection" do
context "logged" do
context "no fallback to guest" do
context "when logged" do
context "without fallback to guest" do
it "calls and return the authenticated_connection" do
expect(subject).to receive(:authenticated_connection).and_return(:conn)
expect(subject.connection).to eq(:conn)
Expand All @@ -245,10 +243,10 @@
end
end

context "not logged" do
context "when not logged" do
let(:bearer_token) { nil }

context "no fallback to guest" do
context "without fallback to guest" do
it "raises when set to raise" do
expect(scalingo.config).to receive(:raise_on_missing_authentication).and_return(true).once

Expand Down
4 changes: 2 additions & 2 deletions spec/scalingo/api/endpoint_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "spec_helper"

RSpec.describe Scalingo::API::Endpoint do
let(:client) { double }

subject { described_class.new(client) }

let(:client) { double }

describe "initialize" do
it "stores the client" do
instance = described_class.new(:client)
Expand Down
8 changes: 5 additions & 3 deletions spec/scalingo/bearer_token_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
require "spec_helper"

RSpec.describe Scalingo::BearerToken do
subject { described_class.new(value, expires_at: expires_at, raise_on_expired: raise_on_expired) }

let(:value) { "my-token" }
let(:expires_at) { nil }
let(:raise_on_expired) { false }

subject { described_class.new(value, expires_at: expires_at, raise_on_expired: raise_on_expired) }

describe "initialize" do
it "stores the value" do
instance = described_class.new(:value)

expect(instance.value).to eq(:value)
expect(instance.expires_at).to eq nil
expect(instance.expires_at).to be_nil
end

it "stores the expiration" do
Expand All @@ -31,11 +31,13 @@

context "with the expiration in the future" do
let(:expires_at) { Time.current + 1.hour }

it { expect(subject).not_to be_expired }
end

context "with the expiration in the past" do
let(:expires_at) { Time.current - 1.minute }

it { expect(subject).to be_expired }
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/scalingo/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe "token" do
it "wraps the token in a BearerToken" do
expect(subject.token).to eq nil
expect(subject.token).to be_nil

subject.token = "my-token"
expect(subject.token).to be_a(Scalingo::BearerToken)
Expand Down Expand Up @@ -70,23 +70,23 @@
expect(subject.auth.tokens).to receive(:exchange).and_return(fake_response)

expect(subject.authenticate_with(access_token: "access token")).to be false
expect(subject.token).to be nil
expect(subject.token).to be_nil
end
end

context "with bearer token" do
it "only sets the bearer token according to the arguments" do
expect(subject.authenticate_with(bearer_token: "my token")).to be true
expect(subject.token.value).to eq "my token"
expect(subject.token.expires_at).to eq nil
expect(subject.token.expires_at).to be_nil

expect(subject.authenticate_with(bearer_token: Scalingo::BearerToken.new("my token"))).to be true
expect(subject.token.value).to eq "my token"
expect(subject.token.expires_at).to eq nil
expect(subject.token.expires_at).to be_nil

expect(subject.authenticate_with(bearer_token: "my token", expires_at: Time.now + 1.hour)).to be true
expect(subject.token.value).to eq "my token"
expect(subject.token.expires_at).not_to eq nil
expect(subject.token.expires_at).not_to be_nil
end
end
end
Expand Down
42 changes: 21 additions & 21 deletions spec/scalingo/faraday/extract_meta_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

RSpec.shared_examples "no meta extraction" do
it "does not extract meta from response" do
expect(subject.meta).to eq(nil)
expect(subject.meta?).to eq(false)
expect(subject.pagination).to eq(nil)
expect(subject.paginated?).to eq(false)
expect(subject.cursor_pagination).to eq(nil)
expect(subject.cursor_paginated?).to eq(false)
expect(subject.meta).to be_nil
expect(subject.meta?).to be(false)
expect(subject.pagination).to be_nil
expect(subject.paginated?).to be(false)
expect(subject.cursor_pagination).to be_nil
expect(subject.cursor_paginated?).to be(false)
end
end

RSpec.describe Scalingo::ExtractMeta do
subject { client.get("/") }

let(:headers) { {"Content-Type" => content_type}.compact }
let(:content_type) { "application/json" }
let(:body) { nil }
Expand All @@ -27,8 +29,6 @@
end
end

subject { client.get("/") }

context "with a non-json response" do
let(:content_type) { "text/html" }
let(:body) { "string".to_json }
Expand Down Expand Up @@ -64,11 +64,11 @@

it "extracts the meta" do
expect(subject.meta).to eq({a: 1})
expect(subject.meta?).to eq(true)
expect(subject.pagination).to eq(nil)
expect(subject.paginated?).to eq(false)
expect(subject.cursor_pagination).to eq(nil)
expect(subject.cursor_paginated?).to eq(false)
expect(subject.meta?).to be(true)
expect(subject.pagination).to be_nil
expect(subject.paginated?).to be(false)
expect(subject.cursor_pagination).to be_nil
expect(subject.cursor_paginated?).to be(false)
end
end

Expand All @@ -77,11 +77,11 @@

it "extracts the meta" do
expect(subject.meta).to eq({pagination: {page: 1}})
expect(subject.meta?).to eq(true)
expect(subject.meta?).to be(true)
expect(subject.pagination).to eq({page: 1})
expect(subject.paginated?).to eq(true)
expect(subject.cursor_pagination).to eq(nil)
expect(subject.cursor_paginated?).to eq(false)
expect(subject.paginated?).to be(true)
expect(subject.cursor_pagination).to be_nil
expect(subject.cursor_paginated?).to be(false)
end
end

Expand All @@ -90,11 +90,11 @@

it "extracts the meta" do
expect(subject.meta).to eq({cursor_pagination: {next_cursor: 1, has_more: true}})
expect(subject.meta?).to eq(true)
expect(subject.pagination).to eq(nil)
expect(subject.paginated?).to eq(false)
expect(subject.meta?).to be(true)
expect(subject.pagination).to be_nil
expect(subject.paginated?).to be(false)
expect(subject.cursor_pagination).to eq({next_cursor: 1, has_more: true})
expect(subject.cursor_paginated?).to eq(true)
expect(subject.cursor_paginated?).to be(true)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/scalingo/faraday/extract_root_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "scalingo/faraday/extract_root_value"

RSpec.describe Scalingo::ExtractRootValue do
subject { client.get("/") }

let(:status) { 200 }
let(:body) { {a: 1} }

Expand All @@ -14,8 +16,6 @@
end
end

subject { client.get("/") }

context "with a non-successful response" do
let(:status) { 300 }

Expand Down
Loading

0 comments on commit 61e0c6a

Please sign in to comment.