add tests

This commit is contained in:
Lisa Durand 2024-07-11 17:50:02 +02:00
parent 3b138d5bb7
commit 3eabcb0736
No known key found for this signature in database
GPG key ID: 0DF91F2CA1E8B816
6 changed files with 70 additions and 12 deletions

View file

@ -34,12 +34,12 @@ class Users::ActivateController < ApplicationController
user = User.find_by(confirmation_token: params[:token]) user = User.find_by(confirmation_token: params[:token])
if user && user.email_verified_at if user && user.email_verified_at
flash[:notice] = "Votre email est déjà vérifié" flash[:notice] = "Votre email est déjà vérifié"
elsif user && user.confirmation_sent_at > 2.days.ago elsif user && 2.days.ago < user.confirmation_sent_at
user.update!(email_verified_at: Time.zone.now) user.update!(email_verified_at: Time.zone.now)
flash[:notice] = 'Votre email a bien été vérifié' flash[:notice] = 'Votre email a bien été vérifié'
else else
flash[:alert] = "le lien est trop vieux" flash[:alert] = "le lien est trop vieux"
#to do relancer un lien if user # to do relancer un lien if user
end end
redirect_to root_path(user) redirect_to root_path(user)
end end

View file

@ -147,7 +147,7 @@ module Users
def update_identite def update_identite
@dossier = dossier @dossier = dossier
@no_description = true @no_description = true
email = dossier_params[:individual_attributes][:email] email = dossier_params.dig('individual_attributes', 'email')
if @dossier.update(dossier_params) && @dossier.individual.valid? if @dossier.update(dossier_params) && @dossier.individual.valid?
# verify for_tiers email # verify for_tiers email

View file

@ -37,4 +37,53 @@ describe Users::ActivateController, type: :controller do
it { expect(response).to redirect_to(users_activate_path(token: token)) } it { expect(response).to redirect_to(users_activate_path(token: token)) }
end end
end end
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 not valid but already used' do
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
before do
user.update!(confirmation_sent_at: 3.days.ago)
get :confirm_email, params: { token: user.confirmation_token }
user.reload
end
it 'redirects to root path with an explanation notice' do
expect(response).to redirect_to(root_path(user))
expect(flash[:alert]).to eq('le lien est trop vieux')
end
end
end
end end

View file

@ -162,19 +162,21 @@ describe Users::DossiersController, type: :controller do
describe 'update_identite' do describe 'update_identite' do
let(:procedure) { create(:procedure, :for_individual) } let(:procedure) { create(:procedure, :for_individual) }
let(:dossier) { create(:dossier, user: user, procedure: procedure) } let(:dossier) { create(:dossier, user: user, procedure: procedure) }
let(:now) { Time.zone.parse('01/01/2100') }
subject { post :update_identite, params: { id: dossier.id, dossier: dossier_params } } subject { post :update_identite, params: { id: dossier.id, dossier: dossier_params } }
before do before do
sign_in(user) sign_in(user)
Timecop.freeze(now) do
subject
end
end end
context 'with correct individual and dossier params' do context 'with correct individual and dossier params' do
let(:dossier_params) { { individual_attributes: { gender: 'M', nom: 'Mouse', prenom: 'Mickey' } } } let(:dossier_params) { { individual_attributes: { gender: 'M', nom: 'Mouse', prenom: 'Mickey' } } }
let(:now) { Time.zone.parse('01/01/2100') }
before do
Timecop.freeze(now) do
subject
end
end
it do it do
expect(response).to redirect_to(brouillon_dossier_path(dossier)) expect(response).to redirect_to(brouillon_dossier_path(dossier))
@ -185,6 +187,7 @@ describe Users::DossiersController, type: :controller do
context 'when the identite cannot be updated by the user' do context 'when the identite cannot be updated by the user' do
let(:dossier) { create(:dossier, :with_individual, :en_instruction, user: user, procedure: procedure) } let(:dossier) { create(:dossier, :with_individual, :en_instruction, user: user, procedure: procedure) }
let(:dossier_params) { { individual_attributes: { gender: 'M', nom: 'Mouse', prenom: 'Mickey' } } } let(:dossier_params) { { individual_attributes: { gender: 'M', nom: 'Mouse', prenom: 'Mickey' } } }
before { subject }
it 'redirects to the dossiers list' do it 'redirects to the dossiers list' do
expect(response).to redirect_to(dossier_path(dossier)) expect(response).to redirect_to(dossier_path(dossier))
@ -194,6 +197,7 @@ describe Users::DossiersController, type: :controller do
context 'with incorrect individual and dossier params' do context 'with incorrect individual and dossier params' do
let(:dossier_params) { { individual_attributes: { gender: '', nom: '', prenom: '' } } } let(:dossier_params) { { individual_attributes: { gender: '', nom: '', prenom: '' } } }
before { subject }
it do it do
expect(response).not_to have_http_status(:redirect) expect(response).not_to have_http_status(:redirect)
@ -201,17 +205,20 @@ describe Users::DossiersController, type: :controller do
end end
end end
context 'when a dossier is in broullon, for_tiers and we want to update the individual' do context 'when a dossier is in brouillon, for_tiers and we want to update the individual' do
let(:dossier) { create(:dossier, :for_tiers_without_notification, state: "brouillon", user: user, procedure: procedure) } let(:dossier) { create(:dossier, :for_tiers_without_notification, state: "brouillon", user: user, procedure: procedure) }
let(:dossier_params) { { individual_attributes: { gender: 'M', nom: 'Mouse', prenom: 'Mickey', email: 'mickey@gmail.com', notification_method: 'email' } } } let(:dossier_params) { { individual_attributes: { gender: 'M', nom: 'Mouse', prenom: 'Mickey', email: 'mickey@gmail.com', notification_method: 'email' } } }
it 'updates the individual with valid notification_method' do it 'updates the individual with valid notification_method' do
expect { subject }.to have_enqueued_mail(UserMailer, :invite_tiers)
.and change(User, :count).by(1)
dossier.reload dossier.reload
individual = dossier.individual.reload individual = dossier.individual.reload
expect(individual.errors.full_messages).to be_empty expect(individual.errors.full_messages).to be_empty
expect(individual.notification_method).to eq('email') expect(individual.notification_method).to eq('email')
expect(individual.email).to eq('mickey@gmail.com') expect(individual.email).to eq('mickey@gmail.com')
expect(individual.email_verified_at).to be_present expect(individual.email_verified_at).to eq nil
expect(response).to redirect_to(brouillon_dossier_path(dossier)) expect(response).to redirect_to(brouillon_dossier_path(dossier))
end end
@ -219,6 +226,8 @@ describe Users::DossiersController, type: :controller do
let(:dossier_params) { { mandataire_first_name: "Jean", mandataire_last_name: "Dupont" } } let(:dossier_params) { { mandataire_first_name: "Jean", mandataire_last_name: "Dupont" } }
it 'updates the dossier mandataire first and last name' do it 'updates the dossier mandataire first and last name' do
expect { subject }.not_to have_enqueued_mail(UserMailer, :invite_tiers)
dossier.reload dossier.reload
individual = dossier.individual.reload individual = dossier.individual.reload
expect(dossier.errors.full_messages).to be_empty expect(dossier.errors.full_messages).to be_empty

View file

@ -25,7 +25,7 @@ class UserMailerPreview < ActionMailer::Preview
end end
def invite_tiers def invite_tiers
UserMailer.invite_tiers(user, 'aedfa0d0', Dossier.first ) UserMailer.invite_tiers(user, 'aedfa0d0', Dossier.first)
end end
def invite_gestionnaire def invite_gestionnaire