|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe SchoolEmailDomain::Create, type: :unit do |
| 6 | + let(:school) { create(:school) } |
| 7 | + let(:domain) { 'school.edu' } |
| 8 | + let(:token) { UserProfileMock::TOKEN } |
| 9 | + |
| 10 | + before { stub_profile_api_update_school_email_domains } |
| 11 | + |
| 12 | + context 'with valid values' do |
| 13 | + it 'returns a successful operation response' do |
| 14 | + response = described_class.call(school:, domain:, token:) |
| 15 | + expect(response.success?).to be(true) |
| 16 | + end |
| 17 | + |
| 18 | + it 'creates a school email domain' do |
| 19 | + expect { described_class.call(school:, domain:, token:) }.to change(SchoolEmailDomain, :count).by(1) |
| 20 | + end |
| 21 | + |
| 22 | + it 'returns the domain in the operation response' do |
| 23 | + response = described_class.call(school:, domain:, token:) |
| 24 | + expect(response[:school_email_domain]).to be_a(SchoolEmailDomain) |
| 25 | + end |
| 26 | + |
| 27 | + it 'assigns the domain' do |
| 28 | + response = described_class.call(school:, domain:, token:) |
| 29 | + expect(response[:school_email_domain].domain).to eq(domain) |
| 30 | + end |
| 31 | + |
| 32 | + it 'assigns the school' do |
| 33 | + response = described_class.call(school:, domain:, token:) |
| 34 | + expect(response[:school_email_domain].school_id).to eq(school.id) |
| 35 | + end |
| 36 | + |
| 37 | + it 'syncs the domains to Profile' do |
| 38 | + described_class.call(school:, domain:, token:) |
| 39 | + expect(ProfileApiClient).to have_received(:update_school_email_domains).with( |
| 40 | + token:, |
| 41 | + school_id: school.id, |
| 42 | + school_email_domains: [domain] |
| 43 | + ) |
| 44 | + end |
| 45 | + |
| 46 | + context 'when multiple domains already exist' do |
| 47 | + before do |
| 48 | + create(:school_email_domain, school:, domain: 'first.edu') |
| 49 | + create(:school_email_domain, school:, domain: 'second.edu') |
| 50 | + create(:school_email_domain, school:, domain: 'third.edu') |
| 51 | + end |
| 52 | + |
| 53 | + it 'syncs all domains to Profile' do |
| 54 | + described_class.call(school:, domain:, token:) |
| 55 | + expect(ProfileApiClient).to have_received(:update_school_email_domains).with( |
| 56 | + token:, |
| 57 | + school_id: school.id, |
| 58 | + school_email_domains: ['first.edu', 'second.edu', 'third.edu', domain] |
| 59 | + ) |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + shared_examples 'an invalid record' do |
| 65 | + before { allow(Sentry).to receive(:capture_exception) } |
| 66 | + |
| 67 | + it 'does not create a school email domain' do |
| 68 | + expect { described_class.call(school:, domain:, token:) }.not_to change(SchoolEmailDomain, :count) |
| 69 | + end |
| 70 | + |
| 71 | + it 'returns a failed operation response' do |
| 72 | + response = described_class.call(school:, domain:, token:) |
| 73 | + expect(response.failure?).to be(true) |
| 74 | + end |
| 75 | + |
| 76 | + it 'does not send the exception to Sentry' do |
| 77 | + described_class.call(school:, domain:, token:) |
| 78 | + expect(Sentry).not_to have_received(:capture_exception).with(kind_of(StandardError)) |
| 79 | + end |
| 80 | + |
| 81 | + it 'returns the error message in the operation response' do |
| 82 | + response = described_class.call(school:, domain:, token:) |
| 83 | + expect(response[:error]).to include('') |
| 84 | + end |
| 85 | + |
| 86 | + it 'does not attempt to update Profile' do |
| 87 | + described_class.call(school:, domain:, token:) |
| 88 | + expect(ProfileApiClient).not_to have_received(:update_school_email_domains) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + context 'when domain is blank' do |
| 93 | + let(:domain) { '' } |
| 94 | + let(:expected_error_message) { "Domain can't be blank" } |
| 95 | + |
| 96 | + it_behaves_like 'an invalid record' |
| 97 | + end |
| 98 | + |
| 99 | + context 'when domain is not an FQDN' do |
| 100 | + let(:domain) { 'edu' } |
| 101 | + let(:expected_error_message) { 'Domain must be a fully qualified domain name' } |
| 102 | + |
| 103 | + it_behaves_like 'an invalid record' |
| 104 | + end |
| 105 | + |
| 106 | + context 'when domain has an invalid URI' do |
| 107 | + let(:domain) { 'exa mple.com' } |
| 108 | + let(:expected_error_message) { 'Domain must be a valid domain format' } |
| 109 | + |
| 110 | + it_behaves_like 'an invalid record' |
| 111 | + end |
| 112 | + |
| 113 | + context 'when domain has an invalid public suffix' do |
| 114 | + let(:domain) { 'co.uk' } |
| 115 | + let(:expected_error_message) { 'Domain must be a registrable domain name' } |
| 116 | + |
| 117 | + it_behaves_like 'an invalid record' |
| 118 | + end |
| 119 | + |
| 120 | + context 'when domain is a duplicate' do |
| 121 | + before { create(:school_email_domain, school:, domain:) } |
| 122 | + |
| 123 | + let(:expected_error_message) { 'Domain has already been taken' } |
| 124 | + |
| 125 | + it_behaves_like 'an invalid record' |
| 126 | + end |
| 127 | + |
| 128 | + context 'when Profile sync fails' do |
| 129 | + let(:profile_error) do |
| 130 | + ProfileApiClient::UnexpectedResponse.new( |
| 131 | + instance_double(Faraday::Response, status: 500, headers: {}, body: '') |
| 132 | + ) |
| 133 | + end |
| 134 | + |
| 135 | + before do |
| 136 | + allow(Sentry).to receive(:capture_exception) |
| 137 | + |
| 138 | + allow(ProfileApiClient).to receive(:update_school_email_domains) |
| 139 | + .and_raise(profile_error) |
| 140 | + end |
| 141 | + |
| 142 | + it 'attempts to sync to Profile' do |
| 143 | + described_class.call(school:, domain:, token:) |
| 144 | + expect(ProfileApiClient).to have_received(:update_school_email_domains).once |
| 145 | + end |
| 146 | + |
| 147 | + it 'does not persist the domain' do |
| 148 | + expect { described_class.call(school:, domain:, token:) } |
| 149 | + .not_to change(SchoolEmailDomain, :count) |
| 150 | + end |
| 151 | + |
| 152 | + it 'sends the exception to Sentry' do |
| 153 | + described_class.call(school:, domain:, token:) |
| 154 | + expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError)) |
| 155 | + end |
| 156 | + |
| 157 | + it 'returns a failed operation response' do |
| 158 | + expect(described_class.call(school:, domain:, token:)).to be_failure |
| 159 | + end |
| 160 | + |
| 161 | + it 'returns the error message in the operation response' do |
| 162 | + response = described_class.call(school:, domain:, token:) |
| 163 | + expect(response[:error]).to eq('Unexpected response from Profile API (status code 500)') |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments