Skip to content

Commit ce277fb

Browse files
committed
feat(Auth): Adding TOTP integration tests (#3047)
* feat(Auth): Adding TOTP support in Amplify Auth category * feat(Auth): Adding TOTP related models to AWSCognitoPlugin * feat(Auth): Adding TOTP service behaviour * feat(Auth): Adding TOTP tasks and requests to AWSAuthCognitoPlugin * feat(Auth): Adding TOTP state machine actions * feat(Auth): Adding TOTP states, events, data models and resolvers * feat(Auth): Add unit tests for TOTP * feat(Auth): Adding TOTP integration tests
1 parent d57e844 commit ce277fb

File tree

9 files changed

+1275
-13
lines changed

9 files changed

+1275
-13
lines changed

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthHostApp.xcodeproj/project.pbxproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
4821B2F2286B5F74000EC1D7 /* AuthDeleteUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4821B2F1286B5F74000EC1D7 /* AuthDeleteUserTests.swift */; };
1111
4821B2F428737130000EC1D7 /* AuthCustomSignInTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4821B2F328737130000EC1D7 /* AuthCustomSignInTests.swift */; };
1212
4834D7C128B0770800DD564B /* FederatedSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4834D7C028B0770800DD564B /* FederatedSessionTests.swift */; };
13+
483B0D342A42BB1400A1196B /* TOTPSetupWhenUnauthenticatedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 483B0D332A42BB1400A1196B /* TOTPSetupWhenUnauthenticatedTests.swift */; };
1314
484834BC27B6ED8800649D11 /* CredentialStoreConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 484834BB27B6ED8700649D11 /* CredentialStoreConfigurationTests.swift */; };
1415
484834BE27B6FD9B00649D11 /* AuthEnvironmentHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 484834BD27B6FD9B00649D11 /* AuthEnvironmentHelper.swift */; };
1516
484EDEB227F4FFBE000284B4 /* AuthEventIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 484EDEB127F4FFBE000284B4 /* AuthEventIntegrationTests.swift */; };
1617
485105AA2840513C002D6FC8 /* AuthUserAttributesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485105A92840513C002D6FC8 /* AuthUserAttributesTests.swift */; };
18+
48599D4A2A429893009DE21C /* MFASignInTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48599D492A429893009DE21C /* MFASignInTests.swift */; };
1719
485CB53E27B614CE006CCEC7 /* AuthHostAppApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB53D27B614CE006CCEC7 /* AuthHostAppApp.swift */; };
1820
485CB54027B614CE006CCEC7 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB53F27B614CE006CCEC7 /* ContentView.swift */; };
1921
485CB54227B614CF006CCEC7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 485CB54127B614CF006CCEC7 /* Assets.xcassets */; };
@@ -26,6 +28,9 @@
2628
485CB5C027B61F1E006CCEC7 /* SignedOutAuthSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB5BC27B61F1D006CCEC7 /* SignedOutAuthSessionTests.swift */; };
2729
485CB5C127B61F1E006CCEC7 /* AuthSignOutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB5BD27B61F1D006CCEC7 /* AuthSignOutTests.swift */; };
2830
485CB5C227B61F1E006CCEC7 /* AuthSRPSignInTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB5BE27B61F1D006CCEC7 /* AuthSRPSignInTests.swift */; };
31+
48916F382A412B2800E3E1B1 /* TOTPSetupWhenAuthenticatedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48916F372A412B2800E3E1B1 /* TOTPSetupWhenAuthenticatedTests.swift */; };
32+
48916F3A2A412CEE00E3E1B1 /* TOTPHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48916F392A412CEE00E3E1B1 /* TOTPHelper.swift */; };
33+
48916F3C2A42333E00E3E1B1 /* MFAPreferenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48916F3B2A42333E00E3E1B1 /* MFAPreferenceTests.swift */; };
2934
48E3AB3128E52590004EE395 /* GetCurrentUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48E3AB3028E52590004EE395 /* GetCurrentUserTests.swift */; };
3035
681B76952A3CB8DA004B59D9 /* AuthHostAppApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB53D27B614CE006CCEC7 /* AuthHostAppApp.swift */; };
3136
681B76962A3CB8DD004B59D9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485CB53F27B614CE006CCEC7 /* ContentView.swift */; };
@@ -118,10 +123,12 @@
118123
4821B2F1286B5F74000EC1D7 /* AuthDeleteUserTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthDeleteUserTests.swift; sourceTree = "<group>"; };
119124
4821B2F328737130000EC1D7 /* AuthCustomSignInTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthCustomSignInTests.swift; sourceTree = "<group>"; };
120125
4834D7C028B0770800DD564B /* FederatedSessionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FederatedSessionTests.swift; sourceTree = "<group>"; };
126+
483B0D332A42BB1400A1196B /* TOTPSetupWhenUnauthenticatedTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TOTPSetupWhenUnauthenticatedTests.swift; sourceTree = "<group>"; };
121127
484834BB27B6ED8700649D11 /* CredentialStoreConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CredentialStoreConfigurationTests.swift; sourceTree = "<group>"; };
122128
484834BD27B6FD9B00649D11 /* AuthEnvironmentHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthEnvironmentHelper.swift; sourceTree = "<group>"; };
123129
484EDEB127F4FFBE000284B4 /* AuthEventIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthEventIntegrationTests.swift; sourceTree = "<group>"; };
124130
485105A92840513C002D6FC8 /* AuthUserAttributesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthUserAttributesTests.swift; sourceTree = "<group>"; };
131+
48599D492A429893009DE21C /* MFASignInTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MFASignInTests.swift; sourceTree = "<group>"; };
125132
485CB53A27B614CE006CCEC7 /* AuthHostApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AuthHostApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
126133
485CB53D27B614CE006CCEC7 /* AuthHostAppApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthHostAppApp.swift; sourceTree = "<group>"; };
127134
485CB53F27B614CE006CCEC7 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -136,6 +143,9 @@
136143
485CB5BC27B61F1D006CCEC7 /* SignedOutAuthSessionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignedOutAuthSessionTests.swift; sourceTree = "<group>"; };
137144
485CB5BD27B61F1D006CCEC7 /* AuthSignOutTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthSignOutTests.swift; sourceTree = "<group>"; };
138145
485CB5BE27B61F1D006CCEC7 /* AuthSRPSignInTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthSRPSignInTests.swift; sourceTree = "<group>"; };
146+
48916F372A412B2800E3E1B1 /* TOTPSetupWhenAuthenticatedTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TOTPSetupWhenAuthenticatedTests.swift; sourceTree = "<group>"; };
147+
48916F392A412CEE00E3E1B1 /* TOTPHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TOTPHelper.swift; sourceTree = "<group>"; };
148+
48916F3B2A42333E00E3E1B1 /* MFAPreferenceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MFAPreferenceTests.swift; sourceTree = "<group>"; };
139149
48E3AB3028E52590004EE395 /* GetCurrentUserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetCurrentUserTests.swift; sourceTree = "<group>"; };
140150
681B76802A3CB86B004B59D9 /* AuthWatchApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AuthWatchApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
141151
681B76C42A3CBBAE004B59D9 /* AuthIntegrationTestsWatch.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AuthIntegrationTestsWatch.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -288,6 +298,7 @@
288298
485CB5A027B61E04006CCEC7 /* AuthIntegrationTests */ = {
289299
isa = PBXGroup;
290300
children = (
301+
48916F362A412AF800E3E1B1 /* MFATests */,
291302
97B370C32878DA3500F1C088 /* DeviceTests */,
292303
4821B2F0286B5F74000EC1D7 /* AuthDeleteUserTests */,
293304
485105A82840510B002D6FC8 /* AuthUserAttributesTests */,
@@ -311,6 +322,7 @@
311322
485CB5B727B61F0F006CCEC7 /* AuthSessionHelper.swift */,
312323
485CB5B827B61F0F006CCEC7 /* AuthSignInHelper.swift */,
313324
484834BD27B6FD9B00649D11 /* AuthEnvironmentHelper.swift */,
325+
48916F392A412CEE00E3E1B1 /* TOTPHelper.swift */,
314326
);
315327
path = Helpers;
316328
sourceTree = "<group>";
@@ -351,6 +363,17 @@
351363
name = Packages;
352364
sourceTree = "<group>";
353365
};
366+
48916F362A412AF800E3E1B1 /* MFATests */ = {
367+
isa = PBXGroup;
368+
children = (
369+
48916F372A412B2800E3E1B1 /* TOTPSetupWhenAuthenticatedTests.swift */,
370+
48916F3B2A42333E00E3E1B1 /* MFAPreferenceTests.swift */,
371+
48599D492A429893009DE21C /* MFASignInTests.swift */,
372+
483B0D332A42BB1400A1196B /* TOTPSetupWhenUnauthenticatedTests.swift */,
373+
);
374+
path = MFATests;
375+
sourceTree = "<group>";
376+
};
354377
681DFEA728E747B80000C36A /* AsyncTesting */ = {
355378
isa = PBXGroup;
356379
children = (
@@ -678,23 +701,28 @@
678701
485CB5C227B61F1E006CCEC7 /* AuthSRPSignInTests.swift in Sources */,
679702
9737C7502880BFD600DA0D2B /* AuthForgetDeviceTests.swift in Sources */,
680703
B43C26CB27BC9D54003F3BF7 /* AuthConfirmSignUpTests.swift in Sources */,
704+
48916F3C2A42333E00E3E1B1 /* MFAPreferenceTests.swift in Sources */,
681705
485CB5C127B61F1E006CCEC7 /* AuthSignOutTests.swift in Sources */,
682706
97B370C52878DA5A00F1C088 /* AuthFetchDeviceTests.swift in Sources */,
707+
483B0D342A42BB1400A1196B /* TOTPSetupWhenUnauthenticatedTests.swift in Sources */,
683708
681DFEAC28E747B80000C36A /* AsyncExpectation.swift in Sources */,
684709
48E3AB3128E52590004EE395 /* GetCurrentUserTests.swift in Sources */,
710+
48916F3A2A412CEE00E3E1B1 /* TOTPHelper.swift in Sources */,
685711
485CB5B127B61EAC006CCEC7 /* AWSAuthBaseTest.swift in Sources */,
686712
485CB5C027B61F1E006CCEC7 /* SignedOutAuthSessionTests.swift in Sources */,
687713
485CB5BA27B61F10006CCEC7 /* AuthSignInHelper.swift in Sources */,
688714
4834D7C128B0770800DD564B /* FederatedSessionTests.swift in Sources */,
689715
4821B2F428737130000EC1D7 /* AuthCustomSignInTests.swift in Sources */,
690716
484EDEB227F4FFBE000284B4 /* AuthEventIntegrationTests.swift in Sources */,
691717
484834BE27B6FD9B00649D11 /* AuthEnvironmentHelper.swift in Sources */,
718+
48916F382A412B2800E3E1B1 /* TOTPSetupWhenAuthenticatedTests.swift in Sources */,
692719
484834BC27B6ED8800649D11 /* CredentialStoreConfigurationTests.swift in Sources */,
693720
9737C74E287E208400DA0D2B /* AuthRememberDeviceTests.swift in Sources */,
694721
681DFEAD28E747B80000C36A /* XCTestCase+AsyncTesting.swift in Sources */,
695722
B43C26CC27BC9D54003F3BF7 /* AuthResendSignUpCodeTests.swift in Sources */,
696723
97829201286B802E000DE190 /* AuthResetPasswordTests.swift in Sources */,
697724
485105AA2840513C002D6FC8 /* AuthUserAttributesTests.swift in Sources */,
725+
48599D4A2A429893009DE21C /* MFASignInTests.swift in Sources */,
698726
485CB5BF27B61F1E006CCEC7 /* SignedInAuthSessionTests.swift in Sources */,
699727
B43C26CA27BC9D54003F3BF7 /* AuthSignUpTests.swift in Sources */,
700728
97829203286E41FA000DE190 /* AuthConfirmResetPasswordTests.swift in Sources */,

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/AWSAuthBaseTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AWSAuthBaseTest: XCTestCase {
1616
var defaultTestEmail = "test-\(UUID().uuidString)@amazon.com"
1717
var defaultTestPassword = UUID().uuidString
1818

19-
let amplifyConfigurationFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-amplifyconfiguration"
19+
var amplifyConfigurationFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-amplifyconfiguration"
2020
let credentialsFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-credentials"
2121

2222
var amplifyConfiguration: AmplifyConfiguration!

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/Helpers/AuthSignInHelper.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,43 @@ enum AuthSignInHelper {
1717
}
1818
}
1919

20-
static func signUpUser(username: String, password: String, email: String) async throws -> Bool {
21-
let options = AuthSignUpRequest.Options(userAttributes: [AuthUserAttribute(.email, value: email)])
22-
let result = try await Amplify.Auth.signUp(username: username, password: password, options: options)
23-
return result.isSignUpComplete
24-
}
20+
static func signUpUser(
21+
username: String,
22+
password: String,
23+
email: String,
24+
phoneNumber: String? = nil) async throws -> Bool {
25+
26+
var userAttributes = [
27+
AuthUserAttribute(.email, value: email)
28+
]
29+
if let phoneNumber = phoneNumber {
30+
userAttributes.append(AuthUserAttribute(.phoneNumber, value: phoneNumber))
31+
}
32+
33+
let options = AuthSignUpRequest.Options(
34+
userAttributes: userAttributes)
35+
let result = try await Amplify.Auth.signUp(username: username, password: password, options: options)
36+
return result.isSignUpComplete
37+
}
2538

2639
static func signInUser(username: String, password: String) async throws -> AuthSignInResult {
2740
return try await Amplify.Auth.signIn(username: username, password: password, options: nil)
2841
}
2942

30-
static func registerAndSignInUser(username: String, password: String, email: String) async throws -> Bool {
31-
let signedUp = try await AuthSignInHelper.signUpUser(username: username, password: password, email: email)
32-
guard signedUp else {
33-
throw AuthError.invalidState("Auth sign up failed", "", nil)
43+
static func registerAndSignInUser(
44+
username: String,
45+
password: String,
46+
email: String,
47+
phoneNumber: String? = nil) async throws -> Bool {
48+
let signedUp = try await AuthSignInHelper.signUpUser(
49+
username: username,
50+
password: password,
51+
email: email,
52+
phoneNumber: phoneNumber)
53+
guard signedUp else {
54+
throw AuthError.invalidState("Auth sign up failed", "", nil)
55+
}
56+
let result = try await AuthSignInHelper.signInUser(username: username, password: password)
57+
return result.isSignedIn
3458
}
35-
let result = try await AuthSignInHelper.signInUser(username: username, password: password)
36-
return result.isSignedIn
37-
}
3859
}

0 commit comments

Comments
 (0)