Skip to content

Commit a29f790

Browse files
committed
chore(auth) Wait for tests operations to complete
1 parent a58eeea commit a29f790

14 files changed

+830
-107
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
5653A9F22979B5F400AC6D82 /* AuthHubEventHandlerSignInApiTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9F12979B5F400AC6D82 /* AuthHubEventHandlerSignInApiTests.swift */; };
1919
5653A9F42979BF7700AC6D82 /* AuthHubEventHandlerSignOutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9F32979BF7700AC6D82 /* AuthHubEventHandlerSignOutTests.swift */; };
2020
5653A9F62979C2FC00AC6D82 /* AuthHubEventHandlerUserDeletedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9F52979C2FC00AC6D82 /* AuthHubEventHandlerUserDeletedTests.swift */; };
21+
5653A9F8297AE40200AC6D82 /* AWSCognitoAuthPluginTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9F7297AE40200AC6D82 /* AWSCognitoAuthPluginTests.swift */; };
22+
5653A9FA297B157900AC6D82 /* AWSCognitoAuthPluginUserDefaultsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9F9297B157900AC6D82 /* AWSCognitoAuthPluginUserDefaultsTests.swift */; };
23+
5653A9FC297B1F8C00AC6D82 /* AuthenticationProviderAdapterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9FB297B1F8C00AC6D82 /* AuthenticationProviderAdapterTests.swift */; };
24+
5653A9FE297B229600AC6D82 /* AuthorizationProviderAdapterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653A9FD297B229600AC6D82 /* AuthorizationProviderAdapterTests.swift */; };
2125
5C33248B2772384600F2C47B /* AWSAuthDeleteUserOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C33248A2772384600F2C47B /* AWSAuthDeleteUserOperation.swift */; };
2226
5C33248D27723D0800F2C47B /* AuthenticationProviderAdapter+DeleteUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C33248C27723D0800F2C47B /* AuthenticationProviderAdapter+DeleteUser.swift */; };
2327
5CD3C21A277D275D007DB926 /* AuthDeleteUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CD3C219277D275D007DB926 /* AuthDeleteUserTests.swift */; };
@@ -210,6 +214,10 @@
210214
5653A9F12979B5F400AC6D82 /* AuthHubEventHandlerSignInApiTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthHubEventHandlerSignInApiTests.swift; sourceTree = "<group>"; };
211215
5653A9F32979BF7700AC6D82 /* AuthHubEventHandlerSignOutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthHubEventHandlerSignOutTests.swift; sourceTree = "<group>"; };
212216
5653A9F52979C2FC00AC6D82 /* AuthHubEventHandlerUserDeletedTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthHubEventHandlerUserDeletedTests.swift; sourceTree = "<group>"; };
217+
5653A9F7297AE40200AC6D82 /* AWSCognitoAuthPluginTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AWSCognitoAuthPluginTests.swift; sourceTree = "<group>"; };
218+
5653A9F9297B157900AC6D82 /* AWSCognitoAuthPluginUserDefaultsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AWSCognitoAuthPluginUserDefaultsTests.swift; sourceTree = "<group>"; };
219+
5653A9FB297B1F8C00AC6D82 /* AuthenticationProviderAdapterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationProviderAdapterTests.swift; sourceTree = "<group>"; };
220+
5653A9FD297B229600AC6D82 /* AuthorizationProviderAdapterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthorizationProviderAdapterTests.swift; sourceTree = "<group>"; };
213221
5C33248A2772384600F2C47B /* AWSAuthDeleteUserOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AWSAuthDeleteUserOperation.swift; sourceTree = "<group>"; };
214222
5C33248C27723D0800F2C47B /* AuthenticationProviderAdapter+DeleteUser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AuthenticationProviderAdapter+DeleteUser.swift"; sourceTree = "<group>"; };
215223
5CD3C219277D275D007DB926 /* AuthDeleteUserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthDeleteUserTests.swift; sourceTree = "<group>"; };
@@ -445,6 +453,7 @@
445453
B4136E3D256D76960011210B /* AuthenticationProviderSignoutTests.swift */,
446454
B4060294256C21BC00D23D50 /* AuthenticationProviderSignupTests.swift */,
447455
B402C915257700610020B83B /* BaseAuthenticationProviderTest.swift */,
456+
5653A9FB297B1F8C00AC6D82 /* AuthenticationProviderAdapterTests.swift */,
448457
);
449458
path = AuthenticationProviderTests;
450459
sourceTree = "<group>";
@@ -542,6 +551,7 @@
542551
B4136E5D256D7A690011210B /* AuthorizationProviderSessionSignInTests.swift */,
543552
B4136E63256D7A740011210B /* AuthorizationProviderSessionSignoutTests.swift */,
544553
B4D5411A256F2C8A00436E5C /* BaseAuthorizationProviderTest.swift */,
554+
5653A9FD297B229600AC6D82 /* AuthorizationProviderAdapterTests.swift */,
545555
);
546556
path = AuthorizationProviderTests;
547557
sourceTree = "<group>";
@@ -789,6 +799,7 @@
789799
B43B4DD72565E803008F345D /* Mocks */,
790800
B41D0FDC2475A3A10049D08D /* Utils */,
791801
B41D0FE02475A3A10049D08D /* Info.plist */,
802+
5653A9F7297AE40200AC6D82 /* AWSCognitoAuthPluginTests.swift */,
792803
);
793804
path = AWSCognitoAuthPluginTests;
794805
sourceTree = "<group>";
@@ -797,6 +808,7 @@
797808
isa = PBXGroup;
798809
children = (
799810
B41D0FDD2475A3A10049D08D /* AuthUserAttributeKeyTests.swift */,
811+
5653A9F9297B157900AC6D82 /* AWSCognitoAuthPluginUserDefaultsTests.swift */,
800812
);
801813
path = Utils;
802814
sourceTree = "<group>";
@@ -1535,8 +1547,10 @@
15351547
files = (
15361548
B4136E1A256D73D80011210B /* AuthenticationProviderSigninTests.swift in Sources */,
15371549
B4136E32256D76640011210B /* AuthenticationProviderSigninWithWebUITests.swift in Sources */,
1550+
5653A9FC297B1F8C00AC6D82 /* AuthenticationProviderAdapterTests.swift in Sources */,
15381551
FA1C817D25868C46006160E9 /* AWSCognitoAuthPluginAmplifyVersionableTests.swift in Sources */,
15391552
B4136E9A256D7B700011210B /* AuthDeviceRememberDeviceTests.swift in Sources */,
1553+
5653A9FA297B157900AC6D82 /* AWSCognitoAuthPluginUserDefaultsTests.swift in Sources */,
15401554
B4136E4E256D79710011210B /* AuthenticationProviderConfirmResetPasswordTests.swift in Sources */,
15411555
B43B4DE32565E8D7008F345D /* MockAuthorizationProviderBehavior.swift in Sources */,
15421556
5653A9F22979B5F400AC6D82 /* AuthHubEventHandlerSignInApiTests.swift in Sources */,
@@ -1549,6 +1563,7 @@
15491563
B43B4E1A2565FBFE008F345D /* AWSCognitoAuthDeviceBehaviorTests.swift in Sources */,
15501564
B43B4E032565E95E008F345D /* MockAuthDeviceServiceBehavior.swift in Sources */,
15511565
B4136E6C256D7AF30011210B /* UserBehaviorFetchAttributeTests.swift in Sources */,
1566+
5653A9F8297AE40200AC6D82 /* AWSCognitoAuthPluginTests.swift in Sources */,
15521567
B4D5411B256F2C8A00436E5C /* BaseAuthorizationProviderTest.swift in Sources */,
15531568
B43B4E102565EA64008F345D /* AWSCognitoAuthClientBehaviorTests.swift in Sources */,
15541569
B4136E2C256D764B0011210B /* AuthenticationProviderResendSignupCodeTests.swift in Sources */,
@@ -1569,6 +1584,7 @@
15691584
5653A9F62979C2FC00AC6D82 /* AuthHubEventHandlerUserDeletedTests.swift in Sources */,
15701585
5653A9F42979BF7700AC6D82 /* AuthHubEventHandlerSignOutTests.swift in Sources */,
15711586
B4060295256C21BC00D23D50 /* AuthenticationProviderSignupTests.swift in Sources */,
1587+
5653A9FE297B229600AC6D82 /* AuthorizationProviderAdapterTests.swift in Sources */,
15721588
B41D0FE22475A3A10049D08D /* AuthUserAttributeKeyTests.swift in Sources */,
15731589
5653A9EC2979B42100AC6D82 /* MockAuthSession.swift in Sources */,
15741590
B4136E26256D76300011210B /* AuthenticationProviderConfirmSigninTests.swift in Sources */,

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Support/Utils/AWSCognitoAuthPluginUserDefaults.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ struct AWSCognitoAuthPluginUserDefaults: AWSCognitoAuthPluginUserDefaultsBehavio
1111

1212
private let preferPrivateSessionKey = "AWSCognitoAuthPluginUserDefaults.privateSessionKey"
1313

14+
var defaults: UserDefaults = .standard
15+
1416
func storePreferredBrowserSession(privateSessionPrefered: Bool) {
15-
UserDefaults.standard.setValue(privateSessionPrefered, forKey: preferPrivateSessionKey)
17+
defaults.setValue(privateSessionPrefered, forKey: preferPrivateSessionKey)
1618
}
1719

1820
func isPrivateSessionPreferred() -> Bool {
19-
return UserDefaults.standard.bool(forKey: preferPrivateSessionKey)
21+
return defaults.bool(forKey: preferPrivateSessionKey)
2022
}
2123
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import Amplify
10+
@testable import AWSCognitoAuthPlugin
11+
@testable import AWSMobileClient
12+
13+
final class AWSCognitoAuthPluginTests: XCTestCase {
14+
15+
var systemUnderTest: AWSCognitoAuthPlugin!
16+
var authorizationProvider: MockAuthorizationProviderBehavior!
17+
18+
override func setUpWithError() throws {
19+
authorizationProvider = MockAuthorizationProviderBehavior()
20+
systemUnderTest = AWSCognitoAuthPlugin()
21+
systemUnderTest.authorizationProvider = authorizationProvider
22+
}
23+
24+
override func tearDownWithError() throws {
25+
systemUnderTest = nil
26+
}
27+
28+
/// - Given: A plugin configured with an authorization provider
29+
/// - When: An invalidateCachedTemporaryCredentials message is sent to the plugin
30+
/// - Then: The message is propagated to the authorization provider
31+
func testInvalidateCachedTemporaryCredentials() throws {
32+
systemUnderTest.invalidateCachedTemporaryCredentials()
33+
XCTAssertEqual(authorizationProvider.interactions, [
34+
"invalidateCachedTemporaryCredentials()"
35+
])
36+
}
37+
38+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
@testable import AWSCognitoAuthPlugin
9+
import XCTest
10+
11+
final class AuthenticationProviderAdapterTests: XCTestCase {
12+
13+
var systemUnderTest: AuthenticationProviderAdapter!
14+
var mobileClient: MockAWSMobileClient!
15+
var authUserDefaults: AWSCognitoAuthPluginUserDefaults!
16+
17+
override func setUpWithError() throws {
18+
let userDefaults = try XCTUnwrap(UserDefaults(suiteName: #file))
19+
userDefaults.removePersistentDomain(forName: #file)
20+
21+
mobileClient = MockAWSMobileClient()
22+
authUserDefaults = AWSCognitoAuthPluginUserDefaults(defaults: userDefaults)
23+
systemUnderTest = AuthenticationProviderAdapter(awsMobileClient: mobileClient,
24+
userdefaults: authUserDefaults)
25+
}
26+
27+
override func tearDownWithError() throws {
28+
mobileClient = nil
29+
authUserDefaults = nil
30+
systemUnderTest = nil
31+
}
32+
33+
/// - Given: A newly-initialized adapter
34+
/// - When: The current user is requested
35+
/// - Then: It builds its result from the underlying mobile client's username and userSub
36+
func testGetCurrentUser() throws {
37+
let username = UUID().uuidString
38+
mobileClient.username = username
39+
40+
let userSub = UUID().uuidString
41+
mobileClient.userSub = userSub
42+
43+
let user = try XCTUnwrap(systemUnderTest.getCurrentUser())
44+
XCTAssertEqual(user.username, username)
45+
XCTAssertEqual(user.username, username)
46+
XCTAssertEqual(mobileClient.interactions, [
47+
"getUsername()",
48+
"getUserSub()"
49+
])
50+
}
51+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Amplify
9+
@testable import AWSCognitoAuthPlugin
10+
import AWSCore
11+
import XCTest
12+
13+
final class AuthorizationProviderAdapterTests: XCTestCase {
14+
15+
var systemUnderTest: AuthorizationProviderAdapter!
16+
var mobileClient: MockAWSMobileClient!
17+
18+
override func setUpWithError() throws {
19+
mobileClient = MockAWSMobileClient()
20+
mobileClient.awsCredentialsMockResult = .success(AWSCredentials(accessKey: UUID().uuidString,
21+
secretKey: UUID().uuidString,
22+
sessionKey: UUID().uuidString,
23+
expiration: Date.distantFuture))
24+
systemUnderTest = AuthorizationProviderAdapter(awsMobileClient: mobileClient)
25+
}
26+
27+
override func tearDownWithError() throws {
28+
mobileClient = nil
29+
systemUnderTest = nil
30+
}
31+
32+
/// - Given: A newly initialized adapter
33+
/// - When: No other direct interaction has taken place
34+
/// - Then: The mobile client has received an "addUserStateListener" message
35+
func testInitializationSideEffects() throws {
36+
XCTAssertEqual(mobileClient.interactions, ["addUserStateListener(_:_:)"])
37+
}
38+
39+
/// - Given: A mobile client user state of "guest"
40+
/// - When: A fetchSession message is sent to the adapter
41+
/// - Then: The credentials and identity are requested from the mobile client
42+
func testFetchSessionForGuest() throws {
43+
mobileClient.mockCurrentUserState = .guest
44+
45+
let request = AuthFetchSessionRequest(options: .init())
46+
let fetchSessionExpectation = expectation(description: "fetchSession")
47+
systemUnderTest.fetchSession(request: request) { result in
48+
switch result {
49+
case .success:
50+
fetchSessionExpectation.fulfill()
51+
case .failure(let error):
52+
XCTFail(error.localizedDescription)
53+
}
54+
}
55+
wait(for: [fetchSessionExpectation], timeout: 1.0)
56+
XCTAssertEqual(mobileClient.interactions, [
57+
"addUserStateListener(_:_:)",
58+
"getCurrentUserState()",
59+
"getAWSCredentials(_:)",
60+
"getIdentityId()"
61+
])
62+
}
63+
64+
/// - Given: A mobile client user state of "signedIn"
65+
/// - When: A fetchSession message is sent to the adapter
66+
/// - Then: The session's tokens are requested from the mobile client
67+
func testFetchSessionForSignedIn() throws {
68+
mobileClient.mockCurrentUserState = .signedIn
69+
70+
let request = AuthFetchSessionRequest(options: .init())
71+
let fetchSessionExpectation = expectation(description: "fetchSession")
72+
systemUnderTest.fetchSession(request: request) { result in
73+
switch result {
74+
case .success:
75+
fetchSessionExpectation.fulfill()
76+
case .failure(let error):
77+
XCTFail(error.localizedDescription)
78+
}
79+
}
80+
wait(for: [fetchSessionExpectation], timeout: 1.0)
81+
XCTAssertEqual(mobileClient.interactions, [
82+
"addUserStateListener(_:_:)",
83+
"getCurrentUserState()",
84+
"getTokens(_:)"
85+
])
86+
}
87+
88+
/// - Given: A mobile client user state of "signedOut"
89+
/// - When: A fetchSession message is sent to the adapter
90+
/// - Then: The credentials and identity are requested from the mobile client
91+
func testFetchSessionForSignedOut() throws {
92+
mobileClient.mockCurrentUserState = .signedOut
93+
94+
let request = AuthFetchSessionRequest(options: .init())
95+
let fetchSessionExpectation = expectation(description: "fetchSession")
96+
systemUnderTest.fetchSession(request: request) { result in
97+
switch result {
98+
case .success:
99+
fetchSessionExpectation.fulfill()
100+
case .failure(let error):
101+
XCTFail(error.localizedDescription)
102+
}
103+
}
104+
wait(for: [fetchSessionExpectation], timeout: 1.0)
105+
XCTAssertEqual(mobileClient.interactions, [
106+
"addUserStateListener(_:_:)",
107+
"getCurrentUserState()",
108+
"getAWSCredentials(_:)",
109+
"getIdentityId()"
110+
])
111+
}
112+
113+
/// - Given: Newly initialized adapter
114+
/// - When: It receives a "invalidateCachedTemporaryCredentials" message
115+
/// - Then: The "invalidateCachedTemporaryCredentials" is propagated to the mobile client
116+
func testInvalidateCachedTemporaryCredentials() throws {
117+
systemUnderTest.invalidateCachedTemporaryCredentials()
118+
XCTAssertEqual(mobileClient.interactions, [
119+
"addUserStateListener(_:_:)",
120+
"invalidateCachedTemporaryCredentials()"
121+
])
122+
}
123+
}

0 commit comments

Comments
 (0)