-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathresend_confirmation_with_token_spec.rb
More file actions
137 lines (115 loc) · 5 KB
/
resend_confirmation_with_token_spec.rb
File metadata and controls
137 lines (115 loc) · 5 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Resend confirmation with token' do
include_context 'with graphql query request'
let(:confirmed_at) { nil }
let!(:user) { create(:user, confirmed_at: nil, email: 'mwallace@wallaceinc.com') }
let(:email) { user.email }
let(:id) { user.id }
let(:confirm_url) { 'https://google.com' }
let(:query) do
<<-GRAPHQL
mutation {
userResendConfirmationWithToken(
email:"#{email}",
confirmUrl:"#{confirm_url}"
) {
message
}
}
GRAPHQL
end
context 'when confirm_url is not whitelisted' do
let(:confirm_url) { 'https://not-safe.com' }
it 'returns a not whitelisted confirm url error' do
expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count)
expect(json_response[:errors]).to containing_exactly(
hash_including(
message: "Redirect to '#{confirm_url}' not allowed.",
extensions: { code: 'USER_ERROR' }
)
)
end
end
context 'when params are correct' do
context 'when using the gem schema' do
it 'sends an email to the user with confirmation url and returns a success message' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(json_response[:data][:userResendConfirmationWithToken]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)
email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
confirm_link = email.css('a').first['href']
confirm_token = confirm_link.match(/\?confirmationToken\=(?<token>.+)\z/)[:token]
expect(User.confirm_by_token(confirm_token)).to eq(user)
end
end
context 'when using a custom schema' do
let(:custom_path) { '/api/v1/graphql' }
it 'sends an email to the user with confirmation url and returns a success message' do
expect { post_request(custom_path) }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(json_response[:data][:userResendConfirmationWithToken]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)
email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
confirm_link = email.css('a').first['href']
confirm_token = confirm_link.match(/\?confirmationToken\=(?<token>.+)\z/)[:token]
expect(User.confirm_by_token(confirm_token)).to eq(user)
end
end
context 'when email address uses different casing' do
let(:email) { 'mWallace@wallaceinc.com' }
it 'honors devise configuration for case insensitive fields' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(json_response[:data][:userResendConfirmationWithToken]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)
end
end
context 'when the user has already been confirmed' do
before { user.confirm }
it 'does *NOT* send an email and raises an error' do
expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count)
expect(json_response[:data][:userResendConfirmationWithToken]).to be_nil
expect(json_response[:errors]).to contain_exactly(
hash_including(
message: 'Email was already confirmed, please try signing in',
extensions: { code: 'USER_ERROR' }
)
)
end
end
end
context 'when the email was changed' do
let(:confirmed_at) { 2.seconds.ago }
let(:email) { 'new-email@wallaceinc.com' }
let(:new_email) { email }
before do
user.update_with_email(
email: new_email,
schema_url: 'http://localhost/test',
confirmation_success_url: 'https://google.com'
)
end
it 'sends new confirmation email' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(new_email)
expect(json_response[:data][:userResendConfirmationWithToken]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)
end
end
context "when the email isn't in the system" do
let(:email) { 'notthere@gmail.com' }
it 'does *NOT* send an email and raises an error' do
expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count)
expect(json_response[:data][:userResendConfirmationWithToken]).to be_nil
expect(json_response[:errors]).to contain_exactly(
hash_including(
message: "Unable to find user with email '#{email}'.",
extensions: { code: 'USER_ERROR' }
)
)
end
end
end