BITAU-112 Support deep link into add item flow from Authenticator app (#4128)

This commit is contained in:
Andrew Haisting
2024-10-23 11:17:31 -05:00
committed by GitHub
parent f1d7d1a530
commit fa248243b6
15 changed files with 365 additions and 21 deletions

View File

@@ -6,6 +6,7 @@ import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.bitwarden.vault.CipherView
import com.x8bit.bitwarden.data.auth.datasource.disk.model.OnboardingStatus
import com.x8bit.bitwarden.data.auth.manager.AddTotpItemFromAuthenticatorManagerImpl
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.EmailTokenResult
import com.x8bit.bitwarden.data.auth.repository.model.SwitchAccountResult
@@ -43,6 +44,7 @@ import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.platform.util.isAddTotpLoginItemFromAuthenticator
import com.x8bit.bitwarden.data.vault.manager.model.VaultStateEvent
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
@@ -79,6 +81,7 @@ class MainViewModelTest : BaseViewModelTest() {
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
private val accessibilitySelectionManager: AccessibilitySelectionManager =
AccessibilitySelectionManagerImpl()
private val addTotpItemAuthenticatorManager = AddTotpItemFromAuthenticatorManagerImpl()
private val mutableUserStateFlow = MutableStateFlow<UserState?>(null)
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
private val mutableScreenCaptureAllowedFlow = MutableStateFlow(true)
@@ -131,6 +134,7 @@ class MainViewModelTest : BaseViewModelTest() {
Intent::getFido2AssertionRequestOrNull,
Intent::getFido2CredentialRequestOrNull,
Intent::getFido2GetCredentialsRequestOrNull,
Intent::isAddTotpLoginItemFromAuthenticator,
)
mockkStatic(
Intent::isMyVaultShortcut,
@@ -150,6 +154,7 @@ class MainViewModelTest : BaseViewModelTest() {
Intent::getFido2AssertionRequestOrNull,
Intent::getFido2CredentialRequestOrNull,
Intent::getFido2GetCredentialsRequestOrNull,
Intent::isAddTotpLoginItemFromAuthenticator,
)
unmockkStatic(
Intent::isMyVaultShortcut,
@@ -321,6 +326,38 @@ class MainViewModelTest : BaseViewModelTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `on ReceiveFirstIntent with TOTP data from Authenticator app should set the special circumstance to AddTotpLoginItem and clear pendingAddTotpLoginItemData`() {
val viewModel = createViewModel()
val totpData = mockk<TotpData>()
val mockIntent = createMockIntent(
mockIsAddTotpLoginItemFromAuthenticator = true,
)
addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData = totpData
viewModel.trySendAction(MainAction.ReceiveFirstIntent(intent = mockIntent))
assertEquals(
SpecialCircumstance.AddTotpLoginItem(data = totpData),
specialCircumstanceManager.specialCircumstance,
)
assertNull(addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData)
}
@Suppress("MaxLineLength")
@Test
fun `on ReceiveFirstIntent when intent is from Authenticator app but pending item is null should not set special circumstance`() {
val viewModel = createViewModel()
val mockIntent = createMockIntent(
mockIsAddTotpLoginItemFromAuthenticator = true,
)
addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData = null
viewModel.trySendAction(MainAction.ReceiveFirstIntent(intent = mockIntent))
assertNull(specialCircumstanceManager.specialCircumstance)
assertNull(addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData)
}
@Suppress("MaxLineLength")
@Test
fun `on ReceiveFirstIntent with share data should set the special circumstance to ShareNewSend`() {
@@ -748,6 +785,38 @@ class MainViewModelTest : BaseViewModelTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `on ReceiveNewIntent with TOTP data from Authenticator app should set the special circumstance to AddTotpLoginItem and clear pendingAddTotpLoginItemData`() {
val viewModel = createViewModel()
val totpData = mockk<TotpData>()
val mockIntent = createMockIntent(
mockIsAddTotpLoginItemFromAuthenticator = true,
)
addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData = totpData
viewModel.trySendAction(MainAction.ReceiveNewIntent(intent = mockIntent))
assertEquals(
SpecialCircumstance.AddTotpLoginItem(data = totpData),
specialCircumstanceManager.specialCircumstance,
)
assertNull(addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData)
}
@Suppress("MaxLineLength")
@Test
fun `on ReceiveNewIntent when intent is from Authenticator app but pending item is null should not set special circumstance`() {
val viewModel = createViewModel()
val mockIntent = createMockIntent(
mockIsAddTotpLoginItemFromAuthenticator = true,
)
addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData = null
viewModel.trySendAction(MainAction.ReceiveNewIntent(intent = mockIntent))
assertNull(specialCircumstanceManager.specialCircumstance)
assertNull(addTotpItemAuthenticatorManager.pendingAddTotpLoginItemData)
}
@Suppress("MaxLineLength")
@Test
fun `on ReceiveNewIntent with autofill data should set the special circumstance to AutofillSelection`() {
@@ -943,6 +1012,7 @@ class MainViewModelTest : BaseViewModelTest() {
initialSpecialCircumstance: SpecialCircumstance? = null,
) = MainViewModel(
accessibilitySelectionManager = accessibilitySelectionManager,
addTotpItemFromAuthenticatorManager = addTotpItemAuthenticatorManager,
autofillSelectionManager = autofillSelectionManager,
specialCircumstanceManager = specialCircumstanceManager,
garbageCollectionManager = garbageCollectionManager,
@@ -1013,6 +1083,7 @@ private fun createMockIntent(
mockIsMyVaultShortcut: Boolean = false,
mockIsPasswordGeneratorShortcut: Boolean = false,
mockIsAccountSecurityShortcut: Boolean = false,
mockIsAddTotpLoginItemFromAuthenticator: Boolean = false,
): Intent = mockk<Intent> {
every { getTotpDataOrNull() } returns mockTotpData
every { getPasswordlessRequestDataIntentOrNull() } returns mockPasswordlessRequestData
@@ -1025,6 +1096,7 @@ private fun createMockIntent(
every { isMyVaultShortcut } returns mockIsMyVaultShortcut
every { isPasswordGeneratorShortcut } returns mockIsPasswordGeneratorShortcut
every { isAccountSecurityShortcut } returns mockIsAccountSecurityShortcut
every { isAddTotpLoginItemFromAuthenticator() } returns mockIsAddTotpLoginItemFromAuthenticator
}
private val FIXED_CLOCK: Clock = Clock.fixed(