|
| 1 | +class EmailRecentlyVerifiedValidatable |
| 2 | + include ActiveModel::Validations |
| 3 | + attr_accessor :email |
| 4 | + |
| 5 | + def initialize(email:) |
| 6 | + @email = email |
| 7 | + end |
| 8 | + |
| 9 | + validates :email, email_recently_verified: true |
| 10 | +end |
| 11 | + |
| 12 | +RSpec.describe EmailRecentlyVerifiedValidator do |
| 13 | + subject { EmailRecentlyVerifiedValidatable.new(email:) } |
| 14 | + |
| 15 | + let(:email_verifier_job) { instance_double(EmailVerifierJob, perform: nil) } |
| 16 | + |
| 17 | + before do |
| 18 | + allow(EmailVerifierJob).to receive(:new).and_return(email_verifier_job) |
| 19 | + end |
| 20 | + |
| 21 | + context 'when there is a verified email' do |
| 22 | + let!(:verified_email) { create(:verified_email, status:, verified_at:) } |
| 23 | + let(:email) { verified_email.email } |
| 24 | + |
| 25 | + context 'when the email was verified less than 1 month ago' do |
| 26 | + let(:status) { :deliverable } |
| 27 | + let(:verified_at) { 1.minute.ago } |
| 28 | + |
| 29 | + it { is_expected.to be_valid } |
| 30 | + |
| 31 | + it 'calls EmailVerifierJob' do |
| 32 | + subject.valid? |
| 33 | + |
| 34 | + expect(email_verifier_job).to have_received(:perform) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when the email was verified more than 1 month ago' do |
| 39 | + let(:status) { :deliverable } |
| 40 | + let(:verified_at) { 3.months.ago } |
| 41 | + |
| 42 | + it { is_expected.to be_valid } |
| 43 | + |
| 44 | + it 'calls EmailVerifierJob' do |
| 45 | + subject.valid? |
| 46 | + |
| 47 | + expect(email_verifier_job).to have_received(:perform) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + context 'when there is no verified email' do |
| 53 | + let(:email) { 'whate@ever.com' } |
| 54 | + |
| 55 | + it 'calls EmailVerifierJob' do |
| 56 | + subject.valid? |
| 57 | + |
| 58 | + expect(email_verifier_job).to have_received(:perform) |
| 59 | + end |
| 60 | + |
| 61 | + context 'when EmailVerifierJob creates a verified email' do |
| 62 | + let(:email_verifier_job) do |
| 63 | + email_verifier_job = instance_double(EmailVerifierJob) |
| 64 | + |
| 65 | + allow(email_verifier_job).to receive(:perform) do |
| 66 | + create(:verified_email, email:, status:) |
| 67 | + end |
| 68 | + |
| 69 | + email_verifier_job |
| 70 | + end |
| 71 | + |
| 72 | + context 'when the email is deliverable' do |
| 73 | + let(:status) { :deliverable } |
| 74 | + |
| 75 | + it { is_expected.to be_valid } |
| 76 | + end |
| 77 | + |
| 78 | + context 'when the email is undeliverable' do |
| 79 | + let(:status) { :undeliverable } |
| 80 | + |
| 81 | + it { is_expected.not_to be_valid } |
| 82 | + |
| 83 | + it 'adds email_unreachable error on attribute' do |
| 84 | + subject.valid? |
| 85 | + |
| 86 | + expect(subject.errors[:email].to_s).to include('une adresse email joignable') |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + context 'when EmailVerifierJob does not create a verified email' do |
| 92 | + it { is_expected.not_to be_valid } |
| 93 | + |
| 94 | + it 'adds email_deliverability_unknown error on attribute' do |
| 95 | + subject.valid? |
| 96 | + |
| 97 | + expect(subject.errors[:email].to_s).to include('pu être vérifiée') |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments