Skip to content

Commit 659cebf

Browse files
fix user pools in tests not being cleaned up (#208) (#209)
1 parent 0bd465d commit 659cebf

File tree

5 files changed

+183
-110
lines changed

5 files changed

+183
-110
lines changed

test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/AuthenticationConfirmUserTests.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Threading.Tasks;
1718
using System.Collections.Generic;
1819
using Xunit;
@@ -26,30 +27,39 @@ public class AuthenticationConfirmUserTests : BaseAuthenticationTestClass
2627
{
2728
public AuthenticationConfirmUserTests() : base()
2829
{
29-
SignUpRequest signUpRequest = new SignUpRequest()
30+
try
3031
{
31-
ClientId = pool.ClientID,
32-
Password = "PassWord1!",
33-
Username = "User5",
34-
UserAttributes = new List<AttributeType>()
32+
SignUpRequest signUpRequest = new SignUpRequest()
3533
{
36-
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"},
37-
},
38-
ValidationData = new List<AttributeType>()
39-
{
40-
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"}
41-
}
42-
};
34+
ClientId = pool.ClientID,
35+
Password = "PassWord1!",
36+
Username = "User5",
37+
UserAttributes = new List<AttributeType>()
38+
{
39+
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"},
40+
},
41+
ValidationData = new List<AttributeType>()
42+
{
43+
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"}
44+
}
45+
};
4346

44-
SignUpResponse signUpResponse = provider.SignUpAsync(signUpRequest).Result;
47+
SignUpResponse signUpResponse = provider.SignUpAsync(signUpRequest).Result;
4548

46-
AdminConfirmSignUpRequest confirmRequest = new AdminConfirmSignUpRequest()
49+
AdminConfirmSignUpRequest confirmRequest = new AdminConfirmSignUpRequest()
50+
{
51+
Username = "User5",
52+
UserPoolId = pool.PoolID
53+
};
54+
AdminConfirmSignUpResponse confirmResponse = provider.AdminConfirmSignUpAsync(confirmRequest).Result;
55+
user = new CognitoUser("User5", pool.ClientID, pool, provider);
56+
}
57+
catch (Exception ex)
4758
{
48-
Username = "User5",
49-
UserPoolId = pool.PoolID
50-
};
51-
AdminConfirmSignUpResponse confirmResponse = provider.AdminConfirmSignUpAsync(confirmRequest).Result;
52-
user = new CognitoUser("User5", pool.ClientID, pool, provider);
59+
Console.WriteLine($"AuthenticationConfirmUserTests constructor failed: {ex.Message}");
60+
Dispose(); // Clean up the user pool that was created in base constructor
61+
throw; // Re-throw so test still fails as expected
62+
}
5363
}
5464

5565
//Tests SRP authentication flow for web applications

test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/AuthenticationCreateUserTests.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Threading.Tasks;
1718
using System.Collections.Generic;
1819
using Xunit;
@@ -27,24 +28,33 @@ public class AuthenticationCreateUserTests : BaseAuthenticationTestClass
2728
{
2829
public AuthenticationCreateUserTests() : base()
2930
{
30-
AdminCreateUserRequest createUserRequest = new AdminCreateUserRequest()
31+
try
3132
{
32-
TemporaryPassword = "PassWord1!",
33-
Username = "User5",
34-
UserAttributes = new List<AttributeType>()
33+
AdminCreateUserRequest createUserRequest = new AdminCreateUserRequest()
3534
{
36-
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"},
37-
},
38-
ValidationData = new List<AttributeType>()
39-
{
40-
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"}
41-
},
42-
UserPoolId = pool.PoolID
43-
};
35+
TemporaryPassword = "PassWord1!",
36+
Username = "User5",
37+
UserAttributes = new List<AttributeType>()
38+
{
39+
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"},
40+
},
41+
ValidationData = new List<AttributeType>()
42+
{
43+
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"}
44+
},
45+
UserPoolId = pool.PoolID
46+
};
4447

45-
AdminCreateUserResponse createReponse = provider.AdminCreateUserAsync(createUserRequest).Result;
48+
AdminCreateUserResponse createReponse = provider.AdminCreateUserAsync(createUserRequest).Result;
4649

47-
user = new CognitoUser("User5", pool.ClientID, pool, provider);
50+
user = new CognitoUser("User5", pool.ClientID, pool, provider);
51+
}
52+
catch (Exception ex)
53+
{
54+
Console.WriteLine($"AuthenticationCreateUserTests constructor failed: {ex.Message}");
55+
Dispose(); // Clean up the user pool that was created in base constructor
56+
throw; // Re-throw so test still fails as expected
57+
}
4858
}
4959

5060
// Tests the sessionauthentication process with a NEW_PASSWORD_REQURIED flow

test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/AuthenticationSignUpUserTests.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ public class AuthenticationSignUpUserTests : BaseAuthenticationTestClass
2727
{
2828
public AuthenticationSignUpUserTests() : base()
2929
{
30-
SignUpRequest signUpRequest = new SignUpRequest()
30+
try
3131
{
32-
ClientId = pool.ClientID,
33-
Password = "PassWord1!",
34-
Username = "User5",
35-
UserAttributes = new List<AttributeType>()
32+
SignUpRequest signUpRequest = new SignUpRequest()
3633
{
37-
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"},
38-
},
39-
ValidationData = new List<AttributeType>()
40-
{
41-
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"}
42-
}
43-
};
34+
ClientId = pool.ClientID,
35+
Password = "PassWord1!",
36+
Username = "User5",
37+
UserAttributes = new List<AttributeType>()
38+
{
39+
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"},
40+
},
41+
ValidationData = new List<AttributeType>()
42+
{
43+
new AttributeType() {Name=CognitoConstants.UserAttrEmail, Value="xxx@yyy.zzz"}
44+
}
45+
};
4446

45-
SignUpResponse signUpResponse = provider.SignUpAsync(signUpRequest).Result;
46-
user = new CognitoUser("User5", pool.ClientID, pool, provider);
47+
SignUpResponse signUpResponse = provider.SignUpAsync(signUpRequest).Result;
48+
user = new CognitoUser("User5", pool.ClientID, pool, provider);
49+
}
50+
catch (Exception ex)
51+
{
52+
Console.WriteLine($"AuthenticationSignUpUserTests constructor failed: {ex.Message}");
53+
Dispose(); // Clean up the user pool that was created in base constructor
54+
throw; // Re-throw so test still fails as expected
55+
}
4756
}
4857

4958
// Tests the SignUp method (using random, dummy email)

test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/BaseAuthenticationTestClass.cs

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/MfaAuthenticationTests.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ await Assert.ThrowsAsync<CodeMismatchException>(() => user.RespondToSmsMfaAuthAs
6262
/// </summary>
6363
public MfaAuthenticationTests()
6464
{
65-
//Delete pool created by BaseAuthenticationTestClass
66-
if(pool != null)
65+
try
6766
{
68-
provider.DeleteUserPoolAsync(new DeleteUserPoolRequest()
67+
//Delete pool created by BaseAuthenticationTestClass
68+
if(pool != null)
6969
{
70-
UserPoolId = pool.PoolID
71-
}).Wait();
72-
}
70+
provider.DeleteUserPoolAsync(new DeleteUserPoolRequest()
71+
{
72+
UserPoolId = pool.PoolID
73+
}).Wait();
74+
}
7375

7476
UserPoolPolicyType passwordPolicy = new UserPoolPolicyType();
7577
List<SchemaAttributeType> requiredAttributes = new List<SchemaAttributeType>();
@@ -217,6 +219,13 @@ public MfaAuthenticationTests()
217219
AdminConfirmSignUpResponse confirmResponse = provider.AdminConfirmSignUpAsync(confirmRequest).Result;
218220

219221
this.user = new CognitoUser("User5", clientCreated.ClientId, pool, provider);
222+
}
223+
catch (Exception ex)
224+
{
225+
Console.WriteLine($"MfaAuthenticationTests constructor failed: {ex.Message}");
226+
Dispose(); // Clean up any resources that were created
227+
throw; // Re-throw so test still fails as expected
228+
}
220229
}
221230

222231
/// <summary>

0 commit comments

Comments
 (0)