Redefine user roles with their role in a study group

This commit is contained in:
Sebastian Serth
2022-09-20 16:19:04 +02:00
committed by Sebastian Serth
parent 04ed45ea73
commit 9c9f45ff77
12 changed files with 90 additions and 44 deletions

View File

@ -7,5 +7,9 @@ FactoryBot.define do
external_id { SecureRandom.uuid }
generated_user_name
singleton_external_user
member_of_study_group
transient do
teacher_in_study_group { false }
end
end
end

View File

@ -7,8 +7,12 @@ FactoryBot.define do
email { 'admin@example.org' }
generated_user_name
password { 'admin' }
role { 'admin' }
platform_admin { true }
singleton_internal_user
member_of_study_group
transient do
teacher_in_study_group { true }
end
end
factory :teacher, class: 'InternalUser' do
@ -17,8 +21,12 @@ FactoryBot.define do
generated_email
generated_user_name
password { 'teacher' }
role { 'teacher' }
platform_admin { false }
singleton_internal_user
member_of_study_group
transient do
teacher_in_study_group { true }
end
end
factory :learner, class: 'InternalUser' do
@ -27,8 +35,12 @@ FactoryBot.define do
generated_email
generated_user_name
password { 'learner' }
role { 'learner' }
platform_admin { false }
singleton_internal_user
member_of_study_group
transient do
teacher_in_study_group { false }
end
end
trait :activated_user do

View File

@ -14,6 +14,7 @@ FactoryBot.define do
after(:create) do |rfc|
rfc.file = rfc.submission.files.first
Comment.create(file: rfc.file, user: rfc.user, row: 1, text: "comment for rfc #{rfc.question}")
rfc.submission.study_group_id = rfc.user.current_study_group_id
end
end
end

View File

@ -20,4 +20,17 @@ FactoryBot.define do
initialize_with { klass.where(email: email).first_or_create }
end
end
trait :member_of_study_group do
after(:create) do |user, evaluator|
# Do not create a study group if already passed
if user.study_groups.blank?
study_group = create(:study_group)
user.study_groups << study_group
end
user.study_group_memberships.update(role: 'teacher') if evaluator.teacher_in_study_group
user.store_current_study_group_id(user.study_group_memberships.first)
end
end
end

View File

@ -36,4 +36,10 @@ describe ExternalUser do
expect(build(:external_user).teacher?).to be false
end
end
describe 'external_user has no current_study_group_id' do
it 'defaults to being a learner' do
expect(build(:external_user).learner?).to be true
end
end
end

View File

@ -63,13 +63,9 @@ describe InternalUser do
end
end
it 'validates the domain of the role' do
user.update(role: 'Foo')
expect(user.errors[:role]).to be_present
end
it 'validates the presence of a role' do
expect(user.errors[:role]).to be_present
it 'validates the presence of the platform_admin flag' do
user.update(platform_admin: nil)
expect(user.errors[:platform_admin]).to be_present
end
describe '#admin?' do

View File

@ -62,14 +62,10 @@ describe ExecutionEnvironmentPolicy do
expect(policy).to permit(build(:admin))
end
shared_examples 'it does not grant access' do |user|
it "does not grant access to a user with role #{user.role}" do
expect(policy).not_to permit(user)
it 'does not grant access to all other users' do
%i[external_user teacher].each do |factory_name|
expect(policy).not_to permit(build(factory_name))
end
end
%i[teacher external_user].each do |user|
include_examples 'it does not grant access', FactoryBot.build(user) # rubocop:disable RSpec/FactoryBot/SyntaxMethods
end
end
end