Skip to content

Commit c937f6f

Browse files
committed
Allow teacher invitation to be accepted multiple times
Previously this was unnecessarily strict which made testing in development a bit harder than it needed to be.
1 parent 5c4bbbd commit c937f6f

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

app/controllers/api/teacher_invitations_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ def show
1313
end
1414

1515
def accept
16-
role = Role.teacher.build(user_id: current_user.id, school: @invitation.school)
17-
if role.valid?
18-
role.save
16+
role = Role.teacher.find_or_initialize_by(user_id: current_user.id, school: @invitation.school)
17+
if role.save
1918
@invitation.update!(accepted_at: Time.current) if @invitation.accepted_at.blank?
2019
head :ok
2120
else

spec/features/teacher_invitations/accepting_an_invitation_spec.rb

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,6 @@
9898
end
9999
end
100100

101-
context 'when user already has teacher role for the same school' do
102-
before do
103-
Role.teacher.create!(user_id: user.id, school:)
104-
end
105-
106-
it 'responds 422 Unprocessable entity' do
107-
put("/api/teacher_invitations/#{token}/accept", headers:)
108-
109-
expect(response).to have_http_status(:unprocessable_entity)
110-
end
111-
112-
it 'leaves the user with the teacher role for that school' do
113-
put("/api/teacher_invitations/#{token}/accept", headers:)
114-
115-
expect(user).to be_school_teacher(school)
116-
end
117-
118-
it 'includes validation errors in response' do
119-
put("/api/teacher_invitations/#{token}/accept", headers:)
120-
121-
json = JSON.parse(response.body)
122-
expect(json['error']).to eq({ 'role' => ['has already been taken'] })
123-
end
124-
125-
it 'does not set the accepted_at timestamp on the invitation' do
126-
freeze_time(with_usec: false) do
127-
put("/api/teacher_invitations/#{token}/accept", headers:)
128-
129-
expect(invitation.reload.accepted_at).to be_blank
130-
end
131-
end
132-
end
133-
134101
context 'when user already has a role for another school' do
135102
let(:another_shool) { create(:school) }
136103

@@ -208,6 +175,32 @@
208175
end
209176
end
210177

178+
context 'when user already has teacher role for the same school' do
179+
before do
180+
Role.teacher.create!(user_id: user.id, school:)
181+
end
182+
183+
it 'responds 200 OK' do
184+
put("/api/teacher_invitations/#{token}/accept", headers:)
185+
186+
expect(response).to have_http_status(:ok)
187+
end
188+
189+
it 'leaves the user with the teacher role for that school' do
190+
put("/api/teacher_invitations/#{token}/accept", headers:)
191+
192+
expect(user).to be_school_teacher(school)
193+
end
194+
195+
it 'sets the accepted_at timestamp on the invitation' do
196+
freeze_time(with_usec: false) do
197+
put("/api/teacher_invitations/#{token}/accept", headers:)
198+
199+
expect(invitation.reload.accepted_at).to eq(Time.current)
200+
end
201+
end
202+
end
203+
211204
context 'when user already has owner role for the same school' do
212205
before do
213206
Role.owner.create!(user_id: user.id, school:)

0 commit comments

Comments
 (0)