Skip to content

Commit cb4ec79

Browse files
committed
chore(auth): Fix SignedInAuthSessionTests
1 parent a29f790 commit cb4ec79

File tree

4 files changed

+70
-47
lines changed

4 files changed

+70
-47
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/AWSAuthBaseTest.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ class AWSAuthBaseTest: XCTestCase {
1818

1919
let amplifyConfigurationFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-amplifyconfiguration"
2020

21+
func initializeAmplifyWithError() throws {
22+
let configuration = try TestConfigHelper.retrieveAmplifyConfiguration(
23+
forResource: amplifyConfigurationFile)
24+
let authPlugin = AWSCognitoAuthPlugin()
25+
try Amplify.add(plugin: authPlugin)
26+
try Amplify.configure(configuration)
27+
}
28+
2129
func initializeAmplify() {
2230
do {
23-
let configuration = try TestConfigHelper.retrieveAmplifyConfiguration(
24-
forResource: amplifyConfigurationFile)
25-
let authPlugin = AWSCognitoAuthPlugin()
26-
try Amplify.add(plugin: authPlugin)
27-
try Amplify.configure(configuration)
31+
try initializeAmplifyWithError()
2832
print("Amplify configured with auth plugin")
2933
} catch {
3034
print(error)

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/AuthSessionTests/SignedInAuthSessionTests.swift

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import AmplifyTestCommon
1414

1515
class SignedInAuthSessionTests: AWSAuthBaseTest {
1616

17-
override func setUp() {
18-
super.setUp()
19-
initializeAmplify()
17+
override func setUpWithError() throws {
18+
try initializeAmplifyWithError()
2019
AuthSessionHelper.clearKeychain()
2120
}
2221

23-
override func tearDown() {
24-
super.tearDown()
22+
override func tearDownWithError() throws {
23+
_ = Amplify.Auth.signOut()
2524
Amplify.reset()
2625
sleep(2)
2726
AuthSessionHelper.clearKeychain()
@@ -41,21 +40,21 @@ class SignedInAuthSessionTests: AWSAuthBaseTest {
4140
let signInExpectation = expectation(description: "SignIn operation should complete")
4241
AuthSignInHelper.registerAndSignInUser(username: username, password: password,
4342
email: email) { didSucceed, error in
44-
signInExpectation.fulfill()
43+
if didSucceed {
44+
signInExpectation.fulfill()
45+
}
4546
XCTAssertTrue(didSucceed, "SignIn operation failed - \(String(describing: error))")
4647
}
4748
wait(for: [signInExpectation], timeout: networkTimeout)
4849

4950
let authSessionExpectation = expectation(description: "Received event result from fetchAuth")
5051
_ = Amplify.Auth.fetchAuthSession { result in
51-
defer {
52-
authSessionExpectation.fulfill()
53-
}
5452
switch result {
53+
case .failure(let error):
54+
XCTFail("Unable to fetch session: \(error)")
5555
case .success(let session):
5656
XCTAssertTrue(session.isSignedIn, "Session state should be signed In")
57-
case .failure(let error):
58-
XCTFail("Should not receive error \(error)")
57+
authSessionExpectation.fulfill()
5958
}
6059
}
6160
wait(for: [authSessionExpectation], timeout: networkTimeout)
@@ -69,52 +68,64 @@ class SignedInAuthSessionTests: AWSAuthBaseTest {
6968
/// - Then:
7069
/// - I should get the signedin state as true but with token result as sessionExpired
7170
///
72-
func testSessionExpired() {
71+
func testSessionExpired() throws {
7372
let username = "integTest\(UUID().uuidString)"
7473
let password = "P123@\(UUID().uuidString)"
7574
let signInExpectation = expectation(description: "SignIn operation should complete")
7675
AuthSignInHelper.registerAndSignInUser(username: username, password: password,
7776
email: email) { didSucceed, error in
78-
signInExpectation.fulfill()
77+
if didSucceed {
78+
signInExpectation.fulfill()
79+
}
7980
XCTAssertTrue(didSucceed, "SignIn operation failed - \(String(describing: error))")
8081
}
8182
wait(for: [signInExpectation], timeout: networkTimeout)
8283

83-
let authSessionExpectation = expectation(description: "Received event result from fetchAuth")
84+
let originalSessionExpectation = expectation(description: "Received event result from fetchAuth")
85+
var originalSession: AuthSession?
8486
_ = Amplify.Auth.fetchAuthSession { result in
85-
defer {
86-
authSessionExpectation.fulfill()
87-
}
88-
89-
do {
90-
let authSession = try result.get() as? AuthCognitoTokensProvider
91-
_ = try authSession?.getCognitoTokens().get()
92-
} catch {
93-
XCTFail("Should not receive error \(error)")
87+
switch result {
88+
case .failure(let error):
89+
XCTFail("Unable to retreive session \(error)")
90+
case .success(let session):
91+
XCTAssertTrue(session.isSignedIn)
92+
originalSession = session
93+
originalSessionExpectation.fulfill()
9494
}
9595
}
96-
wait(for: [authSessionExpectation], timeout: networkTimeout)
96+
wait(for: [originalSessionExpectation], timeout: networkTimeout)
97+
98+
let originalTokenProvider = try XCTUnwrap(originalSession as? AuthCognitoTokensProvider)
99+
_ = try originalTokenProvider.getCognitoTokens().get()
97100

98101
// Manually invalidate the tokens and then try to fetch the session.
99102
AuthSessionHelper.invalidateSession(username: username)
100-
let authSessionExpiredExpectation = expectation(description: "Received event result from fetchAuth")
103+
let postExpirationExpectation = expectation(description: "Received event result from fetchAuth")
104+
var postExpirationSession: AuthSession?
101105
_ = Amplify.Auth.fetchAuthSession { result in
102-
defer {
103-
authSessionExpiredExpectation.fulfill()
106+
switch result {
107+
case .failure(let error):
108+
XCTFail("Unable to retreive session \(error)")
109+
case .success(let session):
110+
XCTAssertTrue(session.isSignedIn)
111+
postExpirationSession = session
112+
postExpirationExpectation.fulfill()
104113
}
114+
}
115+
wait(for: [postExpirationExpectation], timeout: networkTimeout)
105116

106-
do {
107-
let authSession = try result.get() as? AuthCognitoTokensProvider
108-
_ = try authSession?.getCognitoTokens().get()
109-
XCTFail("Should not receive a valid token")
110-
} catch {
111-
guard let authError = error as? AuthError,
112-
case .sessionExpired = authError else {
113-
XCTFail("Should receive a session expired error")
114-
return
115-
}
117+
let postExpirationTokenProvider = try XCTUnwrap(postExpirationSession as? AuthCognitoTokensProvider)
118+
let tokenResult = postExpirationTokenProvider.getCognitoTokens()
119+
switch tokenResult {
120+
case .failure(let error):
121+
switch error {
122+
case .sessionExpired:
123+
break
124+
default:
125+
XCTFail("Unexpected error case: \(error)")
116126
}
127+
case .success(let tokens):
128+
XCTFail("Unexpected tokens: \(String(describing: tokens).prefix(64))")
117129
}
118-
wait(for: [authSessionExpiredExpectation], timeout: networkTimeout)
119130
}
120131
}

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# AWSCognitoAuthPlugin Integration tests
22

3-
The following steps demonstrate how to setup the integration tests for auth plugin.
3+
The following steps demonstrate how to setup the integration tests for the auth plugin.
44

55
## CLI setup
66

7-
The integration test require auth configured with AWS Cognito User Pool and AWS Cognito Identity Pool.
7+
The integration tests require auth configured with AWS Cognito User Pool and
8+
AWS Cognito Identity Pool. So, the first time you attempt to run the
9+
integration tests, you may have to do the following from the (directory
10+
containing the xcodeproj)[../]:
811

912
```
1013
amplify add auth
@@ -99,4 +102,4 @@ This will create a amplifyconfiguration.json file in your local, copy that file
99102

100103
```
101104
cp amplifyconfiguration.json ~/.aws-amplify/amplify-ios/testconfiguration/AWSCognitoAuthPluginIntegrationTests-amplifyconfiguration.json
102-
```
105+
```

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/Support/AuthSessionHelper.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ struct AuthSessionHelper {
2020
static func invalidateSession(username: String) {
2121
let bundleID = Bundle.main.bundleIdentifier
2222
let keychain = AWSUICKeyChainStore(service: "\(bundleID!).\(AWSCognitoIdentityUserPool.self)")
23-
let namespace = "\(AWSMobileClient.default().userPoolClient!.userPoolConfiguration.clientId).\(username)"
23+
let clientId = AWSMobileClient.default().userPoolClient!.userPoolConfiguration.clientId
24+
25+
// Please note that the clientId + username namespace combination is
26+
// normalized by converting it to a lower-case string somewhere upstream
27+
// of the AWSUICKeyChainStore. So, the same is done below.
28+
let namespace = "\(clientId).\(username)".lowercased()
2429
let expirationKey = "\(namespace).tokenExpiration"
2530
let refreshTokenKey = "\(namespace).refreshToken"
2631

0 commit comments

Comments
 (0)