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