Skip to content

Commit

Permalink
fix: skip auditlogs for whatsapp template sync (#8579)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnu-narayanan committed Jan 10, 2024
1 parent 5845881 commit d731c97
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions enterprise/app/models/enterprise/channelable.rb
Expand Up @@ -21,6 +21,9 @@ def create_audit_log_entry

return if audited_changes.blank?

# skip audit log creation if the only change is whatsapp channel template update
return if messaging_template_updates?(audited_changes)

Enterprise::AuditLog.create(
auditable_id: auditable_id,
auditable_type: auditable_type,
Expand All @@ -30,5 +33,13 @@ def create_audit_log_entry
audited_changes: audited_changes
)
end

def messaging_template_updates?(changes)
# if there is more than one key, return false
return false unless changes.keys.length == 1

# if the only key is message_templates_last_updated, return true
changes.key?('message_templates_last_updated')
end
end
end
52 changes: 52 additions & 0 deletions spec/enterprise/models/inbox_spec.rb
Expand Up @@ -97,4 +97,56 @@
end
end
end

describe 'audit log with whatsapp channel' do
let(:channel) { create(:channel_whatsapp, provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false) }
let(:inbox) { channel.inbox }

before do
stub_request(:get, 'https://graph.facebook.com/v14.0//message_templates?access_token=test_key')
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent' => 'Ruby'
}
)
.to_return(status: 200, body: '', headers: {})
end

context 'when inbox is created' do
it 'has associated audit log created' do
expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'create').count).to eq(1)
end
end

context 'when inbox is updated' do
it 'has associated audit log created' do
inbox.update(name: 'Updated Inbox')
expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update').count).to eq(1)
end
end

context 'when channel is updated' do
it 'has associated audit log created' do
previous_phone_number = inbox.channel.phone_number
new_phone_number = '1234567890'
inbox.channel.update(phone_number: new_phone_number)

# check if channel update creates an audit log against inbox
expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update').count).to eq(1)
# Check for the specific phone_number update in the audit log
expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update',
audited_changes: { 'phone_number' => [previous_phone_number, new_phone_number] }).count).to eq(1)
end
end

context 'when template sync runs' do
it 'has no associated audit log created' do
channel.sync_templates
# check if template sync does not create an audit log
expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update').count).to eq(0)
end
end
end
end

0 comments on commit d731c97

Please sign in to comment.