-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/get-user-profile' into develop
- Loading branch information
Showing
13 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/MyRecipes.Application/UseCases/User/Profile/GetUserProfileUseCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using AutoMapper; | ||
using MyRecipes.Communication.Responses; | ||
using MyRecipes.Domain.Services.LoggedUser; | ||
|
||
namespace MyRecipes.Application.UseCases.User.Profile; | ||
internal class GetUserProfileUseCase : IGetUserProfileUseCase | ||
{ | ||
private readonly ILoggedUser _loggedUser; | ||
private readonly IMapper _mapper; | ||
|
||
public GetUserProfileUseCase(ILoggedUser loggedUser, IMapper mapper) | ||
{ | ||
_loggedUser = loggedUser; | ||
_mapper = mapper; | ||
} | ||
|
||
public async Task<ResponseUserProfileJson> Execute() | ||
{ | ||
var user = await _loggedUser.User(); | ||
|
||
return _mapper.Map<ResponseUserProfileJson>(user); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/MyRecipes.Application/UseCases/User/Profile/IGetUserProfileUseCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using MyRecipes.Communication.Responses; | ||
|
||
namespace MyRecipes.Application.UseCases.User.Profile; | ||
public interface IGetUserProfileUseCase | ||
{ | ||
public Task<ResponseUserProfileJson> Execute(); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/MyRecipes.Communication/Responses/ResponseUserProfileJson.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MyRecipes.Communication.Responses; | ||
public class ResponseUserProfileJson | ||
{ | ||
public string Name { get; set; } = string.Empty; | ||
public string Email { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
tests/CommonTestsUtilities/LoggedUser/LoggedUserBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Moq; | ||
using MyRecipes.Domain.Entities; | ||
using MyRecipes.Domain.Services.LoggedUser; | ||
|
||
namespace CommonTestsUtilities.LoggedUser; | ||
public class LoggedUserBuilder | ||
{ | ||
public static ILoggedUser Build(User user) | ||
{ | ||
var mock = new Mock<ILoggedUser>(); | ||
|
||
mock.Setup(x => x.User()).ReturnsAsync(user); | ||
|
||
return mock.Object; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/UseCases.Tests/User/Profile/GetUserProfileUseCaseTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using CommonTestsUtilities.Entities; | ||
using CommonTestsUtilities.LoggedUser; | ||
using CommonTestsUtilities.Mapper; | ||
using FluentAssertions; | ||
using MyRecipes.Application.UseCases.User.Profile; | ||
|
||
namespace UseCases.Tests.User.Profile; | ||
public class GetUserProfileUseCaseTest | ||
{ | ||
[Fact] | ||
public async Task Success() | ||
{ | ||
var (user, _) = UserBuilder.Build(); | ||
|
||
var useCase = CreateUseCase(user); | ||
|
||
var response = await useCase.Execute(); | ||
|
||
response.Should().NotBeNull(); | ||
response.Name.Should().Be(user.Name); | ||
response.Email.Should().Be(user.Email); | ||
} | ||
|
||
private static GetUserProfileUseCase CreateUseCase(MyRecipes.Domain.Entities.User user) | ||
{ | ||
var mapper = MapperBuilder.Build(); | ||
var loggedUser = LoggedUserBuilder.Build(user); | ||
|
||
return new GetUserProfileUseCase(loggedUser, mapper); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/WebApi.Tests/User/Profile/GetUserProfileInvalidTokenTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using CommonTestsUtilities.Tokens; | ||
using FluentAssertions; | ||
using System.Net; | ||
|
||
namespace WebApi.Tests.User.Profile; | ||
public class GetUserProfileInvalidTokenTest : MyRecipesClassFixture | ||
{ | ||
private readonly string _method = "users"; | ||
|
||
public GetUserProfileInvalidTokenTest(CustomWebApplicationFactory factory) : base(factory) { } | ||
|
||
[Fact] | ||
public async Task Error_Token_Invalid() | ||
{ | ||
var response = await DoGet(_method, "invalidToken"); | ||
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); | ||
} | ||
|
||
[Fact] | ||
public async Task Error_Without_Token() | ||
{ | ||
var response = await DoGet(_method, string.Empty); | ||
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); | ||
} | ||
|
||
[Fact] | ||
public async Task Error_Token_With_User_NotFound() | ||
{ | ||
var token = JwtTokenGeneratorBuilder.Build().Generate(Guid.NewGuid()); | ||
var response = await DoGet(_method, token); | ||
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using CommonTestsUtilities.Tokens; | ||
using FluentAssertions; | ||
using System.Net; | ||
using System.Text.Json; | ||
|
||
namespace WebApi.Tests.User.Profile; | ||
public class GetUserProfileTest : MyRecipesClassFixture | ||
{ | ||
private readonly string _method = "users"; | ||
|
||
private readonly string _name; | ||
private readonly string _email; | ||
private readonly Guid _userIdentifier; | ||
|
||
public GetUserProfileTest(CustomWebApplicationFactory factory) : base(factory) | ||
{ | ||
_name = factory.GetName(); | ||
_email = factory.GetEmail(); | ||
_userIdentifier = factory.GetUserIdentifier(); | ||
} | ||
|
||
[Fact] | ||
public async Task Success() | ||
{ | ||
var token = JwtTokenGeneratorBuilder.Build().Generate(_userIdentifier); | ||
|
||
var response = await DoGet(_method, token); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
|
||
using var responseBody = await response.Content.ReadAsStreamAsync(); | ||
|
||
var responseData = await JsonDocument.ParseAsync(responseBody); | ||
|
||
responseData.RootElement.GetProperty("name").GetString().Should().NotBeNullOrWhiteSpace().And.Be(_name); | ||
responseData.RootElement.GetProperty("email").GetString().Should().NotBeNullOrWhiteSpace().And.Be(_email); | ||
} | ||
} |