@@ -52,13 +52,15 @@ import com.amplifyframework.auth.AuthCodeDeliveryDetails
5252import com.amplifyframework.auth.AuthDevice
5353import com.amplifyframework.auth.AuthException
5454import com.amplifyframework.auth.AuthSession
55+ import com.amplifyframework.auth.AuthUser
5556import com.amplifyframework.auth.AuthUserAttribute
5657import com.amplifyframework.auth.AuthUserAttributeKey
5758import com.amplifyframework.auth.MFAType
5859import com.amplifyframework.auth.TOTPSetupDetails
5960import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
6061import com.amplifyframework.auth.cognito.helpers.AuthHelper
6162import com.amplifyframework.auth.cognito.helpers.SRPHelper
63+ import com.amplifyframework.auth.cognito.helpers.SessionHelper
6264import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions
6365import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignInOptions
6466import com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions
@@ -67,6 +69,7 @@ import com.amplifyframework.auth.cognito.options.AWSCognitoAuthVerifyTOTPSetupOp
6769import com.amplifyframework.auth.cognito.options.AuthFlowType
6870import com.amplifyframework.auth.cognito.usecases.ResetPasswordUseCase
6971import com.amplifyframework.auth.exceptions.InvalidStateException
72+ import com.amplifyframework.auth.exceptions.SessionExpiredException
7073import com.amplifyframework.auth.exceptions.SignedOutException
7174import com.amplifyframework.auth.options.AuthConfirmResetPasswordOptions
7275import com.amplifyframework.auth.options.AuthConfirmSignUpOptions
@@ -91,6 +94,7 @@ import com.amplifyframework.statemachine.codegen.data.DeviceMetadata
9194import com.amplifyframework.statemachine.codegen.data.SignInMethod
9295import com.amplifyframework.statemachine.codegen.data.SignedInData
9396import com.amplifyframework.statemachine.codegen.data.UserPoolConfiguration
97+ import com.amplifyframework.statemachine.codegen.errors.SessionError
9498import com.amplifyframework.statemachine.codegen.states.AuthState
9599import com.amplifyframework.statemachine.codegen.states.AuthenticationState
96100import com.amplifyframework.statemachine.codegen.states.AuthorizationState
@@ -242,6 +246,100 @@ class RealAWSCognitoAuthPluginTest {
242246 verify(exactly = 0 ) { onSuccess.accept(any()) }
243247 }
244248
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+
245343 @Test
246344 fun testCustomSignInWithSRPSucceedsWithChallenge () {
247345 // GIVEN
0 commit comments