@@ -7,6 +7,8 @@ namespace EssentialCSharp.Web.Tests;
77public class ChatAvailabilityTests : IntegrationTestBase
88{
99 private const string HCaptchaTestToken = "10000000-aaaa-bbbb-cccc-000000000001" ;
10+ private const string InvalidHCaptchaTestToken = "invalid-captcha-test-token" ;
11+ private const string UnavailableHCaptchaTestToken = "unavailable-captcha-test-token" ;
1012
1113 [ Test ]
1214 [ Arguments ( "/api/chat/message" , "chat-unavailable-message" ) ]
@@ -30,4 +32,34 @@ public async Task ChatEndpoint_WhenBackendUnavailable_Returns503WithContract(str
3032 using JsonDocument payload = JsonDocument . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
3133 await Assert . That ( payload . RootElement . GetProperty ( "errorCode" ) . GetString ( ) ) . IsEqualTo ( "chat_unavailable" ) ;
3234 }
35+
36+ [ Test ]
37+ [ Arguments ( "/api/chat/message" , "chat-invalid-message" , InvalidHCaptchaTestToken , HttpStatusCode . Forbidden , "captcha_failed" ) ]
38+ [ Arguments ( "/api/chat/stream" , "chat-invalid-stream" , InvalidHCaptchaTestToken , HttpStatusCode . Forbidden , "captcha_failed" ) ]
39+ [ Arguments ( "/api/chat/message" , "chat-captcha-unavailable-message" , UnavailableHCaptchaTestToken , HttpStatusCode . ServiceUnavailable , "captcha_unavailable" ) ]
40+ [ Arguments ( "/api/chat/stream" , "chat-captcha-unavailable-stream" , UnavailableHCaptchaTestToken , HttpStatusCode . ServiceUnavailable , "captcha_unavailable" ) ]
41+ public async Task ChatEndpoint_WhenCaptchaValidationFails_ReturnsExpectedStatusAndCode (
42+ string endpoint ,
43+ string userNameSeed ,
44+ string captchaResponse ,
45+ HttpStatusCode expectedStatus ,
46+ string expectedErrorCode )
47+ {
48+ HttpClient client = McpTestHelper . CreateClient ( Factory ) ;
49+
50+ string userId = await McpTestHelper . CreateUserAsync ( Factory , userNameSeed ) ;
51+ ( string cookieName , string cookieValue ) = await McpTestHelper . CreateIdentityApplicationCookieAsync ( Factory , userId ) ;
52+
53+ using var request = new HttpRequestMessage ( HttpMethod . Post , endpoint )
54+ {
55+ Content = JsonContent . Create ( new { message = "Hello" , enableContextualSearch = false , captchaResponse } )
56+ } ;
57+ McpTestHelper . AddCookie ( request , cookieName , cookieValue ) ;
58+
59+ using HttpResponseMessage response = await client . SendAsync ( request ) ;
60+ await Assert . That ( response . StatusCode ) . IsEqualTo ( expectedStatus ) ;
61+
62+ using JsonDocument payload = JsonDocument . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
63+ await Assert . That ( payload . RootElement . GetProperty ( "errorCode" ) . GetString ( ) ) . IsEqualTo ( expectedErrorCode ) ;
64+ }
3365}
0 commit comments