Skip to content

Commit d2f8cd8

Browse files
fix: address remaining PR review feedback
- ignore local test output artifact to prevent noisy diffs - cache chat captcha unavailable response payload - add tests for partial captcha configuration guard paths
1 parent ebae8b2 commit d2f8cd8

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ TestResults/
3333

3434
# Old or generated files to not commit
3535
build_output.txt
36+
test_output.txt
3637
wwwroot/sitemap.xml
3738
wwwroot/Chapters
3839
EssentialCSharp.Web/wwwroot/Chapters

EssentialCSharp.Web.Tests/CaptchaValidationServiceTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,40 @@ public async Task ValidateAsync_MissingConfig_RejectsWithoutVerification()
2424
await Assert.That(captchaService.CallCount).IsEqualTo(0);
2525
}
2626

27+
[Test]
28+
public async Task ValidateAsync_MissingSiteKey_RejectsWithoutVerification()
29+
{
30+
StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called."));
31+
using ServiceProvider serviceProvider = CreateServiceProvider(
32+
new CaptchaOptions { SecretKey = "secret", SiteKey = string.Empty },
33+
captchaService);
34+
35+
ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>();
36+
37+
CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1");
38+
39+
await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Disabled);
40+
await Assert.That(result.ShouldProceed).IsFalse();
41+
await Assert.That(captchaService.CallCount).IsEqualTo(0);
42+
}
43+
44+
[Test]
45+
public async Task ValidateAsync_MissingSecretKey_RejectsWithoutVerification()
46+
{
47+
StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called."));
48+
using ServiceProvider serviceProvider = CreateServiceProvider(
49+
new CaptchaOptions { SecretKey = string.Empty, 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.Disabled);
57+
await Assert.That(result.ShouldProceed).IsFalse();
58+
await Assert.That(captchaService.CallCount).IsEqualTo(0);
59+
}
60+
2761
[Test]
2862
public async Task ValidateAsync_MissingToken_ReturnsMissingToken()
2963
{

EssentialCSharp.Web/Controllers/ChatController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,11 @@ private async Task HandleCaptchaValidationFailureAsync(CaptchaValidationResult c
344344
}
345345
}
346346

347-
private static object CaptchaUnavailableResponseBody =>
348-
new { error = "Human verification is temporarily unavailable. Please try again later.", errorCode = "captcha_unavailable" };
347+
private static readonly object CaptchaUnavailableResponseBody = new
348+
{
349+
error = "Human verification is temporarily unavailable. Please try again later.",
350+
errorCode = "captcha_unavailable"
351+
};
349352

350353
private void LogCaptchaUnavailable(CaptchaValidationOutcome outcome)
351354
{

0 commit comments

Comments
 (0)