From 7648784d8938ff8bb091c9b71d12e5f94bb5e151 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 22:27:27 +0700 Subject: [PATCH 1/3] add: integration test --- .../Users/Queries/GetUserByIdTests.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs diff --git a/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs b/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs new file mode 100644 index 00000000..053c0a7f --- /dev/null +++ b/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs @@ -0,0 +1,59 @@ +using Application.Identity; +using Application.Users.Queries; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Users.Queries; + +public class GetUserByIdTests : BaseClassFixture +{ + public GetUserByIdTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldReturnUser_WhenThatUserExists() + { + // Arrange + var user = CreateUser(IdentityData.Roles.Employee, "randomPassword"); + await AddAsync(user); + + var query = new GetUserById.Query() + { + UserId = user.Id, + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.Username.Should().Be(user.Username); + result.Email.Should().Be(user.Email); + result.FirstName.Should().Be(user.FirstName); + result.LastName.Should().Be(user.LastName); + result.Role.Should().Be(user.Role); + result.Position.Should().Be(user.Position); + result.IsActivated.Should().Be(user.IsActivated); + result.IsActive.Should().Be(user.IsActive); + + // Cleanup + Remove(user); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatUserDoesNotExist() + { + // Arrange + var query = new GetUserById.Query() + { + UserId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("User does not exist"); + } +} \ No newline at end of file From 7466986a7eb78783c009f5caa2b61e5b1c58a409 Mon Sep 17 00:00:00 2001 From: Vzart Date: Tue, 30 May 2023 19:53:48 +0700 Subject: [PATCH 2/3] feat: get user by id --- src/Application/Users/Queries/GetUserById.cs | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Application/Users/Queries/GetUserById.cs b/src/Application/Users/Queries/GetUserById.cs index e34b58d2..c06e28f6 100644 --- a/src/Application/Users/Queries/GetUserById.cs +++ b/src/Application/Users/Queries/GetUserById.cs @@ -1,4 +1,7 @@ +using Application.Common.Interfaces; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Users.Queries; @@ -8,4 +11,29 @@ public record Query : IRequest { public Guid UserId { get; init; } } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Query 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"); + } + + return _mapper.Map(user); + } + } } \ No newline at end of file From 1acf389a53c798c58b63c5c9b73563325387a62f Mon Sep 17 00:00:00 2001 From: Vzart Date: Thu, 1 Jun 2023 19:02:18 +0700 Subject: [PATCH 3/3] refactor: added a dot for convention --- src/Application/Users/Queries/GetUserById.cs | 2 +- .../Users/Queries/GetUserByIdTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Application/Users/Queries/GetUserById.cs b/src/Application/Users/Queries/GetUserById.cs index c06e28f6..c4cd1278 100644 --- a/src/Application/Users/Queries/GetUserById.cs +++ b/src/Application/Users/Queries/GetUserById.cs @@ -30,7 +30,7 @@ public async Task Handle(Query request, CancellationToken cancellationT if (user is null) { - throw new KeyNotFoundException("User does not exist"); + throw new KeyNotFoundException("User does not exist."); } return _mapper.Map(user); diff --git a/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs b/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs index 053c0a7f..f6a758be 100644 --- a/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs +++ b/tests/Application.Tests.Integration/Users/Queries/GetUserByIdTests.cs @@ -54,6 +54,6 @@ public async Task ShouldThrowKeyNotFoundException_WhenThatUserDoesNotExist() // Assert await action.Should().ThrowAsync() - .WithMessage("User does not exist"); + .WithMessage("User does not exist."); } } \ No newline at end of file