|
| 1 | +using EssentialCSharp.Web.Models; |
| 2 | +using EssentialCSharp.Web.Services; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Options; |
| 5 | + |
| 6 | +namespace EssentialCSharp.Web.Tests; |
| 7 | + |
| 8 | +public class CaptchaValidationServiceTests |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + public async Task ValidateAsync_Disabled_SkipsVerification() |
| 12 | + { |
| 13 | + StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called.")); |
| 14 | + using ServiceProvider serviceProvider = CreateServiceProvider( |
| 15 | + new CaptchaOptions { SecretKey = string.Empty, SiteKey = string.Empty }, |
| 16 | + captchaService); |
| 17 | + |
| 18 | + ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); |
| 19 | + |
| 20 | + CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1"); |
| 21 | + |
| 22 | + await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Disabled); |
| 23 | + await Assert.That(result.ShouldProceed).IsTrue(); |
| 24 | + await Assert.That(captchaService.CallCount).IsEqualTo(0); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public async Task ValidateAsync_MissingToken_ReturnsMissingToken() |
| 29 | + { |
| 30 | + StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called.")); |
| 31 | + using ServiceProvider serviceProvider = CreateServiceProvider( |
| 32 | + new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, |
| 33 | + captchaService); |
| 34 | + |
| 35 | + ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); |
| 36 | + |
| 37 | + CaptchaValidationResult result = await validationService.ValidateAsync(string.Empty, "127.0.0.1"); |
| 38 | + |
| 39 | + await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.MissingToken); |
| 40 | + await Assert.That(result.ShouldProceed).IsFalse(); |
| 41 | + await Assert.That(captchaService.CallCount).IsEqualTo(0); |
| 42 | + } |
| 43 | + |
| 44 | + [Test] |
| 45 | + public async Task ValidateAsync_Unavailable_ReturnsUnavailable() |
| 46 | + { |
| 47 | + StubCaptchaService captchaService = new((_, _, _) => Task.FromResult<HCaptchaResult?>(null)); |
| 48 | + using ServiceProvider serviceProvider = CreateServiceProvider( |
| 49 | + new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, |
| 50 | + captchaService); |
| 51 | + |
| 52 | + ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); |
| 53 | + |
| 54 | + CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1"); |
| 55 | + |
| 56 | + await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Unavailable); |
| 57 | + await Assert.That(result.ShouldProceed).IsFalse(); |
| 58 | + await Assert.That(captchaService.CallCount).IsEqualTo(1); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task ValidateAsync_InvalidAndValid_ReturnExpectedOutcome() |
| 63 | + { |
| 64 | + StubCaptchaService invalidCaptchaService = new((_, _, _) => Task.FromResult<HCaptchaResult?>(new HCaptchaResult |
| 65 | + { |
| 66 | + Success = false, |
| 67 | + ErrorCodes = ["invalid-input-response"] |
| 68 | + })); |
| 69 | + using ServiceProvider invalidProvider = CreateServiceProvider( |
| 70 | + new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, |
| 71 | + invalidCaptchaService); |
| 72 | + |
| 73 | + ICaptchaValidationService invalidValidationService = invalidProvider.GetRequiredService<ICaptchaValidationService>(); |
| 74 | + CaptchaValidationResult invalidResult = await invalidValidationService.ValidateAsync("token", "127.0.0.1"); |
| 75 | + |
| 76 | + await Assert.That(invalidResult.Outcome).IsEqualTo(CaptchaValidationOutcome.Invalid); |
| 77 | + await Assert.That(invalidResult.Response).IsNotNull(); |
| 78 | + await Assert.That(invalidResult.ShouldProceed).IsFalse(); |
| 79 | + |
| 80 | + StubCaptchaService validCaptchaService = new((_, _, _) => Task.FromResult<HCaptchaResult?>(new HCaptchaResult |
| 81 | + { |
| 82 | + Success = true |
| 83 | + })); |
| 84 | + using ServiceProvider validProvider = CreateServiceProvider( |
| 85 | + new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, |
| 86 | + validCaptchaService); |
| 87 | + |
| 88 | + ICaptchaValidationService validValidationService = validProvider.GetRequiredService<ICaptchaValidationService>(); |
| 89 | + CaptchaValidationResult validResult = await validValidationService.ValidateAsync("token", "127.0.0.1"); |
| 90 | + |
| 91 | + await Assert.That(validResult.Outcome).IsEqualTo(CaptchaValidationOutcome.Valid); |
| 92 | + await Assert.That(validResult.ShouldProceed).IsTrue(); |
| 93 | + await Assert.That(validCaptchaService.CallCount).IsEqualTo(1); |
| 94 | + } |
| 95 | + |
| 96 | + private static ServiceProvider CreateServiceProvider(CaptchaOptions options, ICaptchaService captchaService) |
| 97 | + { |
| 98 | + ServiceCollection services = new(); |
| 99 | + services.AddSingleton(Options.Create(options)); |
| 100 | + services.AddSingleton(captchaService); |
| 101 | + services.AddSingleton<ICaptchaValidationService, CaptchaValidationService>(); |
| 102 | + return services.BuildServiceProvider(); |
| 103 | + } |
| 104 | + |
| 105 | + private sealed class StubCaptchaService(Func<string?, string?, CancellationToken, Task<HCaptchaResult?>> verifyAsync) : ICaptchaService |
| 106 | + { |
| 107 | + public int CallCount { get; private set; } |
| 108 | + |
| 109 | + public Task<HCaptchaResult?> VerifyAsync(string secret, string response, string sitekey, CancellationToken cancellationToken = default) |
| 110 | + => throw new NotSupportedException(); |
| 111 | + |
| 112 | + public Task<HCaptchaResult?> VerifyAsync(string? response, CancellationToken cancellationToken = default) |
| 113 | + => VerifyAsync(response, remoteIp: null, cancellationToken); |
| 114 | + |
| 115 | + public async Task<HCaptchaResult?> VerifyAsync(string? response, string? remoteIp, CancellationToken cancellationToken = default) |
| 116 | + { |
| 117 | + CallCount++; |
| 118 | + return await verifyAsync(response, remoteIp, cancellationToken); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments