2023-11-03 09:24:38 +01:00
|
|
|
describe ExpiredUsersDeletionService do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:last_signed_in_not_expired) { (ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR - 1).years.ago }
|
|
|
|
let(:last_signed_in_expired) { (ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR + 1).years.ago }
|
|
|
|
let(:before_close_to_expiration) { nil }
|
|
|
|
let(:notified_close_to_expiration) { (ExpiredUsersDeletionService::RETENTION_AFTER_NOTICE_IN_WEEK - 1).weeks.ago }
|
|
|
|
let(:due_close_to_expiration) { (ExpiredUsersDeletionService::RETENTION_AFTER_NOTICE_IN_WEEK + 1).weeks.ago }
|
2023-11-03 13:52:37 +01:00
|
|
|
let(:mail_double) do
|
|
|
|
dbl = double()
|
|
|
|
expect(dbl).to receive(:deliver_later).with(wait: 0)
|
|
|
|
dbl
|
|
|
|
end
|
|
|
|
|
2023-11-03 09:24:38 +01:00
|
|
|
before { user && dossier }
|
2023-11-03 13:52:37 +01:00
|
|
|
|
2023-11-03 09:24:38 +01:00
|
|
|
describe '#process_expired' do
|
2023-11-03 13:52:37 +01:00
|
|
|
subject { ExpiredUsersDeletionService.new.process_expired }
|
|
|
|
|
2023-11-03 12:13:41 +01:00
|
|
|
context 'when user has an expirable dossier' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_expired) }
|
2023-11-03 12:13:41 +01:00
|
|
|
|
|
|
|
context 'when user was not notified' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, inactive_close_to_expiration_notice_sent_at: before_close_to_expiration) }
|
2023-11-03 10:11:08 +01:00
|
|
|
|
|
|
|
it 'update user.inactive_close_to_expiration_notice_sent_at ' do
|
2023-11-03 13:52:37 +01:00
|
|
|
expect(UserMailer).to receive(:notify_inactive_close_to_deletion).with(user).and_return(mail_double)
|
2023-11-03 10:11:08 +01:00
|
|
|
expect { subject }
|
|
|
|
.to change { user.reload.inactive_close_to_expiration_notice_sent_at }
|
|
|
|
.from(nil).to(anything)
|
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
|
|
|
|
2023-11-03 12:13:41 +01:00
|
|
|
context 'user has been notified 1 week ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, inactive_close_to_expiration_notice_sent_at: notified_close_to_expiration) }
|
2023-11-03 10:11:08 +01:00
|
|
|
|
|
|
|
it 'do nothing' do
|
|
|
|
expect { subject }.not_to change { Dossier.count }
|
2023-11-06 09:29:47 +01:00
|
|
|
expect { user.reload }.not_to raise_error
|
2023-11-03 10:11:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-03 12:13:41 +01:00
|
|
|
context 'user has been notified 3 weeks ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, inactive_close_to_expiration_notice_sent_at: due_close_to_expiration) }
|
2023-11-03 10:11:08 +01:00
|
|
|
|
|
|
|
it 'destroys user and dossier' do
|
|
|
|
expect { subject }.to change { Dossier.count }.by(-1)
|
|
|
|
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
|
|
|
end
|
2023-11-03 12:13:41 +01:00
|
|
|
|
|
|
|
context 'when user is expirable' do
|
|
|
|
let(:dossier) { nil }
|
|
|
|
|
|
|
|
context 'when user was not notified' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, last_sign_in_at: last_signed_in_expired, inactive_close_to_expiration_notice_sent_at: before_close_to_expiration) }
|
2023-11-03 12:13:41 +01:00
|
|
|
|
|
|
|
it 'update user.inactive_close_to_expiration_notice_sent_at ' do
|
2023-11-03 13:52:37 +01:00
|
|
|
expect(UserMailer).to receive(:notify_inactive_close_to_deletion).with(user).and_return(mail_double)
|
2023-11-03 12:13:41 +01:00
|
|
|
expect { subject }
|
|
|
|
.to change { user.reload.inactive_close_to_expiration_notice_sent_at }
|
|
|
|
.from(nil).to(anything)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has been notified 1 week ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, last_sign_in_at: last_signed_in_expired, inactive_close_to_expiration_notice_sent_at: notified_close_to_expiration) }
|
2023-11-03 12:13:41 +01:00
|
|
|
|
|
|
|
it 'do nothing' do
|
|
|
|
expect { subject }.not_to change { Dossier.count }
|
2023-11-06 09:29:47 +01:00
|
|
|
expect { user.reload }.not_to raise_error
|
2023-11-03 12:13:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has been notified 3 weeks ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, last_sign_in_at: last_signed_in_expired, inactive_close_to_expiration_notice_sent_at: due_close_to_expiration) }
|
2023-11-03 12:13:41 +01:00
|
|
|
|
|
|
|
it 'destroys user and dossier' do
|
|
|
|
subject
|
|
|
|
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
|
|
|
|
2023-11-03 12:13:41 +01:00
|
|
|
describe '#expiring_users_without_dossiers' do
|
|
|
|
let(:dossier) { nil }
|
2023-11-06 09:29:47 +01:00
|
|
|
subject { ExpiredUsersDeletionService.new.send(:expiring_users_without_dossiers) }
|
2023-11-03 09:24:38 +01:00
|
|
|
|
2023-11-03 12:13:41 +01:00
|
|
|
context 'when user last_sign_in_at is 1 year ago and has no dossier' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, last_sign_in_at: last_signed_in_not_expired) }
|
2023-11-03 12:13:41 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
|
|
|
|
2023-11-03 12:13:41 +01:00
|
|
|
context 'when user last_sign_in_at is 3 year ago and has no dossier' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, last_sign_in_at: last_signed_in_expired) }
|
2023-11-03 12:13:41 +01:00
|
|
|
it { is_expected.to include(user) }
|
|
|
|
end
|
2023-11-06 09:18:22 +01:00
|
|
|
|
|
|
|
context 'when expert last sign in at is 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, expert: create(:expert), last_sign_in_at: last_signed_in_expired) }
|
2023-11-06 09:18:22 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when instructeur last sign in at is 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, instructeur: create(:instructeur), last_sign_in_at: last_signed_in_expired) }
|
2023-11-06 09:18:22 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin last sign in at is 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:user) { create(:user, administrateur: create(:administrateur), last_sign_in_at: last_signed_in_expired) }
|
2023-11-06 09:18:22 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
2023-11-03 12:13:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#expiring_users_with_dossiers' do
|
|
|
|
let(:user) { create(:user) }
|
2023-11-06 09:29:47 +01:00
|
|
|
subject { ExpiredUsersDeletionService.new.send(:expiring_users_with_dossiers) }
|
2023-11-03 12:13:41 +01:00
|
|
|
|
2023-11-03 09:24:38 +01:00
|
|
|
context 'when user has a dossier created 1 year ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_not_expired) }
|
2023-11-03 09:24:38 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has a dossier created 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_expired) }
|
2023-11-03 09:24:38 +01:00
|
|
|
it { is_expected.to include(user) }
|
|
|
|
end
|
2023-11-06 09:18:22 +01:00
|
|
|
|
2023-11-06 09:29:47 +01:00
|
|
|
context 'when user one dossier created 3 years ago and one dossier created 1 year ago' do
|
|
|
|
let(:dossier) { create(:dossier, :brouillon, user:, created_at: last_signed_in_expired) }
|
|
|
|
it 'respects the HAVING MAX(dossier.created_at) ignores the user' do
|
|
|
|
create(:dossier, :brouillon, user:, created_at: last_signed_in_not_expired)
|
|
|
|
is_expected.not_to include(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-06 09:18:22 +01:00
|
|
|
context 'when expert last sign in at is 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_expired) }
|
|
|
|
let(:user) { create(:user, expert: create(:expert), last_sign_in_at: last_signed_in_expired) }
|
2023-11-06 09:18:22 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when instructeur last sign in at is 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_expired) }
|
|
|
|
let(:user) { create(:user, instructeur: create(:instructeur), last_sign_in_at: last_signed_in_expired) }
|
2023-11-06 09:18:22 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin last sign in at is 3 years ago' do
|
2023-11-06 09:29:47 +01:00
|
|
|
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_expired) }
|
|
|
|
let(:user) { create(:user, administrateur: create(:administrateur), last_sign_in_at: last_signed_in_expired) }
|
2023-11-06 09:18:22 +01:00
|
|
|
it { is_expected.not_to include(user) }
|
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
|
|
|
end
|