Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Format and log audit-level messages only when audit logging is e…
…nabled. (#1734)
- Loading branch information
1 parent
1a56612
commit f71521f
Showing
4 changed files
with
139 additions
and
10 deletions.
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
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
tests/Agent/UnitTests/Core.UnitTest/Logging/AuditLogTests.cs
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 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using NUnit.Framework; | ||
using Serilog; | ||
using Serilog.Configuration; | ||
using Telerik.JustMock; | ||
|
||
namespace NewRelic.Agent.Core.Logging.Tests | ||
{ | ||
[TestFixture] | ||
public class AuditLogTests | ||
{ | ||
private ILogger _mockILogger; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_mockILogger = Mock.Create<ILogger>(); | ||
Log.Logger = _mockILogger; | ||
|
||
// reset state for each test | ||
AuditLog.ResetLazyLogger(); | ||
AuditLog.IsAuditLogEnabled = false; | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
AuditLog.ResetLazyLogger(); | ||
} | ||
|
||
[Test] | ||
public void IncludeOnlyAuditLog_EnablesAuditLog() | ||
{ | ||
Assert.False(AuditLog.IsAuditLogEnabled); | ||
|
||
var _ = new LoggerConfiguration().IncludeOnlyAuditLog(); | ||
|
||
Assert.True(AuditLog.IsAuditLogEnabled); | ||
} | ||
|
||
[Test] | ||
public void ExcludeAuditLog_DisablesAuditLog() | ||
{ | ||
AuditLog.IsAuditLogEnabled = true; | ||
|
||
var _ = new LoggerConfiguration().ExcludeAuditLog(); | ||
|
||
Assert.False(AuditLog.IsAuditLogEnabled); | ||
} | ||
|
||
[TestCase(true)] | ||
[TestCase(false)] | ||
public void Log_OnlyLogsWhenAuditLogEnabled(bool logEnabled) | ||
{ | ||
var mockForContextLogger = Mock.Create<ILogger>(); | ||
Mock.Arrange(() => _mockILogger.ForContext(Arg.AnyString, Arg.AnyObject, false)) | ||
.Returns(() => mockForContextLogger); | ||
|
||
AuditLog.IsAuditLogEnabled = logEnabled; | ||
|
||
var message = "This is an audit message"; | ||
|
||
AuditLog.Log(message); | ||
|
||
Mock.Assert(() => mockForContextLogger.Fatal(message), logEnabled ? Occurs.Once() : Occurs.Never()); | ||
} | ||
} | ||
} |