| | | 1 | | using AsiBackbone.AspNetCore.Endpoints; |
| | | 2 | | using AsiBackbone.Core.Decisions; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Testing.Contracts; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Reusable contract fixture for <see cref="IAsiBackboneEndpointCapabilityGrantValidator" /> implementations. |
| | | 9 | | /// </summary> |
| | | 10 | | public abstract class AsiBackboneEndpointCapabilityGrantValidatorContract |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Creates the capability-grant validator implementation under test. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <returns>The capability-grant validator implementation to validate.</returns> |
| | | 16 | | protected abstract IAsiBackboneEndpointCapabilityGrantValidator CreateValidator(); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates the HTTP context supplied to the validator implementation under test. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <returns>The HTTP context to validate with.</returns> |
| | | 22 | | protected virtual HttpContext CreateHttpContext() |
| | | 23 | | { |
| | 14 | 24 | | return new DefaultHttpContext |
| | 14 | 25 | | { |
| | 14 | 26 | | TraceIdentifier = "asibackbone-contract-capability" |
| | 14 | 27 | | }; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Creates a descriptor containing a capability requirement for the invalid-grant contract path. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <returns>The descriptor supplied to the validator implementation under test.</returns> |
| | | 34 | | protected virtual AsiBackboneEndpointGovernanceDescriptor CreateCapabilityDescriptor() |
| | | 35 | | { |
| | 12 | 36 | | var endpoint = new Endpoint( |
| | 0 | 37 | | static _ => Task.CompletedTask, |
| | 12 | 38 | | new EndpointMetadataCollection(new RequireCapabilityGrantAttribute("asibackbone.contract.invalid")), |
| | 12 | 39 | | "asibackbone.contract.capability"); |
| | | 40 | | |
| | 12 | 41 | | return AsiBackboneEndpointGovernanceDescriptor.FromEndpoint(endpoint); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates the current decision supplied to the validator implementation under test. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <returns>The current governance decision.</returns> |
| | | 48 | | protected virtual GovernanceDecision CreateCurrentDecision() |
| | | 49 | | { |
| | 10 | 50 | | return GovernanceDecision.Allow( |
| | 10 | 51 | | correlationId: "asibackbone-contract-capability", |
| | 10 | 52 | | policyVersion: "contract-policy-v1", |
| | 10 | 53 | | policyHash: "contract-policy-hash"); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Allows derived contracts to place a known-invalid grant, token, header, or request state into the context. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="httpContext">The HTTP context supplied to the validator.</param> |
| | | 60 | | /// <param name="descriptor">The endpoint governance descriptor supplied to the validator.</param> |
| | | 61 | | protected virtual void ConfigureKnownInvalidCapabilityGrant( |
| | | 62 | | HttpContext httpContext, |
| | | 63 | | AsiBackboneEndpointGovernanceDescriptor descriptor) |
| | | 64 | | { |
| | 10 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Verifies that a known invalid capability-grant scenario does not produce an allow decision. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="cancellationToken">A token that can cancel the contract validation.</param> |
| | | 71 | | /// <returns>The verified governance decision.</returns> |
| | | 72 | | public async ValueTask<GovernanceDecision> VerifyKnownInvalidCapabilityGrantDoesNotAllowAsync(CancellationToken canc |
| | | 73 | | { |
| | 18 | 74 | | IAsiBackboneEndpointCapabilityGrantValidator validator = CreateValidator() |
| | 18 | 75 | | ?? throw new AsiBackboneContractViolationException("Capability-grant contract must provide a validator insta |
| | 16 | 76 | | HttpContext httpContext = CreateHttpContext() |
| | 16 | 77 | | ?? throw new AsiBackboneContractViolationException("Capability-grant contract must provide an HTTP context." |
| | 14 | 78 | | AsiBackboneEndpointGovernanceDescriptor descriptor = CreateCapabilityDescriptor() |
| | 14 | 79 | | ?? throw new AsiBackboneContractViolationException("Capability-grant contract must provide an endpoint descr |
| | 12 | 80 | | GovernanceDecision currentDecision = CreateCurrentDecision() |
| | 12 | 81 | | ?? throw new AsiBackboneContractViolationException("Capability-grant contract must provide a current decisio |
| | | 82 | | |
| | 10 | 83 | | ConfigureKnownInvalidCapabilityGrant(httpContext, descriptor); |
| | | 84 | | |
| | | 85 | | try |
| | | 86 | | { |
| | 10 | 87 | | GovernanceDecision decision = await validator.ValidateAsync(httpContext, descriptor, currentDecision, cancel |
| | 4 | 88 | | return AsiBackboneDecisionContract.VerifyInvalidCapabilityGrantDoesNotAllow(decision, "Capability-grant vali |
| | | 89 | | } |
| | 2 | 90 | | catch (OperationCanceledException) |
| | | 91 | | { |
| | 2 | 92 | | throw; |
| | | 93 | | } |
| | 4 | 94 | | catch (AsiBackboneContractViolationException) |
| | | 95 | | { |
| | 4 | 96 | | throw; |
| | | 97 | | } |
| | 2 | 98 | | catch (Exception exception) |
| | | 99 | | { |
| | 2 | 100 | | throw new AsiBackboneContractViolationException( |
| | 2 | 101 | | "Capability-grant validators must fail closed, defer, or escalate for invalid grants instead of throwing |
| | 2 | 102 | | exception); |
| | | 103 | | } |
| | 2 | 104 | | } |
| | | 105 | | } |