@@ -52,13 +52,15 @@ import com.amplifyframework.auth.AuthCodeDeliveryDetails
52
52
import com.amplifyframework.auth.AuthDevice
53
53
import com.amplifyframework.auth.AuthException
54
54
import com.amplifyframework.auth.AuthSession
55
+ import com.amplifyframework.auth.AuthUser
55
56
import com.amplifyframework.auth.AuthUserAttribute
56
57
import com.amplifyframework.auth.AuthUserAttributeKey
57
58
import com.amplifyframework.auth.MFAType
58
59
import com.amplifyframework.auth.TOTPSetupDetails
59
60
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
60
61
import com.amplifyframework.auth.cognito.helpers.AuthHelper
61
62
import com.amplifyframework.auth.cognito.helpers.SRPHelper
63
+ import com.amplifyframework.auth.cognito.helpers.SessionHelper
62
64
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions
63
65
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignInOptions
64
66
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions
@@ -67,6 +69,7 @@ import com.amplifyframework.auth.cognito.options.AWSCognitoAuthVerifyTOTPSetupOp
67
69
import com.amplifyframework.auth.cognito.options.AuthFlowType
68
70
import com.amplifyframework.auth.cognito.usecases.ResetPasswordUseCase
69
71
import com.amplifyframework.auth.exceptions.InvalidStateException
72
+ import com.amplifyframework.auth.exceptions.SessionExpiredException
70
73
import com.amplifyframework.auth.exceptions.SignedOutException
71
74
import com.amplifyframework.auth.options.AuthConfirmResetPasswordOptions
72
75
import com.amplifyframework.auth.options.AuthConfirmSignUpOptions
@@ -91,6 +94,7 @@ import com.amplifyframework.statemachine.codegen.data.DeviceMetadata
91
94
import com.amplifyframework.statemachine.codegen.data.SignInMethod
92
95
import com.amplifyframework.statemachine.codegen.data.SignedInData
93
96
import com.amplifyframework.statemachine.codegen.data.UserPoolConfiguration
97
+ import com.amplifyframework.statemachine.codegen.errors.SessionError
94
98
import com.amplifyframework.statemachine.codegen.states.AuthState
95
99
import com.amplifyframework.statemachine.codegen.states.AuthenticationState
96
100
import com.amplifyframework.statemachine.codegen.states.AuthorizationState
@@ -242,6 +246,100 @@ class RealAWSCognitoAuthPluginTest {
242
246
verify(exactly = 0 ) { onSuccess.accept(any()) }
243
247
}
244
248
249
+ @Test
250
+ fun testGetCurrentUserSucceedsIfSignedIn () {
251
+ // GIVEN
252
+ val onSuccess = mockk<Consumer <AuthUser >>()
253
+ val onError = mockk<Consumer <AuthException >>()
254
+ mockkObject(SessionHelper )
255
+ every { SessionHelper .getUsername(any()) } returns " username"
256
+ every { SessionHelper .getUserSub(any()) } returns " sub"
257
+ // WHEN
258
+ plugin.getCurrentUser(onSuccess, onError)
259
+
260
+ // THEN
261
+ verify { onSuccess.accept(any()) }
262
+ verify(exactly = 0 ) { onError.accept(any()) }
263
+ }
264
+
265
+ @Test
266
+ fun testGetCurrentUserFailsWithInvalidStateException () {
267
+ // GIVEN
268
+ val onSuccess = mockk<Consumer <AuthUser >>()
269
+ val onError = mockk<Consumer <AuthException >>(relaxed = true )
270
+
271
+ setupCurrentAuthState(authNState = AuthenticationState .NotConfigured ())
272
+
273
+ // WHEN
274
+ plugin.getCurrentUser(onSuccess, onError)
275
+
276
+ // THEN
277
+ verify(exactly = 0 ) { onSuccess.accept(any()) }
278
+ verify { onError.accept(InvalidStateException ()) }
279
+ }
280
+
281
+ @Test
282
+ fun testGetCurrentUserFailsWithSignedOutException () {
283
+ // GIVEN
284
+ val onSuccess = mockk<Consumer <AuthUser >>()
285
+ val onError = mockk<Consumer <AuthException >>(relaxed = true )
286
+
287
+ setupCurrentAuthState(
288
+ authNState = AuthenticationState .SignedOut (mockk()),
289
+ authZState = AuthorizationState .Configured ()
290
+ )
291
+ // WHEN
292
+ plugin.getCurrentUser(onSuccess, onError)
293
+
294
+ // THEN
295
+ verify(exactly = 0 ) { onSuccess.accept(any()) }
296
+ verify { onError.accept(SignedOutException ()) }
297
+ }
298
+
299
+ @Test
300
+ fun testGetCurrentUserFailsWithExpiredSessionException () {
301
+ // GIVEN
302
+ val onGetCurrentUserSuccess = mockk<Consumer <AuthUser >>()
303
+ val onGetCurrentUserError = mockk<Consumer <AuthException >>(relaxed = true )
304
+ val sessionExpiredException = SessionExpiredException ()
305
+ val sessionError = SessionError (sessionExpiredException, credentials)
306
+ val authNState = AuthenticationState .SignedIn (
307
+ mockk {
308
+ every { username } returns " username"
309
+ },
310
+ mockk()
311
+ )
312
+ val authZState = AuthorizationState .Error (sessionError)
313
+
314
+ setupCurrentAuthState(
315
+ authNState = authNState,
316
+ authZState = authZState
317
+ )
318
+
319
+ val sessionErrorState = mockk<AuthState > {
320
+ every { this @mockk.authNState } returns AuthenticationState .SignedIn (
321
+ mockk {
322
+ every { username } returns " username"
323
+ },
324
+ mockk()
325
+ )
326
+ every { this @mockk.authZState } returns AuthorizationState .Error (sessionError)
327
+ }
328
+
329
+ every {
330
+ authStateMachine.listen(any(), captureLambda(), null )
331
+ } answers {
332
+ lambda< (AuthState ) -> Unit > ().invoke(sessionErrorState)
333
+ }
334
+
335
+ // WHEN
336
+ plugin.getCurrentUser(onGetCurrentUserSuccess, onGetCurrentUserError)
337
+
338
+ // THEN
339
+ verify(exactly = 0 ) { onGetCurrentUserSuccess.accept(any()) }
340
+ verify(timeout = 1000L ) { onGetCurrentUserError.accept(sessionExpiredException) }
341
+ }
342
+
245
343
@Test
246
344
fun testCustomSignInWithSRPSucceedsWithChallenge () {
247
345
// GIVEN
0 commit comments