Skip to content

Commit e63a33d

Browse files
authored
test(Auth): Integration test related to apis for session, signOut (#497)
1 parent a8b994b commit e63a33d

File tree

7 files changed

+295
-4
lines changed

7 files changed

+295
-4
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
B41D10092475AB7E0049D08D /* AWSCognitoAuthPlugin+DeviceBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D10022475AB7E0049D08D /* AWSCognitoAuthPlugin+DeviceBehavior.swift */; };
107107
B41D100A2475AB7E0049D08D /* AWSCognitoAuthPlugin+UserBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D10032475AB7E0049D08D /* AWSCognitoAuthPlugin+UserBehavior.swift */; };
108108
B43DC74F2410572400D40275 /* AWSCognitoAuthPlugin.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B43DC7452410572400D40275 /* AWSCognitoAuthPlugin.framework */; };
109+
B44373A1247C869100DEF43C /* AuthSessionHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B44373A0247C869100DEF43C /* AuthSessionHelper.swift */; };
110+
B44373A4247C8CCE00DEF43C /* AuthSignOutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B44373A3247C8CCE00DEF43C /* AuthSignOutTests.swift */; };
109111
B4F3EA47243A776800F23296 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B4F3EA41243A776800F23296 /* Assets.xcassets */; };
110112
B4F3EA48243A776800F23296 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B4F3EA42243A776800F23296 /* LaunchScreen.storyboard */; };
111113
B4F3EA49243A776800F23296 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B4F3EA44243A776800F23296 /* Main.storyboard */; };
@@ -255,6 +257,8 @@
255257
B41D10032475AB7E0049D08D /* AWSCognitoAuthPlugin+UserBehavior.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AWSCognitoAuthPlugin+UserBehavior.swift"; sourceTree = "<group>"; };
256258
B43DC7452410572400D40275 /* AWSCognitoAuthPlugin.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AWSCognitoAuthPlugin.framework; sourceTree = BUILT_PRODUCTS_DIR; };
257259
B43DC74E2410572400D40275 /* AWSCognitoAuthPluginTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AWSCognitoAuthPluginTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
260+
B44373A0247C869100DEF43C /* AuthSessionHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSessionHelper.swift; sourceTree = "<group>"; };
261+
B44373A3247C8CCE00DEF43C /* AuthSignOutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSignOutTests.swift; sourceTree = "<group>"; };
258262
B4672B5524777C7500C83E96 /* Amplify.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Amplify.framework; sourceTree = BUILT_PRODUCTS_DIR; };
259263
B4672B5624777C7500C83E96 /* AmplifyTestCommon.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = AmplifyTestCommon.framework; sourceTree = BUILT_PRODUCTS_DIR; };
260264
B4672B5724777C7500C83E96 /* AWSAuthCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = AWSAuthCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -322,6 +326,7 @@
322326
B40F167824784B3400CDB920 /* AuthDeviceTests */,
323327
B40F167424784B3400CDB920 /* AuthSessionTests */,
324328
B40F167224784B3400CDB920 /* AuthSignInTests */,
329+
B44373A2247C8A0C00DEF43C /* AuthSignOutTests */,
325330
B40F166E24784B3400CDB920 /* AuthSignUpTests */,
326331
B40F166C24784B3400CDB920 /* AuthUserTests */,
327332
B40F166424784B3400CDB920 /* Configuration */,
@@ -352,6 +357,7 @@
352357
children = (
353358
B40F166B24784B3400CDB920 /* AuthConfigurationHelper.swift */,
354359
B40F166A24784B3400CDB920 /* AuthSignInHelper.swift */,
360+
B44373A0247C869100DEF43C /* AuthSessionHelper.swift */,
355361
);
356362
path = Support;
357363
sourceTree = "<group>";
@@ -656,6 +662,14 @@
656662
name = Products;
657663
sourceTree = "<group>";
658664
};
665+
B44373A2247C8A0C00DEF43C /* AuthSignOutTests */ = {
666+
isa = PBXGroup;
667+
children = (
668+
B44373A3247C8CCE00DEF43C /* AuthSignOutTests.swift */,
669+
);
670+
path = AuthSignOutTests;
671+
sourceTree = "<group>";
672+
};
659673
B4F3EA21243A65BD00F23296 /* HostApp */ = {
660674
isa = PBXGroup;
661675
children = (
@@ -1204,10 +1218,12 @@
12041218
B40F168724784B3400CDB920 /* AuthDeviceOperationTests.swift in Sources */,
12051219
B40F168124784B3400CDB920 /* AuthConfirmSignUpTests.swift in Sources */,
12061220
B40F167E24784B3400CDB920 /* AuthConfigurationHelper.swift in Sources */,
1221+
B44373A1247C869100DEF43C /* AuthSessionHelper.swift in Sources */,
12071222
B40F167F24784B3400CDB920 /* AuthUserTests.swift in Sources */,
12081223
B40F168224784B3400CDB920 /* AuthResendSignUpCodeTests.swift in Sources */,
12091224
B40F168524784B3400CDB920 /* SignedInAuthSessionTests.swift in Sources */,
12101225
B40F168324784B3400CDB920 /* AuthUsernamePasswordSignInTests.swift in Sources */,
1226+
B44373A4247C8CCE00DEF43C /* AuthSignOutTests.swift in Sources */,
12111227
B40F168424784B3400CDB920 /* SignedOutAuthSessionTests.swift in Sources */,
12121228
B40F167D24784B3400CDB920 /* AuthSignInHelper.swift in Sources */,
12131229
B40F168024784B3400CDB920 /* AuthSignUpTests.swift in Sources */,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ struct AuthErrorHelper {
133133
.unableToSignIn(let message), // Called in signout, releaseSignInWait.
134134
.idTokenNotIssued(let message), // Not used anywhere.
135135
.userPoolNotConfigured(let message):
136-
// TODO: Hanlde the above errors inside the plugin #172336364
137136
return AuthError.unknown(message)
138137

139138
case .badRequest(let message),
@@ -145,7 +144,6 @@ struct AuthErrorHelper {
145144
return AuthError.unknown(message)
146145

147146
case .expiredRefreshToken(let message):
148-
// TODO: Should be handled by Auth.fetchSession #172336364
149147
return AuthError.unknown(message)
150148

151149
// These errors arise from the escape hatch methods.

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/AuthSessionTests/SignedInAuthSessionTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
//
77

88
import XCTest
9+
@testable import AWSMobileClient
910
@testable import Amplify
1011
import AWSCognitoAuthPlugin
12+
import AWSPluginsCore
1113

1214
class SignedInAuthSessionTests: AWSAuthBaseTest {
1315

@@ -22,6 +24,14 @@ class SignedInAuthSessionTests: AWSAuthBaseTest {
2224
sleep(2)
2325
}
2426

27+
/// Test if successful session is retreived after a user signin
28+
///
29+
/// - Given: A signedout Amplify Auth Category
30+
/// - When:
31+
/// - I sign in to the Auth Category
32+
/// - Then:
33+
/// - I should receive a valid session in signed in state
34+
///
2535
func testSuccessfulSessionFetch() {
2636
let username = "integTest\(UUID().uuidString)"
2737
let password = "P123@\(UUID().uuidString)"
@@ -46,4 +56,60 @@ class SignedInAuthSessionTests: AWSAuthBaseTest {
4656
}
4757
wait(for: [authSessionExpectation], timeout: networkTimeout)
4858
}
59+
60+
/// Test if I get sessionExpired error on session expiry
61+
///
62+
/// - Given: Valid signedIn session
63+
/// - When:
64+
/// - The session expired and I try to fetch the auth session
65+
/// - Then:
66+
/// - I should get the signedin state as true but with token result as sessionExpired
67+
///
68+
func testSessionExpired() {
69+
let username = "integTest\(UUID().uuidString)"
70+
let password = "P123@\(UUID().uuidString)"
71+
let signInExpectation = expectation(description: "SignIn operation should complete")
72+
AuthSignInHelper.registerAndSignInUser(username: username, password: password) { didSucceed, error in
73+
signInExpectation.fulfill()
74+
XCTAssertTrue(didSucceed, "SignIn operation failed - \(String(describing: error))")
75+
}
76+
wait(for: [signInExpectation], timeout: networkTimeout)
77+
78+
let authSessionExpectation = expectation(description: "Received event result from fetchAuth")
79+
_ = Amplify.Auth.fetchAuthSession { result in
80+
defer {
81+
authSessionExpectation.fulfill()
82+
}
83+
84+
do {
85+
let authSession = try result.get() as? AuthCognitoTokensProvider
86+
_ = try authSession?.getCognitoTokens().get()
87+
} catch {
88+
XCTFail("Should not receive error \(error)")
89+
}
90+
}
91+
wait(for: [authSessionExpectation], timeout: networkTimeout)
92+
93+
// Manually invalidate the tokens and then try to fetch the session.
94+
AuthSessionHelper.invalidateSession(username: username)
95+
let authSessionExpiredExpectation = expectation(description: "Received event result from fetchAuth")
96+
_ = Amplify.Auth.fetchAuthSession { result in
97+
defer {
98+
authSessionExpiredExpectation.fulfill()
99+
}
100+
101+
do {
102+
let authSession = try result.get() as? AuthCognitoTokensProvider
103+
_ = try authSession?.getCognitoTokens().get()
104+
XCTFail("Should not receive a valid token")
105+
} catch {
106+
guard let authError = error as? AuthError,
107+
case .sessionExpired(_, _, _) = authError else {
108+
XCTFail("Should receive a session expired error")
109+
return
110+
}
111+
}
112+
}
113+
wait(for: [authSessionExpiredExpectation], timeout: networkTimeout)
114+
}
49115
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
//
2+
// Copyright 2018-2020 Amazon.com,
3+
// Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import AWSMobileClient
10+
@testable import Amplify
11+
import AWSCognitoAuthPlugin
12+
import AWSPluginsCore
13+
14+
class AuthSignedOutTests: AWSAuthBaseTest {
15+
16+
override func setUp() {
17+
super.setUp()
18+
initializeAmplify()
19+
}
20+
21+
override func tearDown() {
22+
super.tearDown()
23+
Amplify.reset()
24+
sleep(2)
25+
}
26+
27+
/// Test if invoking signOut without unauthenticate state does not fail
28+
///
29+
/// - Given: An unauthenticated state
30+
/// - When:
31+
/// - I invoke signOut
32+
/// - Then:
33+
/// - I should get a successul result
34+
///
35+
func testSignedOutWithUnAuthState() {
36+
let operationExpectation = expectation(description: "Operation should complete")
37+
let operation = Amplify.Auth.signOut { result in
38+
defer {
39+
operationExpectation.fulfill()
40+
}
41+
switch result {
42+
case .success:
43+
print("Success signout")
44+
case .failure(let error):
45+
XCTFail("SignOut should not fail - \(error)")
46+
}
47+
}
48+
XCTAssertNotNil(operation, "SignOut operation should not be nil")
49+
wait(for: [operationExpectation], timeout: networkTimeout)
50+
}
51+
52+
/// Test signOut in authenticated state
53+
///
54+
/// - Given: An authenticated state
55+
/// - When:
56+
/// - I invoke signout
57+
/// - Then:
58+
/// - I should get successul result and the auth session should be signedout
59+
///
60+
func testSignedOutFromAuthState() {
61+
62+
let username = "integTest\(UUID().uuidString)"
63+
let password = "P123@\(UUID().uuidString)"
64+
65+
let signInExpectation = expectation(description: "SignIn operation should complete")
66+
AuthSignInHelper.registerAndSignInUser(username: username, password: password) { didSucceed, error in
67+
signInExpectation.fulfill()
68+
XCTAssertTrue(didSucceed, "SignIn operation failed - \(String(describing: error))")
69+
}
70+
wait(for: [signInExpectation], timeout: networkTimeout)
71+
72+
// Check if the auth session is signed in
73+
let authSignedInSessionExpectation = expectation(description: "Operation should complete")
74+
_ = Amplify.Auth.fetchAuthSession { result in
75+
defer {
76+
authSignedInSessionExpectation.fulfill()
77+
}
78+
do {
79+
let session = try result.get()
80+
XCTAssertTrue(session.isSignedIn, "Auth session should be signedin")
81+
} catch {
82+
XCTFail("Fetch auth session failed with error - \(error)")
83+
}
84+
}
85+
wait(for: [authSignedInSessionExpectation], timeout: networkTimeout)
86+
87+
let operationExpectation = expectation(description: "Operation should complete")
88+
let operation = Amplify.Auth.signOut { result in
89+
defer {
90+
operationExpectation.fulfill()
91+
}
92+
switch result {
93+
case .success:
94+
print("Success signout")
95+
case .failure(let error):
96+
XCTFail("SignOut should not fail - \(error)")
97+
}
98+
}
99+
XCTAssertNotNil(operation, "SignOut operation should not be nil")
100+
wait(for: [operationExpectation], timeout: networkTimeout)
101+
102+
// Check if the auth session is signed out
103+
let authSignedOutSessionExpectation = expectation(description: "Operation should complete")
104+
_ = Amplify.Auth.fetchAuthSession { result in
105+
defer {
106+
authSignedOutSessionExpectation.fulfill()
107+
}
108+
do {
109+
let session = try result.get()
110+
XCTAssertFalse(session.isSignedIn, "Auth session should be signedin")
111+
} catch {
112+
XCTFail("Fetch auth session failed with error - \(error)")
113+
}
114+
}
115+
wait(for: [authSignedOutSessionExpectation], timeout: networkTimeout)
116+
}
117+
118+
/// Test signOut in authenticated session expired state
119+
///
120+
/// - Given: An authenticated state with expired session
121+
/// - When:
122+
/// - I invoke signout
123+
/// - Then:
124+
/// - I should get successul result and the auth session should be signedout
125+
///
126+
func testSignedOutFromSessionExpiredState() {
127+
128+
let username = "integTest\(UUID().uuidString)"
129+
let password = "P123@\(UUID().uuidString)"
130+
131+
let signInExpectation = expectation(description: "SignIn operation should complete")
132+
AuthSignInHelper.registerAndSignInUser(username: username, password: password) { didSucceed, error in
133+
signInExpectation.fulfill()
134+
XCTAssertTrue(didSucceed, "SignIn operation failed - \(String(describing: error))")
135+
}
136+
wait(for: [signInExpectation], timeout: networkTimeout)
137+
AuthSessionHelper.invalidateSession(username: username)
138+
139+
// Check if the auth session is signed in
140+
let authSignedInSessionExpectation = expectation(description: "Operation should complete")
141+
_ = Amplify.Auth.fetchAuthSession { result in
142+
defer {
143+
authSignedInSessionExpectation.fulfill()
144+
}
145+
do {
146+
let session = try result.get()
147+
XCTAssertTrue(session.isSignedIn, "Auth session should be signedin")
148+
} catch {
149+
XCTFail("Fetch auth session failed with error - \(error)")
150+
}
151+
}
152+
wait(for: [authSignedInSessionExpectation], timeout: networkTimeout)
153+
154+
let operationExpectation = expectation(description: "Operation should complete")
155+
let operation = Amplify.Auth.signOut { result in
156+
defer {
157+
operationExpectation.fulfill()
158+
}
159+
switch result {
160+
case .success:
161+
print("Success signout")
162+
case .failure(let error):
163+
XCTFail("SignOut should not fail - \(error)")
164+
}
165+
}
166+
XCTAssertNotNil(operation, "SignOut operation should not be nil")
167+
wait(for: [operationExpectation], timeout: networkTimeout)
168+
169+
// Check if the auth session is signed out
170+
let authSignedOutSessionExpectation = expectation(description: "Operation should complete")
171+
_ = Amplify.Auth.fetchAuthSession { result in
172+
defer {
173+
authSignedOutSessionExpectation.fulfill()
174+
}
175+
do {
176+
let session = try result.get()
177+
XCTAssertFalse(session.isSignedIn, "Auth session should be signedin")
178+
} catch {
179+
XCTFail("Fetch auth session failed with error - \(error)")
180+
}
181+
}
182+
wait(for: [authSignedOutSessionExpectation], timeout: networkTimeout)
183+
}
184+
}

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/AuthSignUpTests/AuthConfirmSignUpTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class AuthConfirmSignUpTests: AWSAuthBaseTest {
6868
}
6969
switch result {
7070
case .success:
71-
XCTFail("confirmSignUp with validation error should not succeed")
71+
XCTFail("""
72+
confirmSignUp with validation error should not succeed
73+
""")
7274
case .failure(let error):
7375
guard case .validation(_, _, _, _) = error else {
7476
XCTFail("Should return validation error")

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ Please edit the file in your editor:
6868
Object.defineProperty(exports, "__esModule", { value: true });
6969
exports.handler = async (event) => {
7070
event.response.autoConfirmUser = true;
71-
event.response.autoVerifyEmail = true;
7271
return event;
7372
};
7473
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Copyright 2018-2020 Amazon.com,
3+
// Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
@testable import AWSMobileClient
9+
@testable import Amplify
10+
import AWSCognitoAuthPlugin
11+
12+
struct AuthSessionHelper {
13+
14+
static func invalidateSession(username: String) {
15+
let bundleID = Bundle.main.bundleIdentifier
16+
let keychain = AWSUICKeyChainStore(service: "\(bundleID!).\(AWSCognitoIdentityUserPool.self)")
17+
let namespace = "\(AWSMobileClient.default().userPoolClient!.userPoolConfiguration.clientId).\(username)"
18+
let expirationKey = "\(namespace).tokenExpiration"
19+
let refreshTokenKey = "\(namespace).refreshToken"
20+
if keychain[expirationKey] == nil {
21+
print("No expiration key found in keychain")
22+
}
23+
keychain[expirationKey] = "2020-05-27T21:01:03Z"
24+
keychain[refreshTokenKey] = "invalid"
25+
}
26+
}

0 commit comments

Comments
 (0)