BIT-1538, BIT-1539, BIT-1660: Implement Search in autofill flow (#889)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 5ceec9b2f7
commit ab84a4b9d3
11 changed files with 1195 additions and 4 deletions
@@ -0,0 +1,40 @@
package com.x8bit.bitwarden.data.platform.manager.util
import com.x8bit.bitwarden.data.autofill.model.AutofillSelectionData
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
class SpecialCircumstanceExtensionsTest {
@Test
fun `toAutofillSelectionDataOrNull should a non-null value for AutofillSelection`() {
val autofillSelectionData = AutofillSelectionData(
type = AutofillSelectionData.Type.LOGIN,
uri = "uri",
)
assertEquals(
autofillSelectionData,
SpecialCircumstance
.AutofillSelection(
autofillSelectionData = autofillSelectionData,
shouldFinishWhenComplete = true,
)
.toAutofillSelectionDataOrNull(),
)
}
@Test
fun `toAutofillSelectionDataOrNull should a non-null value for other types`() {
assertNull(
SpecialCircumstance
.ShareNewSend(
data = mockk(),
shouldFinishWhenComplete = true,
)
.toAutofillSelectionDataOrNull(),
)
}
}
@@ -8,15 +8,18 @@ import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.hasScrollToNodeAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import androidx.compose.ui.test.performTextInput
import androidx.core.net.toUri
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.feature.search.model.AutofillSelectionOption
import com.x8bit.bitwarden.ui.platform.feature.search.util.createMockDisplayItemForCipher
import com.x8bit.bitwarden.ui.platform.feature.search.util.createMockDisplayItemForSend
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
@@ -35,6 +38,7 @@ import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@Suppress("LargeClass")
class SearchScreenTest : BaseComposeTest() {
private val mutableEventFlow = bufferedMutableSharedFlow<SearchEvent>()
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
@@ -197,6 +201,282 @@ class SearchScreenTest : BaseComposeTest() {
}
}
@Suppress("MaxLineLength")
@Test
fun `clicking on a display item with autofill options should open the autofill option selection dialog`() {
mutableStateFlow.value = createStateForAutofill()
composeTestRule.assertNoDialogExists()
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Do you want to auto-fill or view this item?")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Auto-fill")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Auto-fill and save")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("View")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Cancel")
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
verify(exactly = 0) { viewModel.trySendAction(any()) }
}
@Suppress("MaxLineLength")
@Test
fun `clicking on cancel in selection dialog should close dialog`() {
mutableStateFlow.value = createStateForAutofill()
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Cancel")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `clicking on autofill option in selection dialog when no reprompt required should send AutofillItemClick and close dialog`() {
mutableStateFlow.value = createStateForAutofill()
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill")
.assert(hasAnyAncestor(isDialog()))
.performClick()
verify { viewModel.trySendAction(SearchAction.AutofillItemClick(itemId = "mockId-1")) }
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `clicking on autofill option in selection dialog when reprompt is required should show master password dialog`() {
mutableStateFlow.value = createStateForAutofill(isRepromptRequired = true)
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onAllNodesWithText(text = "Master password confirmation")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(
text = "This action is protected, to continue please re-enter your master " +
"password to verify your identity.",
)
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(text = "Master password")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(text = "Cancel")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(text = "Submit")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
}
@Suppress("MaxLineLength")
@Test
fun `clicking on autofill-and-save option in selection dialog when no reprompt required should send AutofillAndSaveItemClick and close dialog`() {
mutableStateFlow.value = createStateForAutofill()
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill and save")
.assert(hasAnyAncestor(isDialog()))
.performClick()
verify { viewModel.trySendAction(SearchAction.AutofillAndSaveItemClick(itemId = "mockId-1")) }
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `clicking on autofill-and-save option in selection dialog when reprompt is required should show master password dialog`() {
mutableStateFlow.value = createStateForAutofill(isRepromptRequired = true)
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill and save")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onAllNodesWithText(text = "Master password confirmation")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(
text = "This action is protected, to continue please re-enter your master " +
"password to verify your identity.",
)
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(text = "Master password")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(text = "Cancel")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(text = "Submit")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
}
@Suppress("MaxLineLength")
@Test
fun `clicking on view option in selection dialog when no reprompt required should send ItemClick and close dialog`() {
mutableStateFlow.value = createStateForAutofill()
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("View")
.assert(hasAnyAncestor(isDialog()))
.performClick()
verify { viewModel.trySendAction(SearchAction.ItemClick(itemId = "mockId-1")) }
composeTestRule.assertNoDialogExists()
}
@Test
fun `clicking cancel on the master password dialog should close the dialog`() {
mutableStateFlow.value = createStateForAutofill(isRepromptRequired = true)
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onNodeWithText("Cancel")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `clicking submit on the master password dialog for autofill should close the dialog and send MasterPasswordRepromptSubmit`() {
mutableStateFlow.value = createStateForAutofill(isRepromptRequired = true)
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onAllNodesWithText(text = "Master password")
.filterToOne(hasAnyAncestor(isDialog()))
.performTextInput("password")
composeTestRule
.onAllNodesWithText(text = "Submit")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
verify {
viewModel.trySendAction(
SearchAction.MasterPasswordRepromptSubmit(
password = "password",
masterPasswordRepromptData = MasterPasswordRepromptData(
cipherId = "mockId-1",
type = MasterPasswordRepromptData.Type.AUTOFILL,
),
),
)
}
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `clicking submit on the master password dialog for autofill-and-save should close the dialog and send MasterPasswordRepromptSubmit`() {
mutableStateFlow.value = createStateForAutofill(isRepromptRequired = true)
composeTestRule
.onNodeWithText(text = "mockName-1")
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithText("Auto-fill and save")
.assert(hasAnyAncestor(isDialog()))
.performClick()
composeTestRule
.onAllNodesWithText(text = "Master password")
.filterToOne(hasAnyAncestor(isDialog()))
.performTextInput("password")
composeTestRule
.onAllNodesWithText(text = "Submit")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
verify {
viewModel.trySendAction(
SearchAction.MasterPasswordRepromptSubmit(
password = "password",
masterPasswordRepromptData = MasterPasswordRepromptData(
cipherId = "mockId-1",
type = MasterPasswordRepromptData.Type.AUTOFILL_AND_SAVE,
),
),
)
}
composeTestRule.assertNoDialogExists()
}
@Test
fun `topBar search placeholder should be displayed according to state`() {
mutableStateFlow.update { DEFAULT_STATE }
@@ -439,3 +719,18 @@ private val DEFAULT_STATE: SearchState = SearchState(
baseIconUrl = "www.test.com",
isIconLoadingDisabled = false,
)
private fun createStateForAutofill(
isRepromptRequired: Boolean = false,
): SearchState = DEFAULT_STATE
.copy(
viewState = SearchState.ViewState.Content(
displayItems = listOf(
createMockDisplayItemForCipher(number = 1)
.copy(
autofillSelectionOptions = AutofillSelectionOption.entries,
shouldDisplayMasterPasswordReprompt = isRepromptRequired,
),
),
),
)
@@ -3,11 +3,20 @@ package com.x8bit.bitwarden.ui.platform.feature.search
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.bitwarden.core.CipherView
import com.bitwarden.core.LoginUriView
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.auth.repository.model.ValidatePasswordResult
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManagerImpl
import com.x8bit.bitwarden.data.autofill.model.AutofillSelectionData
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManager
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManagerImpl
import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardManager
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.DataState
@@ -15,11 +24,14 @@ import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCollectionView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockFolderView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockLoginView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSendView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockUriView
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.DeleteSendResult
import com.x8bit.bitwarden.data.vault.repository.model.GenerateTotpResult
import com.x8bit.bitwarden.data.vault.repository.model.RemovePasswordSendResult
import com.x8bit.bitwarden.data.vault.repository.model.UpdateCipherResult
import com.x8bit.bitwarden.data.vault.repository.model.VaultData
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
@@ -30,7 +42,9 @@ import com.x8bit.bitwarden.ui.platform.feature.search.util.toViewState
import com.x8bit.bitwarden.ui.vault.feature.itemlisting.model.ListingItemOverflowAction
import com.x8bit.bitwarden.ui.vault.feature.vault.model.VaultFilterType
import com.x8bit.bitwarden.ui.vault.feature.vault.util.toFilteredList
import io.mockk.awaits
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
@@ -53,6 +67,9 @@ import java.time.ZoneOffset
@Suppress("LargeClass")
class SearchViewModelTest : BaseViewModelTest() {
private val autofillSelectionManager: AutofillSelectionManager =
AutofillSelectionManagerImpl()
private val clock: Clock = Clock.fixed(
Instant.parse("2023-10-27T12:00:00Z"),
ZoneOffset.UTC,
@@ -79,6 +96,8 @@ class SearchViewModelTest : BaseViewModelTest() {
every { isIconLoadingDisabled } returns false
every { isIconLoadingDisabledFlow } returns mutableIsIconLoadingDisabledFlow
}
private val specialCircumstanceManager: SpecialCircumstanceManager =
SpecialCircumstanceManagerImpl()
@BeforeEach
fun setup() {
@@ -145,6 +164,274 @@ class SearchViewModelTest : BaseViewModelTest() {
}
}
@Test
fun `AutofillItemClick should emit NavigateToViewCipher`() = runTest {
val cipherView = setupForAutofill()
val cipherId = CIPHER_ID
val viewModel = createViewModel()
autofillSelectionManager.autofillSelectionFlow.test {
viewModel.trySendAction(SearchAction.AutofillItemClick(itemId = cipherId))
assertEquals(cipherView, awaitItem())
}
}
@Test
fun `AutofillAndSaveItemClick with request error should show error dialog`() = runTest {
val cipherView = setupForAutofill()
val cipherId = CIPHER_ID
val errorMessage = "Server error"
val updatedCipherView = cipherView.copy(
login = createMockLoginView(1).copy(
uris = listOf(createMockUriView(number = 1)) +
LoginUriView(
uri = AUTOFILL_URI,
match = null,
),
),
)
val viewModel = createViewModel()
coEvery {
vaultRepository.updateCipher(
cipherId = cipherId,
cipherView = updatedCipherView,
)
} returns UpdateCipherResult.Error(errorMessage)
viewModel.stateFlow.test {
assertEquals(INITIAL_STATE_FOR_AUTOFILL, awaitItem())
viewModel.trySendAction(SearchAction.AutofillAndSaveItemClick(itemId = cipherId))
assertEquals(
INITIAL_STATE_FOR_AUTOFILL
.copy(
dialogState = SearchState.DialogState.Loading(
message = R.string.loading.asText(),
),
),
awaitItem(),
)
assertEquals(
INITIAL_STATE_FOR_AUTOFILL
.copy(
dialogState = SearchState.DialogState.Error(
title = null,
message = errorMessage.asText(),
),
),
awaitItem(),
)
}
}
@Suppress("MaxLineLength")
@Test
fun `AutofillAndSaveItemClick with request success should post to the AutofillSelectionManager`() =
runTest {
val cipherView = setupForAutofill()
val cipherId = CIPHER_ID
val updatedCipherView = cipherView.copy(
login = createMockLoginView(1).copy(
uris = listOf(createMockUriView(number = 1)) +
LoginUriView(
uri = AUTOFILL_URI,
match = null,
),
),
)
val viewModel = createViewModel()
coEvery {
vaultRepository.updateCipher(
cipherId = cipherId,
cipherView = updatedCipherView,
)
} returns UpdateCipherResult.Success
turbineScope {
val stateTurbine = viewModel
.stateFlow
.testIn(backgroundScope)
val selectionTurbine = autofillSelectionManager
.autofillSelectionFlow
.testIn(backgroundScope)
assertEquals(INITIAL_STATE_FOR_AUTOFILL, stateTurbine.awaitItem())
viewModel.trySendAction(SearchAction.AutofillAndSaveItemClick(itemId = cipherId))
assertEquals(
INITIAL_STATE_FOR_AUTOFILL
.copy(
dialogState = SearchState.DialogState.Loading(
message = R.string.loading.asText(),
),
),
stateTurbine.awaitItem(),
)
assertEquals(
INITIAL_STATE_FOR_AUTOFILL,
stateTurbine.awaitItem(),
)
// Autofill flow is completed
assertEquals(cipherView, selectionTurbine.awaitItem())
}
}
@Test
fun `MasterPasswordRepromptSubmit for a request Error should show a generic error dialog`() =
runTest {
setupMockUri()
setupForAutofill()
val cipherId = CIPHER_ID
val password = "password"
coEvery {
authRepository.validatePassword(password = password)
} returns ValidatePasswordResult.Error
val viewModel = createViewModel()
assertEquals(
INITIAL_STATE_FOR_AUTOFILL,
viewModel.stateFlow.value,
)
viewModel.trySendAction(
SearchAction.MasterPasswordRepromptSubmit(
password = password,
masterPasswordRepromptData = MasterPasswordRepromptData(
cipherId = cipherId,
type = MasterPasswordRepromptData.Type.AUTOFILL,
),
),
)
assertEquals(
INITIAL_STATE_FOR_AUTOFILL.copy(
dialogState = SearchState.DialogState.Error(
title = null,
message = R.string.generic_error_message.asText(),
),
),
viewModel.stateFlow.value,
)
}
@Suppress("MaxLineLength")
@Test
fun `MasterPasswordRepromptSubmit for a request Success with an invalid password should show an invalid password dialog`() =
runTest {
setupMockUri()
setupForAutofill()
val cipherId = CIPHER_ID
val password = "password"
coEvery {
authRepository.validatePassword(password = password)
} returns ValidatePasswordResult.Success(isValid = false)
val viewModel = createViewModel()
assertEquals(
INITIAL_STATE_FOR_AUTOFILL,
viewModel.stateFlow.value,
)
viewModel.trySendAction(
SearchAction.MasterPasswordRepromptSubmit(
password = password,
masterPasswordRepromptData = MasterPasswordRepromptData(
cipherId = cipherId,
type = MasterPasswordRepromptData.Type.AUTOFILL,
),
),
)
assertEquals(
INITIAL_STATE_FOR_AUTOFILL.copy(
dialogState = SearchState.DialogState.Error(
title = null,
message = R.string.invalid_master_password.asText(),
),
),
viewModel.stateFlow.value,
)
}
@Suppress("MaxLineLength")
@Test
fun `MasterPasswordRepromptSubmit for a request Success with a valid password for autofill should post to the AutofillSelectionManager`() =
runTest {
setupMockUri()
val cipherView = setupForAutofill()
val cipherId = CIPHER_ID
val password = "password"
coEvery {
authRepository.validatePassword(password = password)
} returns ValidatePasswordResult.Success(isValid = true)
val viewModel = createViewModel()
autofillSelectionManager.autofillSelectionFlow.test {
viewModel.trySendAction(
SearchAction.MasterPasswordRepromptSubmit(
password = password,
masterPasswordRepromptData = MasterPasswordRepromptData(
cipherId = cipherId,
type = MasterPasswordRepromptData.Type.AUTOFILL,
),
),
)
assertEquals(
cipherView,
awaitItem(),
)
}
}
@Suppress("MaxLineLength")
@Test
fun `MasterPasswordRepromptSubmit for a request Success with a valid password for autofill-and-save should make a the cipher update request`() =
runTest {
setupMockUri()
val cipherView = setupForAutofill()
val cipherId = CIPHER_ID
val password = "password"
val updatedCipherView = cipherView.copy(
login = createMockLoginView(1).copy(
uris = listOf(createMockUriView(number = 1)) +
LoginUriView(
uri = AUTOFILL_URI,
match = null,
),
),
)
coEvery {
authRepository.validatePassword(password = password)
} returns ValidatePasswordResult.Success(isValid = true)
coEvery {
vaultRepository.updateCipher(
cipherId = cipherId,
cipherView = updatedCipherView,
)
} just awaits
val viewModel = createViewModel()
viewModel.trySendAction(
SearchAction.MasterPasswordRepromptSubmit(
password = password,
masterPasswordRepromptData = MasterPasswordRepromptData(
cipherId = cipherId,
type = MasterPasswordRepromptData.Type.AUTOFILL_AND_SAVE,
),
),
)
coVerify {
vaultRepository.updateCipher(
cipherId = cipherId,
cipherView = updatedCipherView,
)
}
}
@Test
fun `OverflowOptionClick Send EditClick should emit NavigateToEditSend`() = runTest {
val sendId = "sendId"
@@ -492,6 +779,7 @@ class SearchViewModelTest : BaseViewModelTest() {
searchTerm = "",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = false,
)
} returns expectedViewState
val dataState = DataState.Loaded(
@@ -591,6 +879,7 @@ class SearchViewModelTest : BaseViewModelTest() {
searchTerm = "",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = false,
)
} returns expectedViewState
mutableVaultDataStateFlow.tryEmit(
@@ -700,6 +989,7 @@ class SearchViewModelTest : BaseViewModelTest() {
searchTerm = "",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = false,
)
} returns expectedViewState
val dataState = DataState.Error(
@@ -809,6 +1099,7 @@ class SearchViewModelTest : BaseViewModelTest() {
searchTerm = "",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = false,
)
} returns expectedViewState
val dataState = DataState.NoNetwork(
@@ -941,8 +1232,53 @@ class SearchViewModelTest : BaseViewModelTest() {
environmentRepo = environmentRepository,
settingsRepo = settingsRepository,
clipboardManager = clipboardManager,
specialCircumstanceManager = specialCircumstanceManager,
autofillSelectionManager = autofillSelectionManager,
)
/**
* Generates and returns [CipherView] to be populated for autofill testing and sets up the
* state to return that item.
*/
private fun setupForAutofill(): CipherView {
specialCircumstanceManager.specialCircumstance = SpecialCircumstance.AutofillSelection(
autofillSelectionData = AUTOFILL_SELECTION_DATA,
shouldFinishWhenComplete = true,
)
val cipherView = createMockCipherView(number = 1)
val ciphers = listOf(cipherView)
val expectedViewState = SearchState.ViewState.Content(
displayItems = listOf(createMockDisplayItemForCipher(number = 1)),
)
every {
ciphers.filterAndOrganize(
searchTypeData = SearchTypeData.Vault.All,
searchTerm = "",
)
} returns ciphers
every {
ciphers.toFilteredList(vaultFilterType = VaultFilterType.AllVaults)
} returns ciphers
every {
ciphers.toViewState(
searchTerm = "",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = true,
)
} returns expectedViewState
val dataState = DataState.Loaded(
data = VaultData(
cipherViewList = ciphers,
folderViewList = listOf(createMockFolderView(number = 1)),
collectionViewList = listOf(createMockCollectionView(number = 1)),
sendViewList = listOf(createMockSendView(number = 1)),
),
)
mutableVaultDataStateFlow.value = dataState
return cipherView
}
private fun setupMockUri() {
mockkStatic(Uri::class)
val uriMock = mockk<Uri>()
@@ -980,3 +1316,21 @@ private val DEFAULT_USER_STATE = UserState(
),
),
)
private const val AUTOFILL_URI = "autofill-uri"
private const val CIPHER_ID = "mockId-1"
private val AUTOFILL_SELECTION_DATA =
AutofillSelectionData(
type = AutofillSelectionData.Type.LOGIN,
uri = AUTOFILL_URI,
)
private val INITIAL_STATE_FOR_AUTOFILL =
DEFAULT_STATE.copy(
viewState = SearchState.ViewState.Content(
displayItems = listOf(createMockDisplayItemForCipher(number = 1)),
),
autofillSelectionData = AUTOFILL_SELECTION_DATA,
)
@@ -1,6 +1,8 @@
package com.x8bit.bitwarden.ui.platform.feature.search.util
import android.net.Uri
import com.bitwarden.core.CipherRepromptType
import com.bitwarden.core.CipherType
import com.bitwarden.core.CipherView
import com.bitwarden.core.CollectionView
import com.bitwarden.core.FolderView
@@ -11,6 +13,7 @@ import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSendView
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.feature.search.SearchState
import com.x8bit.bitwarden.ui.platform.feature.search.SearchTypeData
import com.x8bit.bitwarden.ui.platform.feature.search.model.AutofillSelectionOption
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
@@ -28,6 +31,11 @@ class SearchTypeDataExtensionsTest {
ZoneOffset.UTC,
)
@Test
fun tearDown() {
unmockkStatic(Uri::parse)
}
@Suppress("MaxLineLength")
@Test
fun `updateWithAdditionalDataIfNecessary should update the collection name when searchTypeData is a vault collection`() {
@@ -262,6 +270,7 @@ class SearchTypeDataExtensionsTest {
searchTerm = "",
baseIconUrl = "www.test.com",
isIconLoadingDisabled = false,
isAutofill = false,
)
assertEquals(SearchState.ViewState.Empty(message = null), result)
@@ -284,6 +293,7 @@ class SearchTypeDataExtensionsTest {
searchTerm = "mock",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = false,
)
assertEquals(
@@ -296,7 +306,70 @@ class SearchTypeDataExtensionsTest {
),
result,
)
unmockkStatic(Uri::parse)
}
@Suppress("MaxLineLength")
@Test
fun `CipherViews toViewState should return content state for autofill when search term is not blank and ciphers is not empty`() {
mockkStatic(Uri::parse)
every { Uri.parse(any()) } returns mockk {
every { host } returns "www.mockuri.com"
}
val sends = listOf(
createMockCipherView(
number = 0,
cipherType = CipherType.CARD,
)
.copy(
reprompt = CipherRepromptType.PASSWORD,
),
createMockCipherView(number = 1),
createMockCipherView(number = 2),
)
val result = sends.toViewState(
searchTerm = "mock",
baseIconUrl = "https://vault.bitwarden.com/icons",
isIconLoadingDisabled = false,
isAutofill = true,
)
assertEquals(
SearchState.ViewState.Content(
displayItems = listOf(
createMockDisplayItemForCipher(
number = 0,
cipherType = CipherType.CARD,
)
.copy(
autofillSelectionOptions = listOf(
AutofillSelectionOption.AUTOFILL,
AutofillSelectionOption.VIEW,
),
shouldDisplayMasterPasswordReprompt = true,
),
createMockDisplayItemForCipher(number = 1)
.copy(
autofillSelectionOptions = listOf(
AutofillSelectionOption.AUTOFILL,
AutofillSelectionOption.AUTOFILL_AND_SAVE,
AutofillSelectionOption.VIEW,
),
shouldDisplayMasterPasswordReprompt = false,
),
createMockDisplayItemForCipher(number = 2)
.copy(
autofillSelectionOptions = listOf(
AutofillSelectionOption.AUTOFILL,
AutofillSelectionOption.AUTOFILL_AND_SAVE,
AutofillSelectionOption.VIEW,
),
shouldDisplayMasterPasswordReprompt = false,
),
),
),
result,
)
}
@Suppress("MaxLineLength")
@@ -306,6 +379,7 @@ class SearchTypeDataExtensionsTest {
searchTerm = "a",
baseIconUrl = "www.test.com",
isIconLoadingDisabled = false,
isAutofill = false,
)
assertEquals(
@@ -53,6 +53,8 @@ fun createMockDisplayItemForCipher(
),
),
totpCode = "mockTotp-$number",
autofillSelectionOptions = emptyList(),
shouldDisplayMasterPasswordReprompt = false,
)
}
@@ -80,6 +82,8 @@ fun createMockDisplayItemForCipher(
),
),
totpCode = null,
autofillSelectionOptions = emptyList(),
shouldDisplayMasterPasswordReprompt = false,
)
}
@@ -87,7 +91,7 @@ fun createMockDisplayItemForCipher(
SearchState.DisplayItem(
id = "mockId-$number",
title = "mockName-$number",
subtitle = "er-$number",
subtitle = "mockBrand-$number, *er-$number",
iconData = IconData.Local(R.drawable.ic_card_item),
extraIconList = listOf(
IconRes(
@@ -110,6 +114,8 @@ fun createMockDisplayItemForCipher(
),
),
totpCode = null,
autofillSelectionOptions = emptyList(),
shouldDisplayMasterPasswordReprompt = false,
)
}
@@ -134,6 +140,8 @@ fun createMockDisplayItemForCipher(
ListingItemOverflowAction.VaultAction.EditClick(cipherId = "mockId-$number"),
),
totpCode = null,
autofillSelectionOptions = emptyList(),
shouldDisplayMasterPasswordReprompt = false,
)
}
}
@@ -175,6 +183,8 @@ fun createMockDisplayItemForSend(
ListingItemOverflowAction.SendAction.DeleteClick(sendId = "mockId-$number"),
),
totpCode = null,
autofillSelectionOptions = emptyList(),
shouldDisplayMasterPasswordReprompt = false,
)
}
@@ -206,6 +216,8 @@ fun createMockDisplayItemForSend(
ListingItemOverflowAction.SendAction.DeleteClick(sendId = "mockId-$number"),
),
totpCode = null,
autofillSelectionOptions = emptyList(),
shouldDisplayMasterPasswordReprompt = false,
)
}
}