-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user anonimisation functionality and tests
- Loading branch information
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
module IdentityTijuana | ||
# TODO add AnonymisationLog in TJ | ||
class UserGhosting | ||
def initialize(user_ids, reason) | ||
@user_ids = user_ids | ||
@reason = reason | ||
end | ||
|
||
def ghost_users | ||
tj_conn = IdentityTijuana::ReadWrite.connection | ||
# log_ids = log_anonymisation_started | ||
begin | ||
tj_conn.transaction do | ||
anon_basic_info(tj_conn) | ||
# anon_automation_events(tj_conn) | ||
# anon_call_outcomes(tj_conn) | ||
# anon_comments(tj_conn) | ||
end | ||
rescue ActiveRecord::RecordInvalid => e | ||
# log_anonymisation_failed(log_ids, e.message, e) | ||
Rails.logger.error "Anonymisation failed: #{e.message}" | ||
end | ||
|
||
# log_anonymisation_finished(log_ids) | ||
end | ||
|
||
private | ||
|
||
def log_anonymisation_started | ||
log_ids = [] | ||
|
||
@user_ids.each do |user_id| | ||
logged = AnonymisationLog.create!( | ||
user_id: user_id, | ||
reason: @reason, | ||
status: 'started', | ||
started_at: DateTime.current.utc, | ||
) | ||
log_ids << logged.id | ||
end | ||
|
||
log_ids | ||
end | ||
|
||
def log_anonymisation_finished(log_ids) | ||
# rubocop:disable Rails/SkipsModelValidations | ||
AnonymisationLog.where(id: log_ids) | ||
.update_all( | ||
finished_at: DateTime.current.utc, | ||
status: 'completed' | ||
) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
end | ||
|
||
def log_anonymisation_failed(log_ids, error_msg, error_stack) | ||
# rubocop:disable Rails/SkipsModelValidations | ||
AnonymisationLog.where(id: log_ids) | ||
.update_all( | ||
finished_at: DateTime.current.utc, | ||
status: 'failed', | ||
message: error_msg, | ||
error_stack: error_stack | ||
) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
end | ||
|
||
def anon_basic_info(tj_conn) | ||
tj_conn.execute(<<~SQL.squish) | ||
UPDATE users | ||
SET email = concat(id, '@#{Settings.ghoster.email_domain}'), | ||
first_name = null, | ||
last_name = null, | ||
mobile_number = null, | ||
home_number = null, | ||
street_address = null, | ||
country_iso = null, | ||
is_member = 0, | ||
encrypted_password = null, | ||
password_salt = null, | ||
reset_password_token = null, | ||
remember_created_at = null, | ||
sign_in_count = 0, | ||
current_sign_in_at = null, | ||
last_sign_in_at = null, | ||
current_sign_in_ip = null, | ||
last_sign_in_ip = null, | ||
is_admin = 0, | ||
postcode_id = null, | ||
old_tags = '', | ||
is_volunteer = 0, | ||
random = null, | ||
notes = null, | ||
quick_donate_trigger_id = null, | ||
facebook_id = null, | ||
otp_secret_key = null, | ||
tracking_token = null, | ||
do_not_call = 1, | ||
active = 0, | ||
do_not_sms = 1, | ||
updated_at = CURRENT_TIMESTAMP | ||
WHERE id IN (#{@user_ids.join(',')}); | ||
SQL | ||
end | ||
# FIX | ||
# exist in production but missing in testing db | ||
# -- new_tags = null, | ||
# -- fragment = null, | ||
# mautic_id = null, | ||
|
||
def anon_automation_events(tj_conn) | ||
tj_conn.execute(<<~SQL.squish) | ||
UPDATE automation_events | ||
SET payload = null | ||
WHERE id IN (#{@user_ids.join(',')}); | ||
SQL | ||
end | ||
|
||
def anon_call_outcomes(tj_conn) | ||
tj_conn.execute(<<~SQL.squish) | ||
UPDATE call_outcomes | ||
SET email = null, | ||
payload = null, | ||
dialed_number = null | ||
WHERE id IN (#{@user_ids.join(',')}); | ||
SQL | ||
end | ||
|
||
def anon_comments(tj_conn) | ||
tj_conn.execute(<<~SQL.squish) | ||
UPDATE comments | ||
SET body = null, | ||
dialed_number = null, | ||
updated_at = CURRENT_TIMESTAMP | ||
WHERE id IN (#{@user_ids.join(',')}); | ||
SQL | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'rails_helper' | ||
|
||
describe IdentityTijuana::UserGhosting do | ||
before(:each) do | ||
allow(Settings).to( | ||
receive_message_chain("tijuana.database_url") { ENV['TIJUANA_DATABASE_URL'] } | ||
) | ||
allow(Settings).to( | ||
receive_message_chain("ghoster.email_domain") { 'anoned.non' } | ||
) | ||
end | ||
|
||
context '#ghosting' do | ||
context 'user with all attributes' do | ||
it 'should ghost all attributes' do | ||
u = FactoryBot.create(:tijuana_user_with_everything) | ||
|
||
anon_domain = Settings.ghoster.email_domain | ||
described_class.new([u.id], 'test-reason').ghost_users | ||
|
||
u.reload | ||
|
||
expect(u.email).to eq("#{u.id}@#{anon_domain}") | ||
|
||
nils = [:first_name, :last_name, :mobile_number, | ||
:home_number, :street_address, :country_iso, | ||
:encrypted_password, :password_salt, :reset_password_token, | ||
:remember_created_at, :current_sign_in_at, :last_sign_in_at, | ||
:current_sign_in_ip, :last_sign_in_ip, :postcode_id, | ||
:random, :notes, :quick_donate_trigger_id, :facebook_id, | ||
:otp_secret_key, :tracking_token] | ||
|
||
nils.each do |prop| | ||
expect(u.send(prop)).to be(nil) | ||
end | ||
|
||
falsey = [:is_member, :is_admin, :active, :is_volunteer] | ||
|
||
falsey.each do |prop| | ||
expect(u.send(prop)).to eq(false) | ||
end | ||
|
||
truthy = [:do_not_call, :do_not_sms] | ||
|
||
truthy.each do |prop| | ||
expect(u.send(prop)).to eq(true) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters