< Summary

Information
Class: Anichron.API.Infrastructure.EndpointResponses
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/EndpointResponses.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 80
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
RequireUser(...)100%11100%
RequireUser(...)100%11100%
RequireUserAllowedWhileMustChangePassword(...)100%11100%
RequireAdmin(...)100%11100%
ProducesRefusals(...)100%11100%
RequireRateLimit(...)100%11100%
ProducesWithoutBody(...)100%11100%
DescribeErrorsAsProblems(...)100%66100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/EndpointResponses.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Http.Metadata;
 3using Microsoft.AspNetCore.Mvc;
 4
 5namespace Anichron.API.Infrastructure;
 6
 7/// <summary>
 8/// Requirements that declare the responses they cause, and the rule that describes every error
 9/// without a body as the Problem Details the status code pages write for it.
 10/// </summary>
 11internal static class EndpointResponses
 12{
 13    internal const string ProblemContentType = "application/problem+json";
 14
 15    extension<TBuilder>(TBuilder builder) where TBuilder : IEndpointConventionBuilder
 16    {
 17        /// <summary>A signed-in user, refused while their password must change.</summary>
 18        internal TBuilder RequireUser()
 15619            => builder.RequireAuthorization().ProducesRefusals();
 20
 21        /// <summary>A signed-in user under <paramref name="policy"/>, which refuses them while their password must chan
 22        /// <param name="policy">The policy that names the schemes the route accepts.</param>
 23        internal TBuilder RequireUser(AuthorizationPolicy policy)
 6624            => builder.RequireAuthorization(policy).ProducesRefusals();
 25
 26        internal TBuilder RequireUserAllowedWhileMustChangePassword()
 23027            => builder.RequireAuthorization()
 23028                      .WithMetadata(new AllowedWhileMustChangePasswordAttribute())
 23029                      .ProducesProblem(StatusCodes.Status401Unauthorized);
 30
 31        internal TBuilder RequireAdmin()
 15132            => builder.RequireAuthorization(AuthPolicies.Admin).ProducesRefusals();
 33
 34        private TBuilder ProducesRefusals()
 37335            => builder.ProducesProblem(StatusCodes.Status401Unauthorized)
 37336                      .ProducesProblem(StatusCodes.Status403Forbidden);
 37
 38        internal TBuilder RequireRateLimit(string policy)
 81839            => builder.RequireRateLimiting(policy).ProducesProblem(StatusCodes.Status429TooManyRequests);
 40
 41        /// <summary>An error the framework answers with an empty body that the status code pages leave alone.</summary>
 42        /// <param name="statusCode">The response status.</param>
 43        internal TBuilder ProducesWithoutBody(int statusCode)
 6644            => builder.WithMetadata(
 6645                new ProducesResponseTypeMetadata(statusCode, typeof(void)), new ResponseWithoutBody(statusCode));
 46    }
 47
 48    /// <summary>
 49    /// Adds the 503 every endpoint answers while the database is unreachable and the 400 and 415 a
 50    /// request body can fail to bind with, then describes every error response still declared
 51    /// without a body as Problem Details.
 52    /// </summary>
 53    /// <param name="endpoint">The endpoint, after all its other conventions.</param>
 54    internal static void DescribeErrorsAsProblems(EndpointBuilder endpoint)
 120155    {
 120156        var metadata = endpoint.Metadata;
 120157        metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status503ServiceUnavailable));
 120158        if (metadata.OfType<IAcceptsMetadata>().Any())
 44059        {
 44060            var undeclared = new[] { StatusCodes.Status400BadRequest, StatusCodes.Status415UnsupportedMediaType }
 44061                .Where(status => !metadata.OfType<IProducesResponseTypeMetadata>().Any(declared => declared.StatusCode =
 44062                .ToList();
 292063            foreach (var status in undeclared)
 80064                metadata.Add(new ProducesResponseTypeMetadata(status));
 44065        }
 66
 120167        var bodiless = metadata.OfType<IProducesResponseTypeMetadata>()
 120168            .GroupBy(declared => declared.StatusCode)
 120169            .Select(declarations => declarations.Last())
 120170            .Where(published => published.StatusCode >= StatusCodes.Status400BadRequest
 120171                                && (published.Type is null || published.Type == typeof(void))
 120172                                && !metadata.OfType<ResponseWithoutBody>().Any(kept => kept.StatusCode == published.Stat
 120173            .Select(published => published.StatusCode)
 120174            .ToList();
 920775        foreach (var status in bodiless)
 280276            metadata.Add(new ProducesResponseTypeMetadata(status, typeof(ProblemDetails), [ProblemContentType]));
 120177    }
 78}
 79
 80internal sealed record ResponseWithoutBody(int StatusCode);