-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschool_spec.rb
More file actions
765 lines (619 loc) · 25.9 KB
/
Copy pathschool_spec.rb
File metadata and controls
765 lines (619 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe School do
let(:student) { create(:student, school:) }
let(:teacher) { create(:teacher, school:) }
let(:school) { create(:school, creator_id: SecureRandom.uuid) }
let!(:us_school) { create(:school, country_code: 'US', district_name: 'Some District', district_nces_id: '0100000', creator_id: SecureRandom.uuid) }
let!(:ireland_school) { create(:school, country_code: 'IE', school_roll_number: '01572D', creator_id: SecureRandom.uuid) }
describe 'associations' do
it 'has many classes' do
create(:school_class, school:, teacher_ids: [teacher.id])
create(:school_class, school:, teacher_ids: [teacher.id])
expect(school.classes.size).to eq(2)
end
it 'has many lessons' do
create(:lesson, school:, user_id: teacher.id)
create(:lesson, school:, user_id: teacher.id)
expect(school.lessons.size).to eq(2)
end
it 'has many projects' do
create(:project, user_id: student.id, school:)
create(:project, user_id: student.id, school:)
expect(school.projects.size).to eq(2)
end
it 'has many roles' do
Role.delete_all
create(:student_role, school:)
create(:owner_role, school:)
expect(school.roles.size).to eq(2)
end
it 'has many school email domains' do
SchoolEmailDomain.create!(school:, domain: 'example.edu')
SchoolEmailDomain.create!(school:, domain: 'other.edu')
expect(school.school_email_domains.size).to eq(2)
end
context 'when a school is destroyed' do
let!(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id]) }
let!(:lesson_1) { create(:lesson, user_id: teacher.id, school_class:) }
let!(:lesson_2) { create(:lesson, user_id: teacher.id, school:) }
let!(:project) { create(:project, user_id: student.id, school:) }
let!(:role) { create(:role, school:) }
before do
create(:class_student, school_class:, student_id: student.id)
end
it 'also destroys school classes to avoid making them invalid' do
expect { school.destroy! }.to change(SchoolClass, :count).by(-1)
end
it 'also destroys class students to avoid making them invalid' do
expect { school.destroy! }.to change(ClassStudent, :count).by(-1)
end
it 'also destroys class teachers to avoid making them invalid' do
expect { school.destroy! }.to change(ClassTeacher, :count).by(-1)
end
it 'does not destroy lessons' do
expect { school.destroy! }.not_to change(Lesson, :count)
end
it 'nullifies school_id and school_class_id fields on lessons' do
school.destroy!
lessons = [lesson_1, lesson_2].map(&:reload)
values = lessons.flat_map { |l| [l.school_id, l.school_class_id] }
expect(values).to eq [nil, nil, nil, nil]
end
it 'does not destroy projects' do
expect { school.destroy! }.not_to change(Project, :count)
end
it 'nullifies the school_id field on projects' do
school.destroy!
expect(project.reload.school_id).to be_nil
end
it 'does not destroy roles' do
expect { school.destroy! }.not_to change(Role, :count)
end
it 'nullifies the school_id field on roles' do
school.destroy!
expect(role.reload.school_id).to be_nil
end
it 'also destroys school email domains' do
SchoolEmailDomain.create!(school:, domain: 'example.edu')
expect { school.destroy! }.to change(SchoolEmailDomain, :count).by(-1)
end
end
end
describe 'validations' do
subject(:school) { create(:school) }
it 'has a valid default factory' do
expect(school).to be_valid
end
it 'can save the default factory' do
expect { school.save! }.not_to raise_error
end
it 'requires a name' do
school.name = ' '
expect(school).not_to be_valid
end
it 'requires a website' do
school.website = ' '
expect(school).not_to be_valid
end
it 'requires a creator_id' do
school.creator_id = nil
expect(school).not_to be_valid
end
it 'requires a unique creator_id' do
school.save!
another_school = build(:school, creator_id: school.creator_id)
another_school.valid?
expect(another_school.errors[:creator_id]).to include('has already been taken')
end
it 'schools can re-use creator_ids if the original school is rejected' do
rejected_school = create(:school, creator_id: SecureRandom.uuid, rejected_at: Time.zone.now)
other_school = build(:school, creator_id: rejected_school.creator_id)
expect(rejected_school).to be_valid
expect(other_school).to be_valid
end
it 'rejects a badly formed url for website' do
school.website = 'http://.example.com'
expect(school).not_to be_valid
end
it 'accepts a url with a multi-part TLD' do
school.website = 'https://example.co.uk'
expect(school).to be_valid
end
it 'does not require a reference for non-UK schools' do
school.country_code = 'DE'
school.reference = nil
expect(school).to be_valid
end
it 'does not require reference for UK schools' do
school.country_code = 'GB'
school.reference = nil
expect(school).to be_valid
end
it 'requires references to be unique if provided' do
school.reference = '100000'
school.save!
duplicate_school = build(:school, reference: '100000')
expect(duplicate_school).not_to be_valid
end
it('returns an error if reference is not unique') do
school.reference = '100000'
school.save!
duplicate_school = build(:school, reference: '100000')
duplicate_school.valid?
expect(duplicate_school.errors.details[:reference]).to include(hash_including(error: :taken))
end
it 'accepts a valid reference format (5-6 digits)' do
school.reference = '100000'
expect(school).to be_valid
end
it 'accepts a 5-digit reference' do
school.reference = '20000'
expect(school).to be_valid
end
it 'rejects new schools with a reference containing non-digit characters' do
school = build(:school, reference: 'URN-123')
expect(school).not_to be_valid
expect(school.errors[:reference]).to include('must be 5-6 digits (e.g., 100000)')
end
it 'rejects new schools with a reference that has too few digits' do
school = build(:school, reference: '1234')
expect(school).not_to be_valid
expect(school.errors[:reference]).to include('must be 5-6 digits (e.g., 100000)')
end
it 'rejects new schools with a reference that has too many digits' do
school = build(:school, reference: '1234567')
expect(school).not_to be_valid
expect(school.errors[:reference]).to include('must be 5-6 digits (e.g., 100000)')
end
it 'allows reference reuse when original school is rejected' do
school.update!(reference: '100000', rejected_at: Time.zone.now)
new_school = build(:school, reference: '100000')
expect(new_school).to be_valid
expect { new_school.save! }.not_to raise_error
end
it 'does not require a district_nces_id for UK schools' do
school.country_code = 'GB'
school.district_nces_id = nil
expect(school).to be_valid
end
it 'does not require a district_nces_id for CA schools' do
school.country_code = 'CA'
school.district_name = 'Some District'
school.district_nces_id = nil
expect(school).to be_valid
end
it 'does not require district_nces_id for US schools' do
us_school.district_nces_id = nil
expect(us_school).to be_valid
end
it 'requires district_name for US schools' do
us_school.district_name = nil
expect(us_school).not_to be_valid
expect(us_school.errors[:district_name]).to include("can't be blank")
end
it 'does not require district_name for non-US schools' do
school.district_name = nil
expect(school).to be_valid
end
it 'does not require district_name for CA schools' do
school.country_code = 'CA'
school.district_name = nil
expect(school).to be_valid
end
it 'requires district_nces_id to be unique if provided' do
duplicate_school = build(:school, country_code: 'US', district_nces_id: '0100000')
expect(duplicate_school).not_to be_valid
end
it 'accepts a valid district_nces_id format (7 digits)' do
us_school.district_nces_id = '0100000'
expect(us_school).to be_valid
end
it 'rejects new schools with a district_nces_id containing non-digit characters' do
us_school = build(:school, country_code: 'US', district_nces_id: '010000A')
expect(us_school).not_to be_valid
expect(us_school.errors[:district_nces_id]).to include('must be 7 digits (e.g., 0100000)')
end
it 'rejects new schools with a district_nces_id with wrong length' do
us_school = build(:school, country_code: 'US', district_nces_id: '123456')
expect(us_school).not_to be_valid
expect(us_school.errors[:district_nces_id]).to include('must be 7 digits (e.g., 0100000)')
end
it 'allows district_nces_id reuse when original school is rejected' do
us_school.update!(rejected_at: Time.zone.now)
new_school = build(:school, country_code: 'US', district_name: 'Some District', district_nces_id: '0100000')
expect(new_school).to be_valid
expect { new_school.save! }.not_to raise_error
end
it 'does not require a school_roll_number for non-Ireland schools' do
school.country_code = 'GB'
school.school_roll_number = nil
expect(school).to be_valid
end
it 'requires school_roll_number for Ireland for new schools' do
ireland_school = build(:school, country_code: 'IE', school_roll_number: nil)
expect(ireland_school).not_to be_valid
expect(ireland_school.errors[:school_roll_number]).to include("can't be blank")
end
it 'requires school_roll_number to be unique if provided' do
duplicate_school = build(:school, school_roll_number: '01572D', country_code: 'IE')
expect(duplicate_school).not_to be_valid
end
it 'returns error if school_roll_number is not unique' do
duplicate_school = build(:school, school_roll_number: '01572D', country_code: 'IE')
duplicate_school.valid?
expect(duplicate_school.errors.details[:school_roll_number]).to include(hash_including(error: :taken))
end
it 'accepts a valid alphanumeric school_roll_number' do
ireland_school.school_roll_number = '01572D'
expect(ireland_school).to be_valid
end
it 'accepts a school_roll_number with one or more letters' do
ireland_school.school_roll_number = '12345ABC'
expect(ireland_school).to be_valid
end
it 'rejects new schools with a school_roll_number that contains only numbers' do
ireland_school = build(:school, country_code: 'IE', school_roll_number: '01572')
expect(ireland_school).not_to be_valid
expect(ireland_school.errors[:school_roll_number]).to include('must be numbers followed by letters (e.g., 01572D)')
end
it 'rejects new schools with a school_roll_number containing only letters' do
ireland_school = build(:school, country_code: 'IE', school_roll_number: 'ABCDE')
expect(ireland_school).not_to be_valid
expect(ireland_school.errors[:school_roll_number]).to include('must be numbers followed by letters (e.g., 01572D)')
end
it 'rejects new schools with a school_roll_number containing special characters' do
ireland_school = build(:school, country_code: 'IE', school_roll_number: '01572-D')
expect(ireland_school).not_to be_valid
expect(ireland_school.errors[:school_roll_number]).to include('must be numbers followed by letters (e.g., 01572D)')
end
it 'normalizes blank school_roll_number to nil' do
school.school_roll_number = ' '
expect(school).to be_valid
expect(school.school_roll_number).to be_nil
end
it 'normalizes school_roll_number to uppercase' do
school.school_roll_number = '01572d'
expect(school).to be_valid
expect(school.school_roll_number).to eq('01572D')
end
it 'allows school_roll_number reuse when original school is rejected' do
ireland_school.update!(rejected_at: Time.zone.now)
new_school = build(:school, school_roll_number: '01572D', country_code: 'IE')
expect(new_school).to be_valid
expect { new_school.save! }.not_to raise_error
end
it 'requires an address_line_1' do
school.address_line_1 = ' '
expect(school).not_to be_valid
end
it 'requires a municipality' do
school.municipality = ' '
expect(school).not_to be_valid
end
it 'requires an administrative_area for new schools' do
school = build(:school, administrative_area: ' ')
expect(school).not_to be_valid
end
it 'requires a postal_code' do
school.postal_code = ' '
expect(school).not_to be_valid
end
it 'requires a country_code' do
school.country_code = ' '
expect(school).not_to be_valid
end
it "requires an 'ISO 3166-1 alpha-2' country_code" do
school.country_code = 'GBR'
expect(school).not_to be_valid
end
it 'does not require a creator_role' do
school.creator_role = nil
expect(school).to be_valid
end
it 'does not require a creator_department' do
school.creator_department = nil
expect(school).to be_valid
end
it 'requires creator_agree_authority to be true' do
school.creator_agree_authority = false
expect(school).not_to be_valid
end
it 'requires creator_agree_terms_and_conditions to be true' do
school.creator_agree_terms_and_conditions = false
expect(school).not_to be_valid
end
it 'requires creator_agree_responsible_safeguarding to be true' do
school.creator_agree_responsible_safeguarding = false
expect(school).not_to be_valid
end
it 'does not require creator_agree_to_ux_contact to be true' do
school.creator_agree_to_ux_contact = false
expect(school).to be_valid
end
it 'cannot have #rejected_at set when #verified_at is present' do
school.verify!
school.update(rejected_at: Time.zone.now)
expect(school.errors[:rejected_at]).to include('must be blank')
end
it 'cannot have #verified_at set when #rejected_at is present' do
school.rejected_at = Time.zone.now
school.update(verified_at: Time.zone.now)
expect(school.errors[:verified_at]).to include('must be blank')
end
it "cannot change #verified_at once it's been set" do
school.verify!
school.update(verified_at: nil)
expect(school.errors[:verified_at]).to include('cannot be changed after verification')
end
describe 'code validations' do
it 'requires #code to be unique' do
school # ensure existing school has a code
another_school = build(:school)
another_school.code = school.code
another_school.valid?
expect(another_school.errors[:code]).to include('has already been taken')
end
it 'requires code to be formatted as 3 pairs of digits separated by hyphens' do
school.update(code: 'invalid')
expect(school.errors[:code]).to include('is invalid')
end
it "cannot change #code once it's been set" do
school.update(code: '00-00-00')
expect(school.errors[:code]).to include('cannot be changed after onboarding')
end
end
it 'requires a user_origin' do
school.user_origin = nil
expect(school).not_to be_valid
end
it 'sets the user_origin to for_education by default' do
expect(school.user_origin).to eq('for_education')
end
end
describe '#creator' do
let(:creator) { create(:owner, school:) }
before do
school.update!(creator_id: creator.id)
stub_user_info_api_for(creator)
end
it 'returns a User instance' do
expect(school.creator).to be_instance_of(User)
end
it 'returns the creator from the UserInfo API matching the creator_id' do
expect(school.creator.id).to eq(creator.id)
end
end
describe '.find_for_user!' do
before do
stub_user_info_api_for(teacher)
end
it 'returns the school that the user has a role in' do
user = User.where(id: teacher.id).first
expect(described_class.find_for_user!(user)).to eq(school)
end
it "returns the school that the user created if they don't have a role in any school" do
creator = create(:user)
school.update!(creator_id: creator.id)
expect(described_class.find_for_user!(creator)).to eq(school)
end
it "raises ActiveRecord::RecordNotFound if the user doesn't have a role in a school" do
user = build(:user)
expect { described_class.find_for_user!(user) }.to raise_error(ActiveRecord::RecordNotFound)
end
it('raises ActiveRecord::RecordNotFound if the user is the creator of a rejected school') do
creator = create(:user)
school.update!(creator_id: creator.id)
school.update!(rejected_at: Time.zone.now)
expect { described_class.find_for_user!(creator) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
describe '#verified?' do
it 'returns true when verified_at is present' do
school.verified_at = Time.zone.now
expect(school).to be_verified
end
it 'returns false when verified_at is blank' do
school.verified_at = nil
expect(school).not_to be_verified
end
end
describe '#rejected?' do
it 'returns true when rejected_at is present' do
school.rejected_at = Time.zone.now
expect(school).to be_rejected
end
it 'returns false when rejected_at is blank' do
school.rejected_at = nil
expect(school).not_to be_rejected
end
end
describe '#verify!' do
it 'sets verified_at to the current time' do
school.verify!
expect(school.verified_at).to be_within(1.second).of(Time.zone.now)
end
it 'returns true on successful verification' do
expect(school.verify!).to be(true)
end
it 'raises ActiveRecord::RecordInvalid if verification fails' do
school.rejected_at = Time.zone.now
expect { school.verify! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe 'code generation' do
it 'automatically generates a code after creation' do
new_school = described_class.create!(build(:school).attributes.except('id', 'created_at', 'updated_at'))
expect(new_school.reload.code).to be_present
end
it 'uses the school code generator to generate the code' do
allow(ForEducationCodeGenerator).to receive(:generate).and_return('00-00-00')
new_school = described_class.create!(build(:school).attributes.except('id', 'created_at', 'updated_at', 'code'))
expect(new_school.reload.code).to eq('00-00-00')
end
it 'retries 5 times if the school code is not unique' do
existing_school = school
allow(ForEducationCodeGenerator).to receive(:generate).and_return(existing_school.code, existing_school.code, existing_school.code, existing_school.code, '00-00-00')
another_school = described_class.create!(build(:school).attributes.except('id', 'created_at', 'updated_at', 'code'))
expect(another_school.reload.code).to eq('00-00-00')
end
it 'raises exception if unique code cannot be generated in 5 retries' do
existing_school = school
allow(ForEducationCodeGenerator).to receive(:generate).and_return(existing_school.code)
another_school_attrs = build(:school).attributes.except('id', 'created_at', 'updated_at', 'code')
expect { described_class.create!(another_school_attrs) }.to raise_error(ActiveRecord::RecordInvalid, /Code has already been taken/)
end
end
describe '#generate_code!' do
it 'does not regenerate the code once it has been set' do
allow(ForEducationCodeGenerator).to receive(:generate)
school = create(:school)
existing_code = school.code
expect { school.generate_code! }.not_to change(school, :code)
expect(school.code).to eq(existing_code)
end
end
describe '#format_uk_postal_code' do
it 'retains correctly formatted UK postal_code' do
school.country_code = 'GB'
school.postal_code = 'SW1A 1AA'
school.save
expect(school.postal_code).to eq('SW1A 1AA')
end
it 'corrects incorrectly formatted UK postal_code' do
school.country_code = 'GB'
school.postal_code = 'SW1 A1AA'
expect { school.save }.to change(school, :postal_code).to('SW1A 1AA')
end
it 'formats UK postal_code with 4 char outcode' do
school.country_code = 'GB'
school.postal_code = 'SW1A1AA'
expect { school.save }.to change(school, :postal_code).to('SW1A 1AA')
end
it 'formats UK postal_code with 3 char outcode' do
school.country_code = 'GB'
school.postal_code = 'SW11AA'
expect { school.save }.to change(school, :postal_code).to('SW1 1AA')
end
it 'formats UK postal_code with 2 char outcode' do
school.country_code = 'GB'
school.postal_code = 'SW1AA'
expect { school.save }.to change(school, :postal_code).to('SW 1AA')
end
it 'does not format UK postal_code for short / invalid codes' do
school.country_code = 'GB'
school.postal_code = 'SW1A'
expect { school.save }.not_to change(school, :postal_code)
end
it 'does not format postal_code for non-UK countries' do
school.country_code = 'FR'
school.postal_code = '123456'
expect { school.save }.not_to change(school, :postal_code)
end
end
describe 'salesforce sync' do
around do |example|
ClimateControl.modify(SALESFORCE_ENABLED: 'true') { example.run }
end
it 'enqueues Salesforce::SchoolSyncJob exactly once on create, with is_create: true' do
expect { create(:school) }
.to have_enqueued_job(Salesforce::SchoolSyncJob)
.exactly(1).times
.with(hash_including(is_create: true))
end
it 'enqueues Salesforce::ContactSyncJob on create' do
expect { create(:school) }.to have_enqueued_job(Salesforce::ContactSyncJob)
end
it 'enqueues Salesforce::SchoolSyncJob on update' do
school = create(:school)
expect { school.update!(name: 'Updated Name') }.to have_enqueued_job(Salesforce::SchoolSyncJob).with(hash_including(is_create: false))
end
it 'enqueues Salesforce::ContactSyncJob on update' do
school = create(:school)
expect { school.update!(name: 'Updated Name') }.to have_enqueued_job(Salesforce::ContactSyncJob)
end
context 'when SALESFORCE_ENABLED is false' do
around do |example|
ClimateControl.modify(SALESFORCE_ENABLED: 'false') { example.run }
end
it 'does not enqueue Salesforce::SchoolSyncJob on create' do
expect { create(:school) }.not_to have_enqueued_job(Salesforce::SchoolSyncJob)
end
it 'does not enqueue Salesforce::ContactSyncJob on create' do
expect { create(:school) }.not_to have_enqueued_job(Salesforce::ContactSyncJob)
end
end
end
describe '#reopen' do
it 'sets rejected_at to nil' do
school.rejected_at = Time.zone.now
school.reopen
expect(school.rejected_at).to be_nil
end
it 'returns true on successful reopening' do
school.rejected_at = Time.zone.now
expect(school.reopen).to be(true)
end
it 'returns false when trying to reopen a non-rejected school' do
expect(school.reopen).to be(false)
end
end
describe '#auto_join_enabled?' do
it 'returns true when the school has at least one registered email domain' do
SchoolEmailDomain.create!(school:, domain: 'valid.edu')
expect(school.auto_join_enabled?).to be(true)
end
it 'returns true when the school has multiple registered email domains' do
SchoolEmailDomain.create!(school:, domain: 'valid.edu')
SchoolEmailDomain.create!(school:, domain: 'other.edu')
expect(school.auto_join_enabled?).to be(true)
end
it 'returns false when the school has no registered email domains' do
expect(school.auto_join_enabled?).to be(false)
end
it 'returns false after all registered email domains are removed' do
domain = SchoolEmailDomain.create!(school:, domain: 'valid.edu')
domain.destroy!
expect(school.reload.auto_join_enabled?).to be(false)
end
end
describe '#valid_domain?' do
let(:valid_domain) { 'valid.edu' }
let(:unregistered_domain) { 'invalid.edu' }
let(:invalid_domain) { 'not a domain' }
before do
SchoolEmailDomain.create!(school:, domain: valid_domain)
end
it 'returns true when school has registered the email domain' do
expect(school.valid_domain?(valid_domain)).to be(true)
end
it 'returns false when school has not registered the email domain' do
expect(school.valid_domain?(unregistered_domain)).to be(false)
end
end
describe '#email_domain_in_school_domains?' do
before do
SchoolEmailDomain.create!(school:, domain: 'valid.edu')
end
it 'returns true when the email domain is registered for the school' do
expect(school.email_domain_in_school_domains?('user@valid.edu')).to be(true)
end
it 'returns false when the email domain is not registered for the school' do
expect(school.email_domain_in_school_domains?('user@other.edu')).to be(false)
end
it 'normalizes case and surrounding whitespace on the domain' do
expect(school.email_domain_in_school_domains?('User@VALID.EDU ')).to be(true)
end
it 'returns false when the email is blank' do
expect(school.email_domain_in_school_domains?('')).to be(false)
expect(school.email_domain_in_school_domains?(nil)).to be(false)
end
it 'returns false when the email has no @ separator' do
expect(school.email_domain_in_school_domains?('not-an-email')).to be(false)
end
it 'returns false when the local part or domain is missing' do
expect(school.email_domain_in_school_domains?('@valid.edu')).to be(false)
expect(school.email_domain_in_school_domains?('user@')).to be(false)
end
end
end