* Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
155 lines
5.6 KiB
C#
155 lines
5.6 KiB
C#
using System.Data;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.Events;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Persistence.Repositories;
|
|
using Umbraco.Cms.Core.Scoping;
|
|
using Umbraco.Cms.Core.Services;
|
|
using Umbraco.Cms.Core.Services.OperationStatus;
|
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services;
|
|
|
|
[TestFixture]
|
|
public class AuditEntryServiceTests
|
|
{
|
|
private static readonly Guid _testUserKey = Guid.NewGuid();
|
|
private IAuditEntryService _auditEntryService;
|
|
private Mock<ICoreScopeProvider> _scopeProviderMock;
|
|
private Mock<IAuditEntryRepository> _auditEntryRepositoryMock;
|
|
private Mock<IUserIdKeyResolver> _userIdKeyResolverMock;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_scopeProviderMock = new Mock<ICoreScopeProvider>(MockBehavior.Strict);
|
|
_auditEntryRepositoryMock = new Mock<IAuditEntryRepository>(MockBehavior.Strict);
|
|
_userIdKeyResolverMock = new Mock<IUserIdKeyResolver>(MockBehavior.Strict);
|
|
|
|
_auditEntryService = new AuditEntryService(
|
|
_auditEntryRepositoryMock.Object,
|
|
_userIdKeyResolverMock.Object,
|
|
_scopeProviderMock.Object,
|
|
Mock.Of<ILoggerFactory>(MockBehavior.Strict),
|
|
Mock.Of<IEventMessagesFactory>(MockBehavior.Strict));
|
|
}
|
|
|
|
[Test]
|
|
public async Task WriteAsync_Calls_Repository_With_Correct_Values()
|
|
{
|
|
SetupScopeProviderMock();
|
|
|
|
var date = DateTime.UtcNow;
|
|
_auditEntryRepositoryMock.Setup(x => x.Save(It.IsAny<IAuditEntry>()))
|
|
.Callback<IAuditEntry>(item =>
|
|
{
|
|
Assert.AreEqual(Constants.Security.SuperUserId, item.PerformingUserId);
|
|
Assert.AreEqual(Constants.Security.SuperUserKey, item.PerformingUserKey);
|
|
Assert.AreEqual("performingDetails", item.PerformingDetails);
|
|
Assert.AreEqual("performingIp", item.PerformingIp);
|
|
Assert.AreEqual(date, item.EventDate);
|
|
Assert.AreEqual(Constants.Security.UnknownUserId, item.AffectedUserId);
|
|
Assert.AreEqual(null, item.AffectedUserKey);
|
|
Assert.AreEqual("affectedDetails", item.AffectedDetails);
|
|
Assert.AreEqual("umbraco/test", item.EventType);
|
|
Assert.AreEqual("eventDetails", item.EventDetails);
|
|
});
|
|
_userIdKeyResolverMock.Setup(x => x.TryGetAsync(Constants.Security.SuperUserKey))
|
|
.ReturnsAsync(Attempt.Succeed(Constants.Security.SuperUserId));
|
|
|
|
var result = await _auditEntryService.WriteAsync(
|
|
Constants.Security.SuperUserKey,
|
|
"performingDetails",
|
|
"performingIp",
|
|
date,
|
|
null,
|
|
"affectedDetails",
|
|
"umbraco/test",
|
|
"eventDetails");
|
|
|
|
_auditEntryRepositoryMock.Verify(x => x.Save(It.IsAny<IAuditEntry>()), Times.Once);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.AreEqual(Constants.Security.SuperUserId, result.PerformingUserId);
|
|
Assert.AreEqual("performingDetails", result.PerformingDetails);
|
|
Assert.AreEqual("performingIp", result.PerformingIp);
|
|
Assert.AreEqual(date, result.EventDate);
|
|
Assert.AreEqual(Constants.Security.UnknownUserId, result.AffectedUserId);
|
|
Assert.AreEqual("affectedDetails", result.AffectedDetails);
|
|
Assert.AreEqual("umbraco/test", result.EventType);
|
|
Assert.AreEqual("eventDetails", result.EventDetails);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserId_UsingKey_Returns_Correct_Id()
|
|
{
|
|
SetupScopeProviderMock();
|
|
|
|
int userId = 12;
|
|
_userIdKeyResolverMock.Setup(x => x.TryGetAsync(_testUserKey))
|
|
.ReturnsAsync(Attempt.Succeed(userId));
|
|
|
|
var actualUserId = await ((AuditEntryService)_auditEntryService).GetUserId(_testUserKey);
|
|
|
|
Assert.AreEqual(actualUserId, userId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserId_UsingNonExistingKey_Returns_Null()
|
|
{
|
|
SetupScopeProviderMock();
|
|
|
|
_userIdKeyResolverMock.Setup(x => x.TryGetAsync(_testUserKey))
|
|
.ReturnsAsync(Attempt.Fail<int>());
|
|
|
|
var actualUserId = await ((AuditEntryService)_auditEntryService).GetUserId(_testUserKey);
|
|
|
|
Assert.AreEqual(null, actualUserId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserKey_UsingKey_Returns_Correct_Id()
|
|
{
|
|
SetupScopeProviderMock();
|
|
|
|
int userId = 12;
|
|
_userIdKeyResolverMock.Setup(x => x.TryGetAsync(userId))
|
|
.ReturnsAsync(Attempt.Succeed(_testUserKey));
|
|
|
|
var actualUserKey = await ((AuditEntryService)_auditEntryService).GetUserKey(userId);
|
|
|
|
Assert.AreEqual(actualUserKey, _testUserKey);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetUserKey_UsingNonExistingId_Returns_Null()
|
|
{
|
|
SetupScopeProviderMock();
|
|
|
|
int userId = 12;
|
|
_userIdKeyResolverMock.Setup(x => x.TryGetAsync(userId))
|
|
.ReturnsAsync(Attempt.Fail<Guid>());
|
|
|
|
var userKey = await ((AuditEntryService)_auditEntryService).GetUserKey(userId);
|
|
|
|
Assert.AreEqual(null, userKey);
|
|
}
|
|
|
|
private void SetupScopeProviderMock() =>
|
|
_scopeProviderMock
|
|
.Setup(x => x.CreateCoreScope(
|
|
It.IsAny<IsolationLevel>(),
|
|
It.IsAny<RepositoryCacheMode>(),
|
|
It.IsAny<IEventDispatcher>(),
|
|
It.IsAny<IScopedNotificationPublisher>(),
|
|
It.IsAny<bool?>(),
|
|
It.IsAny<bool>(),
|
|
It.IsAny<bool>()))
|
|
.Returns(Mock.Of<IScope>());
|
|
}
|