Commit ea584e6b authored by Serena Fang's avatar Serena Fang

Reduce AuditEvent queries

Make audit_event variable to reduce queries
parent 6f420923
...@@ -30,11 +30,17 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -30,11 +30,17 @@ RSpec.describe ResourceAccessTokens::CreateService do
end end
shared_examples 'audit event details' do shared_examples 'audit event details' do
it 'logs author and resource info', :aggregate_failures do it 'creates an audit event' do
expect { subject }.to change { AuditEvent.count }.from(0).to(1) expect { subject }.to change { AuditEvent.count }.from(0).to(1)
expect(AuditEvent.last.author_id).to eq(user.id) end
expect(AuditEvent.last.entity_id).to eq(resource.id)
expect(AuditEvent.last.ip_address).to eq(user.current_sign_in_ip) it 'logs author and resource info', :aggregate_failures do
subject
audit_event = AuditEvent.where(author_id: user.id).last
expect(audit_event.entity_id).to eq(resource.id)
expect(audit_event.ip_address).to eq(user.current_sign_in_ip)
end end
end end
...@@ -77,8 +83,10 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -77,8 +83,10 @@ RSpec.describe ResourceAccessTokens::CreateService do
it 'logs project access token details', :aggregate_failures do it 'logs project access token details', :aggregate_failures do
response = subject response = subject
expect(AuditEvent.last.details[:custom_message]).to eq("Created project access token with id: #{response.payload[:access_token].user.id} with scopes: #{response.payload[:access_token].scopes}") audit_event = AuditEvent.where(author_id: user.id).last
expect(AuditEvent.last.details[:target_details]).to match(response.payload[:access_token].user.name)
expect(audit_event.details[:custom_message]).to eq("Created project access token with id: #{response.payload[:access_token].user.id} with scopes: #{response.payload[:access_token].scopes}")
expect(audit_event.details[:target_details]).to match(response.payload[:access_token].user.name)
end end
end end
......
...@@ -10,11 +10,17 @@ RSpec.describe ResourceAccessTokens::RevokeService do ...@@ -10,11 +10,17 @@ RSpec.describe ResourceAccessTokens::RevokeService do
let(:access_token) { create(:personal_access_token, user: resource_bot) } let(:access_token) { create(:personal_access_token, user: resource_bot) }
shared_examples 'audit event details' do shared_examples 'audit event details' do
it 'logs author and resource info', :aggregate_failures do it 'creates an audit event' do
expect { subject }.to change { AuditEvent.count }.from(0).to(1) expect { subject }.to change { AuditEvent.count }.from(0).to(1)
expect(AuditEvent.last.author_id).to eq(user.id) end
expect(AuditEvent.last.entity_id).to eq(resource.id)
expect(AuditEvent.last.ip_address).to eq(user.current_sign_in_ip) it 'logs author and resource info', :aggregate_failures do
subject
audit_event = AuditEvent.where(author_id: user.id).last
expect(audit_event.entity_id).to eq(resource.id)
expect(audit_event.ip_address).to eq(user.current_sign_in_ip)
end end
end end
...@@ -32,8 +38,10 @@ RSpec.describe ResourceAccessTokens::RevokeService do ...@@ -32,8 +38,10 @@ RSpec.describe ResourceAccessTokens::RevokeService do
it 'logs project access token details', :aggregate_failures do it 'logs project access token details', :aggregate_failures do
subject subject
expect(AuditEvent.last.details[:custom_message]).to match(/Revoked project access token with id: \d+/) audit_event = AuditEvent.where(author_id: user.id).last
expect(AuditEvent.last.details[:target_details]).to eq(access_token.user.name)
expect(audit_event.details[:custom_message]).to match(/Revoked project access token with id: \d+/)
expect(audit_event.details[:target_details]).to eq(access_token.user.name)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment