Skip to content

Commit a31a4bf

Browse files
committed
Specs: Use default study group for internal users
When creating a teacher (or admin) through the respective factory, they were not yet automatically assigned to the respective default study group. This commit changes said behavior. The actual creation process within the UI remains unchanged, and it is still required to choose the appropriate study groups manually. Part of #2986
1 parent 7dc7b82 commit a31a4bf

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

spec/controllers/exercises_controller_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202

203203
context 'with other users accessing an unpublished exercise' do
204204
let(:exercise) { create(:fibonacci, unpublished: true) }
205-
let(:user) { create(:teacher) }
205+
let(:user) { create(:external_teacher) }
206206

207207
before { perform_request.call }
208208

@@ -442,7 +442,8 @@
442442
end
443443

444444
context 'when the user cannot update the exercise' do
445-
let(:codeharbor_link) { create(:codeharbor_link, api_key: 'anotherkey') }
445+
let(:user) { create(:external_teacher) }
446+
let(:codeharbor_link) { create(:codeharbor_link, user:, api_key: 'anotherkey') }
446447

447448
it 'renders correct response' do
448449
post_request

spec/factories/internal_user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
transient do
2828
teacher_in_study_group { true }
2929
end
30+
31+
factory :external_teacher do
32+
consumer { association :consumer, name: 'Other Consumer' }
33+
end
3034
end
3135

3236
factory :learner, class: 'InternalUser' do

spec/factories/shared_traits.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323

2424
trait :member_of_study_group do
2525
after(:create) do |user, evaluator|
26-
# Do not create a study group if already passed
27-
if user.study_groups.blank?
28-
study_group = create(:study_group)
26+
if user.study_groups.blank? && user.is_a?(ExternalUser)
27+
# Do not create a study group if already passed
28+
study_group = create(:study_group, consumer: user.consumer)
2929
user.study_groups << study_group
30+
elsif user.is_a?(InternalUser)
31+
# Always add the user to the default study group
32+
default_study_group = user.consumer.study_groups.find_by(external_id: nil)
33+
StudyGroupMembership.find_or_create_by!(study_group: default_study_group, user:)
34+
# Reload the study groups to ensure the membership is reflected
35+
user.study_groups.reload
3036
end
3137

3238
user.study_group_memberships.update(role: 'teacher') if evaluator.teacher_in_study_group

spec/policies/exercise_policy_spec.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,18 @@
209209
expect(policy).to permit(exercise.author, exercise)
210210
end
211211

212-
it 'does not grant access to everyone' do
213-
%i[external_user teacher].each do |factory_name|
214-
expect(policy).not_to permit(create(factory_name), exercise)
215-
end
212+
it 'grants access to teachers of the same study group' do
213+
# By default, the exercise author and the teacher are internal users and share the same study group
214+
expect(policy).to permit(create(:teacher), exercise)
215+
end
216+
217+
it 'does not grant access to teachers of another consumer' do
218+
# With another consumer, the teacher is in another study group, too.
219+
expect(policy).not_to permit(create(:external_teacher), exercise)
220+
end
221+
222+
it 'does not grant access to external users' do
223+
expect(policy).not_to permit(create(:external_user), exercise)
216224
end
217225
end
218226
end
@@ -222,7 +230,7 @@
222230
describe '#resolve' do
223231
let(:admin) { create(:admin) }
224232
let(:external_user) { create(:external_user) }
225-
let(:teacher) { create(:teacher) }
233+
let(:teacher) { create(:external_teacher) }
226234

227235
before do
228236
[admin, teacher].each do |user|

spec/policies/request_for_comment_policy_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
end
158158
end
159159

160-
let(:rfc_author) { create(:learner, consumer: author_consumer, study_groups: author_study_groups) }
160+
let(:rfc_author) { create(:external_user, consumer: author_consumer, study_groups: author_study_groups) }
161161
let(:author_study_groups) { create_list(:study_group, 1, consumer: author_consumer) }
162162
let(:rfc) { create(:rfc, user: rfc_author) }
163163

spec/services/proforma_service/import_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
end
183183

184184
context 'when another user imports the exercise' do
185-
let(:import_user) { create(:teacher) }
185+
let(:import_user) { create(:external_teacher) }
186186

187187
it 'raises a proforma error' do
188188
expect { imported_exercise.save! }.to raise_error ProformaXML::ExerciseNotOwned

spec/system/authentication_system_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'rails_helper'
44

55
RSpec.describe 'Authentication' do
6-
let(:user) { create(:teacher) }
6+
let(:user) { create(:external_teacher) }
77
let(:password) { attributes_for(:teacher)[:password] }
88

99
context 'when signed out' do

0 commit comments

Comments
 (0)