From cca19f5fbb568bf7f145fe98d6d2497632c8987c Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 4 Dec 2023 07:56:28 -0500 Subject: [PATCH] Use the `Admin::ActionLog` fabricator in admin/action_logs spec (#28194) --- .../admin/accounts_controller_spec.rb | 32 ++++++++++++------- .../admin/action_logs_controller_spec.rb | 31 +++++++++++++++--- .../api/v1/admin/account_actions_spec.rb | 17 +++++++--- spec/requests/api/v1/admin/accounts_spec.rb | 32 ++++++++++++------- 4 files changed, 78 insertions(+), 34 deletions(-) diff --git a/spec/controllers/admin/accounts_controller_spec.rb b/spec/controllers/admin/accounts_controller_spec.rb index 307e819501..1882ea8388 100644 --- a/spec/controllers/admin/accounts_controller_spec.rb +++ b/spec/controllers/admin/accounts_controller_spec.rb @@ -161,12 +161,13 @@ RSpec.describe Admin::AccountsController do it 'logs action' do expect(subject).to have_http_status 302 - log_item = Admin::ActionLog.last - - expect(log_item).to_not be_nil - expect(log_item.action).to eq :approve - expect(log_item.account_id).to eq current_user.account_id - expect(log_item.target_id).to eq account.user.id + expect(latest_admin_action_log) + .to be_present + .and have_attributes( + action: eq(:approve), + account_id: eq(current_user.account_id), + target_id: eq(account.user.id) + ) end end @@ -201,12 +202,13 @@ RSpec.describe Admin::AccountsController do it 'logs action' do expect(subject).to have_http_status 302 - log_item = Admin::ActionLog.last - - expect(log_item).to_not be_nil - expect(log_item.action).to eq :reject - expect(log_item.account_id).to eq current_user.account_id - expect(log_item.target_id).to eq account.user.id + expect(latest_admin_action_log) + .to be_present + .and have_attributes( + action: eq(:reject), + account_id: eq(current_user.account_id), + target_id: eq(account.user.id) + ) end end @@ -427,4 +429,10 @@ RSpec.describe Admin::AccountsController do end end end + + private + + def latest_admin_action_log + Admin::ActionLog.last + end end diff --git a/spec/controllers/admin/action_logs_controller_spec.rb b/spec/controllers/admin/action_logs_controller_spec.rb index b7854469dd..be4222df08 100644 --- a/spec/controllers/admin/action_logs_controller_spec.rb +++ b/spec/controllers/admin/action_logs_controller_spec.rb @@ -9,11 +9,9 @@ describe Admin::ActionLogsController do let!(:account) { Fabricate(:account) } before do - _orphaned_logs = %w( - Account User UserRole Report DomainBlock DomainAllow - EmailDomainBlock UnavailableDomain Status AccountWarning - Announcement IpBlock Instance CustomEmoji CanonicalEmailBlock Appeal - ).map { |type| Admin::ActionLog.new(account: account, action: 'destroy', target_type: type, target_id: 1312).save! } + orphaned_log_types.map do |type| + Fabricate(:action_log, account: account, action: 'destroy', target_type: type, target_id: 1312) + end end describe 'GET #index' do @@ -24,4 +22,27 @@ describe Admin::ActionLogsController do expect(response).to have_http_status(200) end end + + private + + def orphaned_log_types + %w( + Account + AccountWarning + Announcement + Appeal + CanonicalEmailBlock + CustomEmoji + DomainAllow + DomainBlock + EmailDomainBlock + Instance + IpBlock + Report + Status + UnavailableDomain + User + UserRole + ) + end end diff --git a/spec/requests/api/v1/admin/account_actions_spec.rb b/spec/requests/api/v1/admin/account_actions_spec.rb index c14e08c21c..4167911a13 100644 --- a/spec/requests/api/v1/admin/account_actions_spec.rb +++ b/spec/requests/api/v1/admin/account_actions_spec.rb @@ -21,12 +21,19 @@ RSpec.describe 'Account actions' do it 'logs action' do subject - log_item = Admin::ActionLog.last + expect(latest_admin_action_log) + .to be_present + .and have_attributes( + action: eq(action_type), + account_id: eq(user.account_id), + target_id: eq(target_type == :user ? target_account.user.id : target_account.id) + ) + end - expect(log_item).to be_present - expect(log_item.action).to eq(action_type) - expect(log_item.account_id).to eq(user.account_id) - expect(log_item.target_id).to eq(target_type == :user ? target_account.user.id : target_account.id) + private + + def latest_admin_action_log + Admin::ActionLog.last end end diff --git a/spec/requests/api/v1/admin/accounts_spec.rb b/spec/requests/api/v1/admin/accounts_spec.rb index 8e158f623d..1615581f0e 100644 --- a/spec/requests/api/v1/admin/accounts_spec.rb +++ b/spec/requests/api/v1/admin/accounts_spec.rb @@ -151,12 +151,13 @@ RSpec.describe 'Accounts' do it 'logs action', :aggregate_failures do subject - log_item = Admin::ActionLog.last - - expect(log_item).to be_present - expect(log_item.action).to eq :approve - expect(log_item.account_id).to eq user.account_id - expect(log_item.target_id).to eq account.user.id + expect(latest_admin_action_log) + .to be_present + .and have_attributes( + action: eq(:approve), + account_id: eq(user.account_id), + target_id: eq(account.user.id) + ) end end @@ -202,12 +203,13 @@ RSpec.describe 'Accounts' do it 'logs action', :aggregate_failures do subject - log_item = Admin::ActionLog.last - - expect(log_item).to be_present - expect(log_item.action).to eq :reject - expect(log_item.account_id).to eq user.account_id - expect(log_item.target_id).to eq account.user.id + expect(latest_admin_action_log) + .to be_present + .and have_attributes( + action: eq(:reject), + account_id: eq(user.account_id), + target_id: eq(account.user.id) + ) end end @@ -398,4 +400,10 @@ RSpec.describe 'Accounts' do end end end + + private + + def latest_admin_action_log + Admin::ActionLog.last + end end