From bff5c8a7b73a3d4e2f8d1f8a17d2294f32092644 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 22:21:51 +0700 Subject: [PATCH 1/3] add: integration test --- .../Requests/Users/UpdateUserRequest.cs | 4 -- src/Api/Controllers/UsersController.cs | 1 - src/Application/Users/Commands/UpdateUser.cs | 1 - .../Users/Commands/UpdateUserTests.cs | 60 +++++++++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/Application.Tests.Integration/Users/Commands/UpdateUserTests.cs diff --git a/src/Api/Controllers/Payload/Requests/Users/UpdateUserRequest.cs b/src/Api/Controllers/Payload/Requests/Users/UpdateUserRequest.cs index d162c28a..c30e9871 100644 --- a/src/Api/Controllers/Payload/Requests/Users/UpdateUserRequest.cs +++ b/src/Api/Controllers/Payload/Requests/Users/UpdateUserRequest.cs @@ -14,10 +14,6 @@ public class UpdateUserRequest /// public string? LastName { get; set; } /// - /// New role of the user to be updated - /// - public string Role { get; set; } = null!; - /// /// New position of the user to be updated /// public string? Position { get; set; } diff --git a/src/Api/Controllers/UsersController.cs b/src/Api/Controllers/UsersController.cs index dc2b406e..cc0c4ccc 100644 --- a/src/Api/Controllers/UsersController.cs +++ b/src/Api/Controllers/UsersController.cs @@ -141,7 +141,6 @@ public async Task>> Update([FromRoute] Guid userId, UserId = userId, FirstName = request.FirstName, LastName = request.LastName, - Role = request.Role, Position = request.Position, }; var result = await Mediator.Send(command); diff --git a/src/Application/Users/Commands/UpdateUser.cs b/src/Application/Users/Commands/UpdateUser.cs index 4d894cc0..e339eb88 100644 --- a/src/Application/Users/Commands/UpdateUser.cs +++ b/src/Application/Users/Commands/UpdateUser.cs @@ -10,7 +10,6 @@ public record Command : IRequest public Guid UserId { get; init; } public string? FirstName { get; init; } public string? LastName { get; init; } - public string Role { get; init; } = null!; public string? Position { get; init; } } } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Users/Commands/UpdateUserTests.cs b/tests/Application.Tests.Integration/Users/Commands/UpdateUserTests.cs new file mode 100644 index 00000000..3214a6bd --- /dev/null +++ b/tests/Application.Tests.Integration/Users/Commands/UpdateUserTests.cs @@ -0,0 +1,60 @@ +using Application.Identity; +using Application.Users.Commands; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Users.Commands; + +public class UpdateUserTests : BaseClassFixture +{ + public UpdateUserTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldUpdateUser_WhenUpdateDetailsAreValid() + { + // Arrange + var user = CreateUser(IdentityData.Roles.Employee, "randompassword"); + await AddAsync(user); + + var command = new UpdateUser.Command() + { + UserId = user.Id, + FirstName = "khoa", + LastName = "ngu", + Position = "IDK", + }; + + // Act + var result = await SendAsync(command); + + // Assert + result.FirstName.Should().Be(command.FirstName); + result.LastName.Should().Be(command.LastName); + result.Position.Should().Be(command.Position); + + // Cleanup + Remove(user); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatUserDoesNotExist() + { + // Arrange + var command = new UpdateUser.Command() + { + UserId = Guid.NewGuid(), + FirstName = "khoa", + LastName = "ngu", + Position = "IDK", + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("User does not exist."); + } +} \ No newline at end of file From 99d8042e3f724305b71a4ab8d2ba47431291fa57 Mon Sep 17 00:00:00 2001 From: Vzart Date: Tue, 30 May 2023 20:05:32 +0700 Subject: [PATCH 2/3] feat: update user --- src/Application/Users/Commands/UpdateUser.cs | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Application/Users/Commands/UpdateUser.cs b/src/Application/Users/Commands/UpdateUser.cs index e339eb88..3d35a05b 100644 --- a/src/Application/Users/Commands/UpdateUser.cs +++ b/src/Application/Users/Commands/UpdateUser.cs @@ -1,5 +1,8 @@ +using Application.Common.Interfaces; using Application.Users.Queries; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Users.Commands; @@ -12,4 +15,35 @@ public record Command : IRequest public string? LastName { get; init; } public string? Position { get; init; } } + + public class CommandHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public CommandHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Command request, CancellationToken cancellationToken) + { + var user = await _context.Users + .FirstOrDefaultAsync(x => x.Id.Equals(request.UserId), cancellationToken: cancellationToken); + + if (user is null) + { + throw new KeyNotFoundException("User does not exist."); + } + + user.FirstName = request.FirstName; + user.LastName = request.LastName; + user.Position = request.Position; + + var result = _context.Users.Update(user); + await _context.SaveChangesAsync(cancellationToken); + return _mapper.Map(result.Entity); + } + } } \ No newline at end of file From 7658d27b479495b07aae11ffea70078bebb1828c Mon Sep 17 00:00:00 2001 From: Vzart Date: Thu, 1 Jun 2023 19:09:19 +0700 Subject: [PATCH 3/3] refactor: add validator --- src/Application/Users/Commands/UpdateUser.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Application/Users/Commands/UpdateUser.cs b/src/Application/Users/Commands/UpdateUser.cs index 3d35a05b..00b27531 100644 --- a/src/Application/Users/Commands/UpdateUser.cs +++ b/src/Application/Users/Commands/UpdateUser.cs @@ -1,6 +1,7 @@ using Application.Common.Interfaces; using Application.Users.Queries; using AutoMapper; +using FluentValidation; using MediatR; using Microsoft.EntityFrameworkCore; @@ -8,6 +9,22 @@ namespace Application.Users.Commands; public class UpdateUser { + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.FirstName) + .MaximumLength(50).WithMessage("FirstName can not exceed 50 characters."); + + RuleFor(x => x.LastName) + .MaximumLength(50).WithMessage("LastName can not exceed 50 characters."); + + RuleFor(x => x.Position) + .MaximumLength(64).WithMessage("Position can not exceed 64 characters."); + } + } public record Command : IRequest { public Guid UserId { get; init; }