BIT-2287: Display a dialog for unassigned items (#1358)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 8e1ecd1e6c
commit 04ac479e7d
12 changed files with 364 additions and 2 deletions
@@ -142,6 +142,10 @@ class SettingsDiskSourceTest {
systemBioIntegrityState = systemBioIntegrityState,
value = true,
)
settingsDiskSource.storeShouldCheckOrgUnassignedItems(
userId = userId,
shouldCheckOrgUnassignedItems = true,
)
settingsDiskSource.clearData(userId = userId)
@@ -165,6 +169,7 @@ class SettingsDiskSourceTest {
systemBioIntegrityState = systemBioIntegrityState,
),
)
assertNull(settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId))
}
@Suppress("MaxLineLength")
@@ -952,6 +957,40 @@ class SettingsDiskSourceTest {
)
}
@Test
fun `storeShouldCheckOrgUnassignedItems should update shared preferences`() {
val baseKey = "bwPreferencesStorage:shouldCheckOrganizationUnassignedItems"
val mockUserId = "mockUserId"
val key = "${baseKey}_$mockUserId"
assertNull(settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = mockUserId))
// Updating the disk source updates shared preferences
settingsDiskSource.storeShouldCheckOrgUnassignedItems(
userId = mockUserId,
shouldCheckOrgUnassignedItems = true,
)
assertTrue(fakeSharedPreferences.contains(key))
}
@Test
fun `getShouldCheckOrgUnassignedItems should pull from SharedPreferences`() {
val baseKey = "bwPreferencesStorage:shouldCheckOrganizationUnassignedItems"
val mockUserId = "mockUserId"
val expectedValue = true
val key = "${baseKey}_$mockUserId"
assertNull(settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = mockUserId))
// Update SharedPreferences updates the disk source
fakeSharedPreferences.edit { putBoolean(key, expectedValue) }
assertEquals(
expectedValue,
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = mockUserId),
)
}
@Test
fun `initialAutofillDialogShown should pull from and update SharedPreferences`() {
val initialAutofillDialogShownKey = "bwPreferencesStorage:addSitePromptShown"
@@ -60,6 +60,7 @@ class FakeSettingsDiskSource : SettingsDiskSource {
private val storedScreenCaptureAllowed = mutableMapOf<String, Boolean?>()
private var storedSystemBiometricIntegritySource: String? = null
private val storedAccountBiometricIntegrityValidity = mutableMapOf<String, Boolean?>()
private val storedShouldCheckOrgUnassignedItems = mutableMapOf<String, Boolean?>()
override var appLanguage: AppLanguage? = null
@@ -146,12 +147,23 @@ class FakeSettingsDiskSource : SettingsDiskSource {
storedInlineAutofillEnabled.remove(userId)
storedBlockedAutofillUris.remove(userId)
storedClearClipboardFrequency.remove(userId)
storedShouldCheckOrgUnassignedItems.remove(userId)
mutableVaultTimeoutActionsFlowMap.remove(userId)
mutableVaultTimeoutInMinutesFlowMap.remove(userId)
mutableLastSyncCallFlowMap.remove(userId)
}
override fun getShouldCheckOrgUnassignedItems(userId: String): Boolean? =
storedShouldCheckOrgUnassignedItems[userId]
override fun storeShouldCheckOrgUnassignedItems(
userId: String,
shouldCheckOrgUnassignedItems: Boolean?,
) {
storedShouldCheckOrgUnassignedItems[userId] = shouldCheckOrgUnassignedItems
}
override fun getLastSyncTime(userId: String): Instant? = storedLastSyncTime[userId]
override fun getLastSyncTimeFlow(userId: String): Flow<Instant?> =
@@ -134,6 +134,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@@ -5749,6 +5750,99 @@ class VaultRepositoryTest {
)
}
@Test
fun `shouldShowUnassignedItemsInfo should return false if there is no active user`() = runTest {
val result = vaultRepository.shouldShowUnassignedItemsInfo()
assertFalse(result)
}
@Suppress("MaxLineLength")
@Test
fun `shouldShowUnassignedItemsInfo should return false if getShouldCheckOrgUnassignedItems is false`() =
runTest {
val userId = MOCK_USER_STATE.activeUserId
fakeAuthDiskSource.userState = MOCK_USER_STATE
every {
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId)
} returns false
val result = vaultRepository.shouldShowUnassignedItemsInfo()
assertFalse(result)
verify(exactly = 1) {
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId)
}
}
@Suppress("MaxLineLength")
@Test
fun `shouldShowUnassignedItemsInfo should return true if getShouldCheckOrgUnassignedItems is true and hasUnassignedCiphers is true`() =
runTest {
val userId = MOCK_USER_STATE.activeUserId
fakeAuthDiskSource.userState = MOCK_USER_STATE
every {
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId)
} returns true
coEvery { ciphersService.hasUnassignedCiphers() } returns true.asSuccess()
val result = vaultRepository.shouldShowUnassignedItemsInfo()
assertTrue(result)
verify(exactly = 1) {
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId)
}
coVerify(exactly = 1) {
ciphersService.hasUnassignedCiphers()
}
}
@Suppress("MaxLineLength")
@Test
fun `shouldShowUnassignedItemsInfo should return false if getShouldCheckOrgUnassignedItems is true and hasUnassignedCiphers is an error`() =
runTest {
val userId = MOCK_USER_STATE.activeUserId
fakeAuthDiskSource.userState = MOCK_USER_STATE
every {
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId)
} returns true
coEvery { ciphersService.hasUnassignedCiphers() } returns Throwable().asFailure()
val result = vaultRepository.shouldShowUnassignedItemsInfo()
assertFalse(result)
verify(exactly = 1) {
settingsDiskSource.getShouldCheckOrgUnassignedItems(userId = userId)
}
coVerify(exactly = 1) {
ciphersService.hasUnassignedCiphers()
}
}
@Test
fun `acknowledgeUnassignedItemsInfo should do nothing if there is no active user`() {
val hasAcknowledged = true
vaultRepository.acknowledgeUnassignedItemsInfo(hasAcknowledged = hasAcknowledged)
verify(exactly = 0) {
settingsDiskSource.storeShouldCheckOrgUnassignedItems(
userId = any(),
shouldCheckOrgUnassignedItems = !hasAcknowledged,
)
}
}
@Test
fun `acknowledgeUnassignedItemsInfo should do store the appropriate value`() {
val hasAcknowledged = true
every {
settingsDiskSource.storeShouldCheckOrgUnassignedItems(
userId = MOCK_USER_STATE.activeUserId,
shouldCheckOrgUnassignedItems = !hasAcknowledged,
)
} just runs
fakeAuthDiskSource.userState = MOCK_USER_STATE
vaultRepository.acknowledgeUnassignedItemsInfo(hasAcknowledged = hasAcknowledged)
verify(exactly = 1) {
settingsDiskSource.storeShouldCheckOrgUnassignedItems(
userId = MOCK_USER_STATE.activeUserId,
shouldCheckOrgUnassignedItems = !hasAcknowledged,
)
}
}
//region Helper functions
/**
@@ -497,6 +497,60 @@ class VaultScreenTest : BaseComposeTest() {
composeTestRule.onNodeWithContentDescription(fabDescription).assertIsDisplayed()
}
@Suppress("MaxLineLength")
@Test
fun `unassigned items dialog should be shown or hidden according to the state`() {
val title = "Notice"
val message =
"Unassigned organization items are no longer visible in the All Vaults view and only accessible via the Admin Console. Assign these items to a collection from the Admin Console to make them visible."
composeTestRule.assertNoDialogExists()
composeTestRule.onNodeWithText(title).assertDoesNotExist()
composeTestRule.onNodeWithText(message).assertDoesNotExist()
mutableStateFlow.update { it.copy(dialog = VaultState.DialogState.UnassignedItems) }
composeTestRule
.onAllNodesWithText(title)
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText(message)
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText("Ok")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onAllNodesWithText("Remind me later")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
}
@Test
fun `OK button click in unassigned items dialog should send UnassignedItemsAcknowledgeClick`() {
mutableStateFlow.update { it.copy(dialog = VaultState.DialogState.UnassignedItems) }
composeTestRule
.onAllNodesWithText("Ok")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
verify { viewModel.trySendAction(VaultAction.UnassignedItemsAcknowledgeClick) }
}
@Test
fun `Remind me later button click in unassigned items dialog should send DialogDismiss`() {
mutableStateFlow.update { it.copy(dialog = VaultState.DialogState.UnassignedItems) }
composeTestRule
.onAllNodesWithText("Remind me later")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
verify { viewModel.trySendAction(VaultAction.DialogDismiss) }
}
@Test
fun `error dialog should be shown or hidden according to the state`() {
val errorTitle = "Error title"
@@ -96,6 +96,7 @@ class VaultViewModelTest : BaseViewModelTest() {
every { syncIfNecessary() } just runs
every { lockVaultForCurrentUser() } just runs
every { lockVault(any()) } just runs
coEvery { shouldShowUnassignedItemsInfo() } returns false
}
@Test
@@ -1099,6 +1100,47 @@ class VaultViewModelTest : BaseViewModelTest() {
verify { vaultRepository.sync() }
}
@Test
fun `UnassignedItemsAcknowledgeClick should acknowledge the message and clear the dialog state`() {
coEvery { vaultRepository.shouldShowUnassignedItemsInfo() } returns true
every { vaultRepository.acknowledgeUnassignedItemsInfo(hasAcknowledged = true) } just runs
val viewModel = createViewModel()
viewModel.trySendAction(VaultAction.UnassignedItemsAcknowledgeClick)
verify(exactly = 1) {
vaultRepository.acknowledgeUnassignedItemsInfo(hasAcknowledged = true)
}
}
@Suppress("MaxLineLength")
@Test
fun `ReceiveUnassignedItemsResult should display the unassigned items dialog when result is true`() {
val initialState = DEFAULT_STATE
val viewModel = createViewModel()
viewModel.trySendAction(VaultAction.Internal.ReceiveUnassignedItemsResult(result = true))
assertEquals(
initialState.copy(dialog = VaultState.DialogState.UnassignedItems),
viewModel.stateFlow.value,
)
}
@Suppress("MaxLineLength")
@Test
fun `ReceiveUnassignedItemsResult should not display the unassigned items dialog when result is false`() {
val initialState = DEFAULT_STATE
val viewModel = createViewModel()
viewModel.trySendAction(VaultAction.Internal.ReceiveUnassignedItemsResult(result = false))
assertEquals(
initialState.copy(dialog = null),
viewModel.stateFlow.value,
)
}
@Test
fun `DialogDismiss should clear the active dialog`() {
// Show the No Network error dialog