|
| 1 | +using Bewit.Exceptions; |
| 2 | +using Bewit.Validation; |
| 3 | +using FluentAssertions; |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | +using Microsoft.AspNetCore.Http.HttpResults; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using Moq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Bewit.Extensions.Http.Tests; |
| 12 | + |
| 13 | +public class BewitEndpointFilterTests |
| 14 | +{ |
| 15 | + private static IOptions<BewitTokenExtractionOptions> CreateOptions( |
| 16 | + Action<BewitTokenExtractionOptions>? configure = null) |
| 17 | + { |
| 18 | + var options = new BewitTokenExtractionOptions(); |
| 19 | + configure?.Invoke(options); |
| 20 | + |
| 21 | + return Options.Create(options); |
| 22 | + } |
| 23 | + |
| 24 | + private static (DefaultHttpContext Context, ServiceProvider Services) CreateContext( |
| 25 | + IBewitTokenValidator<string>? validator = null, |
| 26 | + string? headerName = null, |
| 27 | + string? headerValue = null, |
| 28 | + string? queryString = null, |
| 29 | + Action<BewitTokenExtractionOptions>? configureOptions = null) |
| 30 | + { |
| 31 | + var serviceCollection = new ServiceCollection() |
| 32 | + .AddHttpContextAccessor() |
| 33 | + .AddSingleton(CreateOptions(configureOptions)); |
| 34 | + |
| 35 | + if (validator is not null) |
| 36 | + { |
| 37 | + serviceCollection.AddSingleton(validator); |
| 38 | + } |
| 39 | + |
| 40 | + var services = serviceCollection.BuildServiceProvider(); |
| 41 | + var context = new DefaultHttpContext { RequestServices = services }; |
| 42 | + |
| 43 | + if (headerName is not null && headerValue is not null) |
| 44 | + { |
| 45 | + context.Request.Headers[headerName] = headerValue; |
| 46 | + } |
| 47 | + |
| 48 | + if (queryString is not null) |
| 49 | + { |
| 50 | + context.Request.QueryString = new QueryString(queryString); |
| 51 | + } |
| 52 | + |
| 53 | + var httpContextAccessor = services.GetRequiredService<IHttpContextAccessor>(); |
| 54 | + httpContextAccessor.HttpContext = context; |
| 55 | + |
| 56 | + return (context, services); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task InvokeAsync_WithNoToken_ShouldReturnUnauthorized() |
| 61 | + { |
| 62 | + var (context, _) = CreateContext(); |
| 63 | + |
| 64 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 65 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 66 | + |
| 67 | + var result = await filter.InvokeAsync( |
| 68 | + invocationContext, _ => ValueTask.FromResult<object?>(Results.Ok())); |
| 69 | + |
| 70 | + result.Should().BeOfType<UnauthorizedHttpResult>(); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task InvokeAsync_WithHeaderToken_ShouldValidateAndCallNext() |
| 75 | + { |
| 76 | + var validatorMock = new Mock<IBewitTokenValidator<string>>(); |
| 77 | + |
| 78 | + validatorMock |
| 79 | + .Setup(v => v.ValidateBewitTokenAsync( |
| 80 | + It.IsAny<BewitToken<string>>(), It.IsAny<CancellationToken>())) |
| 81 | + .ReturnsAsync("payload-value"); |
| 82 | + |
| 83 | + var (context, _) = CreateContext( |
| 84 | + validator: validatorMock.Object, |
| 85 | + headerName: "bewitToken", |
| 86 | + headerValue: "valid-token"); |
| 87 | + |
| 88 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 89 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 90 | + var nextCalled = false; |
| 91 | + |
| 92 | + var result = await filter.InvokeAsync( |
| 93 | + invocationContext, |
| 94 | + _ => |
| 95 | + { |
| 96 | + nextCalled = true; |
| 97 | + |
| 98 | + return ValueTask.FromResult<object?>(Results.Ok()); |
| 99 | + }); |
| 100 | + |
| 101 | + nextCalled.Should().BeTrue(); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public async Task InvokeAsync_WithQueryToken_ShouldValidateAndCallNext() |
| 106 | + { |
| 107 | + var validatorMock = new Mock<IBewitTokenValidator<string>>(); |
| 108 | + |
| 109 | + validatorMock |
| 110 | + .Setup(v => v.ValidateBewitTokenAsync( |
| 111 | + It.IsAny<BewitToken<string>>(), It.IsAny<CancellationToken>())) |
| 112 | + .ReturnsAsync("payload-value"); |
| 113 | + |
| 114 | + var (context, _) = CreateContext( |
| 115 | + validator: validatorMock.Object, |
| 116 | + queryString: "?bewit=valid-token"); |
| 117 | + |
| 118 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 119 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 120 | + var nextCalled = false; |
| 121 | + |
| 122 | + var result = await filter.InvokeAsync( |
| 123 | + invocationContext, |
| 124 | + _ => |
| 125 | + { |
| 126 | + nextCalled = true; |
| 127 | + |
| 128 | + return ValueTask.FromResult<object?>(Results.Ok()); |
| 129 | + }); |
| 130 | + |
| 131 | + nextCalled.Should().BeTrue(); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public async Task InvokeAsync_WithPreExtractedToken_ShouldValidateAndCallNext() |
| 136 | + { |
| 137 | + var validatorMock = new Mock<IBewitTokenValidator<string>>(); |
| 138 | + |
| 139 | + validatorMock |
| 140 | + .Setup(v => v.ValidateBewitTokenAsync( |
| 141 | + It.IsAny<BewitToken<string>>(), It.IsAny<CancellationToken>())) |
| 142 | + .ReturnsAsync("payload-value"); |
| 143 | + |
| 144 | + var (context, _) = CreateContext(validator: validatorMock.Object); |
| 145 | + context.Items["bewitToken"] = "pre-extracted-token"; |
| 146 | + |
| 147 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 148 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 149 | + var nextCalled = false; |
| 150 | + |
| 151 | + var result = await filter.InvokeAsync( |
| 152 | + invocationContext, |
| 153 | + _ => |
| 154 | + { |
| 155 | + nextCalled = true; |
| 156 | + |
| 157 | + return ValueTask.FromResult<object?>(Results.Ok()); |
| 158 | + }); |
| 159 | + |
| 160 | + nextCalled.Should().BeTrue(); |
| 161 | + } |
| 162 | + |
| 163 | + [Fact] |
| 164 | + public async Task InvokeAsync_WithInvalidToken_ShouldReturnForbid() |
| 165 | + { |
| 166 | + var validatorMock = new Mock<IBewitTokenValidator<string>>(); |
| 167 | + |
| 168 | + validatorMock |
| 169 | + .Setup(v => v.ValidateBewitTokenAsync( |
| 170 | + It.IsAny<BewitToken<string>>(), It.IsAny<CancellationToken>())) |
| 171 | + .ThrowsAsync(new BewitNotFoundException()); |
| 172 | + |
| 173 | + var (context, _) = CreateContext( |
| 174 | + validator: validatorMock.Object, |
| 175 | + queryString: "?bewit=invalid-token"); |
| 176 | + |
| 177 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 178 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 179 | + |
| 180 | + var result = await filter.InvokeAsync( |
| 181 | + invocationContext, _ => ValueTask.FromResult<object?>(Results.Ok())); |
| 182 | + |
| 183 | + result.Should().BeOfType<ForbidHttpResult>(); |
| 184 | + } |
| 185 | + |
| 186 | + [Fact] |
| 187 | + public async Task InvokeAsync_WithCustomQueryParam_ShouldReadFromCustomParam() |
| 188 | + { |
| 189 | + var validatorMock = new Mock<IBewitTokenValidator<string>>(); |
| 190 | + |
| 191 | + validatorMock |
| 192 | + .Setup(v => v.ValidateBewitTokenAsync( |
| 193 | + It.IsAny<BewitToken<string>>(), It.IsAny<CancellationToken>())) |
| 194 | + .ReturnsAsync("payload-value"); |
| 195 | + |
| 196 | + var (context, _) = CreateContext( |
| 197 | + validator: validatorMock.Object, |
| 198 | + queryString: "?token=custom-token", |
| 199 | + configureOptions: o => o.QueryParamName = "token"); |
| 200 | + |
| 201 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 202 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 203 | + var nextCalled = false; |
| 204 | + |
| 205 | + var result = await filter.InvokeAsync( |
| 206 | + invocationContext, |
| 207 | + _ => |
| 208 | + { |
| 209 | + nextCalled = true; |
| 210 | + |
| 211 | + return ValueTask.FromResult<object?>(Results.Ok()); |
| 212 | + }); |
| 213 | + |
| 214 | + nextCalled.Should().BeTrue(); |
| 215 | + } |
| 216 | + |
| 217 | + [Fact] |
| 218 | + public async Task InvokeAsync_WithValidToken_ShouldSetPayloadOnAccessor() |
| 219 | + { |
| 220 | + var validatorMock = new Mock<IBewitTokenValidator<string>>(); |
| 221 | + |
| 222 | + validatorMock |
| 223 | + .Setup(v => v.ValidateBewitTokenAsync( |
| 224 | + It.IsAny<BewitToken<string>>(), It.IsAny<CancellationToken>())) |
| 225 | + .ReturnsAsync("expected-payload"); |
| 226 | + |
| 227 | + var (context, services) = CreateContext( |
| 228 | + validator: validatorMock.Object, |
| 229 | + headerName: "bewitToken", |
| 230 | + headerValue: "valid-token"); |
| 231 | + |
| 232 | + var filter = new Bewit.Http.BewitEndpointFilter<string>(); |
| 233 | + var invocationContext = new DefaultEndpointFilterInvocationContext(context); |
| 234 | + |
| 235 | + await filter.InvokeAsync( |
| 236 | + invocationContext, _ => ValueTask.FromResult<object?>(Results.Ok())); |
| 237 | + |
| 238 | + var accessor = services.GetRequiredService<IHttpContextAccessor>(); |
| 239 | + var payload = accessor.GetBewitPayload<string>(); |
| 240 | + payload.Should().Be("expected-payload"); |
| 241 | + } |
| 242 | +} |
0 commit comments