diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5dc25e5dd..03c8bcd57 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -250,7 +250,7 @@ class ApplicationController < ActionController::Base payload: { DS_SIGN_IN_COUNT: current_user&.sign_in_count, DS_CREATED_AT: current_administrateur&.created_at, - DS_ACTIVE: current_administrateur&.active, + DS_ACTIVE: current_administrateur&.active?, DS_ID: current_administrateur&.id, DS_GESTIONNAIRE_ID: current_instructeur&.id, DS_ROLES: current_user_roles diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 250953cdd..70bf196f0 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -56,7 +56,7 @@ class Administrateur < ApplicationRecord end def invitation_expired? - !active && !user.reset_password_period_valid? + !active? && !user.reset_password_period_valid? end def self.reset_password(reset_password_token, password) @@ -66,10 +66,6 @@ class Administrateur < ApplicationRecord reset_password_token: reset_password_token }) - if administrateur && administrateur.errors.empty? - administrateur.update_column(:active, true) - end - administrateur end @@ -84,4 +80,8 @@ class Administrateur < ApplicationRecord def can_be_deleted? dossiers.state_instruction_commencee.none? && procedures.none? end + + def active? + user.last_sign_in_at.present? + end end diff --git a/app/services/administrateur_usage_statistics_service.rb b/app/services/administrateur_usage_statistics_service.rb index c283b388c..b9b38f868 100644 --- a/app/services/administrateur_usage_statistics_service.rb +++ b/app/services/administrateur_usage_statistics_service.rb @@ -29,7 +29,7 @@ class AdministrateurUsageStatisticsService result = { ds_sign_in_count: administrateur.user.sign_in_count, ds_created_at: administrateur.created_at, - ds_active: administrateur.active, + ds_active: administrateur.active?, ds_id: administrateur.id, nb_services: nb_services_by_administrateur_id[administrateur.id], nb_instructeurs: nb_instructeurs_by_administrateur_id[administrateur.id], diff --git a/db/migrate/20191024092136_drop_active_column_from_administrateurs.rb b/db/migrate/20191024092136_drop_active_column_from_administrateurs.rb new file mode 100644 index 000000000..97e45a58e --- /dev/null +++ b/db/migrate/20191024092136_drop_active_column_from_administrateurs.rb @@ -0,0 +1,5 @@ +class DropActiveColumnFromAdministrateurs < ActiveRecord::Migration[5.2] + def change + remove_column :administrateurs, :active + end +end diff --git a/db/schema.rb b/db/schema.rb index b4c4e4823..7f9dfa649 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_10_23_183120) do +ActiveRecord::Schema.define(version: 2019_10_24_092136) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -51,7 +51,6 @@ ActiveRecord::Schema.define(version: 2019_10_23_183120) do t.string "email", default: "", null: false t.datetime "created_at" t.datetime "updated_at" - t.boolean "active", default: false t.string "encrypted_token" t.index ["email"], name: "index_administrateurs_on_email", unique: true end diff --git a/spec/features/admin/admin_creation_spec.rb b/spec/features/admin/admin_creation_spec.rb index 300eb78e8..166983b64 100644 --- a/spec/features/admin/admin_creation_spec.rb +++ b/spec/features/admin/admin_creation_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' feature 'As an administrateur', js: true do let(:administration) { create(:administration) } let(:admin_email) { 'new_admin@gouv.fr' } + let(:new_admin) { Administrateur.find_by(email: admin_email) } before do perform_enqueued_jobs do @@ -11,6 +12,8 @@ feature 'As an administrateur', js: true do end scenario 'I can register' do + expect(new_admin.reload.active?).to be(false) + confirmation_email = open_email(admin_email) token_params = confirmation_email.body.match(/token=[^"]+/) @@ -20,5 +23,7 @@ feature 'As an administrateur', js: true do click_button 'Continuer' expect(page).to have_content 'Mot de passe enregistré' + + expect(new_admin.reload.active?).to be(true) end end diff --git a/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb b/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb index 15ac2f711..2b833d561 100644 --- a/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb +++ b/spec/jobs/administrateurs/activate_before_expiration_job_spec.rb @@ -2,7 +2,8 @@ require 'rails_helper' RSpec.describe Administrateurs::ActivateBeforeExpirationJob, type: :job do describe 'perform' do - let(:administrateur) { create(:administrateur, active: active) } + let(:administrateur) { create(:administrateur) } + let(:user) { administrateur.user } let(:mailer_double) { double('mailer', deliver_later: true) } subject { Administrateurs::ActivateBeforeExpirationJob.perform_now } @@ -16,7 +17,7 @@ RSpec.describe Administrateurs::ActivateBeforeExpirationJob, type: :job do after { Timecop.return } context "with an inactive administrateur" do - let(:active) { false } + before { user.update(last_sign_in_at: nil) } context "created now" do before { subject } @@ -43,7 +44,7 @@ RSpec.describe Administrateurs::ActivateBeforeExpirationJob, type: :job do end context "with an active administrateur" do - let(:active) { true } + before { user.update(last_sign_in_at: Time.zone.now) } context "created now" do before { subject } diff --git a/spec/lib/tasks/deployment/20191007124230_update_admin_last_sign_in_at.rake_spec.rb b/spec/lib/tasks/deployment/20191007124230_update_admin_last_sign_in_at.rake_spec.rb deleted file mode 100644 index a671a535e..000000000 --- a/spec/lib/tasks/deployment/20191007124230_update_admin_last_sign_in_at.rake_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -describe '20191007124230_update_admin_last_sign_in_at.rake' do - let(:rake_task) { Rake::Task['after_party:update_admin_last_sign_in_at'] } - - subject { rake_task.invoke } - after { rake_task.reenable } - - context 'with 2 administrateurs' do - let!(:admin) { create(:administrateur, active: true) } - let(:user) { admin.user } - let!(:admin2) { create(:administrateur, active: false) } - let(:user2) { admin2.user } - - before do - end - - it do - expect(admin.active).to be true - expect(user.last_sign_in_at).to be_nil - expect(admin.updated_at).not_to be_nil - - subject - - expect(user.reload.last_sign_in_at).to eq(admin.reload.updated_at) - expect(user2.reload.last_sign_in_at).to be_nil - end - end -end diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index 4b9fef1f0..e06c53630 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -50,4 +50,22 @@ describe Administrateur, type: :model do # it { expect(subject).to eq([]) } # end # end + + describe '#active?' do + let!(:administrateur) { create(:administrateur) } + + subject { administrateur.active? } + + context 'when the user has never signed in' do + before { administrateur.user.update(last_sign_in_at: nil) } + + it { is_expected.to be false } + end + + context 'when the user has already signed in' do + before { administrateur.user.update(last_sign_in_at: Time.zone.now) } + + it { is_expected.to be true } + end + end end diff --git a/spec/services/administrateur_usage_statistics_service_spec.rb b/spec/services/administrateur_usage_statistics_service_spec.rb index 1ff7ee567..65520376f 100644 --- a/spec/services/administrateur_usage_statistics_service_spec.rb +++ b/spec/services/administrateur_usage_statistics_service_spec.rb @@ -39,7 +39,6 @@ describe AdministrateurUsageStatisticsService do let(:administrateur) do create(:administrateur, user: create(:user, sign_in_count: 17, current_sign_in_at: Time.zone.local(2019, 3, 7), last_sign_in_at: Time.zone.local(2019, 2, 27)), - active: true, services: [create(:service)], instructeurs: [create(:instructeur)]) end