Skip to content

Commit

Permalink
Merge pull request #742 from alphagov/unsubscribe-email-subscriptions
Browse files Browse the repository at this point in the history
Delete subscriptions on manually deleting account
  • Loading branch information
1pretz1 authored Nov 1, 2023
2 parents 9a9b146 + 57469dc commit 387f674
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/support/rake_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ support:delete_user:dry_run[email_address]
```

### Deleting a user
This will delete the user, and confirm the user's
OICD sub
This will delete the user, and confirm the user's OICD sub. Deleting a user
will also remove any email subscriptions they may have in Email Alert API.
```ruby
support:delete_user:real[email_address]
```
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/support.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ namespace :support do
task :real, [:email] => :environment do |_, args|
user = OidcUser.find_by("lower(email) = ?", args[:email].downcase)
if user
begin
subscriber = GdsApi.email_alert_api.find_subscriber_by_govuk_account(
govuk_account_id: user.id,
)

GdsApi.email_alert_api.unsubscribe_subscriber(
subscriber.dig("subscriber", "id"),
)
rescue GdsApi::HTTPNotFound
# No linked email-alert-api account, nothing to do
nil
end

user.destroy!

puts "User '#{user.email}' deleted"
puts "User sub: #{user.sub}"
else
Expand Down
25 changes: 21 additions & 4 deletions spec/lib/support_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "gds_api/test_helpers/email_alert_api"

Rails.application.load_tasks

RSpec.describe "Support tasks" do
include GdsApi::TestHelpers::EmailAlertApi

describe ":find_user" do
subject(:task) { Rake.application["support:find_user"] }

Expand All @@ -24,9 +28,7 @@
end

describe ":delete_user" do
before do
FactoryBot.create(:oidc_user, email: "[email protected]", sub: "123")
end
let!(:user) { FactoryBot.create(:oidc_user, email: "[email protected]", sub: "123") }

context "when a dry run" do
subject(:task) { Rake.application["support:delete_user:dry_run"] }
Expand All @@ -47,18 +49,33 @@

context "when a user with email exists" do
it "reports that the user has been deleted" do
stub_email_alert_api_find_subscriber_by_govuk_account_no_subscriber(user.id)

expect { task.execute({ email: "[email protected]" }) }.to output("User '[email protected]' deleted\nUser sub: 123\n").to_stdout
end

it "deletes the user" do
stub_email_alert_api_find_subscriber_by_govuk_account_no_subscriber(user.id)

count = OidcUser.count
task.execute({ email: "[email protected]" })
expect(OidcUser.count).to eq count - 1
end

it "outputs other users don't exist" do
it "outputs if user doesn't exist" do
stub_email_alert_api_find_subscriber_by_govuk_account_no_subscriber(user.id)

expect { task.execute({ email: "[email protected]" }) }.to output("User '[email protected]' does not exist\n").to_stdout
end

it "deletes email subscriptions if present" do
stub_email_alert_api_find_subscriber_by_govuk_account(user.id, "iddddd", user.email)
request = stub_email_alert_api_unsubscribes_a_subscriber("iddddd")

task.execute({ email: user.email })

expect(request).to have_been_requested
end
end
end
end
Expand Down

0 comments on commit 387f674

Please sign in to comment.