Skip to content

Commit 213c5d6

Browse files
authored
test(Auth): Integration test for user operations (#482)
* test(Auth): Integration test for user operations * Cancel logic added
1 parent b4fe3df commit 213c5d6

18 files changed

+315
-22
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthUserServiceAdapter.swift

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Amplify
9+
import AWSMobileClient
910

1011
class AuthUserServiceAdapter: AuthUserServiceBehavior {
1112

@@ -19,8 +20,16 @@ class AuthUserServiceAdapter: AuthUserServiceBehavior {
1920
completionHandler: @escaping FetchUserAttributesCompletion) {
2021
awsMobileClient.getUserAttributes { result, error in
2122
guard error == nil else {
22-
let authError = AuthErrorHelper.toAuthError(error!)
23-
completionHandler(.failure(authError))
23+
if let awsMobileClientError = error as? AWSMobileClientError,
24+
case .notSignedIn = awsMobileClientError {
25+
let authError = AuthError.signedOut(
26+
AuthPluginErrorConstants.fetchAttributeSignedOutError.errorDescription,
27+
AuthPluginErrorConstants.fetchAttributeSignedOutError.recoverySuggestion, nil)
28+
completionHandler(.failure(authError))
29+
} else {
30+
let authError = AuthErrorHelper.toAuthError(error!)
31+
completionHandler(.failure(authError))
32+
}
2433
return
2534
}
2635
guard let result = result else {
@@ -66,8 +75,16 @@ class AuthUserServiceAdapter: AuthUserServiceBehavior {
6675
awsMobileClient.verifyUserAttribute(attributeName: request.attributeKey.rawValue) { result, error in
6776

6877
guard error == nil else {
69-
let authError = AuthErrorHelper.toAuthError(error!)
70-
completionHandler(.failure(authError))
78+
if let awsMobileClientError = error as? AWSMobileClientError,
79+
case .notSignedIn = awsMobileClientError {
80+
let authError = AuthError.signedOut(
81+
AuthPluginErrorConstants.resendAttributeCodeSignedOutError.errorDescription,
82+
AuthPluginErrorConstants.resendAttributeCodeSignedOutError.recoverySuggestion, nil)
83+
completionHandler(.failure(authError))
84+
} else {
85+
let authError = AuthErrorHelper.toAuthError(error!)
86+
completionHandler(.failure(authError))
87+
}
7188
return
7289
}
7390

@@ -88,27 +105,45 @@ class AuthUserServiceAdapter: AuthUserServiceBehavior {
88105
func confirmAttribute(request: AuthConfirmUserAttributeRequest,
89106
completionHandler: @escaping ConfirmAttributeCompletion) {
90107

91-
awsMobileClient.confirmUpdateUserAttributes(attributeName: request.attributeKey.rawValue,
92-
code: request.confirmationCode) { error in
93-
guard let error = error else {
94-
completionHandler(.success(()))
95-
return
96-
}
97-
let authError = AuthErrorHelper.toAuthError(error)
98-
completionHandler(.failure(authError))
108+
awsMobileClient.confirmUpdateUserAttributes(
109+
attributeName: request.attributeKey.rawValue,
110+
code: request.confirmationCode) { error in
111+
guard let error = error else {
112+
completionHandler(.success(()))
113+
return
114+
}
115+
if let awsMobileClientError = error as? AWSMobileClientError,
116+
case .notSignedIn = awsMobileClientError {
117+
let authError = AuthError.signedOut(
118+
AuthPluginErrorConstants.confirmAttributeSignedOutError.errorDescription,
119+
AuthPluginErrorConstants.confirmAttributeSignedOutError.recoverySuggestion, nil)
120+
completionHandler(.failure(authError))
121+
} else {
122+
let authError = AuthErrorHelper.toAuthError(error)
123+
completionHandler(.failure(authError))
124+
}
99125
}
100126
}
101127

102128
func changePassword(request: AuthChangePasswordRequest,
103129
completionHandler: @escaping ChangePasswordCompletion) {
104-
awsMobileClient.changePassword(currentPassword: request.oldPassword,
105-
proposedPassword: request.newPassword) { error in
106-
guard let error = error else {
107-
completionHandler(.success(()))
108-
return
109-
}
110-
let authError = AuthErrorHelper.toAuthError(error)
111-
completionHandler(.failure(authError))
130+
awsMobileClient.changePassword(
131+
currentPassword: request.oldPassword,
132+
proposedPassword: request.newPassword) { error in
133+
guard let error = error else {
134+
completionHandler(.success(()))
135+
return
136+
}
137+
if let awsMobileClientError = error as? AWSMobileClientError,
138+
case .notSignedIn = awsMobileClientError {
139+
let authError = AuthError.signedOut(
140+
AuthPluginErrorConstants.changePasswordSignedOutError.errorDescription,
141+
AuthPluginErrorConstants.changePasswordSignedOutError.recoverySuggestion, nil)
142+
completionHandler(.failure(authError))
143+
} else {
144+
let authError = AuthErrorHelper.toAuthError(error)
145+
completionHandler(.failure(authError))
146+
}
112147
}
113148

114149
}
@@ -121,8 +156,16 @@ class AuthUserServiceAdapter: AuthUserServiceBehavior {
121156
}
122157
awsMobileClient.updateUserAttributes(attributeMap: attributeMap) { result, error in
123158
guard error == nil else {
124-
let authError = AuthErrorHelper.toAuthError(error!)
125-
completionHandler(.failure(authError))
159+
if let awsMobileClientError = error as? AWSMobileClientError,
160+
case .notSignedIn = awsMobileClientError {
161+
let authError = AuthError.signedOut(
162+
AuthPluginErrorConstants.updateAttributeSignedOutError.errorDescription,
163+
AuthPluginErrorConstants.updateAttributeSignedOutError.recoverySuggestion, nil)
164+
completionHandler(.failure(authError))
165+
} else {
166+
let authError = AuthErrorHelper.toAuthError(error!)
167+
completionHandler(.failure(authError))
168+
}
126169
return
127170
}
128171

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthConfirmSignInOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public class AWSAuthConfirmSignInOperation: AmplifyOperation<
4747
defer {
4848
self.finish()
4949
}
50+
51+
if self.isCancelled {
52+
return
53+
}
54+
5055
switch result {
5156
case .failure(let error):
5257
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthFetchDevicesOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public class AWSAuthFetchDevicesOperation: AmplifyOperation<
3838
defer {
3939
self.finish()
4040
}
41+
42+
if self.isCancelled {
43+
return
44+
}
45+
4146
switch result {
4247
case .failure(let error):
4348
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthFetchSessionOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class AWSAuthFetchSessionOperation: AmplifyOperation<
4242
defer {
4343
self.finish()
4444
}
45+
46+
if self.isCancelled {
47+
return
48+
}
49+
4550
switch result {
4651
case .success(let session):
4752
self.dispatch(session)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthForgetDeviceOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public class AWSAuthForgetDeviceOperation: AmplifyOperation<
3838
defer {
3939
self.finish()
4040
}
41+
42+
if self.isCancelled {
43+
return
44+
}
45+
4146
switch result {
4247
case .failure(let error):
4348
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthRememberDeviceOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public class AWSAuthRememberDeviceOperation: AmplifyOperation<
3939
defer {
4040
self.finish()
4141
}
42+
43+
if self.isCancelled {
44+
return
45+
}
46+
4247
switch result {
4348
case .failure(let error):
4449
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthSignOutOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class AWSAuthSignOutOperation: AmplifyOperation<
4040
defer {
4141
self.finish()
4242
}
43+
44+
if self.isCancelled {
45+
return
46+
}
47+
4348
switch result {
4449
case .failure(let error):
4550
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthSocialWebUISignInOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class AWSAuthSocialWebUISignInOperation: AmplifyOperation<
4040
defer {
4141
self.finish()
4242
}
43+
44+
if self.isCancelled {
45+
return
46+
}
47+
4348
switch result {
4449
case .failure(let error):
4550
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/AWSAuthWebUISignInOperation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class AWSAuthWebUISignInOperation: AmplifyOperation<
4040
defer {
4141
self.finish()
4242
}
43+
44+
if self.isCancelled {
45+
return
46+
}
47+
4348
switch result {
4449
case .failure(let error):
4550
self.dispatch(error)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Operations/UserOperations/AWSAuthAttributeResendConfirmationCodeOperation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class AWSAuthAttributeResendConfirmationCodeOperation: AmplifyOperation<
3737
defer {
3838
self.finish()
3939
}
40+
if self.isCancelled {
41+
return
42+
}
4043
switch result {
4144
case .failure(let error):
4245
self.dispatch(error)

0 commit comments

Comments
 (0)