BIT-1664: Vault add item ownership tests. (#969)

This commit is contained in:
Ramsey Smith 2024-02-07 08:21:37 -07:00 committed by Álison Fernandes
parent e2e52d84b1
commit da176994ca
2 changed files with 158 additions and 19 deletions

View file

@ -51,6 +51,7 @@ import com.x8bit.bitwarden.ui.vault.feature.addedit.model.UriItem
import com.x8bit.bitwarden.ui.vault.model.VaultAddEditType
import com.x8bit.bitwarden.ui.vault.model.VaultCardBrand
import com.x8bit.bitwarden.ui.vault.model.VaultCardExpirationMonth
import com.x8bit.bitwarden.ui.vault.model.VaultCollection
import com.x8bit.bitwarden.ui.vault.model.VaultIdentityTitle
import io.mockk.every
import io.mockk.just
@ -1904,7 +1905,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
VaultAddEditState.Owner(
id = "mockOwnerId-2",
name = "mockOwnerName-2",
collections = emptyList(),
collections = DEFAULT_COLLECTIONS,
),
),
)
@ -1929,6 +1930,45 @@ class VaultAddEditScreenTest : BaseComposeTest() {
.assertIsDisplayed()
}
@Test
fun `clicking a Collection should send CollectionSelect action`() {
updateStateWithOwners(selectedOwnerId = "mockOwnerId-2")
composeTestRule
.onNodeWithTextAfterScroll("mockCollectionName-2")
.performClick()
verify {
viewModel.trySendAction(
VaultAddEditAction.Common.CollectionSelect(
VaultCollection(
id = "mockCollectionId-2",
name = "mockCollectionName-2",
isSelected = false,
),
),
)
}
}
@Test
fun `Collection list should display according to state`() {
updateStateWithOwners(selectedOwnerId = "mockOwnerId-2")
composeTestRule
.onNodeWithTextAfterScroll("mockCollectionName-2")
.assertIsDisplayed()
updateStateWithOwners(
selectedOwnerId = "mockOwnerId-2",
availableOwners = ALTERED_OWNERS,
)
composeTestRule
.onNodeWithTextAfterScroll("mockCollectionName-new")
.assertIsDisplayed()
}
@Test
fun `changing Name text field should trigger NameTextChange`() {
mutableStateFlow.value = DEFAULT_STATE_SECURE_NOTES
@ -2155,7 +2195,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
VaultAddEditState.Owner(
id = "mockOwnerId-2",
name = "mockOwnerName-2",
collections = emptyList(),
collections = DEFAULT_COLLECTIONS,
),
),
)
@ -2785,12 +2825,15 @@ class VaultAddEditScreenTest : BaseComposeTest() {
return currentState.copy(viewState = updatedType)
}
private fun updateStateWithOwners() {
private fun updateStateWithOwners(
selectedOwnerId: String? = null,
availableOwners: List<VaultAddEditState.Owner> = DEFAULT_OWNERS,
) {
mutableStateFlow.update { currentState ->
updateCommonContent(currentState) {
copy(
selectedOwnerId = null,
availableOwners = DEFAULT_OWNERS,
selectedOwnerId = selectedOwnerId,
availableOwners = availableOwners,
)
}
}
@ -2881,6 +2924,40 @@ class VaultAddEditScreenTest : BaseComposeTest() {
dialog = null,
)
private val ALTERED_COLLECTIONS = listOf(
VaultCollection(
id = "mockCollectionId-new",
name = "mockCollectionName-new",
isSelected = true,
),
)
private val ALTERED_OWNERS = listOf(
VaultAddEditState.Owner(
id = null,
name = "placeholder@email.com",
collections = emptyList(),
),
VaultAddEditState.Owner(
id = "mockOwnerId-1",
name = "mockOwnerName-1",
collections = emptyList(),
),
VaultAddEditState.Owner(
id = "mockOwnerId-2",
name = "mockOwnerName-2",
collections = ALTERED_COLLECTIONS,
),
)
private val DEFAULT_COLLECTIONS = listOf(
VaultCollection(
id = "mockCollectionId-2",
name = "mockCollectionName-2",
isSelected = false,
),
)
private val DEFAULT_OWNERS = listOf(
VaultAddEditState.Owner(
id = null,
@ -2895,7 +2972,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
VaultAddEditState.Owner(
id = "mockOwnerId-2",
name = "mockOwnerName-2",
collections = emptyList(),
collections = DEFAULT_COLLECTIONS,
),
)

View file

@ -49,6 +49,7 @@ import com.x8bit.bitwarden.ui.vault.feature.addedit.util.toViewState
import com.x8bit.bitwarden.ui.vault.model.VaultAddEditType
import com.x8bit.bitwarden.ui.vault.model.VaultCardBrand
import com.x8bit.bitwarden.ui.vault.model.VaultCardExpirationMonth
import com.x8bit.bitwarden.ui.vault.model.VaultCollection
import com.x8bit.bitwarden.ui.vault.model.VaultIdentityTitle
import com.x8bit.bitwarden.ui.vault.model.VaultLinkedFieldType
import io.mockk.coEvery
@ -2297,6 +2298,26 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
settingsRepository.initialAutofillDialogShown = true
}
}
@Test
fun `CollectionSelect should update availableOwners collections`() = runTest {
viewModel.actionChannel.trySend(ownershipChangeAction())
val action = collectionSelectAction()
viewModel.actionChannel.trySend(action)
val expectedState = vaultAddItemInitialState.copy(
viewState = VaultAddEditState.ViewState.Content(
common = createCommonContentViewState(
availableOwners = createOwnerList(isCollectionSelected = true),
selectedOwnerId = "organizationId",
),
isIndividualVaultDisabled = false,
type = createLoginTypeContentViewState(),
),
)
assertEquals(expectedState, viewModel.stateFlow.value)
}
}
//region Helper functions
@ -2332,18 +2353,8 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
name = "No Folder",
),
),
availableOwners: List<VaultAddEditState.Owner> = listOf(
VaultAddEditState.Owner(
id = null,
name = "activeEmail",
collections = emptyList(),
),
VaultAddEditState.Owner(
id = "organizationId",
name = "organizationName",
collections = emptyList(),
),
),
availableOwners: List<VaultAddEditState.Owner> = createOwnerList(),
selectedOwnerId: String? = null,
): VaultAddEditState.ViewState.Content.Common =
VaultAddEditState.ViewState.Content.Common(
name = name,
@ -2352,7 +2363,7 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
customFieldData = customFieldData,
masterPasswordReprompt = masterPasswordReprompt,
notes = notes,
selectedOwnerId = null,
selectedOwnerId = selectedOwnerId,
originalCipher = originalCipher,
availableFolders = availableFolders,
availableOwners = availableOwners,
@ -2448,6 +2459,57 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
hasPendingAccountAddition = false,
)
private fun createOwnerList(
hasCollection: Boolean = false,
isCollectionSelected: Boolean = false,
): List<VaultAddEditState.Owner> =
listOf(
VaultAddEditState.Owner(
id = null,
name = "activeEmail",
collections = emptyList(),
),
VaultAddEditState.Owner(
id = "organizationId",
name = "organizationName",
collections = if (hasCollection) {
listOf(
VaultCollection(
id = "mockId-1",
name = "mockName-1",
isSelected = isCollectionSelected,
),
)
} else {
emptyList()
},
),
)
private fun ownershipChangeAction(): VaultAddEditAction.Common.OwnershipChange =
VaultAddEditAction.Common.OwnershipChange(
ownership = VaultAddEditState.Owner(
id = "organizationId",
name = "organizationName",
collections = listOf(
VaultCollection(
id = "mockId-1",
name = "mockName-1",
isSelected = false,
),
),
),
)
private fun collectionSelectAction(): VaultAddEditAction.Common.CollectionSelect =
VaultAddEditAction.Common.CollectionSelect(
VaultCollection(
id = "mockId-1",
name = "mockName-1",
isSelected = false,
),
)
/**
* A function to test the changes in custom fields for each type.
*/