|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Resend confirmation with token' do |
| 6 | + include_context 'with graphql query request' |
| 7 | + |
| 8 | + let(:confirmed_at) { nil } |
| 9 | + let!(:user) { create(:user, confirmed_at: nil, email: 'mwallace@wallaceinc.com') } |
| 10 | + let(:email) { user.email } |
| 11 | + let(:id) { user.id } |
| 12 | + let(:confirm_url) { 'https://google.com' } |
| 13 | + let(:query) do |
| 14 | + <<-GRAPHQL |
| 15 | + mutation { |
| 16 | + userResendConfirmationWithToken( |
| 17 | + email:"#{email}", |
| 18 | + confirmUrl:"#{confirm_url}" |
| 19 | + ) { |
| 20 | + message |
| 21 | + } |
| 22 | + } |
| 23 | + GRAPHQL |
| 24 | + end |
| 25 | + |
| 26 | + context 'when confirm_url is not whitelisted' do |
| 27 | + let(:confirm_url) { 'https://not-safe.com' } |
| 28 | + |
| 29 | + it 'returns a not whitelisted confirm url error' do |
| 30 | + expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count) |
| 31 | + |
| 32 | + expect(json_response[:errors]).to containing_exactly( |
| 33 | + hash_including( |
| 34 | + message: "Redirect to '#{confirm_url}' not allowed.", |
| 35 | + extensions: { code: 'USER_ERROR' } |
| 36 | + ) |
| 37 | + ) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'when params are correct' do |
| 42 | + context 'when using the gem schema' do |
| 43 | + it 'sends an email to the user with confirmation url and returns a success message' do |
| 44 | + expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1) |
| 45 | + expect(json_response[:data][:userResendConfirmationWithToken]).to include( |
| 46 | + message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.' |
| 47 | + ) |
| 48 | + |
| 49 | + email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded) |
| 50 | + confirm_link = email.css('a').first['href'] |
| 51 | + confirm_token = confirm_link.match(/\?confirmationToken\=(?<token>.+)\z/)[:token] |
| 52 | + |
| 53 | + expect(User.confirm_by_token(confirm_token)).to eq(user) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'when using a custom schema' do |
| 58 | + let(:custom_path) { '/api/v1/graphql' } |
| 59 | + |
| 60 | + it 'sends an email to the user with confirmation url and returns a success message' do |
| 61 | + expect { post_request(custom_path) }.to change(ActionMailer::Base.deliveries, :count).by(1) |
| 62 | + expect(json_response[:data][:userResendConfirmationWithToken]).to include( |
| 63 | + message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.' |
| 64 | + ) |
| 65 | + |
| 66 | + email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded) |
| 67 | + confirm_link = email.css('a').first['href'] |
| 68 | + confirm_token = confirm_link.match(/\?confirmationToken\=(?<token>.+)\z/)[:token] |
| 69 | + |
| 70 | + expect(User.confirm_by_token(confirm_token)).to eq(user) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'when email address uses different casing' do |
| 75 | + let(:email) { 'mWallace@wallaceinc.com' } |
| 76 | + |
| 77 | + it 'honors devise configuration for case insensitive fields' do |
| 78 | + expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1) |
| 79 | + expect(json_response[:data][:userResendConfirmationWithToken]).to include( |
| 80 | + message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.' |
| 81 | + ) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when the user has already been confirmed' do |
| 86 | + before { user.confirm } |
| 87 | + |
| 88 | + it 'does *NOT* send an email and raises an error' do |
| 89 | + expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count) |
| 90 | + expect(json_response[:data][:userResendConfirmationWithToken]).to be_nil |
| 91 | + expect(json_response[:errors]).to contain_exactly( |
| 92 | + hash_including( |
| 93 | + message: 'Email was already confirmed, please try signing in', |
| 94 | + extensions: { code: 'USER_ERROR' } |
| 95 | + ) |
| 96 | + ) |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'when the email was changed' do |
| 102 | + let(:confirmed_at) { 2.seconds.ago } |
| 103 | + let(:email) { 'new-email@wallaceinc.com' } |
| 104 | + let(:new_email) { email } |
| 105 | + |
| 106 | + before do |
| 107 | + user.update_with_email( |
| 108 | + email: new_email, |
| 109 | + schema_url: 'http://localhost/test', |
| 110 | + confirmation_success_url: 'https://google.com' |
| 111 | + ) |
| 112 | + end |
| 113 | + |
| 114 | + it 'sends new confirmation email' do |
| 115 | + expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1) |
| 116 | + expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(new_email) |
| 117 | + expect(json_response[:data][:userResendConfirmationWithToken]).to include( |
| 118 | + message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.' |
| 119 | + ) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + context "when the email isn't in the system" do |
| 124 | + let(:email) { 'notthere@gmail.com' } |
| 125 | + |
| 126 | + it 'does *NOT* send an email and raises an error' do |
| 127 | + expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count) |
| 128 | + expect(json_response[:data][:userResendConfirmationWithToken]).to be_nil |
| 129 | + expect(json_response[:errors]).to contain_exactly( |
| 130 | + hash_including( |
| 131 | + message: "Unable to find user with email '#{email}'.", |
| 132 | + extensions: { code: 'USER_ERROR' } |
| 133 | + ) |
| 134 | + ) |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments