-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathemails_controller_spec.rb
92 lines (72 loc) · 2.97 KB
/
emails_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe User::EmailsController, type: :controller do
let!(:instance) { Instance.default }
with_tenant(:instance) do
let!(:user) { create(:administrator) }
let!(:primary_email) { user.send(:default_email_record) }
let!(:non_primary_email) { create(:user_email, user: user, primary: false) }
before { controller_sign_in(controller, user) }
describe '#destroy_primary_email' do
subject { delete :destroy, as: :json, params: { id: primary_email.id } }
context 'when the user only has one email address' do
it 'cannot be deleted' do
expect { subject }.to change { user.emails.count }.by(0)
end
it { is_expected.to have_http_status(:bad_request) }
end
context 'when destroying a primary email' do
before { controller_sign_in(controller, user) }
it 'cannot be deleted' do
expect { subject }.to change { user.emails.count }.by(0)
end
it { is_expected.to have_http_status(:bad_request) }
end
end
describe '#destroy_non_primary_email' do
subject { delete :destroy, as: :json, params: { id: non_primary_email.id } }
context 'when destroying a non-primary email' do
before { controller_sign_in(controller, user) }
it 'deletes that email' do
expect { subject }.to change { user.emails.count }.by(-1)
end
end
end
describe '#set_primary' do
let(:email) { create(:user_email, :unconfirmed, user: user, primary: false) }
subject { post :set_primary, params: { id: email }, format: :json }
context 'when email is not confirmed' do
it 'does not change the primary email' do
subject
expect(email.reload.primary).to be_falsey
expect(user.reload.emails.find(&:primary?)).not_to be_nil
end
it { is_expected.to have_http_status(:ok) }
end
end
describe '#send_confirmation', type: :mailer do
let!(:email) { create(:user_email, email_traits, user: user, primary: false) }
subject { post :send_confirmation, params: { id: email } }
context 'when the email is already confirmed' do
let(:email_traits) { :confirmed }
it 'does not send any confirmations' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(0)
end
it 'sets an error message' do
subject
json_result = JSON.parse(response.body)
expect(json_result['errors']).to eq(I18n.t('user.emails.send_confirmation.already_confirmed'))
end
end
context 'when the email is not confirmed' do
let(:email_traits) { :unconfirmed }
with_active_job_queue_adapter(:test) do
it 'sends out a confirmation email' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
it { is_expected.to have_http_status(:ok) }
end
end
end
end