diff --git a/test/interactors/audit_logging_test.rb b/test/interactors/audit_logging_test.rb new file mode 100644 index 0000000..ad864a9 --- /dev/null +++ b/test/interactors/audit_logging_test.rb @@ -0,0 +1,43 @@ +require "test_helper" + +class AuditLoggingTest < ActiveSupport::TestCase + def setup + @domain = domains(:owner_match) + @identity = Identity.new(subject: @domain.users_array.first) + @cr = CertIssueRequest.new(common_name: @domain.fqdn) + @log = Tempfile.new("log-test") + Rails.configuration.astral[:audit_log_file] = @log.path + end + + def teardown + @log.close + @log.unlink + end + + test ".call will be logged as success" do + Object.const_set("SuccessAction", Class.new do + include Interactor + include AuditLogging + + def call + end + end) + rslt = SuccessAction.call(identity: @identity, request: @cr) + assert rslt.success? + assert_match %Q("action":"SuccessAction","result":"success","error":null,"subject":"john.doe@example.com","cert_common_name":"example.com"), @log.readlines.last + end + + test ".call will be logged as failure" do + Object.const_set("FailAction", Class.new do + include Interactor + include AuditLogging + + def call + context.fail! + end + end) + rslt = FailAction.call(identity: @identity, request: @cr) + assert_not rslt.success? + assert_match %Q("action":"FailAction","result":"failure","error":null,"subject":"john.doe@example.com","cert_common_name":"example.com"), @log.readlines.last + end +end diff --git a/test/lib/audit_log_formatter_test.rb b/test/lib/audit_log_formatter_test.rb index 79e6333..e23e185 100644 --- a/test/lib/audit_log_formatter_test.rb +++ b/test/lib/audit_log_formatter_test.rb @@ -1,6 +1,10 @@ require "test_helper" class AuditLogFormatterTest < ActiveSupport::TestCase + setup do + Thread.current[:request_id] = nil + end + test "#call formats logformatter inputs as json" do t = Time.now result = AuditLogFormatter.new.call("info", t, nil, "some message")