|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe 'Check Password Token Requests' do |
| 4 | + include_context 'with graphql query request' |
| 5 | + |
| 6 | + let(:user) { create(:user, :confirmed) } |
| 7 | + let(:redirect_url) { 'https://google.com' } |
| 8 | + let(:query) do |
| 9 | + <<-GRAPHQL |
| 10 | + mutation { |
| 11 | + userCheckPasswordToken( |
| 12 | + resetPasswordToken: "#{token}", |
| 13 | + redirectUrl: "#{redirect_url}" |
| 14 | + ) { |
| 15 | + authenticable { email } |
| 16 | + } |
| 17 | + } |
| 18 | + GRAPHQL |
| 19 | + end |
| 20 | + |
| 21 | + context 'when reset password token is valid' do |
| 22 | + let(:token) { user.send(:set_reset_password_token) } |
| 23 | + |
| 24 | + context 'when redirect_url is not provided' do |
| 25 | + let(:redirect_url) { nil } |
| 26 | + |
| 27 | + it 'returns authenticable and credentials in the headers' do |
| 28 | + get_request |
| 29 | + |
| 30 | + expect(response).to include_auth_headers |
| 31 | + expect(json_response[:data][:userCheckPasswordToken]).to match( |
| 32 | + authenticable: { email: user.email } |
| 33 | + ) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'when redirect url is provided' do |
| 38 | + it 'redirects to redirect url' do |
| 39 | + expect do |
| 40 | + get_request |
| 41 | + |
| 42 | + user.reload |
| 43 | + end.to change { user.tokens.keys.count }.from(0).to(1).and( |
| 44 | + change(user, :allow_password_change).from(false).to(true) |
| 45 | + ) |
| 46 | + |
| 47 | + expect(response).to redirect_to %r{\Ahttps://google.com} |
| 48 | + expect(response.body).to include("client=#{user.reload.tokens.keys.first}") |
| 49 | + expect(response.body).to include('access-token=') |
| 50 | + expect(response.body).to include('uid=') |
| 51 | + expect(response.body).to include('expiry=') |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when token has expired' do |
| 56 | + it 'returns an expired token error' do |
| 57 | + travel_to 10.hours.ago do |
| 58 | + token |
| 59 | + end |
| 60 | + |
| 61 | + get_request |
| 62 | + |
| 63 | + expect(json_response[:errors]).to contain_exactly( |
| 64 | + hash_including(message: 'Reset password token is no longer valid.', extensions: { code: 'USER_ERROR' }) |
| 65 | + ) |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context 'when reset password token is not found' do |
| 71 | + let(:token) { user.send(:set_reset_password_token) + 'invalid' } |
| 72 | + |
| 73 | + it 'redirects to redirect url' do |
| 74 | + get_request |
| 75 | + |
| 76 | + expect(json_response[:errors]).to contain_exactly( |
| 77 | + hash_including(message: 'No user found for the specified reset token.', extensions: { code: 'USER_ERROR' }) |
| 78 | + ) |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments