diff --git a/app/jobs/administrateurs/activate_before_expiration_job.rb b/app/jobs/administrateurs/activate_before_expiration_job.rb new file mode 100644 index 000000000..d8fd8c27a --- /dev/null +++ b/app/jobs/administrateurs/activate_before_expiration_job.rb @@ -0,0 +1,9 @@ +class Administrateurs::ActivateBeforeExpirationJob < ApplicationJob + queue_as :cron + + def perform(*args) + Administrateur.inactive.where(created_at: 2.days.ago.all_day).each do |a| + AdministrateurMailer.activate_before_expiration(a).deliver_later + end + end +end diff --git a/app/mailers/administrateur_mailer.rb b/app/mailers/administrateur_mailer.rb new file mode 100644 index 000000000..351206064 --- /dev/null +++ b/app/mailers/administrateur_mailer.rb @@ -0,0 +1,11 @@ +class AdministrateurMailer < ApplicationMailer + layout 'mailers/layout' + + def activate_before_expiration(administrateur) + @administrateur = administrateur + @expiration_date = administrateur.reset_password_sent_at + Devise.reset_password_within + mail(to: administrateur.email, + subject: "demarches-simplifiees.fr - N'oubliez pas d'activer votre compte administrateur", + reply_to: "contact@demarches-simplifiees.fr") + end +end diff --git a/app/views/administrateur_mailer/activate_before_expiration.haml b/app/views/administrateur_mailer/activate_before_expiration.haml new file mode 100644 index 000000000..0fa678348 --- /dev/null +++ b/app/views/administrateur_mailer/activate_before_expiration.haml @@ -0,0 +1,17 @@ +- content_for(:title, "N'oubliez pas d'activer votre compte") + +Bonjour, +%br +%br +Vous avez fait la demande d’un compte administrateur sur demarches-simplifiees.fr. +Votre compte a été créé mais reste inactif, il arrivera à expiration le #{@expiration_date.strftime("%d/%m/%Y")} +%br +%br +Afin d’activer votre compte, veuillez cliquer sur le lien ci-dessous : += link_to(admin_activate_url(token: @administrateur.reset_password_token), admin_activate_url(token: @administrateur.reset_password_token)) +%br +%br +Nous restons à votre disposition si vous avez besoin d’accompagnement. +%br +%br += render partial: "layouts/mailers/bizdev_signature" diff --git a/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb b/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb new file mode 100644 index 000000000..1a92e38bf --- /dev/null +++ b/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb @@ -0,0 +1,72 @@ +require 'rails_helper' + +RSpec.describe Administrateurs::ActivateBeforeExpirationJob, type: :job do + describe 'perform' do + let(:administrateur) { create(:administrateur, active: active) } + let(:mailer_double) { double('mailer', deliver_later: true) } + + subject { Administrateurs::ActivateBeforeExpirationJob.perform_now } + + before do + Timecop.freeze(DateTime.new(2018, 03, 20)) + administrateur.reload + allow(AdministrateurMailer).to receive(:activate_before_expiration).and_return(mailer_double) + end + + after { Timecop.return } + + context "with an inactive administrateur" do + let(:active) { false } + + context "created now" do + before { subject } + it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) } + end + + context "created a long time ago" do + before do + administrateur.update_columns(created_at: DateTime.new(2018, 03, 10)) + subject + end + + it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) } + end + + context "created 2 days ago" do + before do + administrateur.update_columns(created_at: DateTime.new(2018, 03, 18, 20, 00)) + subject + end + + it { expect(AdministrateurMailer).to have_received(:activate_before_expiration).with(administrateur) } + end + end + + context "with an active administrateur" do + let(:active) { true } + + context "created now" do + before { subject } + it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) } + end + + context "created a long time ago" do + before do + administrateur.update_columns(created_at: DateTime.new(2018, 03, 10)) + subject + end + + it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) } + end + + context "created 2 days ago" do + before do + administrateur.update_columns(created_at: DateTime.new(2018, 03, 18, 20, 00)) + subject + end + + it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) } + end + end + end +end diff --git a/spec/mailers/previews/administrateur_mailer_preview.rb b/spec/mailers/previews/administrateur_mailer_preview.rb new file mode 100644 index 000000000..67e6a54b9 --- /dev/null +++ b/spec/mailers/previews/administrateur_mailer_preview.rb @@ -0,0 +1,5 @@ +class AdministrateurMailerPreview < ActionMailer::Preview + def activate_before_expiration + AdministrateurMailer.activate_before_expiration(Administrateur.inactive.where.not(reset_password_token: nil).last) + end +end