33
44using System . Collections . Concurrent ;
55using System . Net . Http . Json ;
6+ using System . Text ;
67using System . Text . Json ;
78using Bit . Core . Auth . Models . Api . Request . Accounts ;
89using Bit . Core . Entities ;
@@ -25,6 +26,7 @@ public class IdentityApplicationFactory : WebApplicationFactoryBase<Startup>
2526 public const string DefaultDeviceIdentifier = "92b9d953-b9b6-4eaf-9d3e-11d57144dfeb" ;
2627 public const string DefaultUserEmail = "[email protected] " ; 2728 public const string DefaultUserPasswordHash = "default_password_hash" ;
29+ private const string DefaultEncryptedString = "2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98sp4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=" ;
2830 public bool UseMockClientVersionValidator { get ; set ; } = true ;
2931
3032 /// <summary>
@@ -88,20 +90,8 @@ public async Task<HttpContext> PostRegisterVerificationEmailClicked(RegisterVeri
8890 var context = await ContextFromPasswordAsync (
8991 username , password , deviceIdentifier , clientId , deviceType , deviceName ) ;
9092
91- // Provide clearer diagnostics on failure
92- if ( context . Response . StatusCode != StatusCodes . Status200OK )
93- {
94- var contentType = context . Response . ContentType ?? string . Empty ;
95- if ( context . Response . Body . CanSeek )
96- {
97- context . Response . Body . Position = 0 ;
98- }
99- string rawBody = await new StreamReader ( context . Response . Body ) . ReadToEndAsync ( ) ;
100- throw new Xunit . Sdk . XunitException ( $ "Login failed: status={ context . Response . StatusCode } , contentType='{ contentType } ', body='{ rawBody } '") ;
101- }
102-
103- using var jsonDoc = await AssertHelper . AssertResponseTypeIs < JsonDocument > ( context ) ;
104- var root = jsonDoc . RootElement ;
93+ using var body = await AssertHelper . AssertResponseTypeIs < JsonDocument > ( context ) ;
94+ var root = body . RootElement ;
10595
10696 return ( root . GetProperty ( "access_token" ) . GetString ( ) , root . GetProperty ( "refresh_token" ) . GetString ( ) ) ;
10797 }
@@ -124,13 +114,7 @@ public async Task<HttpContext> ContextFromPasswordAsync(
124114 { "grant_type" , "password" } ,
125115 { "username" , username } ,
126116 { "password" , password } ,
127- } ) ,
128- http =>
129- {
130- // Ensure JSON content negotiation for errors and set a sane client version
131- http . Request . Headers . Append ( "Accept" , "application/json" ) ;
132- http . Request . Headers . Append ( "Bitwarden-Client-Version" , "2025.11.0" ) ;
133- } ) ;
117+ } ) ) ;
134118
135119 return context ;
136120 }
@@ -242,8 +226,11 @@ public async Task<User> RegisterNewIdentityFactoryUserAsync(
242226 requestModel . EmailVerificationToken = RegistrationTokens [ requestModel . Email ] ;
243227
244228 var postRegisterFinishHttpContext = await PostRegisterFinishAsync ( requestModel ) ;
245-
246- Assert . Equal ( StatusCodes . Status200OK , postRegisterFinishHttpContext . Response . StatusCode ) ;
229+ if ( postRegisterFinishHttpContext . Response . StatusCode != StatusCodes . Status200OK )
230+ {
231+ var body = await ReadResponseBodyAsync ( postRegisterFinishHttpContext ) ;
232+ Assert . Fail ( $ "register/finish failed (status { postRegisterFinishHttpContext . Response . StatusCode } ). Body: { body } ") ;
233+ }
247234
248235 var database = GetDatabaseContext ( ) ;
249236 var user = await database . Users
@@ -253,4 +240,32 @@ public async Task<User> RegisterNewIdentityFactoryUserAsync(
253240
254241 return user ;
255242 }
243+
244+ private static async Task < string > ReadResponseBodyAsync ( HttpContext ctx )
245+ {
246+ try
247+ {
248+ if ( ctx ? . Response . Body == null )
249+ {
250+ return "<no body>" ;
251+ }
252+ var stream = ctx . Response . Body ;
253+ if ( stream . CanSeek )
254+ {
255+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
256+ }
257+ using var reader = new StreamReader ( stream , Encoding . UTF8 , detectEncodingFromByteOrderMarks : false , leaveOpen : true ) ;
258+ var text = await reader . ReadToEndAsync ( ) ;
259+ if ( stream . CanSeek )
260+ {
261+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
262+ }
263+ return string . IsNullOrWhiteSpace ( text ) ? "<empty body>" : text ;
264+ }
265+ catch ( Exception ex )
266+ {
267+ return $ "<error reading body: { ex . Message } >";
268+ }
269+ }
270+
256271}
0 commit comments