feat: Audit Logs for Account User Changes (#7405)

- Audit log for user invitations: https://linear.app/chatwoot/issue/CW-1768/invited-a-user-to-the-account
- Audit log for change role: https://linear.app/chatwoot/issue/CW-1767/name-or-email-changed-the-role-of-the-user-email-to-agent-or-admin
- Audit log for status change: https://linear.app/chatwoot/issue/CW-1766/availability-status-as-events-for-audit-logs


Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
Sojan Jose
2023-06-28 22:42:06 +05:30
committed by GitHub
parent d05c953eef
commit 40830046e8
13 changed files with 384 additions and 46 deletions

View File

@@ -2,8 +2,8 @@ require 'rails_helper'
RSpec.describe 'Enterprise Audit API', type: :request do
let!(:account) { create(:account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:admin) { create(:user, account: account, role: :administrator) }
let!(:inbox) { create(:inbox, account: account) }
describe 'GET /api/v1/accounts/{account.id}/audit_logs' do
context 'when it is an un-authenticated user' do
@@ -52,7 +52,8 @@ RSpec.describe 'Enterprise Audit API', type: :request do
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['current_page']).to be(1)
expect(json_response['total_entries']).to be(1)
# contains audit log for account user as well
expect(json_response['total_entries']).to be(2)
end
end
end

View File

@@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AccountUser do
describe 'audit log' do
context 'when account user is created' do
it 'has associated audit log created' do
account_user = create(:account_user)
account_user_audit_log = Audited::Audit.where(auditable_type: 'AccountUser', action: 'create').first
expect(account_user_audit_log).to be_present
expect(account_user_audit_log.associated).to eq(account_user.account)
end
end
context 'when account user is updated' do
it 'has associated audit log created' do
account_user = create(:account_user)
account_user.update!(availability: 'offline')
account_user_audit_log = Audited::Audit.where(auditable_type: 'AccountUser', action: 'update').first
expect(account_user_audit_log).to be_present
expect(account_user_audit_log.associated).to eq(account_user.account)
expect(account_user_audit_log.audited_changes).to eq('availability' => [0, 1])
end
end
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe User do
let(:user) { create(:user) }
describe 'audit log' do
context 'when user is created' do
it 'has no associated audit log created' do
expect(Audited::Audit.where(auditable_type: 'User', action: 'create').count).to eq 0
end
end
end
end