Apply manual rubocop fixes
This commit is contained in:
@ -3,31 +3,31 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ExercisePolicy do
|
||||
subject { described_class }
|
||||
subject(:policy) { described_class }
|
||||
|
||||
let(:exercise) { FactoryBot.build(:dummy, public: true) }
|
||||
|
||||
permissions :batch_update? do
|
||||
it 'grants access to admins only' do
|
||||
expect(subject).to permit(FactoryBot.build(:admin), exercise)
|
||||
expect(policy).to permit(FactoryBot.build(:admin), exercise)
|
||||
%i[external_user teacher].each do |factory_name|
|
||||
expect(subject).not_to permit(FactoryBot.build(factory_name), exercise)
|
||||
expect(policy).not_to permit(FactoryBot.build(factory_name), exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
%i[create? index? new? statistics? feedback? get_rfcs_for_exercise?].each do |action|
|
||||
%i[create? index? new? statistics? feedback? rfcs_for_exercise?].each do |action|
|
||||
permissions(action) do
|
||||
it 'grants access to admins' do
|
||||
expect(subject).to permit(FactoryBot.build(:admin), exercise)
|
||||
expect(policy).to permit(FactoryBot.build(:admin), exercise)
|
||||
end
|
||||
|
||||
it 'grants access to teachers' do
|
||||
expect(subject).to permit(FactoryBot.build(:teacher), exercise)
|
||||
expect(policy).to permit(FactoryBot.build(:teacher), exercise)
|
||||
end
|
||||
|
||||
it 'does not grant access to external users' do
|
||||
expect(subject).not_to permit(FactoryBot.build(:external_user), exercise)
|
||||
expect(policy).not_to permit(FactoryBot.build(:external_user), exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -35,16 +35,16 @@ describe ExercisePolicy do
|
||||
%i[clone? destroy? edit? update?].each do |action|
|
||||
permissions(action) do
|
||||
it 'grants access to admins' do
|
||||
expect(subject).to permit(FactoryBot.build(:admin), exercise)
|
||||
expect(policy).to permit(FactoryBot.build(:admin), exercise)
|
||||
end
|
||||
|
||||
it 'grants access to authors' do
|
||||
expect(subject).to permit(exercise.author, exercise)
|
||||
expect(policy).to permit(exercise.author, exercise)
|
||||
end
|
||||
|
||||
it 'does not grant access to all other users' do
|
||||
%i[external_user teacher].each do |factory_name|
|
||||
expect(subject).not_to permit(FactoryBot.build(factory_name), exercise)
|
||||
expect(policy).not_to permit(FactoryBot.build(factory_name), exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -56,14 +56,14 @@ describe ExercisePolicy do
|
||||
let(:user) { exercise.author }
|
||||
|
||||
it 'does not grant access' do
|
||||
expect(subject).not_to permit(user, exercise)
|
||||
expect(policy).not_to permit(user, exercise)
|
||||
end
|
||||
|
||||
context 'when user has codeharbor_link' do
|
||||
before { user.codeharbor_link = FactoryBot.build(:codeharbor_link) }
|
||||
|
||||
it 'grants access' do
|
||||
expect(subject).to permit(user, exercise)
|
||||
expect(policy).to permit(user, exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -72,14 +72,14 @@ describe ExercisePolicy do
|
||||
let(:user) { FactoryBot.build(:admin) }
|
||||
|
||||
it 'does not grant access' do
|
||||
expect(subject).not_to permit(user, exercise)
|
||||
expect(policy).not_to permit(user, exercise)
|
||||
end
|
||||
|
||||
context 'when user has codeharbor_link' do
|
||||
before { user.codeharbor_link = FactoryBot.build(:codeharbor_link) }
|
||||
|
||||
it 'grants access' do
|
||||
expect(subject).to permit(user, exercise)
|
||||
expect(policy).to permit(user, exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -89,14 +89,14 @@ describe ExercisePolicy do
|
||||
let(:user) { FactoryBot.build(factory_name) }
|
||||
|
||||
it 'does not grant access' do
|
||||
expect(subject).not_to permit(user, exercise)
|
||||
expect(policy).not_to permit(user, exercise)
|
||||
end
|
||||
|
||||
context 'when user has codeharbor_link' do
|
||||
before { user.codeharbor_link = FactoryBot.build(:codeharbor_link) }
|
||||
|
||||
it 'does not grant access' do
|
||||
expect(subject).not_to permit(user, exercise)
|
||||
expect(policy).not_to permit(user, exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -107,7 +107,7 @@ describe ExercisePolicy do
|
||||
[:show?].each do |action|
|
||||
permissions(action) do
|
||||
it 'not grants access to external users' do
|
||||
expect(subject).not_to permit(FactoryBot.build(:external_user), exercise)
|
||||
expect(policy).not_to permit(FactoryBot.build(:external_user), exercise)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -116,7 +116,7 @@ describe ExercisePolicy do
|
||||
permissions(action) do
|
||||
it 'grants access to anyone' do
|
||||
%i[admin external_user teacher].each do |factory_name|
|
||||
expect(subject).to permit(FactoryBot.build(factory_name), Exercise.new)
|
||||
expect(policy).to permit(FactoryBot.build(factory_name), Exercise.new)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -124,47 +124,47 @@ describe ExercisePolicy do
|
||||
|
||||
describe ExercisePolicy::Scope do
|
||||
describe '#resolve' do
|
||||
before(:all) do
|
||||
@admin = FactoryBot.create(:admin)
|
||||
@external_user = FactoryBot.create(:external_user)
|
||||
@teacher = FactoryBot.create(:teacher)
|
||||
let(:admin) { FactoryBot.create(:admin) }
|
||||
let(:external_user) { FactoryBot.create(:external_user) }
|
||||
let(:teacher) { FactoryBot.create(:teacher) }
|
||||
|
||||
[@admin, @teacher].each do |user|
|
||||
before do
|
||||
[admin, teacher].each do |user|
|
||||
[true, false].each do |public|
|
||||
FactoryBot.create(:dummy, public: public, user_id: user.id, user_type: InternalUser.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'for admins' do
|
||||
let(:scope) { Pundit.policy_scope!(@admin, Exercise) }
|
||||
context 'when being an admin' do
|
||||
let(:scope) { Pundit.policy_scope!(admin, Exercise) }
|
||||
|
||||
it 'returns all exercises' do
|
||||
expect(scope.map(&:id)).to include(*Exercise.all.map(&:id))
|
||||
end
|
||||
end
|
||||
|
||||
context 'for external users' do
|
||||
let(:scope) { Pundit.policy_scope!(@external_user, Exercise) }
|
||||
context 'when being an external users' do
|
||||
let(:scope) { Pundit.policy_scope!(external_user, Exercise) }
|
||||
|
||||
it 'returns nothing' do
|
||||
expect(scope.count).to be 0
|
||||
end
|
||||
end
|
||||
|
||||
context 'for teachers' do
|
||||
let(:scope) { Pundit.policy_scope!(@teacher, Exercise) }
|
||||
context 'when being a teacher' do
|
||||
let(:scope) { Pundit.policy_scope!(teacher, Exercise) }
|
||||
|
||||
it 'includes all public exercises' do
|
||||
expect(scope.map(&:id)).to include(*Exercise.where(public: true).map(&:id))
|
||||
end
|
||||
|
||||
it 'includes all authored non-public exercises' do
|
||||
expect(scope.map(&:id)).to include(*Exercise.where(public: false, user_id: @teacher.id).map(&:id))
|
||||
expect(scope.map(&:id)).to include(*Exercise.where(public: false, user_id: teacher.id).map(&:id))
|
||||
end
|
||||
|
||||
it "does not include other authors' non-public exercises" do
|
||||
expect(scope.map(&:id)).not_to include(*Exercise.where(public: false).where("user_id <> #{@teacher.id}").map(&:id))
|
||||
expect(scope.map(&:id)).not_to include(*Exercise.where(public: false).where("user_id <> #{teacher.id}").map(&:id))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user