ActivateController: add tests and clean a bit

This commit is contained in:
simon lehericey 2019-08-11 19:22:03 +02:00
parent d5ddd182a7
commit 58ecf18390
2 changed files with 25 additions and 16 deletions

View file

@ -14,36 +14,25 @@ class Users::ActivateController < ApplicationController
end
def create
password = create_user_params[:password]
user = User.reset_password_by_token({
password: password,
password_confirmation: password,
reset_password_token: create_user_params[:reset_password_token]
password: user_params[:password],
reset_password_token: user_params[:reset_password_token]
})
if user && user.errors.empty?
if user.valid?
sign_in(user, scope: :user)
flash.notice = "Mot de passe enregistré"
redirect_to instructeur_procedures_path
else
flash.alert = user.errors.full_messages
redirect_to users_activate_path(token: create_user_params[:reset_password_token])
redirect_to users_activate_path(token: user_params[:reset_password_token])
end
end
private
def create_user_params
def user_params
params.require(:user).permit(:reset_password_token, :password)
end
def try_to_authenticate(klass, email, password)
resource = klass.find_for_database_authentication(email: email)
if resource&.valid_password?(password)
sign_in resource
resource.force_sync_credentials
end
end
end

View file

@ -17,4 +17,24 @@ describe Users::ActivateController, type: :controller do
it { expect(controller).not_to have_received(:trust_device) }
end
end
describe '#create' do
let!(:user) { create(:user) }
let(:token) { user.send(:set_reset_password_token) }
let(:password) { 'another-password-ok?' }
before { post :create, params: { user: { reset_password_token: token, password: password } } }
context 'when the token is ok' do
it { expect(user.reload.valid_password?(password)).to be true }
it { expect(response).to redirect_to(instructeur_procedures_path) }
end
context 'when the token is bad' do
let(:token) { 'bad' }
it { expect(user.reload.valid_password?(password)).to be false }
it { expect(response).to redirect_to(users_activate_path(token: token)) }
end
end
end