Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import com.amplifyframework.auth.cognito.RealAWSCognitoAuthPlugin
import com.amplifyframework.auth.cognito.helpers.WebAuthnHelper
import com.amplifyframework.auth.cognito.requireIdentityProviderClient
import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter

internal class AuthUseCaseFactory(
private val plugin: RealAWSCognitoAuthPlugin,
private val authEnvironment: AuthEnvironment,
private val stateMachine: AuthStateMachine,
private val hubEmitter: AuthHubEventEmitter = AuthHubEventEmitter()
private val stateMachine: AuthStateMachine

Check warning on line 27 in aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AuthUseCaseFactory.kt

View check run for this annotation

Codecov / codecov/patch

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AuthUseCaseFactory.kt#L27

Added line #L27 was not covered by tests
) {

fun fetchAuthSession() = FetchAuthSessionUseCase(plugin)
Expand Down Expand Up @@ -143,8 +141,7 @@
)

fun autoSignIn() = AutoSignInUseCase(
stateMachine = stateMachine,
hubEmitter = hubEmitter
stateMachine = stateMachine

Check warning on line 144 in aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AuthUseCaseFactory.kt

View check run for this annotation

Codecov / codecov/patch

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AuthUseCaseFactory.kt#L144

Added line #L144 was not covered by tests
)

fun fetchMfaPreference() = FetchMfaPreferenceUseCase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
import com.amplifyframework.statemachine.codegen.states.SignInState
import com.amplifyframework.statemachine.codegen.states.SignUpState
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.transformWhile

internal class AutoSignInUseCase(
private val stateMachine: AuthStateMachine,
private val hubEmitter: AuthHubEventEmitter
private val hubEmitter: AuthHubEventEmitter = AuthHubEventEmitter()
) {
suspend fun execute(): AuthSignInResult {
val authState = waitForSignedOutState()
Expand Down Expand Up @@ -88,7 +89,7 @@
val event = AuthenticationEvent(AuthenticationEvent.EventType.SignInRequested(signInData))
stateMachine.send(event)
}
.transformWhile { authState ->
.mapNotNull { authState ->
val authNState = authState.authNState
val authZState = authState.authZState
when {
Expand All @@ -97,7 +98,7 @@
if (signInState is SignInState.Error) {
throw CognitoAuthExceptionConverter.lookup(signInState.exception, "Sign in failed.")
}
true
null
}
authNState is AuthenticationState.SignedIn &&
authZState is AuthorizationState.SessionEstablished -> {
Expand All @@ -114,11 +115,10 @@
null
)
)
emit(authSignInResult)
hubEmitter.sendHubEvent(AuthChannelEventName.SIGNED_IN.toString())
false
authSignInResult
}
else -> true
else -> null

Check warning on line 121 in aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AutoSignInUseCase.kt

View check run for this annotation

Codecov / codecov/patch

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AutoSignInUseCase.kt#L121

Added line #L121 was not covered by tests
}
}.first()
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amplifyframework.auth.cognito.usecases

import com.amplifyframework.auth.AuthChannelEventName
import com.amplifyframework.auth.cognito.AuthStateMachine
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
import com.amplifyframework.auth.cognito.testUtil.withAuthEvent
Expand Down Expand Up @@ -137,4 +138,29 @@ class AutoSignInUseCaseTest {
result.isSignedIn shouldBe true
result.nextStep.signInStep shouldBe AuthSignInStep.DONE
}

@Test
fun `emits auth hub event`() = runTest {
stateFlow.value = authState(
AuthenticationState.SignedOut(mockk()),
authSignUpState = SignUpState.SignedUp(signUpData, mockk())
)

val deferred = backgroundScope.async { useCase.execute() }
runCurrent()

stateFlow.value = authState(AuthenticationState.SigningIn())
runCurrent()

stateFlow.value = authState(
AuthenticationState.SignedIn(mockk(), mockk()),
AuthorizationState.SessionEstablished(mockk())
)

deferred.await()

verify {
hubEmitter.sendHubEvent(AuthChannelEventName.SIGNED_IN.toString())
}
}
}