|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +describe EmailMxValidator do |
| 6 | + describe '#validate' do |
| 7 | + let(:user) { double(email: 'foo@example.com', errors: double(add: nil)) } |
| 8 | + |
| 9 | + it 'adds an error if there are no DNS records for the e-mail domain' do |
| 10 | + resolver = double |
| 11 | + |
| 12 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([]) |
| 13 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) |
| 14 | + allow(resolver).to receive(:timeouts=).and_return(nil) |
| 15 | + allow(Resolv::DNS).to receive(:open).and_yield(resolver) |
| 16 | + |
| 17 | + subject.validate(user) |
| 18 | + expect(user.errors).to have_received(:add) |
| 19 | + end |
| 20 | + |
| 21 | + it 'adds an error if a MX record exists but does not lead to an IP' do |
| 22 | + resolver = double |
| 23 | + |
| 24 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')]) |
| 25 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) |
| 26 | + allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([]) |
| 27 | + allow(resolver).to receive(:timeouts=).and_return(nil) |
| 28 | + allow(Resolv::DNS).to receive(:open).and_yield(resolver) |
| 29 | + |
| 30 | + subject.validate(user) |
| 31 | + expect(user.errors).to have_received(:add) |
| 32 | + end |
| 33 | + |
| 34 | + it 'adds an error if the A record is blacklisted' do |
| 35 | + EmailDomainBlock.create!(domain: '1.2.3.4') |
| 36 | + resolver = double |
| 37 | + |
| 38 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([]) |
| 39 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '1.2.3.4')]) |
| 40 | + allow(resolver).to receive(:timeouts=).and_return(nil) |
| 41 | + allow(Resolv::DNS).to receive(:open).and_yield(resolver) |
| 42 | + |
| 43 | + subject.validate(user) |
| 44 | + expect(user.errors).to have_received(:add) |
| 45 | + end |
| 46 | + |
| 47 | + it 'adds an error if the MX record is blacklisted' do |
| 48 | + EmailDomainBlock.create!(domain: '2.3.4.5') |
| 49 | + resolver = double |
| 50 | + |
| 51 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')]) |
| 52 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) |
| 53 | + allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')]) |
| 54 | + allow(resolver).to receive(:timeouts=).and_return(nil) |
| 55 | + allow(Resolv::DNS).to receive(:open).and_yield(resolver) |
| 56 | + |
| 57 | + subject.validate(user) |
| 58 | + expect(user.errors).to have_received(:add) |
| 59 | + end |
| 60 | + |
| 61 | + it 'adds an error if the MX hostname is blacklisted' do |
| 62 | + EmailDomainBlock.create!(domain: 'mail.example.com') |
| 63 | + resolver = double |
| 64 | + |
| 65 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')]) |
| 66 | + allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) |
| 67 | + allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')]) |
| 68 | + allow(resolver).to receive(:timeouts=).and_return(nil) |
| 69 | + allow(Resolv::DNS).to receive(:open).and_yield(resolver) |
| 70 | + |
| 71 | + subject.validate(user) |
| 72 | + expect(user.errors).to have_received(:add) |
| 73 | + end |
| 74 | + end |
| 75 | +end |
0 commit comments