|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe SchoolEmailDomainValidator do |
| 6 | + describe '.call' do |
| 7 | + it 'returns success with a normalised host for a valid domain' do |
| 8 | + result = described_class.call('example.com') |
| 9 | + |
| 10 | + expect(result).to eq('example.com') |
| 11 | + end |
| 12 | + |
| 13 | + context 'with a valid domain' do |
| 14 | + it 'extracts the host for http' do |
| 15 | + result = described_class.call('http://mail.school.edu/path?query=1') |
| 16 | + expect(result).to eq('mail.school.edu') |
| 17 | + end |
| 18 | + |
| 19 | + it 'extracts the host for https' do |
| 20 | + result = described_class.call('https://mail.school.edu/path?query=1') |
| 21 | + expect(result).to eq('mail.school.edu') |
| 22 | + end |
| 23 | + |
| 24 | + it 'removes a trailing dot' do |
| 25 | + result = described_class.call('example.edu.') |
| 26 | + expect(result).to eq('example.edu') |
| 27 | + end |
| 28 | + |
| 29 | + it 'lowercases the host' do |
| 30 | + result = described_class.call('EXAMPLE.EDU') |
| 31 | + expect(result).to eq('example.edu') |
| 32 | + end |
| 33 | + |
| 34 | + it 'removes a leading @' do |
| 35 | + result = described_class.call('@example.edu') |
| 36 | + expect(result).to eq('example.edu') |
| 37 | + end |
| 38 | + |
| 39 | + it 'accepts a subdomain' do |
| 40 | + result = described_class.call('mail.example.edu') |
| 41 | + expect(result).to eq('mail.example.edu') |
| 42 | + end |
| 43 | + |
| 44 | + it 'accepts a hostname er a multi-part public suffix' do |
| 45 | + result = described_class.call('school.example.co.uk') |
| 46 | + expect(result).to eq('school.example.co.uk') |
| 47 | + end |
| 48 | + |
| 49 | + it 'accepts a district-style with a multi-part public suffix' do |
| 50 | + result = described_class.call('school.k12.tx.us') |
| 51 | + expect(result).to eq('school.k12.tx.us') |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when the domain is blank' do |
| 56 | + it 'raises BlankDomain' do |
| 57 | + expect { described_class.call('') }.to raise_error(SchoolEmailDomainValidator::BlankDomainError) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when the domain has an invalid URI' do |
| 62 | + it 'raises InvalidURIError' do |
| 63 | + expect { described_class.call('https://exa mple.com') }.to raise_error(SchoolEmailDomainValidator::InvalidURIError) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context 'when the host does not match accounts_host_format' do |
| 68 | + it 'raises InvalidHostError' do |
| 69 | + expect { described_class.call('school_domain.edu') } |
| 70 | + .to raise_error(SchoolEmailDomainValidator::InvalidHostError) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'when the host fails PublicSuffix validation' do |
| 75 | + it 'raises PublicSuffixError' do |
| 76 | + expect { described_class.call('co.uk') } |
| 77 | + .to raise_error(SchoolEmailDomainValidator::PublicSuffixError) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments