Browse Source

CLDC-3600 Clear unconfirmed emails for deactivated users (#2587)

* Discard email changes when deactivating user

* Clear existing unconfirmed emails
pull/2591/head
kosiakkatrina 5 months ago committed by GitHub
parent
commit
573e6302bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      app/models/user.rb
  2. 4
      lib/tasks/clear_unconfirmed_emails.rake
  3. 36
      spec/lib/tasks/clear_unconfirmed_emails_spec.rb
  4. 7
      spec/requests/users_controller_spec.rb

1
app/models/user.rb

@ -142,6 +142,7 @@ class User < ApplicationRecord
sign_in_count: 0,
initial_confirmation_sent: false,
reactivate_with_organisation:,
unconfirmed_email: nil,
)
end

4
lib/tasks/clear_unconfirmed_emails.rake

@ -0,0 +1,4 @@
desc "Clear unconfimed emails for deactivated users"
task clear_unconfirmed_emails: :environment do
User.deactivated.where.not(unconfirmed_email: nil).update(unconfirmed_email: nil)
end

36
spec/lib/tasks/clear_unconfirmed_emails_spec.rb

@ -0,0 +1,36 @@
require "rails_helper"
require "rake"
RSpec.describe "clear_unconfirmed_emails" do
describe ":clear_unconfirmed_emails", type: :task do
subject(:task) { Rake::Task["clear_unconfirmed_emails"] }
before do
Rake.application.rake_require("tasks/clear_unconfirmed_emails")
Rake::Task.define_task(:environment)
task.reenable
end
context "when the rake task is run" do
context "and there are deactivated users with unconfirmed emails" do
let!(:user) { create(:user, active: false, unconfirmed_email: "some_email@example.com") }
it "clears unconfirmed_email" do
task.invoke
expect(user.reload.unconfirmed_email).to eq(nil)
end
end
context "and there are active users with unconfirmed emails" do
let!(:user) { create(:user, active: true, unconfirmed_email: "some_email@example.com") }
it "does not clear unconfirmed_email" do
task.invoke
expect(user.reload.unconfirmed_email).not_to eq(nil)
end
end
end
end
end

7
spec/requests/users_controller_spec.rb

@ -434,7 +434,7 @@ RSpec.describe UsersController, type: :request do
context "when user is signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator, email: "coordinator@example.com", organisation: create(:organisation, :without_dpc)) }
let!(:other_user) { create(:user, organisation: user.organisation, name: "filter name", email: "filter@example.com") }
let!(:other_user) { create(:user, organisation: user.organisation, name: "filter name", email: "filter@example.com", unconfirmed_email: "email@something.com") }
before do
sign_in user
@ -885,6 +885,11 @@ RSpec.describe UsersController, type: :request do
expect { patch "/users/#{other_user.id}", headers:, params: }
.to change { other_user.reload.active }.from(true).to(false)
end
it "discards unconfirmed email" do
expect { patch "/users/#{other_user.id}", headers:, params: }
.to change { other_user.reload.unconfirmed_email }.from("email@something.com").to(nil)
end
end
context "and tries to activate deactivated user" do

Loading…
Cancel
Save