|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe SchoolEmailDomain do |
| 6 | + subject(:school_email_domain) { described_class.create!(school:, domain:) } |
| 7 | + |
| 8 | + let(:school) { create(:school, creator_id: SecureRandom.uuid) } |
| 9 | + let(:domain) { 'example.edu' } |
| 10 | + |
| 11 | + describe 'associations' do |
| 12 | + it { is_expected.to belong_to(:school) } |
| 13 | + it { is_expected.to validate_presence_of(:domain) } |
| 14 | + it { is_expected.to be_valid } |
| 15 | + end |
| 16 | + |
| 17 | + context 'with a valid domain' do |
| 18 | + it 'accepts the domain' do |
| 19 | + expect(school_email_domain.domain).to eq('example.edu') |
| 20 | + end |
| 21 | + |
| 22 | + describe 'domain normalisation' do |
| 23 | + let(:domain) { '@EXAMPLE.EDU.' } |
| 24 | + |
| 25 | + it 'normalises a domain' do |
| 26 | + expect(school_email_domain.domain).to eq('example.edu') |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe 'domain uniqueness' do |
| 31 | + context 'when the proposed domain matches the existing record' do |
| 32 | + subject(:school_email_domain) { described_class.new(school:, domain:) } |
| 33 | + |
| 34 | + let(:domain) { 'example.edu' } |
| 35 | + |
| 36 | + before do |
| 37 | + described_class.create!(school:, domain: 'example.edu') |
| 38 | + school_email_domain.valid? |
| 39 | + end |
| 40 | + |
| 41 | + it 'rejects the duplicate' do |
| 42 | + expect(school_email_domain).not_to be_valid |
| 43 | + end |
| 44 | + |
| 45 | + it 'records :taken on domain' do |
| 46 | + expect(school_email_domain.errors.of_kind?(:domain, :taken)).to be(true) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'when the proposed domain matches after normalisation' do |
| 51 | + subject(:school_email_domain) { described_class.new(school:, domain:) } |
| 52 | + |
| 53 | + let(:domain) { 'http://EXAMPLE.EDU' } |
| 54 | + |
| 55 | + before do |
| 56 | + described_class.create!(school:, domain: 'example.edu') |
| 57 | + school_email_domain.valid? |
| 58 | + end |
| 59 | + |
| 60 | + it 'rejects the duplicate' do |
| 61 | + expect(school_email_domain).not_to be_valid |
| 62 | + end |
| 63 | + |
| 64 | + it 'records :taken on domain' do |
| 65 | + expect(school_email_domain.errors.of_kind?(:domain, :taken)).to be(true) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + it 'allows the same domain for a different school' do |
| 70 | + described_class.create!(school:, domain: 'example.edu') |
| 71 | + other_school = create(:school, creator_id: SecureRandom.uuid) |
| 72 | + other_school_email_domain = described_class.new(school: other_school, domain: 'example.edu') |
| 73 | + |
| 74 | + expect(other_school_email_domain).to be_valid |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context 'with an invalid domain' do |
| 80 | + it { is_expected.not_to allow_value('').for(:domain) } |
| 81 | + it { is_expected.not_to allow_value(' ').for(:domain) } |
| 82 | + it { is_expected.not_to allow_value('http://').for(:domain) } |
| 83 | + it { is_expected.not_to allow_value('edu').for(:domain) } |
| 84 | + it { is_expected.not_to allow_value('com').for(:domain) } |
| 85 | + it { is_expected.not_to allow_value('co.uk').for(:domain) } |
| 86 | + it { is_expected.not_to allow_value('http://invalid uri').for(:domain) } |
| 87 | + it { is_expected.not_to allow_value('-wrong.edu').for(:domain) } |
| 88 | + end |
| 89 | +end |
0 commit comments