From 6bea2cbc4a5f922cce7cb106e41a919dd108134b Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Tue, 20 Jun 2023 13:04:59 +0530 Subject: [PATCH] feat: add audit trail for macros (#7352) --- .../dashboard/i18n/locale/en/auditLogs.json | 5 ++++ .../dashboard/settings/auditlogs/Index.vue | 3 ++ app/models/macro.rb | 2 ++ .../app/models/enterprise/audit/macro.rb | 7 +++++ spec/enterprise/models/macro_spec.rb | 30 +++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 enterprise/app/models/enterprise/audit/macro.rb create mode 100644 spec/enterprise/models/macro_spec.rb diff --git a/app/javascript/dashboard/i18n/locale/en/auditLogs.json b/app/javascript/dashboard/i18n/locale/en/auditLogs.json index 22b0f1bf1..2bad188fa 100644 --- a/app/javascript/dashboard/i18n/locale/en/auditLogs.json +++ b/app/javascript/dashboard/i18n/locale/en/auditLogs.json @@ -43,6 +43,11 @@ "ADD": "%{agentName} created a new team (#%{id})", "EDIT": "%{agentName} updated a team (#%{id})", "DELETE": "%{agentName} deleted a team (#%{id})" + }, + "MACRO": { + "ADD": "%{agentName} created a new macro (#%{id})", + "EDIT": "%{agentName} updated a macro (#%{id})", + "DELETE": "%{agentName} deleted a macro (#%{id})" } } } diff --git a/app/javascript/dashboard/routes/dashboard/settings/auditlogs/Index.vue b/app/javascript/dashboard/routes/dashboard/settings/auditlogs/Index.vue index c20431ad9..5b988b760 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/auditlogs/Index.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/auditlogs/Index.vue @@ -143,6 +143,9 @@ export default { 'team:create': `AUDIT_LOGS.TEAM.ADD`, 'team:update': `AUDIT_LOGS.TEAM.EDIT`, 'team:destroy': `AUDIT_LOGS.TEAM.DELETE`, + 'macro:create': `AUDIT_LOGS.MACRO.ADD`, + 'macro:update': `AUDIT_LOGS.MACRO.EDIT`, + 'macro:destroy': `AUDIT_LOGS.MACRO.DELETE`, }; return this.$t(translationKeys[logActionKey] || '', translationPayload); diff --git a/app/models/macro.rb b/app/models/macro.rb index 0951f7ae9..a54e93339 100644 --- a/app/models/macro.rb +++ b/app/models/macro.rb @@ -73,3 +73,5 @@ class Macro < ApplicationRecord errors.add(:actions, "Macro execution actions #{actions.join(',')} not supported.") if actions.any? end end + +Macro.include_mod_with('Audit::Macro') diff --git a/enterprise/app/models/enterprise/audit/macro.rb b/enterprise/app/models/enterprise/audit/macro.rb new file mode 100644 index 000000000..e1c2d19ff --- /dev/null +++ b/enterprise/app/models/enterprise/audit/macro.rb @@ -0,0 +1,7 @@ +module Enterprise::Audit::Macro + extend ActiveSupport::Concern + + included do + audited associated_with: :account + end +end diff --git a/spec/enterprise/models/macro_spec.rb b/spec/enterprise/models/macro_spec.rb new file mode 100644 index 000000000..ff3368438 --- /dev/null +++ b/spec/enterprise/models/macro_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Macro do + let(:account) { create(:account) } + let!(:macro) { create(:macro, account: account) } + + describe 'audit log' do + context 'when macro is created' do + it 'has associated audit log created' do + expect(Audited::Audit.where(auditable_type: 'Macro', action: 'create').count).to eq 1 + end + end + + context 'when macro is updated' do + it 'has associated audit log created' do + macro.update(name: 'awesome macro') + expect(Audited::Audit.where(auditable_type: 'Macro', action: 'update').count).to eq 1 + end + end + + context 'when macro is deleted' do + it 'has associated audit log created' do + macro.destroy! + expect(Audited::Audit.where(auditable_type: 'Macro', action: 'destroy').count).to eq 1 + end + end + end +end