Skip to content

Commit a0600c4

Browse files
committed
update Project.with_user to not call profile api when user is a student
1 parent 95e3fb6 commit a0600c4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

app/models/project.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def self.with_users(current_user)
5353
end
5454

5555
def with_user(current_user)
56+
# students cannot call the Profile API so do not even try
57+
return [self, nil] if current_user&.student?
58+
5659
school = School.find_by(id: school_id)
5760
students = SchoolStudent::List.call(school:, token: current_user.token,
5861
student_ids: [user_id])[:school_students] || []

spec/models/project_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,40 @@
274274
expect(pair).to eq([project, user])
275275
end
276276

277+
it 'calls the Profile API to fetch the user' do
278+
project = create(:project, user_id: student.id, school_id: school.id)
279+
280+
allow(SchoolStudent::List).to receive(:call).and_call_original
281+
282+
project.with_user(teacher)
283+
284+
expect(SchoolStudent::List).to have_received(:call).with(
285+
school:,
286+
token: teacher.token,
287+
student_ids: [student.id]
288+
)
289+
end
290+
277291
it 'returns a nil value if the member has no profile account' do
278292
user_id = SecureRandom.uuid
279293
project = create(:project, user_id:)
280294

281295
pair = project.with_user(teacher)
282296
expect(pair).to eq([project, nil])
283297
end
298+
299+
context 'when current_user is a student' do
300+
it 'returns the project with nil user without calling Profile API' do
301+
project = create(:project, user_id: student.id, school_id: school.id)
302+
303+
allow(SchoolStudent::List).to receive(:call)
304+
305+
pair = project.with_user(student)
306+
307+
expect(pair).to eq([project, nil])
308+
expect(SchoolStudent::List).not_to have_received(:call)
309+
end
310+
end
284311
end
285312

286313
describe '#last_edited_at' do

0 commit comments

Comments
 (0)