< Summary

Information
Class: Anichron.API.Endpoints.UserProfileResponse
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/UserEndpoints.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 3
Uncovered lines: 0
Coverable lines: 3
Total lines: 65
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
From(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/UserEndpoints.cs

#LineLine coverage
 1using Anichron.API.Infrastructure;
 2using Anichron.API.Services;
 3using Anichron.Core.Data.Repository;
 4using Anichron.Core.Domain;
 5using System.Security.Claims;
 6
 7namespace Anichron.API.Endpoints;
 8
 9public static class UserEndpoints
 10{
 11    public static IEndpointRouteBuilder MapUserEndpoints(this IEndpointRouteBuilder app)
 12    {
 13        var group = app.MapGroup(ApiPaths.Users.Group).WithTags("Users");
 14        group.MapGet(ApiPaths.Users.Me, GetMeAsync)
 15             .RequireUserAllowedWhileMustChangePassword()
 16             .Produces<UserProfileResponse>()
 17             .Produces(StatusCodes.Status404NotFound);
 18        group.MapPost(ApiPaths.Users.ChangePassword, ChangePasswordAsync)
 19             .RequireUserAllowedWhileMustChangePassword()
 20             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 21             .Produces<SessionResponse>()
 22             .ProducesValidationProblem()
 23             .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity);
 24        return app;
 25    }
 26
 27    internal static async Task<IResult> GetMeAsync(
 28        ClaimsPrincipal user,
 29        IUserRepository users,
 30        IUserStorageConfigRepository storageConfigs,
 31        CancellationToken ct)
 32    {
 33        if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId))
 34            return Results.Unauthorized();
 35
 36        var found = await users.FindByIdAsync(userId, ct);
 37        if (found is null)
 38            return Results.NotFound();
 39
 40        return Results.Ok(UserProfileResponse.From(found, await storageConfigs.AnyActiveByUserIdAsync(userId, ct)));
 41    }
 42
 43    internal static async Task<IResult> ChangePasswordAsync(
 44        ChangePasswordRequest req,
 45        ClaimsPrincipal user,
 46        HttpContext http,
 47        IAuthService auth,
 48        ISessionResponseMapper mapper,
 49        CancellationToken ct)
 50    {
 51        if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId))
 52            return Results.Unauthorized();
 53
 54        var result = await auth.ChangePasswordAsync(userId, req.CurrentPassword, req.NewPassword, ct);
 55        return mapper.GetChangePasswordResult(result, http);
 56    }
 57}
 58
 59public sealed record ChangePasswordRequest(string CurrentPassword, string NewPassword);
 3360public sealed record UserProfileResponse(
 3361    Guid Id, string Username, string Email, bool IsAdmin, bool MustChangePassword, bool HasStorageConfig)
 62{
 63    internal static UserProfileResponse From(User user, bool hasStorageConfig)
 3164        => new(user.Id, user.Username, user.Email, user.IsAdmin, user.MustChangePassword, hasStorageConfig);
 65}