2023-10-03 22:34:51 +03:00
|
|
|
package com.x8bit.bitwarden
|
|
|
|
|
|
|
|
import android.content.Intent
|
2024-06-06 18:36:44 +03:00
|
|
|
import android.content.pm.SigningInfo
|
2024-01-26 18:35:31 +03:00
|
|
|
import androidx.lifecycle.SavedStateHandle
|
2024-01-27 01:52:54 +03:00
|
|
|
import app.cash.turbine.test
|
2024-06-12 18:45:21 +03:00
|
|
|
import com.bitwarden.vault.CipherView
|
2024-05-24 23:03:08 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
2024-06-06 18:36:44 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.model.SwitchAccountResult
|
2024-05-24 23:03:08 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
2024-02-01 09:33:10 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.util.getPasswordlessRequestDataIntentOrNull
|
2024-06-06 18:36:44 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.fido2.manager.Fido2CredentialManager
|
|
|
|
import com.x8bit.bitwarden.data.autofill.fido2.model.Fido2CredentialRequest
|
|
|
|
import com.x8bit.bitwarden.data.autofill.fido2.model.Fido2ValidateOriginResult
|
|
|
|
import com.x8bit.bitwarden.data.autofill.fido2.util.getFido2CredentialRequestOrNull
|
2024-01-28 21:25:15 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
|
|
|
|
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManagerImpl
|
2024-01-31 22:27:54 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.model.AutofillSaveItem
|
2024-01-27 22:48:39 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.model.AutofillSelectionData
|
2024-01-31 22:27:54 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.util.getAutofillSaveItemOrNull
|
2024-01-27 22:48:39 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.util.getAutofillSelectionDataOrNull
|
2024-01-23 01:14:06 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManagerImpl
|
2024-05-24 23:03:08 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.manager.garbage.GarbageCollectionManager
|
2024-02-01 09:33:10 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.manager.model.PasswordlessRequestData
|
2024-01-23 01:14:06 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
|
2024-01-17 02:41:34 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
2024-06-06 18:36:44 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
2024-05-24 23:03:08 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
|
|
|
|
import com.x8bit.bitwarden.data.vault.manager.model.VaultStateEvent
|
|
|
|
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
2024-01-17 02:41:34 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
|
|
|
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
|
2024-01-20 00:01:43 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
2024-04-25 23:01:13 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.util.isMyVaultShortcut
|
|
|
|
import com.x8bit.bitwarden.ui.platform.util.isPasswordGeneratorShortcut
|
2024-06-06 18:36:44 +03:00
|
|
|
import io.mockk.coEvery
|
2023-10-03 22:34:51 +03:00
|
|
|
import io.mockk.every
|
2024-05-24 23:03:08 +03:00
|
|
|
import io.mockk.just
|
2023-10-03 22:34:51 +03:00
|
|
|
import io.mockk.mockk
|
2024-02-16 00:33:19 +03:00
|
|
|
import io.mockk.mockkStatic
|
2024-05-24 23:03:08 +03:00
|
|
|
import io.mockk.runs
|
2024-02-16 00:33:19 +03:00
|
|
|
import io.mockk.unmockkStatic
|
2023-10-03 22:34:51 +03:00
|
|
|
import io.mockk.verify
|
2024-01-17 02:41:34 +03:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
2024-01-26 19:26:17 +03:00
|
|
|
import kotlinx.coroutines.test.runTest
|
2024-02-16 00:33:19 +03:00
|
|
|
import org.junit.jupiter.api.AfterEach
|
2024-01-17 02:41:34 +03:00
|
|
|
import org.junit.jupiter.api.Assertions.assertEquals
|
2024-01-26 18:35:31 +03:00
|
|
|
import org.junit.jupiter.api.Assertions.assertNull
|
2024-02-16 00:33:19 +03:00
|
|
|
import org.junit.jupiter.api.BeforeEach
|
2024-01-17 02:41:34 +03:00
|
|
|
import org.junit.jupiter.api.Test
|
2023-10-03 22:34:51 +03:00
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
class MainViewModelTest : BaseViewModelTest() {
|
2023-10-03 22:34:51 +03:00
|
|
|
|
2024-01-28 21:25:15 +03:00
|
|
|
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
|
2024-05-24 23:03:08 +03:00
|
|
|
private val mutableUserStateFlow = MutableStateFlow<UserState?>(null)
|
2024-01-17 02:41:34 +03:00
|
|
|
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
|
2024-01-26 19:26:17 +03:00
|
|
|
private val mutableScreenCaptureAllowedFlow = MutableStateFlow(true)
|
2024-06-06 18:36:44 +03:00
|
|
|
private val fido2CredentialManager = mockk<Fido2CredentialManager>()
|
2024-01-17 02:41:34 +03:00
|
|
|
private val settingsRepository = mockk<SettingsRepository> {
|
|
|
|
every { appTheme } returns AppTheme.DEFAULT
|
|
|
|
every { appThemeStateFlow } returns mutableAppThemeFlow
|
2024-01-26 19:26:17 +03:00
|
|
|
every { isScreenCaptureAllowedStateFlow } returns mutableScreenCaptureAllowedFlow
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-06-06 18:36:44 +03:00
|
|
|
private val authRepository = mockk<AuthRepository> {
|
|
|
|
every { activeUserId } returns DEFAULT_USER_STATE.activeUserId
|
|
|
|
every { userStateFlow } returns mutableUserStateFlow
|
|
|
|
every { switchAccount(any()) } returns SwitchAccountResult.NoChange
|
|
|
|
}
|
2024-05-24 23:03:08 +03:00
|
|
|
private val mutableVaultStateEventFlow = bufferedMutableSharedFlow<VaultStateEvent>()
|
|
|
|
private val vaultRepository = mockk<VaultRepository> {
|
|
|
|
every { vaultStateEventFlow } returns mutableVaultStateEventFlow
|
|
|
|
}
|
|
|
|
private val garbageCollectionManager = mockk<GarbageCollectionManager> {
|
|
|
|
every { tryCollect() } just runs
|
|
|
|
}
|
2024-01-23 01:14:06 +03:00
|
|
|
private val specialCircumstanceManager = SpecialCircumstanceManagerImpl()
|
2024-01-20 00:01:43 +03:00
|
|
|
private val intentManager: IntentManager = mockk {
|
|
|
|
every { getShareDataFromIntent(any()) } returns null
|
|
|
|
}
|
2024-01-26 18:35:31 +03:00
|
|
|
private val savedStateHandle = SavedStateHandle()
|
|
|
|
|
2024-02-16 00:33:19 +03:00
|
|
|
@BeforeEach
|
|
|
|
fun setup() {
|
|
|
|
mockkStatic(
|
|
|
|
Intent::getPasswordlessRequestDataIntentOrNull,
|
|
|
|
Intent::getAutofillSaveItemOrNull,
|
|
|
|
Intent::getAutofillSelectionDataOrNull,
|
2024-06-06 18:36:44 +03:00
|
|
|
Intent::getFido2CredentialRequestOrNull,
|
2024-02-16 00:33:19 +03:00
|
|
|
)
|
2024-04-25 23:01:13 +03:00
|
|
|
mockkStatic(
|
|
|
|
Intent::isMyVaultShortcut,
|
|
|
|
Intent::isPasswordGeneratorShortcut,
|
|
|
|
)
|
2024-02-16 00:33:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
fun tearDown() {
|
|
|
|
unmockkStatic(
|
|
|
|
Intent::getPasswordlessRequestDataIntentOrNull,
|
|
|
|
Intent::getAutofillSaveItemOrNull,
|
|
|
|
Intent::getAutofillSelectionDataOrNull,
|
|
|
|
)
|
2024-04-25 23:01:13 +03:00
|
|
|
unmockkStatic(
|
|
|
|
Intent::isMyVaultShortcut,
|
|
|
|
Intent::isPasswordGeneratorShortcut,
|
|
|
|
)
|
2024-02-16 00:33:19 +03:00
|
|
|
}
|
|
|
|
|
2024-01-26 18:35:31 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `initialization should set a saved SpecialCircumstance to the SpecialCircumstanceManager if present`() {
|
|
|
|
assertNull(specialCircumstanceManager.specialCircumstance)
|
|
|
|
|
|
|
|
val specialCircumstance = mockk<SpecialCircumstance>()
|
|
|
|
createViewModel(
|
|
|
|
initialSpecialCircumstance = specialCircumstance,
|
|
|
|
)
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
specialCircumstance,
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-24 23:03:08 +03:00
|
|
|
@Test
|
|
|
|
fun `user state updates should emit Recreate event and trigger garbage collection`() = runTest {
|
|
|
|
val userId1 = "userId1"
|
|
|
|
val userId2 = "userId12"
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
|
|
|
|
viewModel.eventFlow.test {
|
|
|
|
// Ignore initial screen capture event
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
mutableUserStateFlow.value = UserState(
|
|
|
|
activeUserId = userId1,
|
|
|
|
accounts = listOf(
|
|
|
|
mockk<UserState.Account> {
|
|
|
|
every { userId } returns userId1
|
|
|
|
},
|
|
|
|
),
|
|
|
|
hasPendingAccountAddition = false,
|
|
|
|
)
|
|
|
|
assertEquals(MainEvent.Recreate, awaitItem())
|
|
|
|
|
|
|
|
mutableUserStateFlow.value = UserState(
|
|
|
|
activeUserId = userId1,
|
|
|
|
accounts = listOf(
|
|
|
|
mockk<UserState.Account> {
|
|
|
|
every { userId } returns userId1
|
|
|
|
},
|
|
|
|
),
|
|
|
|
hasPendingAccountAddition = true,
|
|
|
|
)
|
|
|
|
assertEquals(MainEvent.Recreate, awaitItem())
|
|
|
|
|
|
|
|
mutableUserStateFlow.value = UserState(
|
|
|
|
activeUserId = userId2,
|
|
|
|
accounts = listOf(
|
|
|
|
mockk<UserState.Account> {
|
|
|
|
every { userId } returns userId1
|
|
|
|
},
|
|
|
|
mockk<UserState.Account> {
|
|
|
|
every { userId } returns userId2
|
|
|
|
},
|
|
|
|
),
|
|
|
|
hasPendingAccountAddition = true,
|
|
|
|
)
|
|
|
|
assertEquals(MainEvent.Recreate, awaitItem())
|
|
|
|
}
|
|
|
|
verify(exactly = 3) {
|
|
|
|
garbageCollectionManager.tryCollect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `vault state lock events should emit Recreate event and trigger garbage collection`() =
|
|
|
|
runTest {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
|
|
|
|
viewModel.eventFlow.test {
|
|
|
|
// Ignore initial screen capture event
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
mutableVaultStateEventFlow.tryEmit(VaultStateEvent.Unlocked(userId = "userId"))
|
|
|
|
expectNoEvents()
|
|
|
|
|
|
|
|
mutableVaultStateEventFlow.tryEmit(VaultStateEvent.Locked(userId = "userId"))
|
|
|
|
assertEquals(MainEvent.Recreate, awaitItem())
|
|
|
|
}
|
|
|
|
verify(exactly = 1) {
|
|
|
|
garbageCollectionManager.tryCollect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 21:25:15 +03:00
|
|
|
@Test
|
|
|
|
fun `autofill selection updates should emit CompleteAutofill events`() = runTest {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val cipherView = mockk<CipherView>()
|
|
|
|
viewModel.eventFlow.test {
|
|
|
|
// Ignore initial screen capture event
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
autofillSelectionManager.emitAutofillSelection(cipherView = cipherView)
|
|
|
|
assertEquals(
|
|
|
|
MainEvent.CompleteAutofill(cipherView = cipherView),
|
|
|
|
awaitItem(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 18:35:31 +03:00
|
|
|
@Test
|
|
|
|
fun `SpecialCircumstance updates should update the SavedStateHandle`() {
|
|
|
|
createViewModel()
|
|
|
|
|
|
|
|
assertNull(savedStateHandle[SPECIAL_CIRCUMSTANCE_KEY])
|
|
|
|
|
|
|
|
val specialCircumstance = mockk<SpecialCircumstance>()
|
|
|
|
specialCircumstanceManager.specialCircumstance = specialCircumstance
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
specialCircumstance,
|
|
|
|
savedStateHandle[SPECIAL_CIRCUMSTANCE_KEY],
|
|
|
|
)
|
|
|
|
}
|
2024-01-20 00:01:43 +03:00
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
@Test
|
2024-01-17 02:41:34 +03:00
|
|
|
fun `on AppThemeChanged should update state`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
MainState(
|
|
|
|
theme = AppTheme.DEFAULT,
|
|
|
|
),
|
|
|
|
viewModel.stateFlow.value,
|
|
|
|
)
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.Internal.ThemeUpdate(
|
|
|
|
theme = AppTheme.DARK,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
MainState(
|
|
|
|
theme = AppTheme.DARK,
|
|
|
|
),
|
|
|
|
viewModel.stateFlow.value,
|
|
|
|
)
|
|
|
|
|
|
|
|
verify {
|
|
|
|
settingsRepository.appTheme
|
|
|
|
settingsRepository.appThemeStateFlow
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-01-17 02:41:34 +03:00
|
|
|
}
|
|
|
|
|
2024-01-20 00:01:43 +03:00
|
|
|
@Suppress("MaxLineLength")
|
2024-01-17 02:41:34 +03:00
|
|
|
@Test
|
2024-01-20 00:01:43 +03:00
|
|
|
fun `on ReceiveFirstIntent with share data should set the special circumstance to ShareNewSend`() {
|
2024-01-17 02:41:34 +03:00
|
|
|
val viewModel = createViewModel()
|
2024-01-20 00:01:43 +03:00
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val shareData = mockk<IntentManager.ShareData>()
|
2024-02-01 09:33:10 +03:00
|
|
|
every { mockIntent.getPasswordlessRequestDataIntentOrNull() } returns null
|
2024-01-31 22:27:54 +03:00
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns null
|
2024-01-27 22:48:39 +03:00
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns null
|
2024-01-20 00:01:43 +03:00
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns shareData
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-01-20 00:01:43 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
2024-01-23 01:14:06 +03:00
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.ShareNewSend(
|
2024-01-20 00:01:43 +03:00
|
|
|
data = shareData,
|
|
|
|
shouldFinishWhenComplete = true,
|
2024-01-23 01:14:06 +03:00
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
2024-01-20 00:01:43 +03:00
|
|
|
}
|
|
|
|
|
2024-01-27 22:48:39 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveFirstIntent with autofill data should set the special circumstance to AutofillSelection`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val autofillSelectionData = mockk<AutofillSelectionData>()
|
2024-02-01 09:33:10 +03:00
|
|
|
every { mockIntent.getPasswordlessRequestDataIntentOrNull() } returns null
|
2024-01-31 22:27:54 +03:00
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns null
|
2024-01-27 22:48:39 +03:00
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns autofillSelectionData
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-01-27 22:48:39 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.AutofillSelection(
|
|
|
|
autofillSelectionData = autofillSelectionData,
|
|
|
|
shouldFinishWhenComplete = true,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:27:54 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveFirstIntent with an autofill save item should set the special circumstance to AutofillSave`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val autofillSaveItem = mockk<AutofillSaveItem>()
|
2024-02-01 09:33:10 +03:00
|
|
|
every { mockIntent.getPasswordlessRequestDataIntentOrNull() } returns null
|
2024-01-31 22:27:54 +03:00
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns autofillSaveItem
|
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-01-31 22:27:54 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.AutofillSave(
|
|
|
|
autofillSaveItem = autofillSaveItem,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:33:10 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveFirstIntent with a passwordless request data should set the special circumstance to PasswordlessRequest`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val passwordlessRequestData = mockk<PasswordlessRequestData>()
|
|
|
|
every {
|
|
|
|
mockIntent.getPasswordlessRequestDataIntentOrNull()
|
|
|
|
} returns passwordlessRequestData
|
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-02-01 09:33:10 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.PasswordlessRequest(
|
|
|
|
passwordlessRequestData = passwordlessRequestData,
|
|
|
|
shouldFinishWhenComplete = true,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-06 18:36:44 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveFirstIntent with fido2 request data should set the special circumstance to Fido2Save`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val fido2CredentialRequest = Fido2CredentialRequest(
|
|
|
|
userId = DEFAULT_USER_STATE.activeUserId,
|
|
|
|
requestJson = """{"mockRequestJson":1}""",
|
|
|
|
packageName = "com.x8bit.bitwarden",
|
|
|
|
signingInfo = SigningInfo(),
|
|
|
|
origin = "mockOrigin",
|
|
|
|
)
|
|
|
|
val mockIntent = mockk<Intent> {
|
|
|
|
every { getFido2CredentialRequestOrNull() } returns fido2CredentialRequest
|
|
|
|
every { getPasswordlessRequestDataIntentOrNull() } returns null
|
|
|
|
every { getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { isMyVaultShortcut } returns false
|
|
|
|
every { isPasswordGeneratorShortcut } returns false
|
|
|
|
}
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
|
|
|
coEvery {
|
|
|
|
fido2CredentialManager.validateOrigin(any())
|
|
|
|
} returns Fido2ValidateOriginResult.Success
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.Fido2Save(
|
|
|
|
fido2CredentialRequest = fido2CredentialRequest,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveFirstIntent with fido2 request data should switch users if active user is not selected`() {
|
|
|
|
mutableUserStateFlow.value = DEFAULT_USER_STATE
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val fido2CredentialRequest = Fido2CredentialRequest(
|
|
|
|
userId = "selectedUserId",
|
|
|
|
requestJson = """{"mockRequestJson":1}""",
|
|
|
|
packageName = "com.x8bit.bitwarden",
|
|
|
|
signingInfo = SigningInfo(),
|
|
|
|
origin = "mockOrigin",
|
|
|
|
)
|
|
|
|
val mockIntent = mockk<Intent> {
|
|
|
|
every { getFido2CredentialRequestOrNull() } returns fido2CredentialRequest
|
|
|
|
every { getPasswordlessRequestDataIntentOrNull() } returns null
|
|
|
|
every { getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { isMyVaultShortcut } returns false
|
|
|
|
every { isPasswordGeneratorShortcut } returns false
|
|
|
|
}
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
|
|
|
coEvery {
|
|
|
|
fido2CredentialManager.validateOrigin(any())
|
|
|
|
} returns Fido2ValidateOriginResult.Success
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
verify(exactly = 1) { authRepository.switchAccount(fido2CredentialRequest.userId) }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveFirstIntent with fido2 request data should not switch users if active user is selected`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val fido2CredentialRequest = Fido2CredentialRequest(
|
|
|
|
userId = DEFAULT_USER_STATE.activeUserId,
|
|
|
|
requestJson = """{"mockRequestJson":1}""",
|
|
|
|
packageName = "com.x8bit.bitwarden",
|
|
|
|
signingInfo = SigningInfo(),
|
|
|
|
origin = "mockOrigin",
|
|
|
|
)
|
|
|
|
val mockIntent = mockk<Intent> {
|
|
|
|
every { getFido2CredentialRequestOrNull() } returns fido2CredentialRequest
|
|
|
|
every { getPasswordlessRequestDataIntentOrNull() } returns null
|
|
|
|
every { getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { isMyVaultShortcut } returns false
|
|
|
|
every { isPasswordGeneratorShortcut } returns false
|
|
|
|
}
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
|
|
|
coEvery {
|
|
|
|
fido2CredentialManager.validateOrigin(any())
|
|
|
|
} returns Fido2ValidateOriginResult.Success
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
verify(exactly = 0) { authRepository.switchAccount(fido2CredentialRequest.userId) }
|
|
|
|
}
|
|
|
|
|
2024-01-20 00:01:43 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with share data should set the special circumstance to ShareNewSend`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val shareData = mockk<IntentManager.ShareData>()
|
2024-02-01 09:33:10 +03:00
|
|
|
every { mockIntent.getPasswordlessRequestDataIntentOrNull() } returns null
|
2024-01-31 22:27:54 +03:00
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns null
|
2024-01-27 22:48:39 +03:00
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns null
|
2024-01-20 00:01:43 +03:00
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns shareData
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-01-20 00:01:43 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
2024-01-23 01:14:06 +03:00
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.ShareNewSend(
|
2024-01-20 00:01:43 +03:00
|
|
|
data = shareData,
|
|
|
|
shouldFinishWhenComplete = false,
|
2024-01-23 01:14:06 +03:00
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
2024-01-20 00:01:43 +03:00
|
|
|
}
|
|
|
|
|
2024-01-27 22:48:39 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with autofill data should set the special circumstance to AutofillSelection`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val autofillSelectionData = mockk<AutofillSelectionData>()
|
2024-02-01 09:33:10 +03:00
|
|
|
every { mockIntent.getPasswordlessRequestDataIntentOrNull() } returns null
|
2024-01-31 22:27:54 +03:00
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns null
|
2024-01-27 22:48:39 +03:00
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns autofillSelectionData
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-01-27 22:48:39 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.AutofillSelection(
|
|
|
|
autofillSelectionData = autofillSelectionData,
|
|
|
|
shouldFinishWhenComplete = false,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
2024-01-31 22:27:54 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with an autofill save item should set the special circumstance to AutofillSave`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val autofillSaveItem = mockk<AutofillSaveItem>()
|
2024-02-01 09:33:10 +03:00
|
|
|
every { mockIntent.getPasswordlessRequestDataIntentOrNull() } returns null
|
2024-01-31 22:27:54 +03:00
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns autofillSaveItem
|
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-01-31 22:27:54 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.AutofillSave(
|
|
|
|
autofillSaveItem = autofillSaveItem,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
2024-02-01 09:33:10 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with a passwordless auth request data should set the special circumstance to PasswordlessRequest`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val passwordlessRequestData = mockk<PasswordlessRequestData>()
|
|
|
|
every {
|
|
|
|
mockIntent.getPasswordlessRequestDataIntentOrNull()
|
|
|
|
} returns passwordlessRequestData
|
|
|
|
every { mockIntent.getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { mockIntent.getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
2024-04-25 23:01:13 +03:00
|
|
|
every { mockIntent.isMyVaultShortcut } returns false
|
|
|
|
every { mockIntent.isPasswordGeneratorShortcut } returns false
|
2024-02-01 09:33:10 +03:00
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.PasswordlessRequest(
|
|
|
|
passwordlessRequestData = passwordlessRequestData,
|
|
|
|
shouldFinishWhenComplete = false,
|
|
|
|
),
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
2024-01-27 22:48:39 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-25 23:01:13 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with a Vault deeplink data should set the special circumstance to VaultShortcut`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent> {
|
|
|
|
every { getPasswordlessRequestDataIntentOrNull() } returns null
|
|
|
|
every { getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { isMyVaultShortcut } returns true
|
|
|
|
every { isPasswordGeneratorShortcut } returns false
|
|
|
|
}
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.VaultShortcut,
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with a password generator deeplink data should set the special circumstance to GeneratorShortcut`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent> {
|
|
|
|
every { getPasswordlessRequestDataIntentOrNull() } returns null
|
|
|
|
every { getAutofillSaveItemOrNull() } returns null
|
|
|
|
every { getAutofillSelectionDataOrNull() } returns null
|
|
|
|
every { isMyVaultShortcut } returns false
|
|
|
|
every { isPasswordGeneratorShortcut } returns true
|
|
|
|
}
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns null
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
SpecialCircumstance.GeneratorShortcut,
|
|
|
|
specialCircumstanceManager.specialCircumstance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-26 19:26:17 +03:00
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `changes in the allowed screen capture value should result in emissions of ScreenCaptureSettingChange `() =
|
|
|
|
runTest {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
|
|
|
|
viewModel.eventFlow.test {
|
|
|
|
assertEquals(
|
|
|
|
MainEvent.ScreenCaptureSettingChange(isAllowed = true),
|
|
|
|
awaitItem(),
|
|
|
|
)
|
|
|
|
|
|
|
|
mutableScreenCaptureAllowedFlow.value = false
|
|
|
|
assertEquals(
|
|
|
|
MainEvent.ScreenCaptureSettingChange(isAllowed = false),
|
|
|
|
awaitItem(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 18:35:31 +03:00
|
|
|
private fun createViewModel(
|
|
|
|
initialSpecialCircumstance: SpecialCircumstance? = null,
|
|
|
|
) = MainViewModel(
|
2024-01-28 21:25:15 +03:00
|
|
|
autofillSelectionManager = autofillSelectionManager,
|
2024-01-23 01:14:06 +03:00
|
|
|
specialCircumstanceManager = specialCircumstanceManager,
|
2024-05-24 23:03:08 +03:00
|
|
|
garbageCollectionManager = garbageCollectionManager,
|
2024-06-06 18:36:44 +03:00
|
|
|
intentManager = intentManager,
|
2024-01-17 02:41:34 +03:00
|
|
|
settingsRepository = settingsRepository,
|
2024-05-24 23:03:08 +03:00
|
|
|
vaultRepository = vaultRepository,
|
2024-06-06 18:36:44 +03:00
|
|
|
authRepository = authRepository,
|
2024-01-26 18:35:31 +03:00
|
|
|
savedStateHandle = savedStateHandle.apply {
|
|
|
|
set(SPECIAL_CIRCUMSTANCE_KEY, initialSpecialCircumstance)
|
|
|
|
},
|
2024-01-17 02:41:34 +03:00
|
|
|
)
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-02-16 00:33:19 +03:00
|
|
|
|
|
|
|
private const val SPECIAL_CIRCUMSTANCE_KEY: String = "special-circumstance"
|
2024-06-06 18:36:44 +03:00
|
|
|
private val DEFAULT_ACCOUNT = UserState.Account(
|
|
|
|
userId = "activeUserId",
|
|
|
|
name = "Active User",
|
|
|
|
email = "active@bitwarden.com",
|
|
|
|
environment = Environment.Us,
|
|
|
|
avatarColorHex = "#aa00aa",
|
|
|
|
isPremium = true,
|
|
|
|
isLoggedIn = true,
|
|
|
|
isVaultUnlocked = true,
|
|
|
|
needsPasswordReset = false,
|
|
|
|
isBiometricsEnabled = false,
|
|
|
|
organizations = emptyList(),
|
|
|
|
needsMasterPassword = false,
|
|
|
|
trustedDevice = null,
|
|
|
|
)
|
|
|
|
|
|
|
|
private val DEFAULT_USER_STATE = UserState(
|
|
|
|
activeUserId = "activeUserId",
|
|
|
|
accounts = listOf(DEFAULT_ACCOUNT),
|
|
|
|
)
|