Skip to content

Commit afc6d34

Browse files
committed
Fix bugs in SchoolVerificationService
We were previously ignoring the result of the call to `School#update` which meant that if the update failed we'd still create the owner and teacher roles, and return true from `#verify`. Calling `School#update!` means that if the update fails then a `ActiveRecord::RecordInvalid` exception will be raised, the transaction will be rolled back and the `rescue` block will be invoked. Fixing the first problem revealed that the `rescue` block was attempting to access `school_id` which doesn't exist.
1 parent 10f9fea commit afc6d34

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

app/services/school_verification_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ def initialize(school_id)
88
def verify
99
School.transaction do
1010
school = School.find(@school_id)
11-
school.update(verified_at: Time.zone.now, rejected_at: nil)
11+
school.update!(verified_at: Time.zone.now, rejected_at: nil)
12+
1213
Role.owner.create(user_id: school.creator_id, school:)
1314
Role.teacher.create(user_id: school.creator_id, school:)
1415
end
1516
rescue StandardError => e
1617
Sentry.capture_exception(e)
17-
Rails.logger.error { "Failed to verify school #{school_id}: #{e.message}" }
18+
Rails.logger.error { "Failed to verify school #{@school_id}: #{e.message}" }
1819
false
1920
else
2021
true

spec/services/school_verification_service_spec.rb

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,62 @@
1010
let(:organisation_id) { SecureRandom.uuid }
1111

1212
describe '#verify' do
13-
before do
14-
service.verify
15-
school.reload
16-
end
13+
describe 'when school can be saved' do
14+
it 'sets verified_at to a date' do
15+
service.verify
16+
expect(school.reload.verified_at).to be_a(ActiveSupport::TimeWithZone)
17+
end
18+
19+
it 'grants the creator the owner role for the school' do
20+
service.verify
21+
expect(school_creator).to be_school_owner(school)
22+
end
23+
24+
it 'grants the creator the teacher role for the school' do
25+
service.verify
26+
expect(school_creator).to be_school_teacher(school)
27+
end
1728

18-
it 'sets verified_at to a date' do
19-
expect(school.verified_at).to be_a(ActiveSupport::TimeWithZone)
29+
it 'returns true' do
30+
expect(service.verify).to be(true)
31+
end
2032
end
2133

22-
it 'grants the creator the owner role for the school' do
23-
expect(school_creator).to be_school_owner(school)
34+
describe 'when school cannot be saved' do
35+
before do
36+
school.update_attribute(:website, 'invalid') # rubocop:disable Rails/SkipsModelValidations
37+
end
38+
39+
it 'does not set verified_at' do
40+
service.verify
41+
expect(school.reload.verified_at).to be_nil
42+
end
43+
44+
it 'does not create owner role' do
45+
service.verify
46+
expect(school_creator).not_to be_school_owner(school)
47+
end
48+
49+
it 'does not create teacher role' do
50+
service.verify
51+
expect(school_creator).not_to be_school_teacher(school)
52+
end
53+
54+
it 'returns false' do
55+
expect(service.verify).to be(false)
56+
end
2457
end
2558

26-
it 'grants the creator the teacher role for the school' do
27-
expect(school_creator).to be_school_teacher(school)
59+
describe 'when the school cannot be found' do
60+
before do
61+
allow(Sentry).to receive(:capture_exception)
62+
allow(School).to receive(:find).with(school.id).and_raise(ActiveRecord::RecordNotFound)
63+
service.verify
64+
end
65+
66+
it 'reports the error in Sentry' do
67+
expect(Sentry).to have_received(:capture_exception)
68+
end
2869
end
2970
end
3071

0 commit comments

Comments
 (0)