4cb747fdb6
Test helpers are separated between two files: spec_helper and rails_helper. This separation is meant to allow tests that do not require Rails (like testing standalone libs) to boot faster. The spec_helper file is always loaded, through `--require spec_helper` in the `.rspec` config file. When needed, the rails_helper file is expected to be required manually. This is fine, but: - Many test files have a redundant `require 'spec_helper'` line; - Many test files should require `rails_helper`, but don't. Not requiring `rails_helper` will cause the Rails-concerned section of the test environment not to be configured–which may cause subtle bugs (like the test database not being properly initialized). Moreover, Spring loads all the Rails files on preloading anyway. So the gains from using only `spec_helper` are thin. To streamline this process, this commit: - Configures `.rspec` to require `rails_helper` by default; - Remove all manual requires to spec_helper or rails_helper. Reference: https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it
74 lines
2.4 KiB
Ruby
74 lines
2.4 KiB
Ruby
describe ChampPolicy do
|
||
let(:champ) { create(:champ_text, private: private, dossier: dossier) }
|
||
let(:dossier) { create(:dossier, user: dossier_owner) }
|
||
let(:dossier_owner) { create(:user) }
|
||
|
||
let(:signed_in_user) { create(:user) }
|
||
let(:account) { { user: signed_in_user } }
|
||
|
||
subject { Pundit.policy_scope(account, Champ) }
|
||
|
||
shared_examples_for 'they can access a public champ' do
|
||
let(:private) { false }
|
||
it { expect(subject.find_by(id: champ.id)).to eq(champ) }
|
||
end
|
||
|
||
shared_examples_for 'they can’t access a public champ' do
|
||
let(:private) { false }
|
||
it { expect(subject.find_by(id: champ.id)).to eq(nil) }
|
||
end
|
||
|
||
shared_examples_for 'they can access a private champ' do
|
||
let(:private) { true }
|
||
it { expect(subject.find_by(id: champ.id)).to eq(champ) }
|
||
end
|
||
|
||
shared_examples_for 'they can’t access a private champ' do
|
||
let(:private) { true }
|
||
it { expect(subject.find_by(id: champ.id)).to eq(nil) }
|
||
end
|
||
|
||
context 'when an user only has user rights' do
|
||
context 'as the dossier owner' do
|
||
let(:signed_in_user) { dossier_owner }
|
||
|
||
it_behaves_like 'they can access a public champ'
|
||
it_behaves_like 'they can’t access a private champ'
|
||
end
|
||
|
||
context 'as another user' do
|
||
let(:signed_in_user) { create(:user) }
|
||
|
||
it_behaves_like 'they can’t access a public champ'
|
||
it_behaves_like 'they can’t access a private champ'
|
||
end
|
||
end
|
||
|
||
context 'when the user also has instruction rights' do
|
||
let(:instructeur) { create(:instructeur, email: signed_in_user.email, password: signed_in_user.password) }
|
||
let(:account) { { user: signed_in_user, instructeur: instructeur } }
|
||
|
||
context 'as the dossier instructeur and owner' do
|
||
let(:signed_in_user) { dossier_owner }
|
||
before { instructeur.assign_to_procedure(dossier.procedure) }
|
||
|
||
it_behaves_like 'they can access a public champ'
|
||
it_behaves_like 'they can access a private champ'
|
||
end
|
||
|
||
context 'as the dossier instructeur (but not owner)' do
|
||
let(:signed_in_user) { create(:user) }
|
||
before { instructeur.assign_to_procedure(dossier.procedure) }
|
||
|
||
it_behaves_like 'they can’t access a public champ'
|
||
it_behaves_like 'they can access a private champ'
|
||
end
|
||
|
||
context 'as an instructeur not assigned to the procedure' do
|
||
let(:signed_in_user) { create(:user) }
|
||
|
||
it_behaves_like 'they can’t access a public champ'
|
||
it_behaves_like 'they can’t access a private champ'
|
||
end
|
||
end
|
||
end
|