-
Notifications
You must be signed in to change notification settings - Fork 662
AO3-7249 Track past emails and usernames via a separate table #5533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kiyazz
wants to merge
17
commits into
otwcode:master
Choose a base branch
from
Kiyazz:AO3-7249
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−1
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
160024d
AO3-7249 Add new tables for past usernames and emails, define migrati…
Kiyazz 9e28e08
AO3-7249 update migration to include foreign key to users
Kiyazz 09557a5
AO3-7249 update migration to include foreign key to users
Kiyazz 1603cfc
AO3-7249 update migration to include foreign key to users
Kiyazz 99ea0ef
AO3-7249 add logic for writing to new tables on changes
Kiyazz 21aaf80
AO3-7249 add rspec tests for logging logic
Kiyazz 44778cc
AO3-7249 create backfill script
Kiyazz 2041d6b
AO3-7249 rubocop fixes
Kiyazz b5912b5
Merge branch 'master' of https://github.com/otwcode/otwarchive into A…
Kiyazz 9f1d5eb
AO3-7249 make reviewer changes and add test for email change with no …
Kiyazz 1a5699a
AO3-7249 Add tests to backfill script
Kiyazz 3de8e6f
AO3-7249 Alias user_past_emails and user_past_usernames tables as pas…
Kiyazz f155c39
AO3-7249 Rubocop linter fixes
Kiyazz b9fcf41
AO3-7249 Add 60 second window for checking same record in backfill
Kiyazz cc5d48d
AO3-7249 Check and don't include audits with a nil old field
Kiyazz dfefdca
AO3-7249 Use .blank? instead of .nil?
Kiyazz 608eae7
AO3-7249 Fix stupid bugs
Kiyazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| # Job to fill the user_past_usernames and user_past_emails tables using information from the audits table | ||
| class AuditsBackfillJob < RedisSetJob | ||
| queue_as :utilities | ||
|
|
||
| def self.base_key | ||
| "audits_backfill" | ||
| end | ||
|
|
||
| def self.job_size | ||
| 1000 | ||
| end | ||
|
|
||
| def self.batch_size | ||
| 100 | ||
| end | ||
|
|
||
| def perform_on_batch(user_ids) | ||
| # scans by users so audits can be taken from users and limited appropriately | ||
| User.where(id: user_ids).find_each do |user| | ||
| # gets past data audits within limits | ||
| past_data = user.audits.order(id: :desc).where(action: "update").limit(ArchiveConfig.USER_HISTORIC_VALUES_LIMIT).filter_map do |audit| | ||
| if audit.audited_changes.key?("login") && audit.audited_changes["login"].first.present? | ||
| { username: audit.audited_changes["login"].first, changed_at: audit.created_at } | ||
| elsif audit.audited_changes.key?("email") && audit.audited_changes["email"].first.present? | ||
| { email: audit.audited_changes["email"].first, changed_at: audit.created_at } | ||
| end | ||
| end | ||
| past_data = past_data.uniq { |audit| audit[:username] } | ||
| .reject { |audit| audit[:username] == user.login } | ||
| past_data = past_data.uniq { |audit| audit[:email] } | ||
| .reject { |audit| audit[:email] == user.email } | ||
| # adds each type of past data to the appropriate database table | ||
| past_data.each do |audit| | ||
| window = audit[:changed_at] - 60.seconds..audit[:changed_at] + 60.seconds | ||
| UserPastUsername.create!(user_id: user.id, username: audit[:username], changed_at: audit[:changed_at]) if audit.key?(:username) && !UserPastUsername.exists?(user_id: user.id, username: audit[:username], changed_at: window) | ||
| next unless audit.key?(:email) | ||
|
|
||
| UserPastEmail.create!(user_id: user.id, email_address: audit[:email], changed_at: audit[:changed_at]) unless UserPastEmail.exists?(user_id: user.id, email_address: audit[:email], changed_at: window) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,4 @@ | ||
| # Allows admins to track all past emails of a user | ||
| class UserPastEmail < ApplicationRecord | ||
| belongs_to :user | ||
| end |
This file contains hidden or 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,4 @@ | ||
| # Allows admins to track all past usernames of a user | ||
| class UserPastUsername < ApplicationRecord | ||
| belongs_to :user | ||
| end |
This file contains hidden or 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,12 @@ | ||
| class CreateUserPastEmails < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :user_past_emails do |t| | ||
| t.references :user, null: false, foreign_key: true, type: :integer | ||
|
|
||
| t.string :email_address | ||
| t.datetime :changed_at | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,12 @@ | ||
| class CreateUserPastUsernames < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :user_past_usernames do |t| | ||
| t.references :user, null: false, foreign_key: true, type: :integer | ||
|
|
||
| t.string :username | ||
| t.datetime :changed_at | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,58 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| describe AuditsBackfillJob do | ||
| let(:existing_user) { create(:user) } | ||
|
|
||
| context "when audits previously exist" do | ||
| before do | ||
| # Manually create audit record so user_past_usernames isn't updated | ||
| existing_user.audits.create!(action: "update", auditable: existing_user, user: existing_user, | ||
| auditable_id: existing_user.id, audited_changes: { "login" => ["old_login", existing_user.login] }) | ||
| existing_user.audits.create!(action: "update", auditable: existing_user, user: existing_user, | ||
| auditable_id: existing_user.id, audited_changes: { "email" => ["[email protected]", existing_user.email] }) | ||
| end | ||
|
|
||
| it "creates backfilled records in user_past_usernames" do | ||
| user_ids = [existing_user.id] | ||
| AuditsBackfillJob.new.perform_on_batch(user_ids) | ||
| expect(existing_user.past_usernames.last.username).to eq("old_login") | ||
| end | ||
|
|
||
| it "creates backfilled records in user_past_emails" do | ||
| user_ids = [existing_user.id] | ||
| AuditsBackfillJob.new.perform_on_batch(user_ids) | ||
| expect(existing_user.past_emails.last.email_address).to eq("[email protected]") | ||
| end | ||
|
|
||
| it "contains both backfilled and new changes" do | ||
| user_ids = [existing_user.id] | ||
| AuditsBackfillJob.new.perform_on_batch(user_ids) | ||
| old_username = existing_user.login | ||
| existing_user.update!(login: "new_login") | ||
| expect(existing_user.past_usernames.count).to eq(2) | ||
| expect(existing_user.past_usernames.last.username).to eq(old_username) | ||
| expect(existing_user.past_usernames.first.username).to eq("old_login") | ||
| end | ||
| end | ||
|
|
||
| it "doesn't create duplicate records" do | ||
| # change with update first, then run backfill | ||
| old_username = existing_user.login | ||
| existing_user.update!(login: "new_login") | ||
| user_ids = [existing_user.id] | ||
| AuditsBackfillJob.new.perform_on_batch(user_ids) | ||
|
|
||
| expect(existing_user.past_usernames.count).to eq(1) | ||
| expect(existing_user.past_usernames.last.username).to eq(old_username) | ||
| end | ||
|
|
||
| it "handles audits with a empty field" do | ||
| existing_user.audits.create!(action: "update", auditable: existing_user, user: existing_user, | ||
| auditable_id: existing_user.id, audited_changes: { "email" => ["", existing_user.email] }) | ||
| user_ids = [existing_user.id] | ||
| AuditsBackfillJob.new.perform_on_batch(user_ids) | ||
| expect(existing_user.past_emails.count).to eq(0) | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -254,6 +254,15 @@ | |
| expect(existing_user.renamed_at).to eq(Time.current) | ||
| expect(existing_user.admin_renamed_at).to be nil | ||
| end | ||
|
|
||
| it "records past username" do | ||
| freeze_time | ||
| existing_user.update!(login: "new_username") | ||
| user_change = existing_user.past_usernames.last | ||
| expect(user_change.user_id).to eq(existing_user.id) | ||
| expect(user_change.username).not_to eq("new_username") | ||
| expect(user_change.changed_at).to eq(Time.current) | ||
| end | ||
|
|
||
| context "username was recently changed" do | ||
| before do | ||
|
|
@@ -340,6 +349,21 @@ | |
| expect(log_item.admin_id).to be_nil | ||
| expect(log_item.note).to eq("System Generated") | ||
| end | ||
|
|
||
| it "records previous email" do | ||
| old_email = existing_user.past_emails.last | ||
| expect(old_email.email_address).not_to eq(existing_user.email) | ||
| expect(old_email.user_id).to eq(existing_user.id) | ||
| expect(old_email.changed_at).to eq(existing_user.updated_at) | ||
| end | ||
|
|
||
| it "doesn't record previous email before confirmation" do | ||
| old_email = existing_user.user_past_emails.last | ||
| existing_user.update!(email: "[email protected]") | ||
| existing_user.reload | ||
| after_email = existing_user.user_past_emails.last | ||
| expect(old_email).to eq(after_email) | ||
| end | ||
| end | ||
|
|
||
| context "as an admin" do | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.