From da176994ca8218cbc970c04b9d9771cbdf858a0a Mon Sep 17 00:00:00 2001 From: Ramsey Smith <142836716+ramsey-livefront@users.noreply.github.com> Date: Wed, 7 Feb 2024 08:21:37 -0700 Subject: [PATCH] BIT-1664: Vault add item ownership tests. (#969) --- .../feature/addedit/VaultAddEditScreenTest.kt | 89 +++++++++++++++++-- .../addedit/VaultAddEditViewModelTest.kt | 88 +++++++++++++++--- 2 files changed, 158 insertions(+), 19 deletions(-) diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditScreenTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditScreenTest.kt index 7699d04c4..715b5536a 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditScreenTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditScreenTest.kt @@ -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 = 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, ), ) diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModelTest.kt index cad26fc84..cc53733e4 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModelTest.kt @@ -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 = listOf( - VaultAddEditState.Owner( - id = null, - name = "activeEmail", - collections = emptyList(), - ), - VaultAddEditState.Owner( - id = "organizationId", - name = "organizationName", - collections = emptyList(), - ), - ), + availableOwners: List = 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 = + 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. */