Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Rubocop Rspec and Resolve Issues Raised by Rubocop Rspec #51

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require: rubocop-rspec

LineLength:
Max: 120

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
2 changes: 2 additions & 0 deletions Rakefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env rake
# frozen_string_literal: true

require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
Expand Down
1 change: 1 addition & 0 deletions cassette.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
43 changes: 22 additions & 21 deletions spec/cassette/authentication/authorities_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@

# frozen_string_literal: true

describe Cassette::Authentication::Authorities do
subject do
Cassette::Authentication::Authorities
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 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

Expand All @@ -27,57 +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(:authentication) { described_class.new('[]') }

before do
stub_const('ENV', ENV.to_hash.merge('NOAUTH' => 'true'))
end
subject { Cassette::Authentication::Authorities.new('[]') }

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 eql(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 eql(true)
expect(authentication.has_raw_role?('SAPI_CUSTOMER-CREATOR')).to be(true)
end
end
end
18 changes: 8 additions & 10 deletions spec/cassette/authentication/cache_spec.rb
Original file line number Diff line number Diff line change
@@ -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')) }
Expand All @@ -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

Expand All @@ -23,33 +24,30 @@
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)
second_call
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

it 'uses the cache store set in configuration' do
# setup
global_cache = double('cache_store')
global_cache = instance_double(described_class, 'cache_store')
toshitapandey marked this conversation as resolved.
Show resolved Hide resolved
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)

Expand Down
Loading