2017-05-29 18:10:50 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 07:12:25 +02:00
|
|
|
RSpec.describe Auth::PasswordsController do
|
2017-08-03 17:45:45 +02:00
|
|
|
include Devise::Test::ControllerHelpers
|
|
|
|
|
2017-05-29 18:10:50 +02:00
|
|
|
describe 'GET #new' do
|
|
|
|
it 'returns http success' do
|
2023-11-08 09:17:43 +01:00
|
|
|
request.env['devise.mapping'] = Devise.mappings[:user]
|
2017-05-29 18:10:50 +02:00
|
|
|
get :new
|
2018-04-21 21:35:07 +02:00
|
|
|
expect(response).to have_http_status(200)
|
2017-05-29 18:10:50 +02:00
|
|
|
end
|
|
|
|
end
|
2017-08-03 17:45:45 +02:00
|
|
|
|
|
|
|
describe 'GET #edit' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
request.env['devise.mapping'] = Devise.mappings[:user]
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid reset_password_token' do
|
|
|
|
it 'returns http success' do
|
2023-11-08 16:42:30 +01:00
|
|
|
token = user.send_reset_password_instructions
|
|
|
|
|
|
|
|
get :edit, params: { reset_password_token: token }
|
|
|
|
|
2018-04-21 21:35:07 +02:00
|
|
|
expect(response).to have_http_status(200)
|
2017-08-03 17:45:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid reset_password_token' do
|
|
|
|
it 'redirects to #new' do
|
|
|
|
get :edit, params: { reset_password_token: 'some_invalid_value' }
|
|
|
|
expect(response).to redirect_to subject.new_password_path(subject.send(:resource_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-15 15:47:06 +01:00
|
|
|
|
|
|
|
describe 'POST #update' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
2023-11-08 16:42:30 +01:00
|
|
|
let(:password) { 'reset0password' }
|
2022-12-15 15:47:06 +01:00
|
|
|
|
|
|
|
before do
|
|
|
|
request.env['devise.mapping'] = Devise.mappings[:user]
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid reset_password_token' do
|
|
|
|
let!(:session_activation) { Fabricate(:session_activation, user: user) }
|
|
|
|
let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
|
|
|
|
let!(:web_push_subscription) { Fabricate(:web_push_subscription, access_token: access_token) }
|
|
|
|
|
|
|
|
before do
|
2023-11-08 16:42:30 +01:00
|
|
|
token = user.send_reset_password_instructions
|
2022-12-15 15:47:06 +01:00
|
|
|
|
2023-11-08 16:42:30 +01:00
|
|
|
post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: token } }
|
2022-12-15 15:47:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirect to sign in' do
|
|
|
|
expect(response).to redirect_to '/auth/sign_in'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes password' do
|
|
|
|
this_user = User.find(user.id)
|
|
|
|
|
|
|
|
expect(this_user).to_not be_nil
|
2023-11-08 16:42:30 +01:00
|
|
|
expect(this_user.valid_password?(password)).to be true
|
2022-12-15 15:47:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deactivates all sessions' do
|
|
|
|
expect(user.session_activations.count).to eq 0
|
2023-12-22 09:03:59 +01:00
|
|
|
expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2022-12-15 15:47:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'revokes all access tokens' do
|
|
|
|
expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes push subscriptions' do
|
|
|
|
expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
|
2023-12-22 09:03:59 +01:00
|
|
|
expect { web_push_subscription.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2022-12-15 15:47:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid reset_password_token' do
|
|
|
|
before do
|
2023-11-08 16:42:30 +01:00
|
|
|
post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: 'some_invalid_value' } }
|
2022-12-15 15:47:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders reset password' do
|
|
|
|
expect(response).to render_template(:new)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retains password' do
|
|
|
|
this_user = User.find(user.id)
|
|
|
|
|
|
|
|
expect(this_user).to_not be_nil
|
|
|
|
expect(this_user.external_or_valid_password?(user.password)).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-29 18:10:50 +02:00
|
|
|
end
|