Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/action_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def script_base
stderr:,
subtask_class: subtask(argv.shift),
banner: banner,
reason_arg: true,
)
end

Expand Down Expand Up @@ -58,8 +59,8 @@ def subtask(name)
end

module LogBase
def log_message(uuid:, log:, table:, messages:)
table << [uuid, log]
def log_message(uuid:, log:, reason:, table:, messages:)
table << [uuid, log, reason]
messages << '`' + uuid + '`: ' + log
[table, messages]
end
Expand Down Expand Up @@ -88,7 +89,7 @@ def perform_user_action(args:, config:, action:)
table = []
messages = []
uuids = args
table << %w[uuid status]
table << %w[uuid status reason]

users = User.where(uuid: uuids).order(:uuid)
users.each do |user|
Expand Down Expand Up @@ -124,6 +125,7 @@ def perform_user_action(args:, config:, action:)
table, messages = log_message(
uuid: user.uuid,
log: text,
reason: config.reason,
table:,
messages:,
)
Expand All @@ -135,6 +137,7 @@ def perform_user_action(args:, config:, action:)
table, messages = log_message(
uuid: missing_uuid,
log: log_text[:missing_uuid],
reason: config.reason,
table:,
messages:,
)
Expand Down Expand Up @@ -164,7 +167,7 @@ def run(args:, config:)
users = User.where(uuid: uuids).order(:uuid)

table = []
table << %w[uuid status]
table << %w[uuid status reason]

messages = []

Expand All @@ -191,6 +194,7 @@ def run(args:, config:)
table, messages = log_message(
uuid: user.uuid,
log: text,
reason: config.reason,
table:,
messages:,
)
Expand All @@ -215,6 +219,7 @@ def run(args:, config:)
table, messages = log_message(
uuid: missing_uuid,
log: log_text[:missing_uuid],
reason: config.reason,
table:,
messages:,
)
Expand Down Expand Up @@ -255,7 +260,7 @@ def run(args:, config:)
users = User.where(uuid: uuids).order(:uuid)

table = []
table << %w[uuid status]
table << %w[uuid status reason]

messages = []
users.each do |user|
Expand Down Expand Up @@ -289,6 +294,7 @@ def run(args:, config:)
table, messages = log_message(
uuid: user.uuid,
log: text,
reason: config.reason,
table:,
messages:,
)
Expand All @@ -313,6 +319,7 @@ def run(args:, config:)
table, messages = log_message(
uuid: missing_uuid,
log: log_text[:missing_uuid],
reason: config.reason,
table:,
messages:,
)
Expand Down
1 change: 1 addition & 0 deletions lib/data_pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def script_base
stderr:,
subtask_class: subtask(argv.shift),
banner: banner,
reason_arg: false,
)
end

Expand Down
15 changes: 14 additions & 1 deletion lib/script_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
class ScriptBase
attr_reader :argv, :stdout, :stderr, :subtask_class, :banner

def initialize(argv:, stdout:, stderr:, subtask_class:, banner:)
def initialize(argv:, stdout:, stderr:, subtask_class:, banner:, reason_arg:)
@argv = argv
@stdout = stdout
@stderr = stderr
@subtask_class = subtask_class
@banner = banner
@reason_arg = reason_arg
end

def reason_arg?
!!@reason_arg
end

Result = Struct.new(
Expand All @@ -26,6 +31,7 @@ def initialize(argv:, stdout:, stderr:, subtask_class:, banner:)
:show_help,
:requesting_issuers,
:deflate,
:reason,
keyword_init: true,
) do
alias_method :include_missing?, :include_missing
Expand All @@ -40,6 +46,7 @@ def config
show_help: false,
requesting_issuers: [],
deflate: false,
reason: nil,
)
end

Expand Down Expand Up @@ -127,6 +134,12 @@ def option_parser
STR
config.include_missing = include_missing
end

if reason_arg?
opts.on('--reason=REASON', 'Reason for command') do |reason|
config.reason = reason
end
end
end
end
# rubocop:enable Metrics/BlockLength
Expand Down
70 changes: 39 additions & 31 deletions spec/lib/action_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
subject(:action_account) { ActionAccount.new(argv:, stdout:, stderr:) }

describe 'command line run' do
let(:argv) { ['review-pass', user.uuid] }
let(:argv) { ['review-pass', user.uuid, '--reason', 'INV1234'] }
let(:user) { create(:user) }

it 'logs UUIDs and the command name to STDERR formatted for Slack', aggregate_failures: true do
Expand Down Expand Up @@ -60,8 +60,8 @@

expect(CSV.parse(stdout.string)).to eq(
[
['uuid', 'status'],
[user.uuid, 'Error: User does not have a pending fraud review'],
['uuid', 'status', 'reason'],
[user.uuid, 'Error: User does not have a pending fraud review', 'INV1234'],
],
)
end
Expand All @@ -74,8 +74,8 @@

expect(Tableparser.parse(stdout.string)).to eq(
[
['uuid', 'status'],
[user.uuid, 'Error: User does not have a pending fraud review'],
['uuid', 'status', 'reason'],
[user.uuid, 'Error: User does not have a pending fraud review', 'INV1234'],
],
)
end
Expand All @@ -91,6 +91,7 @@
{
'uuid' => user.uuid,
'status' => 'Error: User does not have a pending fraud review',
'reason' => 'INV1234',
},
],
)
Expand All @@ -109,6 +110,7 @@
{
'uuid' => 'does_not_exist@example.com',
'status' => 'Error: Could not find user with that UUID',
'reason' => nil,
},
],
)
Expand Down Expand Up @@ -143,20 +145,22 @@

let(:args) { [user.uuid, user_without_profile.uuid, 'uuid-does-not-exist'] }
let(:include_missing) { true }
let(:config) { ScriptBase::Config.new(include_missing:) }
let(:config) { ScriptBase::Config.new(include_missing:, reason: 'INV1234') }
subject(:result) { subtask.run(args:, config:) }

it 'Reject a user that has a pending review', aggregate_failures: true do
profile_fraud_review_pending_at = user.pending_profile.fraud_review_pending_at

# rubocop:disable Layout/LineLength
expect(result.table).to match_array(
[
['uuid', 'status'],
[user.uuid, "User's profile has been deactivated due to fraud rejection."],
[user_without_profile.uuid, 'Error: User does not have a pending fraud review'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID'],
['uuid', 'status', 'reason'],
[user.uuid, "User's profile has been deactivated due to fraud rejection.", 'INV1234'],
[user_without_profile.uuid, 'Error: User does not have a pending fraud review', 'INV1234'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID', 'INV1234'],
],
)
# rubocop:enable Layout/LineLength

expect(result.subtask).to eq('review-reject')
expect(result.uuids).to match_array([user.uuid, user_without_profile.uuid])
Expand Down Expand Up @@ -201,20 +205,22 @@

let(:args) { [user.uuid, user_without_profile.uuid, 'uuid-does-not-exist'] }
let(:include_missing) { true }
let(:config) { ScriptBase::Config.new(include_missing:) }
let(:config) { ScriptBase::Config.new(include_missing:, reason: 'INV1234') }
subject(:result) { subtask.run(args:, config:) }

it 'Pass a user that has a pending review', aggregate_failures: true do
profile_fraud_review_pending_at = user.pending_profile.fraud_review_pending_at

# rubocop:disable Layout/LineLength
expect(result.table).to match_array(
[
['uuid', 'status'],
[user.uuid, "User's profile has been activated and the user has been emailed."],
[user_without_profile.uuid, 'Error: User does not have a pending fraud review'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID'],
['uuid', 'status', 'reason'],
[user.uuid, "User's profile has been activated and the user has been emailed.", 'INV1234'],
[user_without_profile.uuid, 'Error: User does not have a pending fraud review', 'INV1234'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID', 'INV1234'],
],
)
# rubocop:enable Layout/LineLength

expect(result.subtask).to eq('review-pass')
expect(result.uuids).to match_array([user.uuid, user_without_profile.uuid])
Expand Down Expand Up @@ -253,17 +259,17 @@
let(:reinstated_user) { create(:user, :reinstated) }
let(:args) { [user.uuid, suspended_user.uuid, reinstated_user.uuid, 'uuid-does-not-exist'] }
let(:include_missing) { true }
let(:config) { ScriptBase::Config.new(include_missing:) }
let(:config) { ScriptBase::Config.new(include_missing:, reason: 'INV1234') }
subject(:result) { subtask.run(args:, config:) }

it 'suspend a user that is not suspended already', aggregate_failures: true do
expect(result.table).to match_array(
[
['uuid', 'status'],
[user.uuid, 'User has been suspended'],
[suspended_user.uuid, 'User has already been suspended'],
[reinstated_user.uuid, 'User has been suspended'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID'],
['uuid', 'status', 'reason'],
[user.uuid, 'User has been suspended', 'INV1234'],
[suspended_user.uuid, 'User has already been suspended', 'INV1234'],
[reinstated_user.uuid, 'User has been suspended', 'INV1234'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID', 'INV1234'],
],
)

Expand All @@ -281,20 +287,22 @@
let(:suspended_user) { create(:user, :suspended) }
let(:args) { [user.uuid, suspended_user.uuid, 'uuid-does-not-exist'] }
let(:include_missing) { true }
let(:config) { ScriptBase::Config.new(include_missing:) }
let(:config) { ScriptBase::Config.new(include_missing:, reason: 'INV1234') }
subject(:result) { subtask.run(args:, config:) }

it 'suspends users that are not suspended already', aggregate_failures: true do
expect { result }.to(change { ActionMailer::Base.deliveries.count }.by(1))

# rubocop:disable Layout/LineLength
expect(result.table).to match_array(
[
['uuid', 'status'],
[user.uuid, 'User is not suspended'],
[suspended_user.uuid, 'User has been reinstated and the user has been emailed'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID'],
['uuid', 'status', 'reason'],
[user.uuid, 'User is not suspended', 'INV1234'],
[suspended_user.uuid, 'User has been reinstated and the user has been emailed', 'INV1234'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID', 'INV1234'],
],
)
# rubocop:enable Layout/LineLength

expect(result.subtask).to eq('reinstate-user')
expect(result.uuids).to match_array([user.uuid, suspended_user.uuid])
Expand All @@ -310,7 +318,7 @@
let(:suspended_user) { create(:user, :suspended) }
let(:args) { [suspended_user.uuid, user.uuid, 'uuid-does-not-exist'] }
let(:include_missing) { true }
let(:config) { ScriptBase::Config.new(include_missing:) }
let(:config) { ScriptBase::Config.new(include_missing:, reason: 'INV1234') }
subject(:result) { subtask.run(args:, config:) }

let(:analytics) { FakeAnalytics.new }
Expand All @@ -324,10 +332,10 @@

expect(result.table).to match_array(
[
['uuid', 'status'],
[suspended_user.uuid, 'User has been emailed'],
[user.uuid, 'User is not suspended'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID'],
['uuid', 'status', 'reason'],
[suspended_user.uuid, 'User has been emailed', 'INV1234'],
[user.uuid, 'User is not suspended', 'INV1234'],
['uuid-does-not-exist', 'Error: Could not find user with that UUID', 'INV1234'],
],
)

Expand Down
1 change: 1 addition & 0 deletions spec/lib/script_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def run(args:, config:) # rubocop:disable Lint/UnusedMethodArgument
stderr:,
subtask_class:,
banner: '',
reason_arg: false,
)
end

Expand Down