< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MapUserEndpoints(...)100%11100%
GetMeAsync()100%44100%
ChangePasswordAsync()100%22100%

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)
 7412    {
 7413        var group = app.MapGroup(ApiPaths.Users.Group).WithTags("Users");
 7414        group.MapGet(ApiPaths.Users.Me, GetMeAsync)
 7415             .RequireUserAllowedWhileMustChangePassword()
 7416             .Produces<UserProfileResponse>()
 7417             .Produces(StatusCodes.Status404NotFound);
 7418        group.MapPost(ApiPaths.Users.ChangePassword, ChangePasswordAsync)
 7419             .RequireUserAllowedWhileMustChangePassword()
 7420             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 7421             .Produces<SessionResponse>()
 7422             .ProducesValidationProblem()
 7423             .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity);
 7424        return app;
 7425    }
 26
 27    internal static async Task<IResult> GetMeAsync(
 28        ClaimsPrincipal user,
 29        IUserRepository users,
 30        IUserStorageConfigRepository storageConfigs,
 31        CancellationToken ct)
 532    {
 533        if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId))
 234            return Results.Unauthorized();
 35
 336        var found = await users.FindByIdAsync(userId, ct);
 337        if (found is null)
 138            return Results.NotFound();
 39
 240        return Results.Ok(UserProfileResponse.From(found, await storageConfigs.AnyActiveByUserIdAsync(userId, ct)));
 541    }
 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)
 1150    {
 1151        if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId))
 252            return Results.Unauthorized();
 53
 954        var result = await auth.ChangePasswordAsync(userId, req.CurrentPassword, req.NewPassword, ct);
 955        return mapper.GetChangePasswordResult(result, http);
 1156    }
 57}
 58
 59public sealed record ChangePasswordRequest(string CurrentPassword, string NewPassword);
 60public sealed record UserProfileResponse(
 61    Guid Id, string Username, string Email, bool IsAdmin, bool MustChangePassword, bool HasStorageConfig)
 62{
 63    internal static UserProfileResponse From(User user, bool hasStorageConfig)
 64        => new(user.Id, user.Username, user.Email, user.IsAdmin, user.MustChangePassword, hasStorageConfig);
 65}