From ea825d49da81cfc3c57137a8468fd2f3643f4820 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Wed, 19 Jul 2023 23:52:34 +0530 Subject: [PATCH] feat: add auditlogs for account model (#7511) --- app/javascript/dashboard/helper/auditlogHelper.js | 1 + .../dashboard/i18n/locale/en/auditLogs.json | 3 +++ enterprise/app/models/enterprise/audit/account.rb | 1 + enterprise/app/models/enterprise/audit_log.rb | 6 +++++- .../api/v1/accounts/audit_logs_controller_spec.rb | 11 ++++++----- spec/enterprise/models/account_spec.rb | 5 +++++ 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/javascript/dashboard/helper/auditlogHelper.js b/app/javascript/dashboard/helper/auditlogHelper.js index 7f912fb95..467b416f9 100644 --- a/app/javascript/dashboard/helper/auditlogHelper.js +++ b/app/javascript/dashboard/helper/auditlogHelper.js @@ -30,6 +30,7 @@ const translationKeys = { 'accountuser:create': `AUDIT_LOGS.ACCOUNT_USER.ADD`, 'accountuser:update:self': `AUDIT_LOGS.ACCOUNT_USER.EDIT.SELF`, 'accountuser:update:other': `AUDIT_LOGS.ACCOUNT_USER.EDIT.OTHER`, + 'account:update': `AUDIT_LOGS.ACCOUNT.EDIT`, }; function extractAttrChange(attrChange) { diff --git a/app/javascript/dashboard/i18n/locale/en/auditLogs.json b/app/javascript/dashboard/i18n/locale/en/auditLogs.json index a1c64b51e..f3e8edc2c 100644 --- a/app/javascript/dashboard/i18n/locale/en/auditLogs.json +++ b/app/javascript/dashboard/i18n/locale/en/auditLogs.json @@ -55,6 +55,9 @@ "ADD": "%{agentName} created a new macro (#%{id})", "EDIT": "%{agentName} updated a macro (#%{id})", "DELETE": "%{agentName} deleted a macro (#%{id})" + }, + "ACCOUNT": { + "EDIT": "%{agentName} updated the account configuration (#%{id})" } } } diff --git a/enterprise/app/models/enterprise/audit/account.rb b/enterprise/app/models/enterprise/audit/account.rb index c9a1f0f13..66efaa83d 100644 --- a/enterprise/app/models/enterprise/audit/account.rb +++ b/enterprise/app/models/enterprise/audit/account.rb @@ -2,6 +2,7 @@ module Enterprise::Audit::Account extend ActiveSupport::Concern included do + audited except: :updated_at, on: [:update] has_associated_audits end end diff --git a/enterprise/app/models/enterprise/audit_log.rb b/enterprise/app/models/enterprise/audit_log.rb index 49188110e..c84aefcb1 100644 --- a/enterprise/app/models/enterprise/audit_log.rb +++ b/enterprise/app/models/enterprise/audit_log.rb @@ -5,7 +5,11 @@ class Enterprise::AuditLog < Audited::Audit def log_additional_information # rubocop:disable Rails/SkipsModelValidations - update_columns(username: user&.email) + if auditable_type == 'Account' && auditable_id.present? + update_columns(associated_type: auditable_type, associated_id: auditable_id, username: user&.email) + else + update_columns(username: user&.email) + end # rubocop:enable Rails/SkipsModelValidations end end diff --git a/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb index ab2e3f3f4..8cf28f93a 100644 --- a/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/audit_logs_controller_spec.rb @@ -47,13 +47,14 @@ RSpec.describe 'Enterprise Audit API', type: :request do expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) - expect(json_response['audit_logs'][0]['auditable_type']).to eql('Inbox') - expect(json_response['audit_logs'][0]['action']).to eql('create') - expect(json_response['audit_logs'][0]['audited_changes']['name']).to eql(inbox.name) - expect(json_response['audit_logs'][0]['associated_id']).to eql(account.id) + expect(json_response['audit_logs'][1]['auditable_type']).to eql('Inbox') + expect(json_response['audit_logs'][1]['action']).to eql('create') + expect(json_response['audit_logs'][1]['audited_changes']['name']).to eql(inbox.name) + expect(json_response['audit_logs'][1]['associated_id']).to eql(account.id) expect(json_response['current_page']).to be(1) # contains audit log for account user as well - expect(json_response['total_entries']).to be(2) + # contains audit logs for account update(enable audit logs) + expect(json_response['total_entries']).to be(3) end end end diff --git a/spec/enterprise/models/account_spec.rb b/spec/enterprise/models/account_spec.rb index af73da48f..87e4ac5c2 100644 --- a/spec/enterprise/models/account_spec.rb +++ b/spec/enterprise/models/account_spec.rb @@ -33,6 +33,11 @@ RSpec.describe Account do # checking whether associated_audits method is present expect(account.associated_audits.present?).to be false end + + it 'creates audit logs when account is updated' do + account.update(name: 'New Name') + expect(Audited::Audit.where(auditable_type: 'Account', action: 'update').count).to eq 1 + end end it 'returns max limits from global config when enterprise version' do