@@ -14,14 +14,13 @@ import AmplifyTestCommon
14
14
15
15
class SignedInAuthSessionTests : AWSAuthBaseTest {
16
16
17
- override func setUp( ) {
18
- super. setUp ( )
19
- initializeAmplify ( )
17
+ override func setUpWithError( ) throws {
18
+ try initializeAmplifyWithError ( )
20
19
AuthSessionHelper . clearKeychain ( )
21
20
}
22
21
23
- override func tearDown ( ) {
24
- super . tearDown ( )
22
+ override func tearDownWithError ( ) throws {
23
+ _ = Amplify . Auth . signOut ( )
25
24
Amplify . reset ( )
26
25
sleep ( 2 )
27
26
AuthSessionHelper . clearKeychain ( )
@@ -41,21 +40,21 @@ class SignedInAuthSessionTests: AWSAuthBaseTest {
41
40
let signInExpectation = expectation ( description: " SignIn operation should complete " )
42
41
AuthSignInHelper . registerAndSignInUser ( username: username, password: password,
43
42
email: email) { didSucceed, error in
44
- signInExpectation. fulfill ( )
43
+ if didSucceed {
44
+ signInExpectation. fulfill ( )
45
+ }
45
46
XCTAssertTrue ( didSucceed, " SignIn operation failed - \( String ( describing: error) ) " )
46
47
}
47
48
wait ( for: [ signInExpectation] , timeout: networkTimeout)
48
49
49
50
let authSessionExpectation = expectation ( description: " Received event result from fetchAuth " )
50
51
_ = Amplify . Auth. fetchAuthSession { result in
51
- defer {
52
- authSessionExpectation. fulfill ( )
53
- }
54
52
switch result {
53
+ case . failure( let error) :
54
+ XCTFail ( " Unable to fetch session: \( error) " )
55
55
case . success( let session) :
56
56
XCTAssertTrue ( session. isSignedIn, " Session state should be signed In " )
57
- case . failure( let error) :
58
- XCTFail ( " Should not receive error \( error) " )
57
+ authSessionExpectation. fulfill ( )
59
58
}
60
59
}
61
60
wait ( for: [ authSessionExpectation] , timeout: networkTimeout)
@@ -69,52 +68,64 @@ class SignedInAuthSessionTests: AWSAuthBaseTest {
69
68
/// - Then:
70
69
/// - I should get the signedin state as true but with token result as sessionExpired
71
70
///
72
- func testSessionExpired( ) {
71
+ func testSessionExpired( ) throws {
73
72
let username = " integTest \( UUID ( ) . uuidString) "
74
73
let password = " P123@ \( UUID ( ) . uuidString) "
75
74
let signInExpectation = expectation ( description: " SignIn operation should complete " )
76
75
AuthSignInHelper . registerAndSignInUser ( username: username, password: password,
77
76
email: email) { didSucceed, error in
78
- signInExpectation. fulfill ( )
77
+ if didSucceed {
78
+ signInExpectation. fulfill ( )
79
+ }
79
80
XCTAssertTrue ( didSucceed, " SignIn operation failed - \( String ( describing: error) ) " )
80
81
}
81
82
wait ( for: [ signInExpectation] , timeout: networkTimeout)
82
83
83
- let authSessionExpectation = expectation ( description: " Received event result from fetchAuth " )
84
+ let originalSessionExpectation = expectation ( description: " Received event result from fetchAuth " )
85
+ var originalSession : AuthSession ?
84
86
_ = 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 ( )
94
94
}
95
95
}
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 ( )
97
100
98
101
// Manually invalidate the tokens and then try to fetch the session.
99
102
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 ?
101
105
_ = 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 ( )
104
113
}
114
+ }
115
+ wait ( for: [ postExpirationExpectation] , timeout: networkTimeout)
105
116
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) " )
116
126
}
127
+ case . success( let tokens) :
128
+ XCTFail ( " Unexpected tokens: \( String ( describing: tokens) . prefix ( 64 ) ) " )
117
129
}
118
- wait ( for: [ authSessionExpiredExpectation] , timeout: networkTimeout)
119
130
}
120
131
}
0 commit comments