< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MapAdminEndpoints(...)100%11100%
CreateUserAsync()100%11100%
ResetUserPasswordAsync()100%11100%
GetUsersAsync()100%11100%
GetUserAsync()100%11100%
PatchUserAsync()100%22100%
DeleteUserAsync()100%22100%
GetStorageConfigsAsync()100%11100%
CreateStorageConfigAsync()100%11100%
DeleteStorageConfigAsync()100%11100%

File(s)

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

#LineLine coverage
 1using Anichron.API.Infrastructure;
 2using Anichron.API.Services;
 3using System.Security.Claims;
 4
 5namespace Anichron.API.Endpoints;
 6
 7public static class AdminEndpoints
 8{
 9    public static IEndpointRouteBuilder MapAdminEndpoints(this IEndpointRouteBuilder app)
 5010    {
 5011        var group = app.MapGroup(ApiPaths.Users.Group).WithTags("Users")
 5012                       .RequireAdmin();
 5013        group.MapPost(string.Empty, CreateUserAsync)
 5014             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 5015             .Produces<AdminCreatedUserResponse>(StatusCodes.Status201Created)
 5016             .ProducesValidationProblem(StatusCodes.Status409Conflict)
 5017             .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity);
 5018        group.MapPost(ApiPaths.Users.PasswordReset, ResetUserPasswordAsync)
 5019             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 5020             .Produces<AdminPasswordResetResponse>()
 5021             .Produces(StatusCodes.Status404NotFound);
 5022        group.MapGet(string.Empty, GetUsersAsync)
 5023             .Produces<List<AdminUserResponse>>();
 5024        group.MapGet(ApiPaths.Users.ById, GetUserAsync)
 5025             .Produces<AdminUserResponse>()
 5026             .Produces(StatusCodes.Status404NotFound);
 5027        group.MapPatch(ApiPaths.Users.ById, PatchUserAsync)
 5028             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 5029             .Produces<AdminUserResponse>()
 5030             .Produces(StatusCodes.Status404NotFound);
 5031        group.MapDelete(ApiPaths.Users.ById, DeleteUserAsync)
 5032             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 5033             .Produces(StatusCodes.Status204NoContent)
 5034             .Produces(StatusCodes.Status404NotFound);
 5035        group.MapGet(ApiPaths.Users.UserStorageConfigs, GetStorageConfigsAsync)
 5036             .Produces<List<AdminStorageConfigResponse>>()
 5037             .Produces(StatusCodes.Status404NotFound);
 5038        group.MapPost(ApiPaths.Users.UserStorageConfigs, CreateStorageConfigAsync)
 5039             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 5040             .Produces<AdminStorageConfigResponse>(StatusCodes.Status201Created)
 5041             .Produces(StatusCodes.Status404NotFound)
 5042             .ProducesValidationProblem(StatusCodes.Status409Conflict)
 5043             .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity);
 5044        group.MapDelete(ApiPaths.Users.UserStorageConfigById, DeleteStorageConfigAsync)
 5045             .RequireRateLimit(AuthRateLimitPolicies.Sensitive)
 5046             .Produces(StatusCodes.Status204NoContent)
 5047             .Produces(StatusCodes.Status404NotFound);
 5048        return app;
 5049    }
 50
 51    internal static async Task<IResult> CreateUserAsync(
 52        CreateAdminUserRequest request,
 53        IAuthService auth,
 54        IAdminResponseMapper mapper,
 55        CancellationToken ct)
 656    {
 657        var result = await auth.AdminCreateUserAsync(request.Username, request.Email, ct);
 658        return mapper.GetCreateUserResult(result);
 659    }
 60
 61    internal static async Task<IResult> ResetUserPasswordAsync(
 62        Guid userId,
 63        IAdminResetService adminReset,
 64        IAdminResponseMapper mapper,
 65        CancellationToken ct)
 166    {
 167        var result = await adminReset.ResetUserPasswordAsync(userId, ct);
 168        return mapper.GetResetPasswordResult(result);
 169    }
 70
 71    internal static async Task<IResult> GetUsersAsync(
 72        IAdminUserService adminUsers,
 73        IAdminResponseMapper mapper,
 74        CancellationToken ct)
 175    {
 176        var users = await adminUsers.GetAllAsync(ct);
 177        return mapper.GetUsersResult(users);
 178    }
 79
 80    internal static async Task<IResult> GetUserAsync(
 81        Guid userId,
 82        IAdminUserService adminUsers,
 83        IAdminResponseMapper mapper,
 84        CancellationToken ct)
 185    {
 186        var user = await adminUsers.GetByIdAsync(userId, ct);
 187        return mapper.GetUserResult(user);
 188    }
 89
 90    internal static async Task<IResult> PatchUserAsync(
 91        Guid userId,
 92        PatchAdminUserRequest request,
 93        ClaimsPrincipal caller,
 94        IAdminUserService adminUsers,
 95        IAdminResponseMapper mapper,
 96        CancellationToken ct)
 497    {
 498        if (!Guid.TryParse(caller.FindFirstValue(ClaimTypes.NameIdentifier), out var callerId))
 199            return Results.Unauthorized();
 100
 3101        var result = await adminUsers.UpdateAsync(callerId, userId, request.IsAdmin, request.IsDisabled, ct);
 3102        return mapper.GetPatchUserResult(result);
 4103    }
 104
 105    internal static async Task<IResult> DeleteUserAsync(
 106        Guid userId,
 107        ClaimsPrincipal caller,
 108        IAdminUserService adminUsers,
 109        IAdminResponseMapper mapper,
 110        CancellationToken ct)
 4111    {
 4112        if (!Guid.TryParse(caller.FindFirstValue(ClaimTypes.NameIdentifier), out var callerId))
 1113            return Results.Unauthorized();
 114
 3115        var result = await adminUsers.DeleteAsync(callerId, userId, ct);
 3116        return mapper.GetDeleteUserResult(result);
 4117    }
 118
 119    internal static async Task<IResult> GetStorageConfigsAsync(
 120        Guid userId,
 121        IAdminStorageConfigService adminStorageConfigs,
 122        IAdminResponseMapper mapper,
 123        CancellationToken ct)
 1124    {
 1125        var result = await adminStorageConfigs.GetByUserIdAsync(userId, ct);
 1126        return mapper.GetStorageConfigsResult(result);
 1127    }
 128
 129    internal static async Task<IResult> CreateStorageConfigAsync(
 130        Guid userId,
 131        CreateStorageConfigRequest request,
 132        IAdminStorageConfigService adminStorageConfigs,
 133        IAdminResponseMapper mapper,
 134        CancellationToken ct)
 5135    {
 5136        var result = await adminStorageConfigs.AddAsync(userId, request.RootPath, ct);
 5137        return mapper.GetCreateStorageConfigResult(result);
 5138    }
 139
 140    internal static async Task<IResult> DeleteStorageConfigAsync(
 141        Guid userId,
 142        Guid configId,
 143        IAdminStorageConfigService adminStorageConfigs,
 144        IAdminResponseMapper mapper,
 145        CancellationToken ct)
 1146    {
 1147        var result = await adminStorageConfigs.DeleteAsync(userId, configId, ct);
 1148        return mapper.GetDeleteStorageConfigResult(result);
 1149    }
 150}
 151
 152public sealed record CreateAdminUserRequest(string Username, string Email);
 153public sealed record AdminCreatedUserResponse(Guid Id, string Username, string Email, string TemporaryPassword);
 154public sealed record AdminPasswordResetResponse(string TemporaryPassword);
 155public sealed record AdminUserResponse(Guid Id, string Username, string Email, bool IsAdmin, bool IsDisabled, int Storag
 156public sealed record PatchAdminUserRequest(bool? IsAdmin, bool? IsDisabled);
 157public sealed record CreateStorageConfigRequest(string RootPath);
 158public sealed record AdminStorageConfigResponse(Guid Id, Guid UserId, string RootPath, bool IsActive);