demarches-normaliennes/spec/controllers/users/activate_controller_spec.rb

91 lines
3 KiB
Ruby
Raw Normal View History

2019-08-07 15:52:38 +02:00
describe Users::ActivateController, type: :controller do
2018-10-30 18:24:29 +01:00
describe '#new' do
2019-08-07 15:52:38 +02:00
let(:user) { create(:user) }
let(:token) { user.send(:set_reset_password_token) }
2018-10-30 18:24:29 +01:00
before { allow(controller).to receive(:trust_device) }
context 'when the token is ok' do
before { get :new, params: { token: token } }
it { expect(controller).to have_received(:trust_device) }
end
context 'when the token is bad' do
before { get :new, params: { token: 'bad' } }
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
2024-07-11 17:50:02 +02:00
describe '#confirm_email' do
let(:user) { create(:user) }
let(:dossier) { create(:dossier, user: user) }
before { user.invite_tiers!(dossier) }
context 'when the confirmation token is valid' do
before do
get :confirm_email, params: { token: user.confirmation_token }
user.reload
end
it 'updates the email_verified_at' do
expect(user.email_verified_at).to be_present
expect(user.confirmation_token).to be_present
end
it 'redirects to root path with a success notice' do
expect(response).to redirect_to(root_path(user))
expect(flash[:notice]).to eq('Votre email a bien été vérifié')
end
end
context 'when the confirmation token is valid but already used' do
2024-07-11 17:50:02 +02:00
before do
get :confirm_email, params: { token: user.confirmation_token }
get :confirm_email, params: { token: user.confirmation_token }
end
it 'redirects to root path with an explanation notice' do
expect(response).to redirect_to(root_path(user))
expect(flash[:notice]).to eq('Votre email est déjà vérifié')
end
end
context 'when the confirmation token is too old or not valid' do
subject { get :confirm_email, params: { token: user.confirmation_token } }
2024-07-11 17:50:02 +02:00
before do
user.update!(confirmation_sent_at: 3.days.ago)
end
it 'redirects to root path with an explanation notice and it send a new link if user present' do
2024-08-22 11:27:28 +02:00
expect { subject }.to have_enqueued_mail(UserMailer, :resend_confirmation_email)
2024-07-11 17:50:02 +02:00
expect(response).to redirect_to(root_path(user))
expect(flash[:alert]).to eq("Ce lien n'est plus valable, un nouveau lien a été envoyé à l'adresse #{user.email}")
2024-07-11 17:50:02 +02:00
end
end
end
2018-10-30 18:24:29 +01:00
end