Skip to content

Commit

Permalink
Merge pull request #10456 from demarches-simplifiees/infra_email_conf…
Browse files Browse the repository at this point in the history
…irmation

Mise en place de l 'infrastructure de rejet d'envoi de mails non confirmés
  • Loading branch information
LeSim authored May 27, 2024
2 parents dbfd5ee + 555df3a commit 0b60940
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def update_identite
@no_description = true

if @dossier.update(dossier_params) && @dossier.individual.valid?
# TODO: remove this after proper mandat email validation
@dossier.individual.update!(email_verified_at: Time.zone.now)
@dossier.update!(autorisation_donnees: true, identity_updated_at: Time.zone.now)
flash.notice = t('.identity_saved')

Expand Down
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def send_confirmation_instructions

# Callback provided by Devise
def after_confirmation
update!(email_verified_at: Time.zone.now)
link_invites!
end

Expand Down Expand Up @@ -98,7 +99,7 @@ def remind_invitation!

def self.create_or_promote_to_instructeur(email, password, administrateurs: [])
user = User
.create_with(password: password, confirmed_at: Time.zone.now)
.create_with(password: password, confirmed_at: Time.zone.now, email_verified_at: Time.zone.now)
.find_or_create_by(email: email)

if user.valid?
Expand Down Expand Up @@ -137,7 +138,7 @@ def self.create_or_promote_to_administrateur(email, password)

def self.create_or_promote_to_expert(email, password)
user = User
.create_with(password: password, confirmed_at: Time.zone.now)
.create_with(password: password, confirmed_at: Time.zone.now, email_verified_at: Time.zone.now)
.find_or_create_by(email: email)

if user.valid?
Expand Down
17 changes: 17 additions & 0 deletions app/tasks/maintenance/prefill_individual_email_verified_at_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# We are going to confirm the various email addresses of the users in the system.
# Individual model (mandant) needs their email_verified_at attribute to be set in order to receive emails.
# This task sets the email_verified_at attribute to the current time for all the individual to be backward compatible
# See https://github.com/demarches-simplifiees/demarches-simplifiees.fr/issues/10450
module Maintenance
class PrefillIndividualEmailVerifiedAtTask < MaintenanceTasks::Task
def collection
Individual.in_batches
end

def process(batch_of_individuals)
batch_of_individuals.update_all(email_verified_at: Time.zone.now)
end
end
end
17 changes: 17 additions & 0 deletions app/tasks/maintenance/prefill_user_email_verified_at_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# We are going to confirm the various email addresses of the users in the system.
# User model needs their email_verified_at attribute to be set in order to receive emails.
# This task sets the email_verified_at attribute to the current time for all users to be backward compatible
# See https://github.com/demarches-simplifiees/demarches-simplifiees.fr/issues/10450
module Maintenance
class PrefillUserEmailVerifiedAtTask < MaintenanceTasks::Task
def collection
User.in_batches
end

def process(batch_of_users)
batch_of_users.update_all(email_verified_at: Time.zone.now)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEmailVerifiedAtColumnToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :email_verified_at, :datetime
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEmailVerifiedAtColumnToIndividuals < ActiveRecord::Migration[7.0]
def change
add_column :individuals, :email_verified_at, :datetime
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_04_17_053843) do
ActiveRecord::Schema[7.0].define(version: 2024_05_27_090508) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_buffercache"
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -752,6 +752,7 @@
t.datetime "created_at", precision: nil
t.integer "dossier_id"
t.string "email"
t.datetime "email_verified_at"
t.string "gender"
t.string "nom"
t.string "notification_method"
Expand Down Expand Up @@ -1137,6 +1138,7 @@
t.datetime "current_sign_in_at", precision: nil
t.string "current_sign_in_ip"
t.string "email", default: "", null: false
t.datetime "email_verified_at"
t.string "encrypted_password", default: "", null: false
t.integer "failed_attempts", default: 0, null: false
t.datetime "inactive_close_to_expiration_notice_sent_at"
Expand Down
3 changes: 2 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
user = User.create!(
email: default_user,
password: default_password,
confirmed_at: Time.zone.now
confirmed_at: Time.zone.now,
email_verified_at: Time.zone.now
)
user.create_instructeur!
user.create_administrateur!
1 change: 1 addition & 0 deletions spec/controllers/users/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
expect(individual.errors.full_messages).to be_empty
expect(individual.notification_method).to eq('email')
expect(individual.email).to eq('[email protected]')
expect(individual.email_verified_at).to be_present
expect(response).to redirect_to(brouillon_dossier_path(dossier))
end

Expand Down
1 change: 1 addition & 0 deletions spec/models/france_connect_information_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
it do
subject
expect(fci.user.email).to eq('[email protected]')
expect(fci.user.email_verified_at).to be_present
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
user.confirm
expect(user.reload.invites.size).to eq(2)
end

it 'verifies its email' do
expect(user.email_verified_at).to be_nil
user.confirm
expect(user.email_verified_at).to be_present
end
end

describe '#owns?' do
Expand Down Expand Up @@ -111,6 +117,7 @@
user = subject
expect(user.valid_password?(password)).to be true
expect(user.confirmed_at).to be_present
expect(user.email_verified_at).to be_present
expect(user.instructeur).to be_present
end

Expand Down Expand Up @@ -184,6 +191,7 @@
user = subject
expect(user.valid_password?(password)).to be true
expect(user.confirmed_at).to be_present
expect(user.email_verified_at).to be_present
expect(user.expert).to be_present
end
end
Expand Down Expand Up @@ -214,6 +222,18 @@
end
end

describe '.create_or_promote_to_gestionnaire' do
let(:email) { '[email protected]' }
let(:password) { 'un super password !' }

subject { User.create_or_promote_to_gestionnaire(email, password) }

it 'verifies its email' do
user = subject
expect(user.email_verified_at).to be_present
end
end

describe 'invite_administrateur!' do
let(:super_admin) { create(:super_admin) }
let(:administrateur) { create(:administrateur) }
Expand Down

0 comments on commit 0b60940

Please sign in to comment.