more testing
This commit is contained in:
parent
3a26c007d2
commit
1cd75cf481
@ -1,3 +1,5 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UserManager.Application.Features.Users.Interfaces;
|
||||
using UserManager.Application.Features.Users.Requests;
|
||||
@ -7,17 +9,31 @@ namespace UserManager.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController(ILogger<UserController> logger, IUserService userService) : ControllerBase
|
||||
public class UserController(ILogger<UserController> logger, IUserService userService, IValidator<CreateUserRequestDto> createUserValidator) : ControllerBase
|
||||
{
|
||||
private readonly ILogger<UserController> _logger = logger;
|
||||
private readonly IUserService _userService = userService;
|
||||
|
||||
private readonly IValidator<CreateUserRequestDto> _createUserValidator = createUserValidator;
|
||||
|
||||
[HttpPost(Name = "CreateUser")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<CreateUserResponseDto>> CreateUserAsync(CreateUserRequestDto request)
|
||||
{
|
||||
_logger.LogInformation("Creating user with first name {FirstName} and last name {LastName}", request.FirstName, request.LastName);
|
||||
|
||||
ValidationResult? validationResult = await _createUserValidator.ValidateAsync(request);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return BadRequest(validationResult.Errors);
|
||||
}
|
||||
else
|
||||
{
|
||||
CreateUserResponseDto response = await _userService.CreateUserAsync(request);
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using UserManager.Application.Features.Users.Interfaces;
|
||||
using UserManager.Application.Features.Users.Requests;
|
||||
using UserManager.Application.Features.Users.Services;
|
||||
using FluentValidation;
|
||||
|
||||
namespace UserManager.Application
|
||||
{
|
||||
@ -9,6 +11,8 @@ namespace UserManager.Application
|
||||
public static void AddApplication(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
|
||||
services.AddValidatorsFromAssemblyContaining<CreateUserRequestDtoValidator>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,19 @@
|
||||
namespace UserManager.Application.Features.Users.Requests
|
||||
using FluentValidation;
|
||||
|
||||
namespace UserManager.Application.Features.Users.Requests
|
||||
{
|
||||
public class CreateUserRequestDto
|
||||
{
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public int? FavouriteRestaurantId { get; set; }
|
||||
public string? FavouriteRestaurantName { get; set; }
|
||||
}
|
||||
|
||||
public class CreateUserRequestDtoValidator : AbstractValidator<CreateUserRequestDto>
|
||||
{
|
||||
public CreateUserRequestDtoValidator()
|
||||
{
|
||||
RuleFor(x => x.FirstName).NotEmpty().WithMessage("FirstName is required.");
|
||||
RuleFor(x => x.LastName).NotEmpty().WithMessage("LastName is required.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ using UserManager.Domain.Entities;
|
||||
|
||||
namespace UserManager.Application.Features.Users.Services
|
||||
{
|
||||
class UserService(IUnitOfWork unitOfWork) : IUserService
|
||||
public class UserService(IUnitOfWork unitOfWork) : IUserService
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork = unitOfWork;
|
||||
|
||||
@ -18,6 +18,7 @@ namespace UserManager.Application.Features.Users.Services
|
||||
{
|
||||
FirstName = request.FirstName,
|
||||
LastName = request.LastName,
|
||||
CreateAtUtc = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
await _unitOfWork.UserRepository.CreateAsync(user);
|
||||
|
@ -7,10 +7,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Features\Restaurants\" />
|
||||
<Compile Remove="Features\Restaurants\**" />
|
||||
<EmbeddedResource Remove="Features\Restaurants\**" />
|
||||
<None Remove="Features\Restaurants\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="11.10.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.10.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UserManager.Domain.Entities
|
||||
namespace UserManager.Domain.Entities
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public int UserId { get; set; }
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
public DateTime CreateAtUtc { get; set; }
|
||||
public DateTime? UpdatedAtUtc { get; set; }
|
||||
public DateTime? DeletedAtUtc { get; set; }
|
||||
}
|
||||
}
|
@ -7,15 +7,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{74E412ED-9D1
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{5B673FE8-32DA-4CD3-8394-1BD8E1275270}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManager.API", "UserManager.API\UserManager.API.csproj", "{A9A6D6F8-424B-4555-95D9-ECE60837B800}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManager.API", "UserManager.API\UserManager.API.csproj", "{A9A6D6F8-424B-4555-95D9-ECE60837B800}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManager.Domain", "UserManager.Domain\UserManager.Domain.csproj", "{17851242-59E3-4790-9FD8-32B848BBAB3D}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManager.Domain", "UserManager.Domain\UserManager.Domain.csproj", "{17851242-59E3-4790-9FD8-32B848BBAB3D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManager.Infrastructure", "UserManager.Infrastructure\UserManager.Infrastructure.csproj", "{A85D13CB-884E-439D-AAE7-C4C4C2511FDE}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManager.Infrastructure", "UserManager.Infrastructure\UserManager.Infrastructure.csproj", "{A85D13CB-884E-439D-AAE7-C4C4C2511FDE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManager.Application", "UserManager.Application\UserManager.Application.csproj", "{C7215859-A216-4527-A192-7B8F57E50BB4}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManager.Application", "UserManager.Application\UserManager.Application.csproj", "{C7215859-A216-4527-A192-7B8F57E50BB4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManager.Application.UnitTests", "..\tests\UserManager.Application.Tests\UserManager.Application.UnitTests.csproj", "{B0F63D11-6933-4D87-B0D8-6DD7DFC5A23C}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserManager.Application.UnitTests", "..\tests\UserManager.Application.Tests\UserManager.Application.UnitTests.csproj", "{B0F63D11-6933-4D87-B0D8-6DD7DFC5A23C}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManager.Application.IntegrationTests", "..\tests\UserManager.Application.IntegrationTests\UserManager.Application.IntegrationTests.csproj", "{2EDF27EB-CC43-4F38-86A5-BDC1D6974BA5}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -43,6 +45,10 @@ Global
|
||||
{B0F63D11-6933-4D87-B0D8-6DD7DFC5A23C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B0F63D11-6933-4D87-B0D8-6DD7DFC5A23C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B0F63D11-6933-4D87-B0D8-6DD7DFC5A23C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2EDF27EB-CC43-4F38-86A5-BDC1D6974BA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2EDF27EB-CC43-4F38-86A5-BDC1D6974BA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2EDF27EB-CC43-4F38-86A5-BDC1D6974BA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2EDF27EB-CC43-4F38-86A5-BDC1D6974BA5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@ -53,6 +59,7 @@ Global
|
||||
{A85D13CB-884E-439D-AAE7-C4C4C2511FDE} = {74E412ED-9D1B-4597-BBFD-05623C814F98}
|
||||
{C7215859-A216-4527-A192-7B8F57E50BB4} = {74E412ED-9D1B-4597-BBFD-05623C814F98}
|
||||
{B0F63D11-6933-4D87-B0D8-6DD7DFC5A23C} = {5B673FE8-32DA-4CD3-8394-1BD8E1275270}
|
||||
{2EDF27EB-CC43-4F38-86A5-BDC1D6974BA5} = {5B673FE8-32DA-4CD3-8394-1BD8E1275270}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {6E7F90F4-4A09-4CDB-8653-6E094E164853}
|
||||
|
@ -0,0 +1,63 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UserManager.Application.Features.Users.Requests;
|
||||
using UserManager.Application.Features.Users.Responses;
|
||||
using UserManager.Application.Features.Users.Services;
|
||||
using UserManager.Domain.Entities;
|
||||
using UserManager.Infrastructure;
|
||||
|
||||
namespace UserManager.Application.IntegrationTests.Features.Users
|
||||
{
|
||||
[TestClass]
|
||||
public class UserServiceIntegrationTests
|
||||
{
|
||||
|
||||
private UserManagerContext _context = null!;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
DbContextOptions<UserManagerContext> options = new DbContextOptionsBuilder<UserManagerContext>()
|
||||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
||||
.Options;
|
||||
|
||||
_context = new UserManagerContext(options);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_context.Dispose();
|
||||
_context = null!;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateUserAsync_ShouldAddUserToDatabase_WhenCreateUserCalled()
|
||||
{
|
||||
// Arrange
|
||||
var request = new CreateUserRequestDto()
|
||||
{
|
||||
FirstName = "John",
|
||||
LastName = "Doe",
|
||||
};
|
||||
|
||||
UnitOfWork unitOfWork = new(_context);
|
||||
UserService userService = new(unitOfWork);
|
||||
|
||||
// Act
|
||||
CreateUserResponseDto responseDto = await userService.CreateUserAsync(request);
|
||||
|
||||
// Assert
|
||||
User user = _context.Users.First();
|
||||
Assert.AreEqual(1, _context.Users.Count());
|
||||
Assert.AreEqual("John", user.FirstName);
|
||||
Assert.AreEqual("John", responseDto.FirstName);
|
||||
Assert.AreEqual(1, user.UserId);
|
||||
Assert.AreEqual(1, responseDto.UserId);
|
||||
Assert.IsTrue(user.CreateAtUtc > DateTime.UtcNow.AddSeconds(-1));
|
||||
Assert.IsTrue(user.CreateAtUtc < DateTime.UtcNow.AddSeconds(1));
|
||||
Assert.IsNull(user.UpdatedAtUtc);
|
||||
Assert.IsNull(user.DeletedAtUtc);
|
||||
Assert.IsFalse(user.IsDeleted);
|
||||
}
|
||||
}
|
||||
}
|
11
tests/UserManager.Application.IntegrationTests/UnitTest1.cs
Normal file
11
tests/UserManager.Application.IntegrationTests/UnitTest1.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace UserManager.Application.IntegrationTests
|
||||
{
|
||||
[TestClass]
|
||||
public class UnitTest1
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestMethod1()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\UserManager.Infrastructure\UserManager.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,46 @@
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using UserManager.Application.Features.Users.Requests;
|
||||
|
||||
namespace UserManager.Application.UnitTests.Features.Users
|
||||
{
|
||||
[TestClass]
|
||||
public class CreateUserRequestDtoValidationUnitTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void CreateUserRequestDtoValidator_WhenFirstNameIsEmpty_ShouldReturnValidationError()
|
||||
{
|
||||
// Arrange
|
||||
CreateUserRequestDto request = new()
|
||||
{
|
||||
FirstName = string.Empty,
|
||||
LastName = "Doe"
|
||||
};
|
||||
|
||||
// Act
|
||||
ValidationResult validationResult = new CreateUserRequestDtoValidator().Validate(request);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(validationResult.IsValid);
|
||||
Assert.AreEqual("FirstName is required.", validationResult.Errors[0].ErrorMessage);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CreateUserRequestDtoValidator_WhenLastNameIsEmpty_ShouldReturnValidationError()
|
||||
{
|
||||
// Arrange
|
||||
CreateUserRequestDto request = new()
|
||||
{
|
||||
FirstName = "John",
|
||||
LastName = string.Empty
|
||||
};
|
||||
|
||||
// Act
|
||||
ValidationResult validationResult = new CreateUserRequestDtoValidator().Validate(request);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(validationResult.IsValid);
|
||||
Assert.AreEqual("LastName is required.", validationResult.Errors[0].ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using UserManager.Application.Features.Users.Requests;
|
||||
using UserManager.Application.Interfaces;
|
||||
using UserManager.Application.Features.Users.Services;
|
||||
using UserManager.Application.Features.Users.Responses;
|
||||
using UserManager.Domain.Entities;
|
||||
|
||||
namespace UserManager.Application.UnitTests.Features.Users
|
||||
{
|
||||
[TestClass]
|
||||
public class UserServiceUnitTests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task CreateUserAsync_ShouldCallAddAndSave_WhenCreateUserCalled()
|
||||
{
|
||||
// Arrange
|
||||
Mock<IUnitOfWork> unitOfWork = new();
|
||||
Mock<IUserRepository> userRepository = new();
|
||||
unitOfWork.SetupGet(u => u.UserRepository).Returns(userRepository.Object);
|
||||
var request = new CreateUserRequestDto()
|
||||
{
|
||||
FirstName = "John",
|
||||
LastName = "Doe",
|
||||
};
|
||||
|
||||
UserService userService = new(unitOfWork.Object);
|
||||
|
||||
// Act
|
||||
CreateUserResponseDto responseDto = await userService.CreateUserAsync(request);
|
||||
|
||||
// Assert
|
||||
unitOfWork.Verify(u => u.UserRepository.CreateAsync(It.IsAny<User>()), Times.Once);
|
||||
unitOfWork.Verify(u => u.SaveChangesAsync(), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,18 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\UserManager.Application\UserManager.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user