Commit 42f7a58b authored by James Lopez's avatar James Lopez

refactor email service to log events and added specs

parent e738b80a
...@@ -5,5 +5,10 @@ module Emails ...@@ -5,5 +5,10 @@ module Emails
@user = user @user = user
@email = opts[:email] @email = opts[:email]
end end
def log_audit_event(options = {})
AuditEventService.new(@current_user, @user, options)
.for_email(@email).security_event
end
end end
end end
...@@ -2,6 +2,8 @@ module Emails ...@@ -2,6 +2,8 @@ module Emails
class CreateService < ::Emails::BaseService class CreateService < ::Emails::BaseService
def execute def execute
@user.emails.create(email: @email) @user.emails.create(email: @email)
log_audit_event(action: :create)
end end
end end
end end
...@@ -2,6 +2,8 @@ module Emails ...@@ -2,6 +2,8 @@ module Emails
class DestroyService < ::Emails::BaseService class DestroyService < ::Emails::BaseService
def execute def execute
Email.find_by_email!(@email).destroy && update_secondary_emails! Email.find_by_email!(@email).destroy && update_secondary_emails!
log_audit_event(action: :destroy)
end end
private private
......
...@@ -17,5 +17,9 @@ describe Emails::CreateService do ...@@ -17,5 +17,9 @@ describe Emails::CreateService do
expect(user.emails).to eq(Email.where(opts)) expect(user.emails).to eq(Email.where(opts))
end end
it 'registers a security event' do
expect { service.execute }.to change { SecurityEvent.count }.by(1)
end
end end
end end
...@@ -10,5 +10,9 @@ describe Emails::DestroyService do ...@@ -10,5 +10,9 @@ describe Emails::DestroyService do
it 'removes an email' do it 'removes an email' do
expect { service.execute }.to change { user.emails.count }.by(-1) expect { service.execute }.to change { user.emails.count }.by(-1)
end end
it 'registers a security event' do
expect { service.execute }.to change { SecurityEvent.count }.by(1)
end
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