|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Update Password With Token' do |
| 6 | + include_context 'with graphql query request' |
| 7 | + |
| 8 | + let(:password) { '12345678' } |
| 9 | + let(:password_confirmation) { password } |
| 10 | + |
| 11 | + context 'when using the user model' do |
| 12 | + let(:user) { create(:user, :confirmed) } |
| 13 | + let(:query) do |
| 14 | + <<-GRAPHQL |
| 15 | + mutation { |
| 16 | + userUpdatePasswordWithToken( |
| 17 | + resetPasswordToken: "#{token}", |
| 18 | + password: "#{password}", |
| 19 | + passwordConfirmation: "#{password_confirmation}" |
| 20 | + ) { |
| 21 | + authenticatable { email } |
| 22 | + credentials { accessToken } |
| 23 | + } |
| 24 | + } |
| 25 | + GRAPHQL |
| 26 | + end |
| 27 | + |
| 28 | + context 'when reset password token is valid' do |
| 29 | + let(:token) { user.send(:set_reset_password_token) } |
| 30 | + |
| 31 | + it 'updates the password' do |
| 32 | + expect do |
| 33 | + post_request |
| 34 | + user.reload |
| 35 | + end.to change(user, :encrypted_password) |
| 36 | + |
| 37 | + expect(user).to be_valid_password(password) |
| 38 | + expect(json_response[:data][:userUpdatePasswordWithToken][:credentials]).to be_nil |
| 39 | + expect(json_response[:data][:userUpdatePasswordWithToken][:authenticatable]).to include(email: user.email) |
| 40 | + end |
| 41 | + |
| 42 | + context 'when token has expired' do |
| 43 | + it 'returns an expired token error' do |
| 44 | + travel_to 10.hours.ago do |
| 45 | + token |
| 46 | + end |
| 47 | + |
| 48 | + post_request |
| 49 | + |
| 50 | + expect(json_response[:errors]).to contain_exactly( |
| 51 | + hash_including(message: 'Reset password token is no longer valid.', extensions: { code: 'USER_ERROR' }) |
| 52 | + ) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'when password confirmation does not match' do |
| 57 | + let(:password_confirmation) { 'does not match' } |
| 58 | + |
| 59 | + it 'returns an error' do |
| 60 | + post_request |
| 61 | + |
| 62 | + expect(json_response[:errors]).to contain_exactly( |
| 63 | + hash_including( |
| 64 | + message: 'Unable to update user password', |
| 65 | + extensions: { code: 'USER_ERROR', detailed_errors: ["Password confirmation doesn't match Password"] } |
| 66 | + ) |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'when reset password token is not found' do |
| 73 | + let(:token) { user.send(:set_reset_password_token) + 'invalid' } |
| 74 | + |
| 75 | + it 'returns an error' do |
| 76 | + post_request |
| 77 | + |
| 78 | + expect(json_response[:errors]).to contain_exactly( |
| 79 | + hash_including(message: 'No user found for the specified reset token.', extensions: { code: 'USER_ERROR' }) |
| 80 | + ) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when using the admin model' do |
| 86 | + let(:admin) { create(:admin, :confirmed) } |
| 87 | + let(:query) do |
| 88 | + <<-GRAPHQL |
| 89 | + mutation { |
| 90 | + adminUpdatePasswordWithToken( |
| 91 | + resetPasswordToken: "#{token}", |
| 92 | + password: "#{password}", |
| 93 | + passwordConfirmation: "#{password_confirmation}" |
| 94 | + ) { |
| 95 | + authenticatable { email } |
| 96 | + credentials { uid } |
| 97 | + } |
| 98 | + } |
| 99 | + GRAPHQL |
| 100 | + end |
| 101 | + |
| 102 | + context 'when reset password token is valid' do |
| 103 | + let(:token) { admin.send(:set_reset_password_token) } |
| 104 | + |
| 105 | + it 'updates the password' do |
| 106 | + expect do |
| 107 | + post_request |
| 108 | + admin.reload |
| 109 | + end.to change(admin, :encrypted_password) |
| 110 | + |
| 111 | + expect(admin).to be_valid_password(password) |
| 112 | + expect(json_response[:data][:adminUpdatePasswordWithToken]).to include( |
| 113 | + credentials: { uid: admin.email }, |
| 114 | + authenticatable: { email: admin.email } |
| 115 | + ) |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments