First commit. Include junk from template to remove

This commit is contained in:
Dominic Villemure
2024-03-09 20:25:30 -05:00
commit bbcefcf76f
140 changed files with 8151 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
using Hutopy.Application.Common.Behaviours;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.TodoItems.Commands.CreateTodoItem;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
namespace Hutopy.Application.UnitTests.Common.Behaviours;
public class RequestLoggerTests
{
private Mock<ILogger<CreateTodoItemCommand>> _logger = null!;
private Mock<IUser> _user = null!;
private Mock<IIdentityService> _identityService = null!;
[SetUp]
public void Setup()
{
_logger = new Mock<ILogger<CreateTodoItemCommand>>();
_user = new Mock<IUser>();
_identityService = new Mock<IIdentityService>();
}
[Test]
public async Task ShouldCallGetUserNameAsyncOnceIfAuthenticated()
{
_user.Setup(x => x.Id).Returns(Guid.NewGuid().ToString());
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _user.Object, _identityService.Object);
await requestLogger.Process(new CreateTodoItemCommand { ListId = 1, Title = "title" }, new CancellationToken());
_identityService.Verify(i => i.GetUserNameAsync(It.IsAny<string>()), Times.Once);
}
[Test]
public async Task ShouldNotCallGetUserNameAsyncOnceIfUnauthenticated()
{
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _user.Object, _identityService.Object);
await requestLogger.Process(new CreateTodoItemCommand { ListId = 1, Title = "title" }, new CancellationToken());
_identityService.Verify(i => i.GetUserNameAsync(It.IsAny<string>()), Times.Never);
}
}

View File

@@ -0,0 +1,63 @@
using Hutopy.Application.Common.Exceptions;
using FluentAssertions;
using FluentValidation.Results;
using NUnit.Framework;
namespace Hutopy.Application.UnitTests.Common.Exceptions;
public class ValidationExceptionTests
{
[Test]
public void DefaultConstructorCreatesAnEmptyErrorDictionary()
{
var actual = new ValidationException().Errors;
actual.Keys.Should().BeEquivalentTo(Array.Empty<string>());
}
[Test]
public void SingleValidationFailureCreatesASingleElementErrorDictionary()
{
var failures = new List<ValidationFailure>
{
new ValidationFailure("Age", "must be over 18"),
};
var actual = new ValidationException(failures).Errors;
actual.Keys.Should().BeEquivalentTo(new string[] { "Age" });
actual["Age"].Should().BeEquivalentTo(new string[] { "must be over 18" });
}
[Test]
public void MulitpleValidationFailureForMultiplePropertiesCreatesAMultipleElementErrorDictionaryEachWithMultipleValues()
{
var failures = new List<ValidationFailure>
{
new ValidationFailure("Age", "must be 18 or older"),
new ValidationFailure("Age", "must be 25 or younger"),
new ValidationFailure("Password", "must contain at least 8 characters"),
new ValidationFailure("Password", "must contain a digit"),
new ValidationFailure("Password", "must contain upper case letter"),
new ValidationFailure("Password", "must contain lower case letter"),
};
var actual = new ValidationException(failures).Errors;
actual.Keys.Should().BeEquivalentTo(new string[] { "Password", "Age" });
actual["Age"].Should().BeEquivalentTo(new string[]
{
"must be 25 or younger",
"must be 18 or older",
});
actual["Password"].Should().BeEquivalentTo(new string[]
{
"must contain lower case letter",
"must contain upper case letter",
"must contain at least 8 characters",
"must contain a digit",
});
}
}

View File

@@ -0,0 +1,53 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using AutoMapper;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Common.Models;
using Hutopy.Application.TodoItems.Queries.GetTodoItemsWithPagination;
using Hutopy.Application.TodoLists.Queries.GetTodos;
using Hutopy.Domain.Entities;
using NUnit.Framework;
namespace Hutopy.Application.UnitTests.Common.Mappings;
public class MappingTests
{
private readonly IConfigurationProvider _configuration;
private readonly IMapper _mapper;
public MappingTests()
{
_configuration = new MapperConfiguration(config =>
config.AddMaps(Assembly.GetAssembly(typeof(IApplicationDbContext))));
_mapper = _configuration.CreateMapper();
}
[Test]
public void ShouldHaveValidConfiguration()
{
_configuration.AssertConfigurationIsValid();
}
[Test]
[TestCase(typeof(TodoList), typeof(TodoListDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemDto))]
[TestCase(typeof(TodoList), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemBriefDto))]
public void ShouldSupportMappingFromSourceToDestination(Type source, Type destination)
{
var instance = GetInstanceOf(source);
_mapper.Map(instance, source, destination);
}
private object GetInstanceOf(Type type)
{
if (type.GetConstructor(Type.EmptyTypes) != null)
return Activator.CreateInstance(type)!;
// Type without parameterless constructor
return RuntimeHelpers.GetUninitializedObject(type);
}
}