-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1711 from unboxed/add-custom-instrumentation-for-…
…auditing Use ActiveSupport::Notifications for auditing
- Loading branch information
Showing
25 changed files
with
719 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
-I engines/bops_core/spec | ||
-I engines/bops_admin/spec | ||
-I engines/bops_api/spec | ||
-I engines/bops_config/spec | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
engines/bops_config/spec/controllers/bops_config/application_types_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bops_config_helper" | ||
|
||
RSpec.describe BopsConfig::ApplicationTypesController, type: :controller do | ||
let(:user) { create(:user, :global_administrator) } | ||
|
||
before do | ||
sign_in(user) | ||
end | ||
|
||
routes { BopsConfig::Engine.routes } | ||
|
||
describe "#create" do | ||
context "with invalid params" do | ||
it "doesn't send an audit event" do | ||
expect { | ||
post :create, params: {application_type: {code: "", suffix: ""}} | ||
}.not_to have_audit("created.application_type") | ||
end | ||
end | ||
|
||
context "with valid params" do | ||
it "sends an audit event" do | ||
expect { | ||
post :create, params: {application_type: {code: "advertConsent", suffix: "ADVT"}} | ||
}.to have_audit("created.application_type").with_payload(a_hash_including( | ||
engine: "bops_config", | ||
params: {action: "create", controller: "bops_config/application_types"}, | ||
user: {"id" => user.id, "name" => user.name, "role" => user.role}, | ||
application_type: {"code" => "advertConsent"}, | ||
changes: a_hash_including( | ||
"code" => [nil, "advertConsent"], | ||
"suffix" => [nil, "ADVT"] | ||
), | ||
automated: false | ||
)) | ||
end | ||
end | ||
end | ||
|
||
describe "#update" do | ||
let!(:application_type) { create(:application_type, :inactive, code: "advertConsent", suffix: "ADVR") } | ||
|
||
context "with invalid params" do | ||
it "doesn't send an audit event" do | ||
expect { | ||
post :update, params: {id: application_type.id, application_type: {code: "", suffix: ""}} | ||
}.not_to have_audit("updated.application_type") | ||
end | ||
end | ||
|
||
context "with valid params" do | ||
it "sends an audit event" do | ||
expect { | ||
post :update, params: {id: application_type.id, application_type: {code: "advertConsent", suffix: "ADVT"}} | ||
}.to have_audit("updated.application_type").with_payload(a_hash_including( | ||
engine: "bops_config", | ||
params: {action: "update", controller: "bops_config/application_types", id: application_type.to_param}, | ||
user: {"id" => user.id, "name" => user.name, "role" => user.role}, | ||
application_type: {"code" => "advertConsent"}, | ||
changes: a_hash_including( | ||
"suffix" => ["ADVR", "ADVT"] | ||
), | ||
automated: false | ||
)) | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
engines/bops_core/app/controllers/concerns/bops_core/auditable_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module AuditableController | ||
extend ActiveSupport::Concern | ||
include Auditable | ||
|
||
AUDITABLE_ACTIONS = {}.tap do |actions| | ||
actions.merge!( | ||
"create" => "created", | ||
"update" => "updated", | ||
"destroy" => "destroyed" | ||
) | ||
|
||
actions.default_proc = ->(_, key) { key } | ||
end.freeze | ||
|
||
module ClassMethods | ||
def audit(*actions, event: nil, payload: {}, **options) | ||
after_action(only: actions, **options) do | ||
default_event = [ | ||
AUDITABLE_ACTIONS[action_name], | ||
controller_name.singularize | ||
].join(".") | ||
|
||
audit(event || default_event, payload) | ||
end | ||
end | ||
end | ||
end | ||
end |
Empty file.
16 changes: 16 additions & 0 deletions
16
engines/bops_core/app/jobs/concerns/bops_core/auditable_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module AuditableJob | ||
extend ActiveSupport::Concern | ||
include Auditable | ||
|
||
module ClassMethods | ||
def audit(event, payload: {}, **options) | ||
after_perform(**options) do | ||
audit(event, payload) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module Auditable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :audit_payload, instance_writer: false, default: -> { {} } | ||
end | ||
|
||
def audit(event, payload = {}, &) | ||
event = "#{event}.bops_audit" | ||
|
||
if payload.is_a?(Symbol) | ||
payload = send(payload) | ||
elsif payload.is_a?(Proc) | ||
payload = instance_exec(&payload) | ||
end | ||
|
||
if audit_payload.is_a?(Symbol) | ||
payload.merge!(send(audit_payload)) | ||
elsif audit_payload.is_a?(Proc) | ||
payload.merge!(instance_exec(&audit_payload)) | ||
else | ||
payload.merge!(audit_payload) | ||
end | ||
|
||
if block_given? | ||
ActiveSupport::Notifications.instrument(event, payload, &) | ||
else | ||
ActiveSupport::Notifications.instrument(event, payload) | ||
end | ||
end | ||
end | ||
end |
Empty file.
17 changes: 17 additions & 0 deletions
17
engines/bops_core/app/mailers/concerns/bops_core/auditable_mailer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module AuditableMailer | ||
extend ActiveSupport::Concern | ||
include Auditable | ||
|
||
module ClassMethods | ||
def audit(*actions, event: nil, payload: {}, **options) | ||
after_deliver(only: actions, **options) do | ||
default_event = [mailer_name, action_name].join(".") | ||
audit(event || default_event, payload) | ||
end | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
engines/bops_core/app/models/concerns/bops_core/auditable_model.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module AuditableModel | ||
extend ActiveSupport::Concern | ||
include Auditable | ||
|
||
included do | ||
with_options instance_accessor: false do | ||
class_attribute :audit_attributes, default: %w[id] | ||
class_attribute :audit_changes, default: %w[created_at updated_at] | ||
end | ||
end | ||
|
||
def audit_attributes | ||
attributes.slice(*self.class.audit_attributes) | ||
end | ||
|
||
def audit_changes | ||
previous_changes.except(*self.class.audit_changes) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
Dir[BopsCore::Engine.root.join("spec/support/**/*.rb")].each { |f| require f } |
20 changes: 20 additions & 0 deletions
20
engines/bops_core/spec/controllers/bops_core/auditable_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bops_core_helper" | ||
|
||
RSpec.describe BopsCore::AuditableController, type: :controller do | ||
controller(ActionController::Base) do | ||
include BopsCore::AuditableController | ||
|
||
audit :create, event: "event.scope", payload: {foo: "bar"} | ||
|
||
def create | ||
end | ||
end | ||
|
||
it "sends an audit event for the create action" do | ||
expect { | ||
post :create | ||
}.to have_audit("event.scope").with_payload(foo: "bar") | ||
end | ||
end |
Oops, something went wrong.