@@ -31,6 +31,7 @@ public partial class BaseAuthenticationTestClass : IDisposable
3131 protected AmazonCognitoIdentityProviderClient provider ;
3232 protected CognitoUserPool pool ;
3333 protected CognitoUser user ;
34+ private readonly string _testInstanceId ;
3435
3536 static BaseAuthenticationTestClass ( )
3637 {
@@ -39,64 +40,78 @@ static BaseAuthenticationTestClass()
3940
4041 public BaseAuthenticationTestClass ( )
4142 {
42- UserPoolPolicyType passwordPolicy = new UserPoolPolicyType ( ) ;
43- List < SchemaAttributeType > requiredAttributes = new List < SchemaAttributeType > ( ) ;
44- List < string > verifiedAttributes = new List < string > ( ) ;
43+ _testInstanceId = Guid . NewGuid ( ) . ToString ( "N" ) [ ..8 ] ; // Short unique identifier
44+
45+ try
46+ {
47+ UserPoolPolicyType passwordPolicy = new UserPoolPolicyType ( ) ;
48+ List < SchemaAttributeType > requiredAttributes = new List < SchemaAttributeType > ( ) ;
49+ List < string > verifiedAttributes = new List < string > ( ) ;
4550
46- provider = new AmazonCognitoIdentityProviderClient ( ) ;
51+ provider = new AmazonCognitoIdentityProviderClient ( ) ;
4752
48- AdminCreateUserConfigType adminCreateUser = new AdminCreateUserConfigType ( )
49- {
50- UnusedAccountValidityDays = 8 ,
51- AllowAdminCreateUserOnly = false
52- } ;
53+ AdminCreateUserConfigType adminCreateUser = new AdminCreateUserConfigType ( )
54+ {
55+ UnusedAccountValidityDays = 8 ,
56+ AllowAdminCreateUserOnly = false
57+ } ;
5358
54- passwordPolicy . PasswordPolicy = new PasswordPolicyType ( )
55- {
56- MinimumLength = 8 ,
57- RequireNumbers = true ,
58- RequireSymbols = true ,
59- RequireUppercase = true ,
60- RequireLowercase = true
61- } ;
62-
63- SchemaAttributeType tempSchema = new SchemaAttributeType ( )
64- {
65- Required = true ,
66- Name = CognitoConstants . UserAttrEmail ,
67- AttributeDataType = AttributeDataType . String
68- } ;
69- requiredAttributes . Add ( tempSchema ) ;
70- verifiedAttributes . Add ( CognitoConstants . UserAttrEmail ) ;
71-
72- CreateUserPoolRequest createPoolRequest = new CreateUserPoolRequest
73- {
74- PoolName = "testPool_" + DateTime . UtcNow . ToString ( "yyyyMMdd_HHmmss" ) ,
75- Policies = passwordPolicy ,
76- Schema = requiredAttributes ,
77- AdminCreateUserConfig = adminCreateUser ,
78- MfaConfiguration = "OFF" ,
79- AutoVerifiedAttributes = verifiedAttributes ,
80- DeviceConfiguration = new DeviceConfigurationType ( )
59+ passwordPolicy . PasswordPolicy = new PasswordPolicyType ( )
8160 {
82- ChallengeRequiredOnNewDevice = false ,
83- DeviceOnlyRememberedOnUserPrompt = false
84- }
85- } ;
86- CreateUserPoolResponse createPoolResponse = provider . CreateUserPoolAsync ( createPoolRequest ) . Result ;
87- UserPoolType userPoolCreated = createPoolResponse . UserPool ;
61+ MinimumLength = 8 ,
62+ RequireNumbers = true ,
63+ RequireSymbols = true ,
64+ RequireUppercase = true ,
65+ RequireLowercase = true
66+ } ;
8867
89- CreateUserPoolClientRequest clientRequest = new CreateUserPoolClientRequest ( )
90- {
91- ClientName = "App_" + DateTime . UtcNow . ToString ( "yyyyMMdd_HHmmss" ) ,
92- UserPoolId = userPoolCreated . Id ,
93- GenerateSecret = false ,
68+ SchemaAttributeType tempSchema = new SchemaAttributeType ( )
69+ {
70+ Required = true ,
71+ Name = CognitoConstants . UserAttrEmail ,
72+ AttributeDataType = AttributeDataType . String
73+ } ;
74+ requiredAttributes . Add ( tempSchema ) ;
75+ verifiedAttributes . Add ( CognitoConstants . UserAttrEmail ) ;
76+
77+ CreateUserPoolRequest createPoolRequest = new CreateUserPoolRequest
78+ {
79+ PoolName = "testPool_" + DateTime . UtcNow . ToString ( "yyyyMMdd_HHmmss" ) ,
80+ Policies = passwordPolicy ,
81+ Schema = requiredAttributes ,
82+ AdminCreateUserConfig = adminCreateUser ,
83+ MfaConfiguration = "OFF" ,
84+ AutoVerifiedAttributes = verifiedAttributes ,
85+ DeviceConfiguration = new DeviceConfigurationType ( )
86+ {
87+ ChallengeRequiredOnNewDevice = false ,
88+ DeviceOnlyRememberedOnUserPrompt = false
89+ }
90+ } ;
91+ CreateUserPoolResponse createPoolResponse = provider . CreateUserPoolAsync ( createPoolRequest ) . Result ;
92+ UserPoolType userPoolCreated = createPoolResponse . UserPool ;
93+
94+ CreateUserPoolClientRequest clientRequest = new CreateUserPoolClientRequest ( )
95+ {
96+ ClientName = "App_" + DateTime . UtcNow . ToString ( "yyyyMMdd_HHmmss" ) ,
97+ UserPoolId = userPoolCreated . Id ,
98+ GenerateSecret = false ,
9499
95- } ;
96- CreateUserPoolClientResponse clientResponse = provider . CreateUserPoolClientAsync ( clientRequest ) . Result ;
97- UserPoolClientType clientCreated = clientResponse . UserPoolClient ;
100+ } ;
101+ CreateUserPoolClientResponse clientResponse = provider . CreateUserPoolClientAsync ( clientRequest ) . Result ;
102+ UserPoolClientType clientCreated = clientResponse . UserPoolClient ;
98103
99- pool = new CognitoUserPool ( userPoolCreated . Id , clientCreated . ClientId , provider , "" ) ;
104+ pool = new CognitoUserPool ( userPoolCreated . Id , clientCreated . ClientId , provider , "" ) ;
105+
106+ // Log user pool creation
107+ Console . WriteLine ( $ "[{ _testInstanceId } ] Created user pool: { createPoolRequest . PoolName } (ID: { userPoolCreated . Id } ) at { DateTime . UtcNow : yyyy-MM-dd HH:mm:ss} UTC") ;
108+ }
109+ catch ( Exception ex )
110+ {
111+ Console . WriteLine ( $ "[{ _testInstanceId } ] Constructor failed: { ex . Message } ") ;
112+ Dispose ( ) ; // Clean up any resources that were created
113+ throw ; // Re-throw the original exception so test fails as expected
114+ }
100115 }
101116
102117 /// <summary>
@@ -105,18 +120,38 @@ public BaseAuthenticationTestClass()
105120 /// </summary>
106121 public virtual void Dispose ( )
107122 {
108- try
123+ // Handle partial construction - pool might be null if constructor failed early
124+ if ( pool ? . PoolID != null )
109125 {
110- provider . DeleteUserPoolAsync ( new DeleteUserPoolRequest ( )
126+ Console . WriteLine ( $ "[{ _testInstanceId } ] Disposing user pool: { pool . PoolID } at { DateTime . UtcNow : yyyy-MM-dd HH:mm:ss} UTC") ;
127+ try
128+ {
129+ provider ? . DeleteUserPoolAsync ( new DeleteUserPoolRequest ( )
130+ {
131+ UserPoolId = pool . PoolID
132+ } ) . Wait ( ) ;
133+
134+ Console . WriteLine ( $ "[{ _testInstanceId } ] Successfully disposed user pool: { pool . PoolID } ") ;
135+ }
136+ catch ( Exception ex )
111137 {
112- UserPoolId = pool . PoolID
113- } ) . Wait ( ) ;
138+ Console . WriteLine ( $ "[{ _testInstanceId } ] ERROR disposing user pool { pool . PoolID } : { ex . Message } ") ;
139+ System . Diagnostics . Debug . WriteLine ( $ "Full exception details: { ex } ") ;
140+ }
141+ }
142+ else
143+ {
144+ Console . WriteLine ( $ "[{ _testInstanceId } ] Dispose called but no user pool to clean up (partial construction)") ;
145+ }
114146
115- provider . Dispose ( ) ;
147+ // Always dispose the provider if it exists
148+ try
149+ {
150+ provider ? . Dispose ( ) ;
116151 }
117152 catch ( Exception ex )
118153 {
119- System . Diagnostics . Debug . WriteLine ( ex . Message ) ;
154+ Console . WriteLine ( $ "[ { _testInstanceId } ] ERROR disposing provider: { ex . Message } " ) ;
120155 }
121156 }
122157 }
0 commit comments