Fix tests and slightly refactor some policies

This commit is contained in:
Sebastian Serth
2018-11-30 15:44:45 +01:00
parent 2125fb1c1d
commit 3665058435
14 changed files with 19 additions and 64 deletions

View File

@ -63,7 +63,7 @@ module Lti
else # 'learner' else # 'learner'
next next
end end
end end unless provider.roles.blank?
result result
end end

View File

@ -1,9 +1,9 @@
class ExecutionEnvironmentPolicy < AdminOnlyPolicy class ExecutionEnvironmentPolicy < AdminOnlyPolicy
[:execute_command?, :shell?, :statistics?].each do |action| [:execute_command?, :shell?, :statistics?, :show?].each do |action|
define_method(action) { admin? || author? } define_method(action) { admin? || author? }
end end
[:show?, :index?, :new?].each do |action| [:index?].each do |action|
define_method(action) { admin? || teacher? } define_method(action) { admin? || teacher? }
end end
end end

View File

@ -1,13 +1,5 @@
class FileTemplatePolicy < AdminOnlyPolicy class FileTemplatePolicy < AdminOnlyPolicy
def index?
admin? || teacher?
end
def show?
admin? || teacher?
end
def by_file_type? def by_file_type?
everyone everyone
end end

View File

@ -1,6 +1,3 @@
class FileTypePolicy < AdminOnlyPolicy class FileTypePolicy < AdminOnlyPolicy
[:index?, :show?].each do |action|
define_method(action) { admin? || teacher? }
end
end end

View File

@ -11,8 +11,8 @@ class InterventionPolicy < AdminOrAuthorPolicy
define_method(action) { admin? || author?} define_method(action) { admin? || author?}
end end
[:reload?].each do |action| def reload?
define_method(action) { everyone } everyone
end end
class Scope < Scope class Scope < Scope

View File

@ -20,7 +20,7 @@ class ProxyExercisePolicy < AdminOrAuthorPolicy
if @user.admin? if @user.admin?
@scope.all @scope.all
elsif @user.teacher? elsif @user.teacher?
@scope.where('user_id = ?', @user.id) @scope.where('user_id = ? OR public = TRUE', @user.id)
else else
@scope.none @scope.none
end end

View File

@ -11,8 +11,8 @@ class RequestForCommentPolicy < ApplicationPolicy
everyone everyone
end end
[:destroy?].each do |action| def destroy?
define_method(action) { admin? } admin?
end end
def mark_as_solved? def mark_as_solved?

View File

@ -7,10 +7,6 @@ class SubscriptionPolicy < ApplicationPolicy
author? || admin? author? || admin?
end end
def show_error?
everyone
end
def author? def author?
@user == @record.user @user == @record.user
end end

View File

@ -1,29 +1,13 @@
class TagPolicy < AdminOrAuthorPolicy class TagPolicy < AdminOnlyPolicy
def batch_update?
admin?
end
def show?
admin? || teacher?
end
[:clone?, :destroy?, :edit?, :update?].each do |action|
define_method(action) { admin? || author?}
end
[:reload?].each do |action|
define_method(action) { everyone }
end
class Scope < Scope class Scope < Scope
def resolve def resolve
if @user.admin? if @user.admin? || @user.teacher?
@scope.all @scope.all
elsif @user.teacher?
@scope.where('user_id = ? OR public = TRUE', @user.id)
else else
@scope.none @scope.none
end end
end end
end end
end end

View File

@ -47,6 +47,7 @@ de:
consumer: Konsument consumer: Konsument
email: E-Mail email: E-Mail
name: Name name: Name
role: Rolle
file: file:
content: Inhalt content: Inhalt
feedback_message: Feedback-Nachricht feedback_message: Feedback-Nachricht

View File

@ -47,6 +47,7 @@ en:
consumer: Consumer consumer: Consumer
email: Email email: Email
name: Name name: Name
role: Role
file: file:
content: Content content: Content
feedback_message: Feedback Message feedback_message: Feedback Message

View File

@ -23,11 +23,11 @@ describe 'Authorization' do
let(:user) { FactoryBot.create(:teacher) } let(:user) { FactoryBot.create(:teacher) }
before(:each) { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) } before(:each) { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
[Consumer, InternalUser].each do |model| [Consumer, InternalUser, ExecutionEnvironment, FileType].each do |model|
expect_forbidden_path(:"new_#{model.model_name.singular}_path") expect_forbidden_path(:"new_#{model.model_name.singular}_path")
end end
[ExecutionEnvironment, Exercise, FileType].each do |model| [Exercise].each do |model|
expect_permitted_path(:"new_#{model.model_name.singular}_path") expect_permitted_path(:"new_#{model.model_name.singular}_path")
end end
end end

View File

@ -5,7 +5,7 @@ describe ExecutionEnvironmentPolicy do
let(:execution_environment) { FactoryBot.build(:ruby) } let(:execution_environment) { FactoryBot.build(:ruby) }
[:create?, :index?, :new?].each do |action| [:index?].each do |action|
permissions(action) do permissions(action) do
it 'grants access to admins' do it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), execution_environment) expect(subject).to permit(FactoryBot.build(:admin), execution_environment)
@ -21,7 +21,7 @@ describe ExecutionEnvironmentPolicy do
end end
end end
[:execute_command?, :shell?, :statistics?].each do |action| [:execute_command?, :shell?, :statistics?, :show?].each do |action|
permissions(action) do permissions(action) do
it 'grants access to admins' do it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), execution_environment) expect(subject).to permit(FactoryBot.build(:admin), execution_environment)
@ -39,7 +39,7 @@ describe ExecutionEnvironmentPolicy do
end end
end end
[:destroy?, :edit?, :show?, :update?].each do |action| [:destroy?, :edit?, :update?, :new?, :create?].each do |action|
permissions(action) do permissions(action) do
it 'grants access to admins' do it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), execution_environment) expect(subject).to permit(FactoryBot.build(:admin), execution_environment)

View File

@ -5,23 +5,7 @@ describe FileTypePolicy do
let(:file_type) { FactoryBot.build(:dot_rb) } let(:file_type) { FactoryBot.build(:dot_rb) }
[:create?, :index?, :new?].each do |action| [:destroy?, :edit?, :update?, :new?, :create?, :index?, :show?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), file_type)
end
it 'grants access to teachers' do
expect(subject).to permit(FactoryBot.build(:teacher), file_type)
end
it 'does not grant access to external users' do
expect(subject).not_to permit(FactoryBot.build(:external_user), file_type)
end
end
end
[:destroy?, :edit?, :show?, :update?].each do |action|
permissions(action) do permissions(action) do
it 'grants access to admins' do it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), file_type) expect(subject).to permit(FactoryBot.build(:admin), file_type)