Skip to content

Commit

Permalink
Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
suprjinx committed Sep 13, 2024
1 parent 36bf579 commit 4bd9c4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/lib/audit_log_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def call(severity, timestamp, _progname, message)
request_id = Thread.current[:request_id]
json = {
type: severity,
time: timestamp,
time: "#{timestamp}",
request_id: request_id
}
if message.is_a? Hash
Expand Down
24 changes: 24 additions & 0 deletions test/lib/audit_log_formatter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "test_helper"

class AuditLogFormatterTest < ActiveSupport::TestCase
test "#call formats logformatter inputs as json" do
t = Time.now
result = AuditLogFormatter.new.call("info", t, nil, "some message")
assert_equal %Q({"type":"info","time":"#{t}","request_id":null,"message":"some message"}\n), result
end

test "#call accepts and merges a Hash type for the message" do
t = Time.now
result = AuditLogFormatter.new.call("info", t, nil, { key: "some message", key2: "another" })
assert_equal %Q({"type":"info","time":"#{t}","request_id":null,"key":"some message","key2":"another"}\n), result
end

test "#call can render a thread local request_id" do
t = Time.now
req_id = SecureRandom.hex
Thread.stub :current, { request_id: req_id } do
result = AuditLogFormatter.new.call("info", t, nil, { key: "some message" })
assert_equal %Q({"type":"info","time":"#{t}","request_id":"#{req_id}","key":"some message"}\n), result
end
end
end

0 comments on commit 4bd9c4a

Please sign in to comment.