From 9323a451ddfd3ea69ac68d9e24296ae371964598 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Thu, 6 Oct 2022 10:32:26 +0530 Subject: [PATCH 01/17] add rubocop-rspec gem + require rubocop rspec in rubocop yml --- .rubocop.yml | 2 ++ cassette.gemspec | 1 + 2 files changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 1f46aca..376c933 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,5 @@ +require: rubocop-rspec + LineLength: Max: 120 diff --git a/cassette.gemspec b/cassette.gemspec index 77270d6..440c1c1 100644 --- a/cassette.gemspec +++ b/cassette.gemspec @@ -23,6 +23,7 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'rubycas-client' gem.add_development_dependency 'simplecov' gem.add_development_dependency 'rubocop' + gem.add_development_dependency 'rubocop-rspec' gem.add_development_dependency 'simplecov-rcov' gem.add_development_dependency 'simplecov-gem-adapter' gem.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0' From 211f0d51f689869e548f1d75ccfcd362494c33b1 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Thu, 6 Oct 2022 10:47:05 +0530 Subject: [PATCH 02/17] add autocorrectable rubocop fixes --- Gemfile | 2 + Rakefile | 2 + .../authentication/authorities_spec.rb | 23 ++++---- spec/cassette/authentication/cache_spec.rb | 7 ++- spec/cassette/authentication/filter_spec.rb | 48 +++++++++-------- .../authentication/user_factory_spec.rb | 10 ++-- spec/cassette/authentication/user_spec.rb | 46 ++++++++-------- spec/cassette/authentication_spec.rb | 16 +++--- spec/cassette/cache/null_store_spec.rb | 44 ++++++++-------- spec/cassette/cache_spec.rb | 4 +- spec/cassette/client/cache_spec.rb | 4 +- spec/cassette/client_spec.rb | 52 ++++++++++--------- spec/cassette/errors_spec.rb | 22 ++++---- spec/cassette/http/request_spec.rb | 10 ++-- spec/cassette/http/ticket_response_spec.rb | 8 +-- .../rubycas/routing_constraint_spec.rb | 21 ++++---- spec/cassette_spec.rb | 38 +++++++------- spec/integration/cas/client_spec.rb | 33 ++---------- spec/spec_helper.rb | 6 +-- spec/support/controllers/controller_mock.rb | 4 +- 20 files changed, 193 insertions(+), 207 deletions(-) mode change 100644 => 100755 Rakefile diff --git a/Gemfile b/Gemfile index fa75df1..7f4f5e9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec diff --git a/Rakefile b/Rakefile old mode 100644 new mode 100755 index 8bf2be1..48c3d76 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,6 @@ #!/usr/bin/env rake +# frozen_string_literal: true + require 'bundler/gem_tasks' require 'rspec/core/rake_task' diff --git a/spec/cassette/authentication/authorities_spec.rb b/spec/cassette/authentication/authorities_spec.rb index 0d03e20..ba808f5 100644 --- a/spec/cassette/authentication/authorities_spec.rb +++ b/spec/cassette/authentication/authorities_spec.rb @@ -1,8 +1,8 @@ - +# frozen_string_literal: true describe Cassette::Authentication::Authorities do subject do - Cassette::Authentication::Authorities + described_class end describe '#has_role?' do @@ -10,15 +10,15 @@ let(:authorities) { subject.parse(input) } it 'adds the application prefix to roles' do - expect(authorities.has_role?('CREATE-USER')).to eql(true) + expect(authorities.has_role?('CREATE-USER')).to be(true) end it 'ignores role case' do - expect(authorities.has_role?('create-user')).to eql(true) + expect(authorities.has_role?('create-user')).to be(true) end it 'replaces underscores with dashes' do - expect(authorities.has_role?('create_user')).to eql(true) + expect(authorities.has_role?('create_user')).to be(true) end end @@ -46,17 +46,17 @@ context 'CAS authorities parsing' do it 'handles single authority' do input = 'CUSTOMERAPI' - expect(subject.parse(input).authorities).to eq(%w(CUSTOMERAPI)) + expect(subject.parse(input).authorities).to eq(%w[CUSTOMERAPI]) end it 'handles multiple authorities with surrounding []' do input = '[CUSTOMERAPI, SAPI]' - expect(subject.parse(input).authorities).to eq(%w(CUSTOMERAPI SAPI)) + expect(subject.parse(input).authorities).to eq(%w[CUSTOMERAPI SAPI]) end it 'ignores whitespace in multiple authorities' do input = '[CUSTOMERAPI,SAPI]' - expect(subject.parse(input).authorities).to eq(%w(CUSTOMERAPI SAPI)) + expect(subject.parse(input).authorities).to eq(%w[CUSTOMERAPI SAPI]) end it 'returns an empty array when input is nil' do @@ -65,19 +65,20 @@ end context 'with authentication disabled' do + subject { described_class.new('[]') } + before do stub_const('ENV', ENV.to_hash.merge('NOAUTH' => 'true')) end - subject { Cassette::Authentication::Authorities.new('[]') } it '#has_role? returns true for every role' do expect(subject.authorities).to be_empty - expect(subject.has_role?(:can_manage)).to eql(true) + expect(subject.has_role?(:can_manage)).to be(true) end it '#has_raw_role? returns true for every role' do expect(subject.authorities).to be_empty - expect(subject.has_raw_role?('SAPI_CUSTOMER-CREATOR')).to eql(true) + expect(subject.has_raw_role?('SAPI_CUSTOMER-CREATOR')).to be(true) end end end diff --git a/spec/cassette/authentication/cache_spec.rb b/spec/cassette/authentication/cache_spec.rb index de5f0ff..e6f9f0e 100644 --- a/spec/cassette/authentication/cache_spec.rb +++ b/spec/cassette/authentication/cache_spec.rb @@ -1,4 +1,4 @@ -# encoding: utf-8 +# frozen_string_literal: true describe Cassette::Authentication::Cache do subject(:cache) { described_class.new(Logger.new('/dev/null')) } @@ -23,13 +23,12 @@ let(:block) { -> { 1 } } let(:other_block) { -> { 2 } } - before { cache.fetch_authentication(ticket, service, &block) } it { is_expected.to eq(1) } context 'when for a second time' do - it { expect(second_call).to eq(1) } + it { expect(second_call).to eq(1) } it do expect(other_block).not_to receive(:call) @@ -37,7 +36,7 @@ end context 'when calling with a different service' do - it { expect(call_with_other_service).to eq(2) } + it { expect(call_with_other_service).to eq(2) } end end diff --git a/spec/cassette/authentication/filter_spec.rb b/spec/cassette/authentication/filter_spec.rb index 23777e9..49a25df 100644 --- a/spec/cassette/authentication/filter_spec.rb +++ b/spec/cassette/authentication/filter_spec.rb @@ -1,4 +1,4 @@ -# encoding: utf-8 +# frozen_string_literal: true describe Cassette::Authentication::Filter do before do @@ -13,7 +13,7 @@ shared_examples_for 'controller behaviour' do describe '#validate_raw_role!' do - let(:controller) { controller_factory.(described_class).new } + let(:controller) { controller_factory.call(described_class).new } let(:current_user) { instance_double(Cassette::Authentication::User) } before do @@ -47,7 +47,7 @@ end describe '#validate_role!' do - let(:controller) { controller_factory.(described_class).new } + let(:controller) { controller_factory.call(described_class).new } let(:current_user) { instance_double(Cassette::Authentication::User) } before do @@ -95,14 +95,14 @@ it_behaves_like 'with NOAUTH' do context 'and no ticket' do - let(:controller) { controller_factory.(described_class).new } + let(:controller) { controller_factory.call(described_class).new } it_behaves_like 'controller without authentication' end context 'and a ticket header' do let(:controller) do - controller_factory.(described_class).new({}, 'Service-Ticket' => 'le ticket') + controller_factory.call(described_class).new({}, 'Service-Ticket' => 'le ticket') end it_behaves_like 'controller without authentication' @@ -110,7 +110,7 @@ context 'and a ticket param' do let(:controller) do - controller_factory.(described_class).new(ticket: 'le ticket') + controller_factory.call(described_class).new(ticket: 'le ticket') end it_behaves_like 'controller without authentication' @@ -119,12 +119,12 @@ context 'when accepts_authentication_service? returns false' do let(:controller) do - controller_factory.(described_class).new(ticket: 'le ticket') + controller_factory.call(described_class).new(ticket: 'le ticket') end before do expect(controller).to receive(:accepts_authentication_service?) - .with(Cassette.config.service) { false } + .with(Cassette.config.service).and_return(false) end it 'raises a Cassette::Errors::Forbidden' do @@ -135,29 +135,31 @@ context 'when accepts_authentication_service? returns true' do before do - expect(controller).to receive(:accepts_authentication_service?).with(anything) { true } + expect(controller).to receive(:accepts_authentication_service?).with(anything).and_return(true) end context 'with a ticket in the query string *AND* headers' do let(:controller) do - controller_factory.(described_class).new({ 'ticket' => 'le other ticket' }, - 'Service-Ticket' => 'le ticket') + controller_factory.call(described_class).new({ 'ticket' => 'le other ticket' }, + 'Service-Ticket' => 'le ticket') end - it 'should send only the header ticket to validation' do + it 'sends only the header ticket to validation' do controller.validate_authentication_ticket - expect(Cassette::Authentication).to have_received(:validate_ticket).with('le ticket', Cassette.config.service) + expect(Cassette::Authentication).to have_received(:validate_ticket).with('le ticket', + Cassette.config.service) end end context 'with a ticket in the query string' do let(:controller) do - controller_factory.(described_class).new('ticket' => 'le ticket') + controller_factory.call(described_class).new('ticket' => 'le ticket') end - it 'should send the ticket to validation' do + it 'sends the ticket to validation' do controller.validate_authentication_ticket - expect(Cassette::Authentication).to have_received(:validate_ticket).with('le ticket', Cassette.config.service) + expect(Cassette::Authentication).to have_received(:validate_ticket).with('le ticket', + Cassette.config.service) end end @@ -169,7 +171,7 @@ def authentication_service end end - controller_factory.(described_class, mod).new({}, 'Service-Ticket' => 'le ticket') + controller_factory.call(described_class, mod).new({}, 'Service-Ticket' => 'le ticket') end it 'validates with the overriden value and not the config' do @@ -182,7 +184,7 @@ def authentication_service context 'with a ticket in the Service-Ticket header' do let(:controller) do - controller_factory.(described_class).new({}, 'Service-Ticket' => 'le ticket') + controller_factory.call(described_class).new({}, 'Service-Ticket' => 'le ticket') end it 'sends the ticket to validation' do @@ -196,19 +198,19 @@ def authentication_service end describe '#accepts_authentication_service?' do + subject { controller.accepts_authentication_service?(service) } + let(:controller) do - controller_factory.(described_class).new(ticket: 'le ticket') + controller_factory.call(described_class).new(ticket: 'le ticket') end before do allow(Cassette).to receive(:config) { config } end - subject { controller.accepts_authentication_service?(service) } - context 'when config responds to #services' do - let(:subdomain) { "subdomain.acme.org" } - let(:not_related) { "acme.org" } + let(:subdomain) { 'subdomain.acme.org' } + let(:not_related) { 'acme.org' } let(:config) do OpenStruct.new(YAML.load_file('spec/config.yml').merge(services: [subdomain])) diff --git a/spec/cassette/authentication/user_factory_spec.rb b/spec/cassette/authentication/user_factory_spec.rb index 65daa21..bce92a1 100644 --- a/spec/cassette/authentication/user_factory_spec.rb +++ b/spec/cassette/authentication/user_factory_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe Cassette::Rubycas::UserFactory do @@ -9,6 +11,10 @@ end describe '#from_session' do + subject do + mod.from_session(session) + end + let(:session) do name = Faker.name @@ -26,10 +32,6 @@ session[:cas_extra_attributes] end - subject do - mod.from_session(session) - end - context 'with default attributes' do its(:login) { is_expected.to eq(session[:cas_user]) } its(:name) { is_expected.to eq(attributes[:name]) } diff --git a/spec/cassette/authentication/user_spec.rb b/spec/cassette/authentication/user_spec.rb index 299f22c..cef0a38 100644 --- a/spec/cassette/authentication/user_spec.rb +++ b/spec/cassette/authentication/user_spec.rb @@ -1,4 +1,4 @@ - +# frozen_string_literal: true describe Cassette::Authentication::User do let(:base_authority) do @@ -9,7 +9,7 @@ context 'without a config' do it 'forwards authorities parsing' do expect(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil) - Cassette::Authentication::User.new(login: 'john.doe', name: 'John Doe', authorities: '[CUSTOMERAPI, SAPI]') + described_class.new(login: 'john.doe', name: 'John Doe', authorities: '[CUSTOMERAPI, SAPI]') end end @@ -20,51 +20,51 @@ expect(config).to receive(:base_authority).and_return('TESTAPI') expect(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', 'TESTAPI') - Cassette::Authentication::User.new(login: 'john.doe', name: 'John Doe', - authorities: '[CUSTOMERAPI, SAPI]', config: config) + described_class.new(login: 'john.doe', name: 'John Doe', + authorities: '[CUSTOMERAPI, SAPI]', config: config) end end end describe '#has_role?' do let(:user) do - Cassette::Authentication::User.new(login: 'john.doe', name: 'John Doe', - authorities: "[#{base_authority}, SAPI, #{base_authority}_CREATE-USER]") + described_class.new(login: 'john.doe', name: 'John Doe', + authorities: "[#{base_authority}, SAPI, #{base_authority}_CREATE-USER]") end it 'adds the application prefix to roles' do - expect(user.has_role?('CREATE-USER')).to eql(true) + expect(user.has_role?('CREATE-USER')).to be(true) end it 'ignores role case' do - expect(user.has_role?('create-user')).to eql(true) + expect(user.has_role?('create-user')).to be(true) end it 'replaces underscores with dashes' do - expect(user.has_role?('create_user')).to eql(true) + expect(user.has_role?('create_user')).to be(true) end end context 'user types' do - context '#employee?' do + describe '#employee?' do it 'returns true when user is an employee' do - expect(Cassette::Authentication::User.new(type: 'employee')).to be_employee - expect(Cassette::Authentication::User.new(type: 'Employee')).to be_employee - expect(Cassette::Authentication::User.new(type: :employee)).to be_employee - expect(Cassette::Authentication::User.new(type: 'customer')).not_to be_employee - expect(Cassette::Authentication::User.new(type: nil)).not_to be_employee - expect(Cassette::Authentication::User.new(type: '')).not_to be_employee + expect(described_class.new(type: 'employee')).to be_employee + expect(described_class.new(type: 'Employee')).to be_employee + expect(described_class.new(type: :employee)).to be_employee + expect(described_class.new(type: 'customer')).not_to be_employee + expect(described_class.new(type: nil)).not_to be_employee + expect(described_class.new(type: '')).not_to be_employee end end - context '#customer?' do + describe '#customer?' do it 'returns true when the user is a customer' do - expect(Cassette::Authentication::User.new(type: 'customer')).to be_customer - expect(Cassette::Authentication::User.new(type: 'Customer')).to be_customer - expect(Cassette::Authentication::User.new(type: :customer)).to be_customer - expect(Cassette::Authentication::User.new(type: 'employee')).not_to be_customer - expect(Cassette::Authentication::User.new(type: nil)).not_to be_customer - expect(Cassette::Authentication::User.new(type: '')).not_to be_customer + expect(described_class.new(type: 'customer')).to be_customer + expect(described_class.new(type: 'Customer')).to be_customer + expect(described_class.new(type: :customer)).to be_customer + expect(described_class.new(type: 'employee')).not_to be_customer + expect(described_class.new(type: nil)).not_to be_customer + expect(described_class.new(type: '')).not_to be_customer end end end diff --git a/spec/cassette/authentication_spec.rb b/spec/cassette/authentication_spec.rb index 36db87a..d6b7ba9 100644 --- a/spec/cassette/authentication_spec.rb +++ b/spec/cassette/authentication_spec.rb @@ -1,12 +1,13 @@ -# encoding: utf-8 -describe Cassette::Authentication do - let(:cache) { instance_double(Cassette::Authentication::Cache) } - let(:http) { instance_double(Cassette::Http::Request) } +# frozen_string_literal: true +describe Cassette::Authentication do subject(:authentication) do - Cassette::Authentication.new(cache: cache, http_client: http) + described_class.new(cache: cache, http_client: http) end + let(:cache) { instance_double(Cassette::Authentication::Cache) } + let(:http) { instance_double(Cassette::Http::Request) } + describe '#ticket_user' do subject(:ticket_user) { authentication.ticket_user(ticket) } @@ -21,6 +22,7 @@ end it { is_expected.to eql(cached_value) } + it 'calls Cache#fetch_authentication with ticket and service' do expect(cache).to receive(:fetch_authentication) .with(ticket, service) @@ -52,7 +54,7 @@ context 'with a failed CAS response' do before do allow(http).to receive(:get).with(anything, anything) - .and_return(OpenStruct.new(body: fixture('cas/fail.xml'))) + .and_return(OpenStruct.new(body: fixture('cas/fail.xml'))) end it 'returns nil' do @@ -63,7 +65,7 @@ context 'with a successful CAS response' do before do allow(http).to receive(:get).with(anything, anything) - .and_return(OpenStruct.new(body: fixture('cas/success.xml'))) + .and_return(OpenStruct.new(body: fixture('cas/success.xml'))) end it 'returns an User' do diff --git a/spec/cassette/cache/null_store_spec.rb b/spec/cassette/cache/null_store_spec.rb index 425159d..2a292c9 100644 --- a/spec/cassette/cache/null_store_spec.rb +++ b/spec/cassette/cache/null_store_spec.rb @@ -1,21 +1,21 @@ -# encoding: utf-8 +# frozen_string_literal: true -require "cassette/cache/null_store" +require 'cassette/cache/null_store' describe Cassette::Cache::NullStore do subject(:cache) { described_class.new } describe '#read' do it 'always return nils' do - expect(cache.read("key")).to be_nil + expect(cache.read('key')).to be_nil - cache.write("key", "value") + cache.write('key', 'value') - expect(cache.read("key")).to be_nil + expect(cache.read('key')).to be_nil end it 'accepts options' do - expect(cache.read("key", raw: true)).to be_nil + expect(cache.read('key', raw: true)).to be_nil end end @@ -27,52 +27,52 @@ describe '#write' do it 'returns true' do - expect(cache.write("key", "value")).to eq(true) + expect(cache.write('key', 'value')).to eq(true) end it 'accepts options' do - expect(cache.write("key", "value", expires_in: 3600)).to eq(true) + expect(cache.write('key', 'value', expires_in: 3600)).to eq(true) end it 'does not actually write' do - cache.write("key", "value") + cache.write('key', 'value') - expect(cache.read("key")).to be_nil + expect(cache.read('key')).to be_nil end end - describe "#increment" do + describe '#increment' do it 'returns 0' do - expect(cache.increment("key")).to be_zero + expect(cache.increment('key')).to be_zero end it '"accepts" a value to increment by' do - expect(cache.increment("key", 2)).to be_zero + expect(cache.increment('key', 2)).to be_zero end it '"accepts" options' do - expect(cache.increment("key", 2, raw: true)).to be_zero + expect(cache.increment('key', 2, raw: true)).to be_zero end it 'does not really increment' do - cache.increment("key") + cache.increment('key') - expect(cache.increment("key")).to be_zero + expect(cache.increment('key')).to be_zero end end - describe "#fetch" do - it "always calls the block" do + describe '#fetch' do + it 'always calls the block' do counter = 0 - cache.fetch("key") { counter += 1 } - cache.fetch("key") { counter += 1 } + cache.fetch('key') { counter += 1 } + cache.fetch('key') { counter += 1 } expect(counter).to eq(2) end - it "accepts options" do - expect(cache.fetch("key", expires_in: 3600) { 0 }).to eq(0) + it 'accepts options' do + expect(cache.fetch('key', expires_in: 3600) { 0 }).to eq(0) end end end diff --git a/spec/cassette/cache_spec.rb b/spec/cassette/cache_spec.rb index d7b19e1..7d2acbb 100644 --- a/spec/cassette/cache_spec.rb +++ b/spec/cassette/cache_spec.rb @@ -1,8 +1,8 @@ -# encoding: utf-8 +# frozen_string_literal: true describe Cassette::Cache do subject do - cached() + cached end describe '#backend' do diff --git a/spec/cassette/client/cache_spec.rb b/spec/cassette/client/cache_spec.rb index 8fdb90d..1d9fd45 100644 --- a/spec/cassette/client/cache_spec.rb +++ b/spec/cassette/client/cache_spec.rb @@ -1,6 +1,4 @@ -# encoding: utf-8 - - +# frozen_string_literal: true describe Cassette::Client::Cache do it 'uses the cache store set in configuration' do diff --git a/spec/cassette/client_spec.rb b/spec/cassette/client_spec.rb index a6c1e55..45bf289 100644 --- a/spec/cassette/client_spec.rb +++ b/spec/cassette/client_spec.rb @@ -1,4 +1,4 @@ -# encoding: utf-8 +# frozen_string_literal: true require 'spec_helper' @@ -8,12 +8,12 @@ let(:options) do { http_client: http, - cache: cache, + cache: cache } end let(:client) do - Cassette::Client.new(options) + described_class.new(options) end describe '#health_check' do @@ -68,23 +68,23 @@ end it 'returns the tgt and logs correctly' do - Cassette::Client.cache.backend.clear + described_class.cache.backend.clear tgt = 'TGT-Something-example' - response = double('response', headers: {'Location' => "tickets/#{tgt}"}) + response = double('response', headers: { 'Location' => "tickets/#{tgt}" }) allow(http).to receive(:post).and_return(response) logger = spy(:logger) Cassette.logger = logger - client = Cassette::Client.new(http_client: http) + client = described_class.new(http_client: http) # exercise result = client.tgt('user', 'pass') # verify expect(result).to eq(tgt) - expect(logger).to have_received(:info).with("TGT cache miss").ordered + expect(logger).to have_received(:info).with('TGT cache miss').ordered expect(logger).to have_received(:info).with("TGT is #{tgt}").ordered end end @@ -95,19 +95,19 @@ end it 'returns the tgt from the cache and logs correctly' do - Cassette::Client.cache.backend.clear + described_class.cache.backend.clear tgt = 'TGT-Something-example' - response = double('response', headers: {'Location' => "tickets/#{tgt}"}) + response = double('response', headers: { 'Location' => "tickets/#{tgt}" }) allow(http).to receive(:post).and_return(response) # this first call is to set the cache - client = Cassette::Client.new(http_client: http) + client = described_class.new(http_client: http) result = client.tgt('user', 'pass') logger = spy(:logger) Cassette.logger = logger - client = Cassette::Client.new(http_client: http) + client = described_class.new(http_client: http) # exercise result = client.tgt('user', 'pass') @@ -118,23 +118,23 @@ end it 'generates another tgt when the param force is true' do - Cassette::Client.cache.backend.clear + described_class.cache.backend.clear tgt = 'TGT-Something-example' - response = double('response', headers: {'Location' => "tickets/#{tgt}"}) + response = double('response', headers: { 'Location' => "tickets/#{tgt}" }) allow(http).to receive(:post).and_return(response) # this first call is to set the cache - client = Cassette::Client.new(http_client: http) + client = described_class.new(http_client: http) result = client.tgt('user', 'pass') tgt2 = 'TGT2-Something-example' - response = double('response', headers: {'Location' => "tickets/#{tgt2}"}) + response = double('response', headers: { 'Location' => "tickets/#{tgt2}" }) allow(http).to receive(:post).and_return(response) logger = spy(:logger) Cassette.logger = logger - client = Cassette::Client.new(http_client: http) + client = described_class.new(http_client: http) # exercise force = true @@ -142,7 +142,7 @@ # verify expect(result).to eq(tgt2) - expect(logger).to have_received(:info).with("TGT cache miss").ordered + expect(logger).to have_received(:info).with('TGT cache miss').ordered expect(logger).to have_received(:info).with("TGT is #{tgt2}").ordered end end @@ -150,6 +150,7 @@ describe '#st' do subject { client.st(tgt_param, service, force) } + let(:service) { 'example.org' } let(:tgt) { 'TGT-Example' } let(:st) { 'ST-Something-example' } @@ -190,7 +191,7 @@ end context 'when tgt is a callable' do - let(:tgt_param) { ->{ tgt } } + let(:tgt_param) { -> { tgt } } it_behaves_like 'http client interactions' end @@ -198,7 +199,7 @@ context 'cache control' do before do allow(cache).to receive(:fetch_st).with(tgt, service, hash_including(force: force)) - .and_return(st) + .and_return(st) end shared_context 'controlling the force' do @@ -227,6 +228,7 @@ describe '#st_for' do subject { client.st_for(service) } + let(:service) { 'example.org' } let(:cached_tgt) { 'TGT-Something' } let(:tgt) { 'TGT-Something-NEW' } @@ -242,11 +244,11 @@ .and_return(tgt_response) allow(http).to receive(:post).with(%r{/v1/tickets/#{tgt}\z}, service: service) - .and_return(st_response) + .and_return(st_response) end let(:st_response) { Faraday::Response.new(body: st) } - let(:tgt_response) { Faraday::Response.new(response_headers: {'Location' => "/v1/tickets/#{tgt}"}) } + let(:tgt_response) { Faraday::Response.new(response_headers: { 'Location' => "/v1/tickets/#{tgt}" }) } it 'returns the generated st' do expect(subject).to eq st @@ -272,7 +274,7 @@ allow(cache).to receive(:fetch_st).with(tgt, service, hash_including(force: false)).and_yield allow(http).to receive(:post).with(%r{/v1/tickets/#{tgt}\z}, service: service) - .and_return(st_response) + .and_return(st_response) end let(:st_response) { Faraday::Response.new(body: st) } @@ -306,17 +308,17 @@ allow(cache).to receive(:fetch_st).and_yield allow(http).to receive(:post).with(%r{/v1/tickets/#{cached_tgt}\z}, service: service) - .and_raise(Cassette::Errors::NotFound) + .and_raise(Cassette::Errors::NotFound) allow(http).to receive(:post) .with(%r{/v1/tickets\z}, username: Cassette.config.username, password: Cassette.config.password) .and_return(tgt_response) allow(http).to receive(:post).with(%r{/v1/tickets/#{tgt}\z}, service: service) - .and_return(st_response) + .and_return(st_response) end - let(:tgt_response) { Faraday::Response.new(response_headers: {'Location' => "/v1/tickets/#{tgt}"}) } + let(:tgt_response) { Faraday::Response.new(response_headers: { 'Location' => "/v1/tickets/#{tgt}" }) } let(:st_response) { Faraday::Response.new(body: st) } it 'calls #fetch_st twice' do diff --git a/spec/cassette/errors_spec.rb b/spec/cassette/errors_spec.rb index f07acb7..87475a1 100644 --- a/spec/cassette/errors_spec.rb +++ b/spec/cassette/errors_spec.rb @@ -1,29 +1,27 @@ -# encoding: UTF-8 - - +# frozen_string_literal: true describe Cassette::Errors do describe Cassette::Errors::Base do describe '#code' do it 'returns the HTTP status code accordlingly' do - expect(Cassette::Errors::Forbidden.new.code).to eql(403) - expect(Cassette::Errors::NotFound.new.code).to eql(404) - expect(Cassette::Errors::InternalServerError.new.code).to eql(500) + expect(Cassette::Errors::Forbidden.new.code).to be(403) + expect(Cassette::Errors::NotFound.new.code).to be(404) + expect(Cassette::Errors::InternalServerError.new.code).to be(500) end end end describe '.raise_by_code' do it 'raises the correct exception for the status code' do - expect { Cassette::Errors.raise_by_code(404) }.to raise_error(Cassette::Errors::NotFound) - expect { Cassette::Errors.raise_by_code(403) }.to raise_error(Cassette::Errors::Forbidden) - expect { Cassette::Errors.raise_by_code(412) }.to raise_error(Cassette::Errors::PreconditionFailed) - expect { Cassette::Errors.raise_by_code(500) }.to raise_error(Cassette::Errors::InternalServerError) + expect { described_class.raise_by_code(404) }.to raise_error(Cassette::Errors::NotFound) + expect { described_class.raise_by_code(403) }.to raise_error(Cassette::Errors::Forbidden) + expect { described_class.raise_by_code(412) }.to raise_error(Cassette::Errors::PreconditionFailed) + expect { described_class.raise_by_code(500) }.to raise_error(Cassette::Errors::InternalServerError) end it 'raises internal server error for unmapped errors' do - expect { Cassette::Errors.raise_by_code(406) }.to raise_error(Cassette::Errors::InternalServerError) - expect { Cassette::Errors.raise_by_code(200) }.to raise_error(Cassette::Errors::InternalServerError) + expect { described_class.raise_by_code(406) }.to raise_error(Cassette::Errors::InternalServerError) + expect { described_class.raise_by_code(200) }.to raise_error(Cassette::Errors::InternalServerError) end end end diff --git a/spec/cassette/http/request_spec.rb b/spec/cassette/http/request_spec.rb index fbf583f..ef670a5 100644 --- a/spec/cassette/http/request_spec.rb +++ b/spec/cassette/http/request_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe Cassette::Http::Request do subject(:request) { described_class } @@ -5,26 +7,26 @@ subject(:post) { request.post(path, payload) } let(:uri) { "#{Cassette.config.base}#{path}" } - let(:path) { "/something" } + let(:path) { '/something' } let(:payload) { { ping: :pong } } let(:response) do { headers: { 'Content-Type' => 'application/json' }, - body: { ok: :true }.to_json, + body: { ok: true }.to_json, status: 200 } end before { stub_request(:post, uri).to_return(response) } - it 'performs a http post request with the proper params'do + it 'performs a http post request with the proper params' do post expect(a_request(:post, uri).with(body: 'ping=pong')).to have_been_made end it do - is_expected.to have_attributes( + expect(subject).to have_attributes( headers: { 'Content-Type' => 'application/json' }, body: '{"ok":"true"}', status: 200 diff --git a/spec/cassette/http/ticket_response_spec.rb b/spec/cassette/http/ticket_response_spec.rb index 7ebf96e..e8fec47 100644 --- a/spec/cassette/http/ticket_response_spec.rb +++ b/spec/cassette/http/ticket_response_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe Cassette::Http::TicketResponse do subject(:ticket_response) { described_class.new(xml_response) } @@ -8,7 +10,7 @@ it { is_expected.to eq('test-user') } - context "when response isn't successful" do + context "when response isn't successful" do let(:xml_response) { fixture('cas/fail.xml') } it { is_expected.to be_nil } @@ -20,7 +22,7 @@ it { is_expected.to eq('Test System') } - context "when response isn't successful" do + context "when response isn't successful" do let(:xml_response) { fixture('cas/fail.xml') } it { is_expected.to be_nil } @@ -32,7 +34,7 @@ it { is_expected.to eq('[CUPOM, AUDITING,]') } - context "when response isn't successful" do + context "when response isn't successful" do let(:xml_response) { fixture('cas/fail.xml') } it { is_expected.to be_nil } diff --git a/spec/cassette/rubycas/routing_constraint_spec.rb b/spec/cassette/rubycas/routing_constraint_spec.rb index 23c987a..5a93eee 100644 --- a/spec/cassette/rubycas/routing_constraint_spec.rb +++ b/spec/cassette/rubycas/routing_constraint_spec.rb @@ -1,22 +1,14 @@ -# encoding: utf-8 +# frozen_string_literal: true require 'spec_helper' describe Cassette::Rubycas::RoutingConstraint do describe '#matches?' do - let(:request) do - OpenStruct.new(session: session) - end - - let(:user) { instance_double(Cassette::Authentication::User) } - let(:constraint) { described_class.new(role, options) } - subject { constraint.matches?(request) } - before do - allow(constraint).to receive(:from_session).with(session).and_return(user) + let(:request) do + OpenStruct.new(session: session) end - let(:session) do { cas_user: 'test.user', @@ -27,6 +19,13 @@ } end + let(:user) { instance_double(Cassette::Authentication::User) } + let(:constraint) { described_class.new(role, options) } + + before do + allow(constraint).to receive(:from_session).with(session).and_return(user) + end + context 'with no options' do let(:role) { :admin } let(:options) { {} } diff --git a/spec/cassette_spec.rb b/spec/cassette_spec.rb index f9e6741..74adf01 100644 --- a/spec/cassette_spec.rb +++ b/spec/cassette_spec.rb @@ -1,4 +1,4 @@ - +# frozen_string_literal: true describe Cassette do def keeping_logger(&block) @@ -9,14 +9,13 @@ def keeping_logger(&block) describe '.config' do it 'defines Cassette initial configuration based on a OpenStruct' do - config = OpenStruct.new( YAML.load_file('spec/config.yml') ) - Cassette.config = config + described_class.config = config - expect(Cassette.config) + expect(described_class.config) .to eq(config) end @@ -29,9 +28,9 @@ def keeping_logger(&block) # Removing tls_version field config.delete_field(:tls_version) - Cassette.config = config + described_class.config = config - expect(Cassette.config.tls_version) + expect(described_class.config.tls_version) .to eq('TLSv1_2') end end @@ -42,9 +41,9 @@ def keeping_logger(&block) YAML.load_file('spec/config.yml') ) - Cassette.config = config + described_class.config = config - expect(Cassette.config.tls_version) + expect(described_class.config.tls_version) .to eq('TLSv1_2') end end @@ -58,9 +57,9 @@ def keeping_logger(&block) # Removing verify_ssl field config.delete_field(:verify_ssl) - Cassette.config = config + described_class.config = config - expect(Cassette.config.verify_ssl) + expect(described_class.config.verify_ssl) .to be false end end @@ -71,9 +70,9 @@ def keeping_logger(&block) YAML.load_file('spec/config.yml') ) - Cassette.config = config + described_class.config = config - expect(Cassette.config.verify_ssl) + expect(described_class.config.verify_ssl) .to be true end end @@ -81,27 +80,28 @@ def keeping_logger(&block) describe '.logger' do it 'returns a default instance' do - expect(Cassette.logger).not_to be_nil - expect(Cassette.logger.is_a?(Logger)).to eql(true) + expect(described_class.logger).not_to be_nil + expect(described_class.logger.is_a?(Logger)).to be(true) end it 'returns rails logger when Rails is available' do keeping_logger do - Cassette.logger = nil + described_class.logger = nil rails = double('Rails') expect(rails).to receive(:logger).and_return(rails).at_least(:once) stub_const('Rails', rails) - expect(Cassette.logger).to eql(rails) + expect(described_class.logger).to eql(rails) end end end describe '.logger=' do - let(:logger) { Logger.new(STDOUT) } + let(:logger) { Logger.new($stdout) } + it 'defines the logger instance' do keeping_logger do - Cassette.logger = logger - expect(Cassette.logger).to eq(logger) + described_class.logger = logger + expect(described_class.logger).to eq(logger) end end end diff --git a/spec/integration/cas/client_spec.rb b/spec/integration/cas/client_spec.rb index e3e2b9c..111649b 100644 --- a/spec/integration/cas/client_spec.rb +++ b/spec/integration/cas/client_spec.rb @@ -1,7 +1,8 @@ -# encoding: utf-8 +# frozen_string_literal: true + RSpec.describe 'Cassette::Client, Cassette::Authentication integration' do shared_examples_for 'a Cassette client and validator' do - let(:config) { fail 'implement config!' } + let(:config) { raise 'implement config!' } let(:client) { Cassette::Client.new(config: config) } let(:authentication) { Cassette::Authentication.new(config: config) } @@ -17,32 +18,4 @@ expect(user.login).to eql(config.username) end end - - context 'with a configuration' do - # it_behaves_like "a Cassette client and validator" do - # let(:config) do - # OpenStruct.new( - # username: "test.user", - # password: "anothersecret", - # base: "https://anothercas.example.org", - # service: "qualquercoisa" - # base_authority: "SYSTEM" - # ) - # end - # end - end - - context 'with a another configuration' do - # it_behaves_like "a Cassette client and validator" do - # let(:config) do - # OpenStruct.new( - # username: "test.user", - # password: "anothersecret", - # base: "https://anothercas.example.org", - # service: "qualquercoisa" - # base_authority: "SYSTEM" - # ) - # end - # end - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7b04341..d31c4bb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'simplecov' require 'simplecov-rcov' require 'simplecov-gem-adapter' @@ -5,9 +7,7 @@ require 'webmock/rspec' require 'rspec/its' require 'faker' -if RUBY_VERSION >= '2.4.0' - require 'pry-byebug' -end +require 'pry-byebug' if RUBY_VERSION >= '2.4.0' Dir['spec/support/**/*.rb'].each { |f| load f } diff --git a/spec/support/controllers/controller_mock.rb b/spec/support/controllers/controller_mock.rb index 3136798..b19fa01 100644 --- a/spec/support/controllers/controller_mock.rb +++ b/spec/support/controllers/controller_mock.rb @@ -1,4 +1,4 @@ -# encoding: utf-8 +# frozen_string_literal: true require 'active_support/core_ext/hash/indifferent_access' @@ -16,6 +16,7 @@ def LegacyControllerMock(*mods) class ControllerMock attr_accessor :params, :request, :current_user + def self.before_action(*); end def initialize(params = {}, headers = {}) @@ -26,6 +27,7 @@ def initialize(params = {}, headers = {}) class LegacyControllerMock attr_accessor :params, :request, :current_user + def self.before_filter(*); end def initialize(params = {}, headers = {}) From 9e5a9e30a21992d0ed31de922e207841ee33310b Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Thu, 6 Oct 2022 10:58:27 +0530 Subject: [PATCH 03/17] resolve rubocop rspec issues in authorities spec --- .../authentication/authorities_spec.rb | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/spec/cassette/authentication/authorities_spec.rb b/spec/cassette/authentication/authorities_spec.rb index ba808f5..025657a 100644 --- a/spec/cassette/authentication/authorities_spec.rb +++ b/spec/cassette/authentication/authorities_spec.rb @@ -1,13 +1,11 @@ # frozen_string_literal: true describe Cassette::Authentication::Authorities do - subject do - described_class - end + subject(:authentication) { described_class } describe '#has_role?' do let(:input) { "[#{Cassette.config.base_authority}, SAPI, #{Cassette.config.base_authority}_CREATE-USER]" } - let(:authorities) { subject.parse(input) } + let(:authorities) { authentication.parse(input) } it 'adds the application prefix to roles' do expect(authorities.has_role?('CREATE-USER')).to be(true) @@ -27,58 +25,60 @@ it 'stores the base authority' do input = 'CUSTOMERAPI' - expect(subject.parse(input, base_authority).base).to eql(base_authority) + expect(authentication.parse(input, base_authority).base).to eql(base_authority) end describe '#has_role?' do let(:input) { "[#{Cassette.config.base_authority}_TEST2, SOMEAPI_TEST]" } it 'returns true for a role that is using the base authority' do - expect(subject.parse(input, base_authority)).to have_role(:test) + expect(authentication.parse(input, base_authority)).to have_role(:test) end it 'returns false for a role that is not using the base authority' do - expect(subject.parse(input, base_authority)).not_to have_role(:test2) + expect(authentication.parse(input, base_authority)).not_to have_role(:test2) end end end - context 'CAS authorities parsing' do + context 'when CAS authorities parsing' do it 'handles single authority' do input = 'CUSTOMERAPI' - expect(subject.parse(input).authorities).to eq(%w[CUSTOMERAPI]) + expect(authentication.parse(input).authorities).to eq(%w[CUSTOMERAPI]) end it 'handles multiple authorities with surrounding []' do input = '[CUSTOMERAPI, SAPI]' - expect(subject.parse(input).authorities).to eq(%w[CUSTOMERAPI SAPI]) + expect(authentication.parse(input).authorities).to eq(%w[CUSTOMERAPI SAPI]) end it 'ignores whitespace in multiple authorities' do input = '[CUSTOMERAPI,SAPI]' - expect(subject.parse(input).authorities).to eq(%w[CUSTOMERAPI SAPI]) + expect(authentication.parse(input).authorities).to eq(%w[CUSTOMERAPI SAPI]) end it 'returns an empty array when input is nil' do - expect(subject.parse(nil).authorities).to eq([]) + expect(authentication.parse(nil).authorities).to eq([]) end end context 'with authentication disabled' do - subject { described_class.new('[]') } + subject(:authentication) { described_class.new('[]') } before do stub_const('ENV', ENV.to_hash.merge('NOAUTH' => 'true')) end + it '#authorities returns empty' do + expect(authentication.authorities).to be_empty + end + it '#has_role? returns true for every role' do - expect(subject.authorities).to be_empty - expect(subject.has_role?(:can_manage)).to be(true) + expect(authentication.has_role?(:can_manage)).to be(true) end it '#has_raw_role? returns true for every role' do - expect(subject.authorities).to be_empty - expect(subject.has_raw_role?('SAPI_CUSTOMER-CREATOR')).to be(true) + expect(authentication.has_raw_role?('SAPI_CUSTOMER-CREATOR')).to be(true) end end end From d2ac85201b49bbc64e8a799380cc9c1ae5d42ad5 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Thu, 6 Oct 2022 11:44:10 +0530 Subject: [PATCH 04/17] resolve rubocop rspec issues in user spec --- spec/cassette/authentication/user_spec.rb | 38 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/spec/cassette/authentication/user_spec.rb b/spec/cassette/authentication/user_spec.rb index cef0a38..fd994d9 100644 --- a/spec/cassette/authentication/user_spec.rb +++ b/spec/cassette/authentication/user_spec.rb @@ -8,7 +8,7 @@ describe '#initialize' do context 'without a config' do it 'forwards authorities parsing' do - expect(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil) + allow(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', nil) described_class.new(login: 'john.doe', name: 'John Doe', authorities: '[CUSTOMERAPI, SAPI]') end end @@ -17,8 +17,8 @@ it 'forwards authorities parsing passing along the base authority' do config = object_double(Cassette.config) - expect(config).to receive(:base_authority).and_return('TESTAPI') - expect(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', 'TESTAPI') + allow(config).to receive(:base_authority).and_return('TESTAPI') + allow(Cassette::Authentication::Authorities).to receive(:new).with('[CUSTOMERAPI, SAPI]', 'TESTAPI') described_class.new(login: 'john.doe', name: 'John Doe', authorities: '[CUSTOMERAPI, SAPI]', config: config) @@ -45,14 +45,29 @@ end end - context 'user types' do + context 'when checking user types' do describe '#employee?' do it 'returns true when user is an employee' do expect(described_class.new(type: 'employee')).to be_employee + end + + it 'returns true when user is an Employee' do expect(described_class.new(type: 'Employee')).to be_employee + end + + it 'returns true when user is an :employee' do expect(described_class.new(type: :employee)).to be_employee + end + + it 'returns false when user is an customer' do expect(described_class.new(type: 'customer')).not_to be_employee + end + + it 'returns false when user is nil' do expect(described_class.new(type: nil)).not_to be_employee + end + + it 'returns false when user type is empty' do expect(described_class.new(type: '')).not_to be_employee end end @@ -60,10 +75,25 @@ describe '#customer?' do it 'returns true when the user is a customer' do expect(described_class.new(type: 'customer')).to be_customer + end + + it 'returns true when the user is a Customer' do expect(described_class.new(type: 'Customer')).to be_customer + end + + it 'returns true when the user is a :customer' do expect(described_class.new(type: :customer)).to be_customer + end + + it 'returns false when the user is a employee' do expect(described_class.new(type: 'employee')).not_to be_customer + end + + it 'returns false when the user is nil' do expect(described_class.new(type: nil)).not_to be_customer + end + + it 'returns false when the user type is empty' do expect(described_class.new(type: '')).not_to be_customer end end From 36033749839fab0dd184c2abb0086a83e309a843 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Thu, 6 Oct 2022 15:08:13 +0530 Subject: [PATCH 05/17] resolve rubocop rspec issues in cassette spec --- spec/cassette_spec.rb | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/spec/cassette_spec.rb b/spec/cassette_spec.rb index 74adf01..6572168 100644 --- a/spec/cassette_spec.rb +++ b/spec/cassette_spec.rb @@ -15,23 +15,19 @@ def keeping_logger(&block) described_class.config = config - expect(described_class.config) - .to eq(config) + expect(described_class.config).to eq(config) end context 'when tls_version was not specified' do it 'uses default TLS version: TLSv1_2' do - config = OpenStruct.new( - YAML.load_file('spec/config.yml') - ) + config = OpenStruct.new(YAML.load_file('spec/config.yml')) # Removing tls_version field config.delete_field(:tls_version) described_class.config = config - expect(described_class.config.tls_version) - .to eq('TLSv1_2') + expect(described_class.config.tls_version).to eq('TLSv1_2') end end @@ -43,24 +39,20 @@ def keeping_logger(&block) described_class.config = config - expect(described_class.config.tls_version) - .to eq('TLSv1_2') + expect(described_class.config.tls_version).to eq('TLSv1_2') end end context 'when verify_ssl was not specified' do it 'uses default verify_ssl value: false' do - config = OpenStruct.new( - YAML.load_file('spec/config.yml') - ) + config = OpenStruct.new(YAML.load_file('spec/config.yml')) # Removing verify_ssl field config.delete_field(:verify_ssl) described_class.config = config - expect(described_class.config.verify_ssl) - .to be false + expect(described_class.config.verify_ssl).to be false end end @@ -72,23 +64,25 @@ def keeping_logger(&block) described_class.config = config - expect(described_class.config.verify_ssl) - .to be true + expect(described_class.config.verify_ssl).to be true end end end describe '.logger' do - it 'returns a default instance' do + it 'returns a not nil' do expect(described_class.logger).not_to be_nil + end + + it 'returns a default instance' do expect(described_class.logger.is_a?(Logger)).to be(true) end it 'returns rails logger when Rails is available' do keeping_logger do described_class.logger = nil - rails = double('Rails') - expect(rails).to receive(:logger).and_return(rails).at_least(:once) + rails = instance_double(described_class, 'Rails') + allow(rails).to receive(:logger).and_return(rails).at_least(:once) stub_const('Rails', rails) expect(described_class.logger).to eql(rails) end From dacca3df967620eec3532e6b75eeafb3c5bd86f7 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 19:04:52 +0530 Subject: [PATCH 06/17] resolve filter spec and client spec --- spec/cassette/authentication/filter_spec.rb | 32 ++++++++++----------- spec/integration/cas/client_spec.rb | 11 +++++-- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/spec/cassette/authentication/filter_spec.rb b/spec/cassette/authentication/filter_spec.rb index 49a25df..894bc5b 100644 --- a/spec/cassette/authentication/filter_spec.rb +++ b/spec/cassette/authentication/filter_spec.rb @@ -22,7 +22,7 @@ it_behaves_like 'with NOAUTH' do it 'never checks the role' do - expect(current_user).not_to receive(:has_raw_role?) + allow(current_user).not_to receive(:has_raw_role?) controller.validate_raw_role!(:something) end @@ -34,14 +34,14 @@ it 'forwards to current_user' do role = instance_double(String) - expect(current_user).to receive(:has_raw_role?).with(role).and_return(true) + allow(current_user).to receive(:has_raw_role?).with(role).and_return(true) controller.validate_raw_role!(role) end it 'raises a Cassette::Errors::Forbidden when current_user does not have the role' do role = instance_double(String) - expect(current_user).to receive(:has_raw_role?).with(role).and_return(false) + allow(current_user).to receive(:has_raw_role?).with(role).and_return(false) expect { controller.validate_raw_role!(role) }.to raise_error(Cassette::Errors::Forbidden) end end @@ -56,7 +56,7 @@ it_behaves_like 'with NOAUTH' do it 'never checks the role' do - expect(current_user).not_to receive(:has_role?) + allow(current_user).not_to receive(:has_role?) controller.validate_role!(:something) end @@ -68,14 +68,14 @@ it 'forwards to current_user' do role = instance_double(String) - expect(current_user).to receive(:has_role?).with(role).and_return(true) + allow(current_user).to receive(:has_role?).with(role).and_return(true) controller.validate_role!(role) end it 'raises a Cassette::Errors::Forbidden when current_user does not have the role' do role = instance_double(String) - expect(current_user).to receive(:has_role?).with(role).and_return(false) + allow(current_user).to receive(:has_role?).with(role).and_return(false) expect { controller.validate_role!(role) }.to raise_error(Cassette::Errors::Forbidden) end end @@ -94,13 +94,13 @@ end it_behaves_like 'with NOAUTH' do - context 'and no ticket' do + context 'when no ticket is passed' do let(:controller) { controller_factory.call(described_class).new } it_behaves_like 'controller without authentication' end - context 'and a ticket header' do + context 'when a ticket header is passed' do let(:controller) do controller_factory.call(described_class).new({}, 'Service-Ticket' => 'le ticket') end @@ -108,7 +108,7 @@ it_behaves_like 'controller without authentication' end - context 'and a ticket param' do + context 'when a ticket param is passed' do let(:controller) do controller_factory.call(described_class).new(ticket: 'le ticket') end @@ -123,7 +123,7 @@ end before do - expect(controller).to receive(:accepts_authentication_service?) + allow(controller).to receive(:accepts_authentication_service?) .with(Cassette.config.service).and_return(false) end @@ -135,7 +135,7 @@ context 'when accepts_authentication_service? returns true' do before do - expect(controller).to receive(:accepts_authentication_service?).with(anything).and_return(true) + allow(controller).to receive(:accepts_authentication_service?).with(anything).and_return(true) end context 'with a ticket in the query string *AND* headers' do @@ -216,19 +216,19 @@ def authentication_service OpenStruct.new(YAML.load_file('spec/config.yml').merge(services: [subdomain])) end - context 'and the authentication service is included in the configuration' do + context 'when the authentication service is included in the configuration' do let(:service) { subdomain } it { is_expected.to eq true } end - context 'and the authentication service is Cassette.config.service' do + context 'when the authentication service is Cassette.config.service' do let(:service) { Cassette.config.service } it { is_expected.to eq true } end - context 'and the authentication service is not included in the configuration' do + context 'when the authentication service is not included in the configuration' do let(:service) { not_related } it { is_expected.to eq false } @@ -237,13 +237,13 @@ def authentication_service end end - context 'a Rails 4+ controller' do + context 'when controller belongs to Rails 4+' do let(:controller_factory) { method(:ControllerMock) } it_behaves_like 'controller behaviour' end - context 'a Rails 3 controller' do + context 'when controller belongs to Rails 3' do let(:controller_factory) { method(:LegacyControllerMock) } it_behaves_like 'controller behaviour' diff --git a/spec/integration/cas/client_spec.rb b/spec/integration/cas/client_spec.rb index 111649b..f37b5a0 100644 --- a/spec/integration/cas/client_spec.rb +++ b/spec/integration/cas/client_spec.rb @@ -10,12 +10,17 @@ expect(client.st_for(config.service)).not_to be_blank end - it 'validates an ST, extracting the user' do + context 'when validates an ST, extracting the user' do st = client.st_for(config.service) user = authentication.ticket_user(st, config.service) - expect(user).not_to be_blank - expect(user.login).to eql(config.username) + it do + expect(user).not_to be_blank + end + + it do + expect(user.login).to eql(config.username) + end end end end From bbc26273cef33e8bc7c9f21a2a9c65e6f6ede608 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 19:18:58 +0530 Subject: [PATCH 07/17] fix rubocop rspec issues in authentication spec --- spec/cassette/authentication_spec.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/spec/cassette/authentication_spec.rb b/spec/cassette/authentication_spec.rb index d6b7ba9..a2806d7 100644 --- a/spec/cassette/authentication_spec.rb +++ b/spec/cassette/authentication_spec.rb @@ -24,14 +24,14 @@ it { is_expected.to eql(cached_value) } it 'calls Cache#fetch_authentication with ticket and service' do - expect(cache).to receive(:fetch_authentication) + allow(cache).to receive(:fetch_authentication) .with(ticket, service) ticket_user end it 'passes a block to Cache#fetch_authentication' do - expect(cache).to receive(:fetch_authentication) do |*, &block| + allow(cache).to receive(:fetch_authentication) do |*, &block| expect(block).to be_present end @@ -41,7 +41,7 @@ context 'when not cached' do before do - expect(cache).to receive(:fetch_authentication) do |_ticket, &block| + allow(cache).to receive(:fetch_authentication) do |_ticket, &block| block.call end end @@ -76,23 +76,25 @@ end describe '#validate_ticket' do + subject(:ticket) { described_class.new } + it 'raises a authorization required error when no ticket is provided' do - expect { subject.validate_ticket(nil) }.to raise_error(Cassette::Errors::AuthorizationRequired) + expect { ticket.validate_ticket(nil) }.to raise_error(Cassette::Errors::AuthorizationRequired) end it 'raises a authorization required error when ticket is blank' do - expect { subject.validate_ticket('') }.to raise_error(Cassette::Errors::AuthorizationRequired) + expect { ticket.validate_ticket('') }.to raise_error(Cassette::Errors::AuthorizationRequired) end it 'raises a forbidden error when the associated user is not found' do - expect(subject).to receive(:ticket_user).with('ticket', Cassette.config.service).and_return(nil) - expect { subject.validate_ticket('ticket') }.to raise_error(Cassette::Errors::Forbidden) + allow(ticket).to receive(:ticket_user).with('ticket', Cassette.config.service).and_return(nil) + expect { ticket.validate_ticket('ticket') }.to raise_error(Cassette::Errors::Forbidden) end it 'returns the associated user' do user = double('User') - expect(subject).to receive(:ticket_user).with('ticket', Cassette.config.service).and_return(user) - expect(subject.validate_ticket('ticket')).to eql(user) + allow(ticket).to receive(:ticket_user).with('ticket', Cassette.config.service).and_return(user) + expect(ticket.validate_ticket('ticket')).to eql(user) end end end From 259c7a09b4f8ce017329e2f25c18788ba64a65aa Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 19:50:16 +0530 Subject: [PATCH 08/17] resolve rubocop rspec issues - cache spec --- spec/cassette/authentication/cache_spec.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/cassette/authentication/cache_spec.rb b/spec/cassette/authentication/cache_spec.rb index e6f9f0e..70e2eae 100644 --- a/spec/cassette/authentication/cache_spec.rb +++ b/spec/cassette/authentication/cache_spec.rb @@ -8,10 +8,11 @@ cache.fetch_authentication(ticket, service, &block) end - let(:second_call) do + def second_call cache.fetch_authentication(ticket, service, &other_block) end - let(:call_with_other_service) do + + def call_with_other_service cache.fetch_authentication(ticket, other_service, &other_block) end @@ -42,13 +43,11 @@ it 'uses the cache store set in configuration' do # setup - global_cache = double('cache_store') + global_cache = instance_double(described_class, 'cache_store') Cassette.cache_backend = global_cache - logger = Logger.new('/dev/null') - # exercise - auth_cache = described_class.new(logger) + auth_cache = described_class.new(Logger.new('/dev/null')) expect(auth_cache.backend).to eq(global_cache) From 60da8f2846bc65f02818387424f0ae131f69b63d Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 20:07:15 +0530 Subject: [PATCH 09/17] fix rubocop rspec issues - client spec --- spec/cassette/client_spec.rb | 44 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/spec/cassette/client_spec.rb b/spec/cassette/client_spec.rb index 45bf289..885d5fa 100644 --- a/spec/cassette/client_spec.rb +++ b/spec/cassette/client_spec.rb @@ -17,25 +17,25 @@ end describe '#health_check' do - subject { client.health_check } + subject(:health_check) { client.health_check } it 'raises any Error' do - expect(client).to receive(:st_for).with(anything).and_raise('Failure') + allow(client).to receive(:st_for).with(anything).and_raise('Failure') - expect { subject }.to raise_error('Failure') + expect { health_check }.to raise_error('Failure') end it 'tries to generate a ST' do - expect(client).to receive(:st_for).with(an_instance_of(String)).and_return('ST-Something') + allow(client).to receive(:st_for).with(an_instance_of(String)).and_return('ST-Something') - expect(subject).to eq 'ST-Something' + expect(health_check).to eq 'ST-Something' end end describe '#tgt' do - subject { client.tgt('user', 'pass', force) } + subject(:client_tgt) { client.tgt('user', 'pass', force) } - context 'http client interactions' do + context 'with http client interactions' do let(:force) { true } let(:response) { Faraday::Response.new } @@ -48,16 +48,16 @@ tgt = 'TGT-something' allow(response).to receive(:headers).and_return('Location' => "/tickets/#{tgt}") - expect(subject).to eq tgt + expect(client_tgt).to eq tgt end it 'posts the username and password' do - subject + client_tgt expect(http).to have_received(:post).with(anything, username: 'user', password: 'pass') end it 'posts to a /v1/tickets uri' do - subject + client_tgt expect(http).to have_received(:post).with(%r{/v1/tickets\z}, instance_of(Hash)) end end @@ -103,7 +103,7 @@ # this first call is to set the cache client = described_class.new(http_client: http) - result = client.tgt('user', 'pass') + client.tgt('user', 'pass') logger = spy(:logger) Cassette.logger = logger @@ -126,7 +126,7 @@ # this first call is to set the cache client = described_class.new(http_client: http) - result = client.tgt('user', 'pass') + client.tgt('user', 'pass') tgt2 = 'TGT2-Something-example' response = double('response', headers: { 'Location' => "tickets/#{tgt2}" }) @@ -149,7 +149,7 @@ end describe '#st' do - subject { client.st(tgt_param, service, force) } + subject(:client_st) { client.st(tgt_param, service, force) } let(:service) { 'example.org' } let(:tgt) { 'TGT-Example' } @@ -168,17 +168,17 @@ it 'extracts the tgt from the Location header' do allow(response).to receive(:body) { st } - expect(subject).to eq st + expect(client_st).to eq st end it 'posts the service' do - subject + client_st expect(http).to have_received(:post).with(anything, service: service) end it 'posts to the tgt uri' do - subject + client_st expect(http).to have_received(:post).with(%r{/#{tgt}\z}, instance_of(Hash)) end @@ -212,13 +212,13 @@ end end - context 'not using the force' do + context 'when not using the force' do let(:force) { false } include_context 'controlling the force' end - context 'using the force' do + context 'when using the force' do let(:force) { true } include_context 'controlling the force' @@ -227,7 +227,7 @@ end describe '#st_for' do - subject { client.st_for(service) } + subject(:client_st_for) { client.st_for(service) } let(:service) { 'example.org' } let(:cached_tgt) { 'TGT-Something' } @@ -251,17 +251,17 @@ let(:tgt_response) { Faraday::Response.new(response_headers: { 'Location' => "/v1/tickets/#{tgt}" }) } it 'returns the generated st' do - expect(subject).to eq st + expect(client_st_for).to eq st end it 'generates an ST' do - subject + client_st_for expect(http).to have_received(:post).with(%r{/v1/tickets/#{tgt}\z}, service: service) end it 'generates a TGT' do - subject + client_st_for expect(http).to have_received(:post) .with(%r{/v1/tickets\z}, username: Cassette.config.username, password: Cassette.config.password) From fa7b8afbbbf8ae642b5f80a175be892d0d8eca67 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 20:58:40 +0530 Subject: [PATCH 10/17] resolve rubocop rspec - errors + routing constraint spec --- spec/cassette/errors_spec.rb | 24 ++++++++++++++++--- .../rubycas/routing_constraint_spec.rb | 6 ++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/spec/cassette/errors_spec.rb b/spec/cassette/errors_spec.rb index 87475a1..fac2b4d 100644 --- a/spec/cassette/errors_spec.rb +++ b/spec/cassette/errors_spec.rb @@ -3,24 +3,42 @@ describe Cassette::Errors do describe Cassette::Errors::Base do describe '#code' do - it 'returns the HTTP status code accordlingly' do + it 'returns the HTTP status 403 code accordlingly' do expect(Cassette::Errors::Forbidden.new.code).to be(403) + end + + it 'returns the HTTP status code 404 accordlingly' do expect(Cassette::Errors::NotFound.new.code).to be(404) + end + + it 'returns the HTTP status code 500 accordlingly' do expect(Cassette::Errors::InternalServerError.new.code).to be(500) end end end describe '.raise_by_code' do - it 'raises the correct exception for the status code' do + it 'raises the correct exception for the status code 404' do expect { described_class.raise_by_code(404) }.to raise_error(Cassette::Errors::NotFound) + end + + it 'raises the correct exception for the status code 403' do expect { described_class.raise_by_code(403) }.to raise_error(Cassette::Errors::Forbidden) + end + + it 'raises the correct exception for the status code 412' do expect { described_class.raise_by_code(412) }.to raise_error(Cassette::Errors::PreconditionFailed) + end + + it 'raises the correct exception for the status code 500' do expect { described_class.raise_by_code(500) }.to raise_error(Cassette::Errors::InternalServerError) end - it 'raises internal server error for unmapped errors' do + it 'raises internal server error for unmapped errors 406' do expect { described_class.raise_by_code(406) }.to raise_error(Cassette::Errors::InternalServerError) + end + + it 'raises internal server error for unmapped errors 200' do expect { described_class.raise_by_code(200) }.to raise_error(Cassette::Errors::InternalServerError) end end diff --git a/spec/cassette/rubycas/routing_constraint_spec.rb b/spec/cassette/rubycas/routing_constraint_spec.rb index 5a93eee..748f990 100644 --- a/spec/cassette/rubycas/routing_constraint_spec.rb +++ b/spec/cassette/rubycas/routing_constraint_spec.rb @@ -4,7 +4,7 @@ describe Cassette::Rubycas::RoutingConstraint do describe '#matches?' do - subject { constraint.matches?(request) } + subject(:matches) { constraint.matches?(request) } let(:request) do OpenStruct.new(session: session) @@ -36,7 +36,7 @@ end it 'checks the User role' do - subject + matches expect(user).to have_received(:has_role?).with(role) end @@ -63,7 +63,7 @@ end it 'checks the User role' do - subject + matches expect(user).to have_received(:has_raw_role?).with(role) end From 5ecc322730102d826d6cd5b4aeab2f05f44eb60f Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 21:03:32 +0530 Subject: [PATCH 11/17] resolve rubocop rspec - cache spec --- spec/cassette/cache_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/cassette/cache_spec.rb b/spec/cassette/cache_spec.rb index 7d2acbb..b8f6347 100644 --- a/spec/cassette/cache_spec.rb +++ b/spec/cassette/cache_spec.rb @@ -1,18 +1,18 @@ # frozen_string_literal: true describe Cassette::Cache do - subject do + subject(:cached) do cached end describe '#backend' do it 'returns the global cache' do # setup - global_cache = double('cache_store') + global_cache = instance_double(described_class, 'cache_store') Cassette.cache_backend = global_cache # exercise and verify - expect(subject.backend).to eql(global_cache) + expect(cached.backend).to eql(global_cache) # tear down Cassette.cache_backend = nil @@ -22,11 +22,11 @@ describe '.backend=' do it 'sets the cache' do # setup - global_cache = double('cache_store') + global_cache = instance_double(described_class, 'cache_store') described_class.backend = global_cache # exercise and verify - expect(subject.backend).to eql(global_cache) + expect(cached.backend).to eql(global_cache) # tear down Cassette.cache_backend = nil @@ -35,10 +35,10 @@ it 'invalidates the cache after the configured number of uses' do generator = double('Generator') - expect(generator).to receive(:generate).twice + allow(generator).to receive(:generate).twice 6.times do - subject.fetch('Generator', max_uses: 5) { generator.generate } + cached.fetch('Generator', max_uses: 5) { generator.generate } end end From 068957fe560b928f9d9ede6366ff38a989ff8883 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Fri, 7 Oct 2022 21:09:21 +0530 Subject: [PATCH 12/17] resolve rubocop rspec - cassette spec --- spec/cassette_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/cassette_spec.rb b/spec/cassette_spec.rb index 6572168..f8b1452 100644 --- a/spec/cassette_spec.rb +++ b/spec/cassette_spec.rb @@ -103,7 +103,7 @@ def keeping_logger(&block) describe '.cache_backend' do context 'when the cache_backend is already set' do it 'returns the cache_backend set' do - new_cache = double('cache_backend') + new_cache = instance_double(described_class, 'cache_backend') described_class.cache_backend = new_cache # exercise and verify @@ -117,7 +117,7 @@ def keeping_logger(&block) context 'when the cache_backend is not set' do it 'returns Rails.cache if set' do described_class.cache_backend = nil - rails_cache = double('cache') + rails_cache = instance_double(described_class, 'cache') rails = double('Rails') allow(rails).to receive(:cache).and_return(rails_cache) stub_const('Rails', rails) From 7ebb997d5b3bf57dfc69cc3787647a7d577ec236 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Sat, 8 Oct 2022 11:26:15 +0530 Subject: [PATCH 13/17] resolve rubocop rspec - client and null store --- spec/cassette/cache/null_store_spec.rb | 2 ++ spec/cassette/client_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/cassette/cache/null_store_spec.rb b/spec/cassette/cache/null_store_spec.rb index 2a292c9..e5b393f 100644 --- a/spec/cassette/cache/null_store_spec.rb +++ b/spec/cassette/cache/null_store_spec.rb @@ -8,7 +8,9 @@ describe '#read' do it 'always return nils' do expect(cache.read('key')).to be_nil + end + it 'always return nils even when cache is written' do cache.write('key', 'value') expect(cache.read('key')).to be_nil diff --git a/spec/cassette/client_spec.rb b/spec/cassette/client_spec.rb index 885d5fa..27e9c0c 100644 --- a/spec/cassette/client_spec.rb +++ b/spec/cassette/client_spec.rb @@ -156,7 +156,7 @@ let(:st) { 'ST-Something-example' } let(:tgt_param) { tgt } - shared_context 'http client interactions' do + shared_context 'with http client interactions' do let(:force) { true } let(:response) { Faraday::Response.new } @@ -196,13 +196,13 @@ it_behaves_like 'http client interactions' end - context 'cache control' do + context 'with cache control' do before do allow(cache).to receive(:fetch_st).with(tgt, service, hash_including(force: force)) .and_return(st) end - shared_context 'controlling the force' do + shared_context 'with controlling the force' do it { is_expected.to eq st } it 'forwards force to the cache' do From 39271d8115e724030593f7c09abdbc78783c602d Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Sat, 8 Oct 2022 11:49:18 +0530 Subject: [PATCH 14/17] resolve rubocop rspec issues - authentication spec --- spec/cassette/authentication_spec.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/spec/cassette/authentication_spec.rb b/spec/cassette/authentication_spec.rb index a2806d7..53c2c0f 100644 --- a/spec/cassette/authentication_spec.rb +++ b/spec/cassette/authentication_spec.rb @@ -40,6 +40,10 @@ end context 'when not cached' do + def auth + subject + end + before do allow(cache).to receive(:fetch_authentication) do |_ticket, &block| block.call @@ -48,7 +52,7 @@ it 'raises a Forbidden exception on any exceptions' do allow(http).to receive(:get).with(anything, anything).and_raise(Cassette::Errors::BadRequest) - expect { subject.ticket_user('ticket') }.to raise_error(Cassette::Errors::Forbidden) + expect { auth.ticket_user('ticket') }.to raise_error(Cassette::Errors::Forbidden) end context 'with a failed CAS response' do @@ -76,7 +80,9 @@ end describe '#validate_ticket' do - subject(:ticket) { described_class.new } + subject(:service) { Cassette.config.service } + + let(:ticket) { described_class.new } it 'raises a authorization required error when no ticket is provided' do expect { ticket.validate_ticket(nil) }.to raise_error(Cassette::Errors::AuthorizationRequired) @@ -87,13 +93,13 @@ end it 'raises a forbidden error when the associated user is not found' do - allow(ticket).to receive(:ticket_user).with('ticket', Cassette.config.service).and_return(nil) + allow(ticket).to receive(:ticket_user).with('ticket', service).and_return(nil) expect { ticket.validate_ticket('ticket') }.to raise_error(Cassette::Errors::Forbidden) end it 'returns the associated user' do - user = double('User') - allow(ticket).to receive(:ticket_user).with('ticket', Cassette.config.service).and_return(user) + user = instance_double(described_class, 'User') + allow(ticket).to receive(:ticket_user).with('ticket', service).and_return(user) expect(ticket.validate_ticket('ticket')).to eql(user) end end From ec82d64304f38bda48591a392dd4dc29a19ce82a Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Sat, 8 Oct 2022 12:06:21 +0530 Subject: [PATCH 15/17] resolve rubocop rspec - cache spec --- spec/cassette/client/cache_spec.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/spec/cassette/client/cache_spec.rb b/spec/cassette/client/cache_spec.rb index 1d9fd45..f7762cf 100644 --- a/spec/cassette/client/cache_spec.rb +++ b/spec/cassette/client/cache_spec.rb @@ -3,13 +3,11 @@ describe Cassette::Client::Cache do it 'uses the cache store set in configuration' do # setup - global_cache = double('cache_store') + global_cache = instance_double(described_class, 'cache_store') Cassette.cache_backend = global_cache - logger = Logger.new('/dev/null') - # exercise - client_cache = described_class.new(logger) + client_cache = described_class.new(Logger.new('/dev/null')) expect(client_cache.backend).to eq(global_cache) @@ -20,14 +18,13 @@ describe '.backend=' do it 'sets the cache' do # setup - global_cache = double('cache_store') - logger = Logger.new('/dev/null') + global_cache = instance_double(described_class, 'cache_store') # exercise Cassette::Client.cache.backend = global_cache # verify - client_cache = described_class.new(logger) + client_cache = described_class.new(Logger.new('/dev/null')) expect(client_cache.backend).to eql(global_cache) # tear down From 81fb82eed1bf09727ff055a27abaea01cc143c1f Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Sat, 8 Oct 2022 12:17:02 +0530 Subject: [PATCH 16/17] resolve rspec issues - client spec --- spec/cassette/client_spec.rb | 50 ++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/spec/cassette/client_spec.rb b/spec/cassette/client_spec.rb index 27e9c0c..dce3654 100644 --- a/spec/cassette/client_spec.rb +++ b/spec/cassette/client_spec.rb @@ -121,7 +121,7 @@ described_class.cache.backend.clear tgt = 'TGT-Something-example' - response = double('response', headers: { 'Location' => "tickets/#{tgt}" }) + response = instance_double('response', headers: { 'Location' => "tickets/#{tgt}" }) allow(http).to receive(:post).and_return(response) # this first call is to set the cache @@ -129,7 +129,7 @@ client.tgt('user', 'pass') tgt2 = 'TGT2-Something-example' - response = double('response', headers: { 'Location' => "tickets/#{tgt2}" }) + response = instance_double('response', headers: { 'Location' => "tickets/#{tgt2}" }) allow(http).to receive(:post).and_return(response) logger = spy(:logger) @@ -187,26 +187,28 @@ context 'when tgt is a string' do let(:tgt_param) { tgt } - it_behaves_like 'http client interactions' + it_behaves_like 'with http client interactions' end context 'when tgt is a callable' do let(:tgt_param) { -> { tgt } } - it_behaves_like 'http client interactions' + it_behaves_like 'with http client interactions' end context 'with cache control' do + let(:cache_control) { subject } + before do allow(cache).to receive(:fetch_st).with(tgt, service, hash_including(force: force)) .and_return(st) end - shared_context 'with controlling the force' do + shared_examples 'with controlling the force' do it { is_expected.to eq st } it 'forwards force to the cache' do - subject + cache_control expect(cache).to have_received(:fetch_st).with(tgt, service, hash_including(force: force)) end @@ -215,13 +217,13 @@ context 'when not using the force' do let(:force) { false } - include_context 'controlling the force' + include_context 'with controlling the force' end context 'when using the force' do let(:force) { true } - include_context 'controlling the force' + include_context 'with controlling the force' end end end @@ -269,6 +271,9 @@ end context 'when tgt is cached but st is not' do + let(:cached_tgt) { subject } + let(:st_response) { Faraday::Response.new(body: st) } + before do allow(cache).to receive(:fetch_tgt).with(hash_including(force: false)).and_return(tgt) allow(cache).to receive(:fetch_st).with(tgt, service, hash_including(force: false)).and_yield @@ -277,31 +282,35 @@ .and_return(st_response) end - let(:st_response) { Faraday::Response.new(body: st) } - it 'returns the generated st' do - expect(subject).to eq st + expect(cached_tgt).to eq st end it 'generates an ST' do - subject + cached_tgt expect(http).to have_received(:post).with(%r{/v1/tickets/#{tgt}\z}, service: service) end end context 'when st is cached' do + let(:cached_st) { subject } + before do allow(cache).to receive(:fetch_st).with(tgt, service, hash_including(force: false)).and_return(st) allow(cache).to receive(:fetch_tgt).and_return(tgt) end it 'returns the cached value' do - expect(subject).to eq st + expect(cached_st).to eq st end end context 'when tgt is expired' do + let(:expired_tgt) { subject } + let(:tgt_response) { Faraday::Response.new(response_headers: { 'Location' => "/v1/tickets/#{tgt}" }) } + let(:st_response) { Faraday::Response.new(body: st) } + before do allow(cache).to receive(:fetch_tgt).with(hash_including(force: false)).and_return(cached_tgt) allow(cache).to receive(:fetch_tgt).with(hash_including(force: true)).and_yield @@ -318,41 +327,38 @@ .and_return(st_response) end - let(:tgt_response) { Faraday::Response.new(response_headers: { 'Location' => "/v1/tickets/#{tgt}" }) } - let(:st_response) { Faraday::Response.new(body: st) } - it 'calls #fetch_st twice' do - subject + expired_tgt expect(cache).to have_received(:fetch_st).twice end it 'calls #fetch_tgt without forcing' do - subject + expired_tgt expect(cache).to have_received(:fetch_tgt).with(force: false) end it 'calls #fetch_tgt forcing' do - subject + expired_tgt expect(cache).to have_received(:fetch_tgt).with(force: true) end it 'tries to generate a ST with the expired TGT' do - subject + expired_tgt expect(http).to have_received(:post).with(%r{/v1/tickets/#{cached_tgt}\z}, service: service) end it 'retries to generate a ST with the new TGT' do - subject + expired_tgt expect(http).to have_received(:post).with(%r{/v1/tickets/#{tgt}\z}, service: service) end it 'returns a brand new tgt' do - expect(subject).to eq st + expect(expired_tgt).to eq st end end end From aa3beffe75d6390bdeee1d6a06d79bbaebe456f0 Mon Sep 17 00:00:00 2001 From: Toshita Pandey Date: Sat, 8 Oct 2022 12:29:37 +0530 Subject: [PATCH 17/17] resolve rubocop rspec issues - client and request spec --- spec/cassette/client_spec.rb | 4 ++-- spec/cassette/http/request_spec.rb | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/cassette/client_spec.rb b/spec/cassette/client_spec.rb index dce3654..ec46ee0 100644 --- a/spec/cassette/client_spec.rb +++ b/spec/cassette/client_spec.rb @@ -71,7 +71,7 @@ described_class.cache.backend.clear tgt = 'TGT-Something-example' - response = double('response', headers: { 'Location' => "tickets/#{tgt}" }) + response = instance_double('response', headers: { 'Location' => "tickets/#{tgt}" }) allow(http).to receive(:post).and_return(response) logger = spy(:logger) @@ -98,7 +98,7 @@ described_class.cache.backend.clear tgt = 'TGT-Something-example' - response = double('response', headers: { 'Location' => "tickets/#{tgt}" }) + response = instance_double('response', headers: { 'Location' => "tickets/#{tgt}" }) allow(http).to receive(:post).and_return(response) # this first call is to set the cache diff --git a/spec/cassette/http/request_spec.rb b/spec/cassette/http/request_spec.rb index ef670a5..1c6824d 100644 --- a/spec/cassette/http/request_spec.rb +++ b/spec/cassette/http/request_spec.rb @@ -3,6 +3,8 @@ describe Cassette::Http::Request do subject(:request) { described_class } + let(:request_subject) { subject } + describe '.post' do subject(:post) { request.post(path, payload) } @@ -26,9 +28,9 @@ end it do - expect(subject).to have_attributes( - headers: { 'Content-Type' => 'application/json' }, - body: '{"ok":"true"}', + expect(request_subject).to have_attributes( + headers: { 'content-type' => 'application/json' }, + body: '{"ok":true}', status: 200 ) end