BIT-1207: Fully implement account switcher lock and logout (#362)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent b7578b8f96
commit 1adf58aca8
19 changed files with 588 additions and 43 deletions
@@ -22,10 +22,15 @@ import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import com.x8bit.bitwarden.ui.util.assertLockOrLogoutDialogIsDisplayed
import com.x8bit.bitwarden.ui.util.assertNoDialogExists
import com.x8bit.bitwarden.ui.util.assertSwitcherIsDisplayed
import com.x8bit.bitwarden.ui.util.assertSwitcherIsNotDisplayed
import com.x8bit.bitwarden.ui.util.performAccountIconClick
import com.x8bit.bitwarden.ui.util.performAccountClick
import com.x8bit.bitwarden.ui.util.performAccountIconClick
import com.x8bit.bitwarden.ui.util.performAccountLongClick
import com.x8bit.bitwarden.ui.util.performLockAccountClick
import com.x8bit.bitwarden.ui.util.performLogoutAccountClick
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@@ -114,6 +119,60 @@ class LandingScreenTest : BaseComposeTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `account long click in the account switcher should show the lock-or-logout dialog and close the switcher`() {
// Show the account switcher
val accountSummaries = listOf(ACTIVE_ACCOUNT_SUMMARY)
mutableStateFlow.update {
it.copy(accountSummaries = accountSummaries)
}
composeTestRule.performAccountIconClick()
composeTestRule.assertNoDialogExists()
composeTestRule.performAccountLongClick(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
composeTestRule.assertLockOrLogoutDialogIsDisplayed(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
}
@Suppress("MaxLineLength")
@Test
fun `lock button click in the lock-or-logout dialog should send LockAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
val accountSummaries = listOf(ACTIVE_ACCOUNT_SUMMARY)
mutableStateFlow.update {
it.copy(accountSummaries = accountSummaries)
}
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLockAccountClick()
verify { viewModel.trySendAction(LandingAction.LockAccountClick(ACTIVE_ACCOUNT_SUMMARY)) }
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `logout button click in the lock-or-logout dialog should send LogoutAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
val accountSummaries = listOf(ACTIVE_ACCOUNT_SUMMARY)
mutableStateFlow.update {
it.copy(accountSummaries = accountSummaries)
}
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLogoutAccountClick()
verify { viewModel.trySendAction(LandingAction.LogoutAccountClick(ACTIVE_ACCOUNT_SUMMARY)) }
composeTestRule.assertNoDialogExists()
}
@Test
fun `continue button should be enabled or disabled according to the state`() {
composeTestRule.onNodeWithText("Continue").assertIsEnabled()
@@ -7,13 +7,16 @@ import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.platform.repository.util.FakeEnvironmentRepository
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import com.x8bit.bitwarden.ui.vault.feature.vault.util.toAccountSummaries
import com.x8bit.bitwarden.ui.vault.feature.vault.util.toAccountSummary
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
@@ -22,7 +25,12 @@ import org.junit.jupiter.api.Test
class LandingViewModelTest : BaseViewModelTest() {
private val authRepository: AuthRepository = mockk(relaxed = true)
private val authRepository: AuthRepository = mockk(relaxed = true) {
every { logout(any()) } just runs
}
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVaultIfNecessary(any()) } just runs
}
private val fakeEnvironmentRepository = FakeEnvironmentRepository()
@Test
@@ -88,6 +96,32 @@ class LandingViewModelTest : BaseViewModelTest() {
}
}
@Test
fun `LockAccountClick should call lockVaultIfNecessary for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(LandingAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
}
@Test
fun `LogoutAccountClick should call logout for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(LandingAction.LogoutAccountClick(accountSummary))
verify { authRepository.logout(userId = accountUserId) }
}
@Test
fun `SwitchAccountClick should call switchAccount for the given account`() {
val matchingAccountUserId = "matchingAccountUserId"
@@ -314,6 +348,7 @@ class LandingViewModelTest : BaseViewModelTest() {
every { rememberedEmailAddress } returns rememberedEmail
every { userStateFlow } returns MutableStateFlow(userState)
},
vaultRepository = vaultRepository,
environmentRepository = fakeEnvironmentRepository,
savedStateHandle = savedStateHandle,
)
@@ -18,10 +18,15 @@ import com.x8bit.bitwarden.ui.platform.base.util.IntentHandler
import com.x8bit.bitwarden.ui.platform.components.BasicDialogState
import com.x8bit.bitwarden.ui.platform.components.LoadingDialogState
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import com.x8bit.bitwarden.ui.util.assertLockOrLogoutDialogIsDisplayed
import com.x8bit.bitwarden.ui.util.assertNoDialogExists
import com.x8bit.bitwarden.ui.util.assertSwitcherIsDisplayed
import com.x8bit.bitwarden.ui.util.assertSwitcherIsNotDisplayed
import com.x8bit.bitwarden.ui.util.performAccountIconClick
import com.x8bit.bitwarden.ui.util.performAccountClick
import com.x8bit.bitwarden.ui.util.performAccountIconClick
import com.x8bit.bitwarden.ui.util.performAccountLongClick
import com.x8bit.bitwarden.ui.util.performLockAccountClick
import com.x8bit.bitwarden.ui.util.performLogoutAccountClick
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@@ -103,6 +108,60 @@ class LoginScreenTest : BaseComposeTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `account long click in the account switcher should show the lock-or-logout dialog and close the switcher`() {
// Show the account switcher
val accountSummaries = listOf(ACTIVE_ACCOUNT_SUMMARY)
mutableStateFlow.update {
it.copy(accountSummaries = accountSummaries)
}
composeTestRule.performAccountIconClick()
composeTestRule.assertNoDialogExists()
composeTestRule.performAccountLongClick(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
composeTestRule.assertLockOrLogoutDialogIsDisplayed(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
}
@Suppress("MaxLineLength")
@Test
fun `lock button click in the lock-or-logout dialog should send LockAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
val accountSummaries = listOf(ACTIVE_ACCOUNT_SUMMARY)
mutableStateFlow.update {
it.copy(accountSummaries = accountSummaries)
}
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLockAccountClick()
verify { viewModel.trySendAction(LoginAction.LockAccountClick(ACTIVE_ACCOUNT_SUMMARY)) }
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `logout button click in the lock-or-logout dialog should send LogoutAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
val accountSummaries = listOf(ACTIVE_ACCOUNT_SUMMARY)
mutableStateFlow.update {
it.copy(accountSummaries = accountSummaries)
}
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLogoutAccountClick()
verify { viewModel.trySendAction(LoginAction.LogoutAccountClick(ACTIVE_ACCOUNT_SUMMARY)) }
composeTestRule.assertNoDialogExists()
}
@Test
fun `close button click should send CloseButtonClick action`() {
composeTestRule.onNodeWithContentDescription("Close").performClick()
@@ -12,17 +12,22 @@ import com.x8bit.bitwarden.data.auth.repository.util.CaptchaCallbackTokenResult
import com.x8bit.bitwarden.data.auth.repository.util.generateUriForCaptcha
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.platform.repository.util.FakeEnvironmentRepository
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.components.BasicDialogState
import com.x8bit.bitwarden.ui.platform.components.LoadingDialogState
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import com.x8bit.bitwarden.ui.vault.feature.vault.util.toAccountSummaries
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
@@ -43,6 +48,10 @@ class LoginViewModelTest : BaseViewModelTest() {
private val authRepository: AuthRepository = mockk(relaxed = true) {
every { captchaTokenResultFlow } returns mutableCaptchaTokenResultFlow
every { userStateFlow } returns mutableUserStateFlow
every { logout(any()) } just runs
}
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVaultIfNecessary(any()) } just runs
}
private val fakeEnvironmentRepository = FakeEnvironmentRepository()
@@ -140,6 +149,45 @@ class LoginViewModelTest : BaseViewModelTest() {
}
}
@Test
fun `LockAccountClick should call lockVaultIfNecessary for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(LoginAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
}
@Test
fun `LogoutAccountClick should call logout for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(LoginAction.LogoutAccountClick(accountSummary))
verify { authRepository.logout(userId = accountUserId) }
}
@Test
fun `SwitchAccountClick should call switchAccount for the given account`() {
val matchingAccountUserId = "matchingAccountUserId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns matchingAccountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(LoginAction.SwitchAccountClick(accountSummary))
verify { authRepository.switchAccount(userId = matchingAccountUserId) }
}
@Test
fun `CloseButtonClick should emit NavigateBack`() = runTest {
val viewModel = createViewModel()
@@ -316,6 +364,7 @@ class LoginViewModelTest : BaseViewModelTest() {
LoginViewModel(
authRepository = authRepository,
environmentRepository = fakeEnvironmentRepository,
vaultRepository = vaultRepository,
savedStateHandle = savedStateHandle,
)
@@ -15,11 +15,16 @@ import androidx.compose.ui.test.performScrollTo
import androidx.compose.ui.test.performTextInput
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import com.x8bit.bitwarden.ui.util.assertLockOrLogoutDialogIsDisplayed
import com.x8bit.bitwarden.ui.util.assertNoDialogExists
import com.x8bit.bitwarden.ui.util.assertSwitcherIsDisplayed
import com.x8bit.bitwarden.ui.util.assertSwitcherIsNotDisplayed
import com.x8bit.bitwarden.ui.util.performAccountClick
import com.x8bit.bitwarden.ui.util.performAccountIconClick
import com.x8bit.bitwarden.ui.util.performAccountLongClick
import com.x8bit.bitwarden.ui.util.performAddAccountClick
import com.x8bit.bitwarden.ui.util.performLockAccountClick
import com.x8bit.bitwarden.ui.util.performLogoutAccountClick
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@@ -92,6 +97,52 @@ class VaultUnlockScreenTest : BaseComposeTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `account long click in the account switcher should show the lock-or-logout dialog and close the switcher`() {
// Show the account switcher
composeTestRule.performAccountIconClick()
composeTestRule.assertNoDialogExists()
composeTestRule.performAccountLongClick(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
composeTestRule.assertLockOrLogoutDialogIsDisplayed(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
}
@Suppress("MaxLineLength")
@Test
fun `lock button click in the lock-or-logout dialog should send LockAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLockAccountClick()
verify {
viewModel.trySendAction(VaultUnlockAction.LockAccountClick(ACTIVE_ACCOUNT_SUMMARY))
}
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `logout button click in the lock-or-logout dialog should send LogoutAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLogoutAccountClick()
verify {
viewModel.trySendAction(VaultUnlockAction.LogoutAccountClick(ACTIVE_ACCOUNT_SUMMARY))
}
composeTestRule.assertNoDialogExists()
}
@Test
fun `logout click in the overflow menu should show the logout confirmation dialog`() {
// Confirm neither the popup nor the dialog are showing
@@ -36,9 +36,12 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
every { specialCircumstance } returns null
every { specialCircumstance = any() } just runs
every { logout() } just runs
every { logout(any()) } just runs
every { switchAccount(any()) } returns SwitchAccountResult.AccountSwitched
}
private val vaultRepository = mockk<VaultRepository>()
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVaultIfNecessary(any()) } just runs
}
@Test
fun `initial state should be correct when not set`() {
@@ -167,6 +170,32 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
)
}
@Test
fun `on LockAccountClick should call lockVaultIfNecessary for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(VaultUnlockAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
}
@Test
fun `on LogoutAccountClick should call logout for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(VaultUnlockAction.LogoutAccountClick(accountSummary))
verify { authRepository.logout(userId = accountUserId) }
}
@Test
fun `on SwitchAccountClick should switch to the given account`() = runTest {
val viewModel = createViewModel()
@@ -1,10 +1,16 @@
package com.x8bit.bitwarden.ui.util
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.filterToOne
import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.longClick
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTouchInput
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
private const val ACCOUNT = "Account"
@@ -43,6 +49,35 @@ fun ComposeContentTestRule.assertSwitcherIsNotDisplayed(
this.onNodeWithText(ADD_ACCOUNT).assertDoesNotExist()
}
/**
* Asserts the "lock or logout" dialog is currently displayed with information from the given
* [accountSummary].
*/
fun ComposeContentTestRule.assertLockOrLogoutDialogIsDisplayed(
accountSummary: AccountSummary,
) {
this.waitForIdle()
this
.onNode(isDialog())
.assertIsDisplayed()
this
.onAllNodesWithText("Lock")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
this
.onAllNodesWithText("Log out")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
this
.onAllNodesWithText(accountSummary.email, substring = true)
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
this
.onAllNodesWithText(accountSummary.environmentLabel, substring = true)
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
}
/**
* Clicks on the given [accountSummary] in the account switcher.
*/
@@ -52,6 +87,37 @@ fun ComposeContentTestRule.performAccountClick(
this.onNodeWithText(accountSummary.email).performClick()
}
/**
* Long clicks on the given [accountSummary] in the account switcher.
*/
fun ComposeContentTestRule.performAccountLongClick(
accountSummary: AccountSummary,
) {
this.onNodeWithText(accountSummary.email).performTouchInput {
this.longClick()
}
}
/**
* Clicks the "Lock" button in the "lock or logout" dialog.
*/
fun ComposeContentTestRule.performLockAccountClick() {
this
.onAllNodesWithText("Lock")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
}
/**
* Clicks the "Lock" button in the "lock or logout" dialog.
*/
fun ComposeContentTestRule.performLogoutAccountClick() {
this
.onAllNodesWithText("Log out")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
}
/**
* Opens the account switcher.
*
@@ -8,6 +8,7 @@ import androidx.compose.ui.test.SemanticsNodeInteractionCollection
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasScrollToNodeAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.onAllNodesWithContentDescription
import androidx.compose.ui.test.onAllNodesWithText
@@ -28,6 +29,15 @@ val isProgressBar: SemanticsMatcher
?: false
}
/**
* Asserts that no dialog currently exists.
*/
fun ComposeContentTestRule.assertNoDialogExists() {
this
.onNode(isDialog())
.assertDoesNotExist()
}
/**
* A helper that asserts that the node does not exist in the scrollable list.
*/
@@ -12,11 +12,16 @@ import androidx.compose.ui.test.performScrollToNode
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
import com.x8bit.bitwarden.ui.util.assertLockOrLogoutDialogIsDisplayed
import com.x8bit.bitwarden.ui.util.assertNoDialogExists
import com.x8bit.bitwarden.ui.util.assertSwitcherIsDisplayed
import com.x8bit.bitwarden.ui.util.assertSwitcherIsNotDisplayed
import com.x8bit.bitwarden.ui.util.performAccountClick
import com.x8bit.bitwarden.ui.util.performAccountIconClick
import com.x8bit.bitwarden.ui.util.performAccountLongClick
import com.x8bit.bitwarden.ui.util.performAddAccountClick
import com.x8bit.bitwarden.ui.util.performLockAccountClick
import com.x8bit.bitwarden.ui.util.performLogoutAccountClick
import com.x8bit.bitwarden.ui.vault.model.VaultItemListingType
import io.mockk.every
import io.mockk.mockk
@@ -109,6 +114,48 @@ class VaultScreenTest : BaseComposeTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `account long click in the account switcher should show the lock-or-logout dialog and close the switcher`() {
// Show the account switcher
composeTestRule.performAccountIconClick()
composeTestRule.assertNoDialogExists()
composeTestRule.performAccountLongClick(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
composeTestRule.assertLockOrLogoutDialogIsDisplayed(
accountSummary = ACTIVE_ACCOUNT_SUMMARY,
)
}
@Suppress("MaxLineLength")
@Test
fun `lock button click in the lock-or-logout dialog should send LockAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLockAccountClick()
verify { viewModel.trySendAction(VaultAction.LockAccountClick(ACTIVE_ACCOUNT_SUMMARY)) }
composeTestRule.assertNoDialogExists()
}
@Suppress("MaxLineLength")
@Test
fun `logout button click in the lock-or-logout dialog should send LogoutAccountClick action and close the dialog`() {
// Show the lock-or-logout dialog
composeTestRule.performAccountIconClick()
composeTestRule.performAccountLongClick(ACTIVE_ACCOUNT_SUMMARY)
composeTestRule.performLogoutAccountClick()
verify { viewModel.trySendAction(VaultAction.LogoutAccountClick(ACTIVE_ACCOUNT_SUMMARY)) }
composeTestRule.assertNoDialogExists()
}
@Test
fun `search icon click should send SearchIconClick action`() {
mutableStateFlow.update { it.copy(viewState = VaultState.ViewState.NoItems) }
@@ -40,6 +40,7 @@ class VaultViewModelTest : BaseViewModelTest() {
every { userStateFlow } returns mutableUserStateFlow
every { specialCircumstance } returns null
every { specialCircumstance = any() } just runs
every { logout(any()) } just runs
every { switchAccount(any()) } answers { switchAccountResult }
}
@@ -47,6 +48,7 @@ class VaultViewModelTest : BaseViewModelTest() {
mockk {
every { vaultDataStateFlow } returns mutableVaultDataStateFlow
every { sync() } returns Unit
every { lockVaultIfNecessary(any()) } just runs
}
@Test
@@ -144,6 +146,32 @@ class VaultViewModelTest : BaseViewModelTest() {
)
}
@Test
fun `on LockAccountClick should call lockVaultIfNecessary for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(VaultAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
}
@Test
fun `on LogoutAccountClick should call logout for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
}
val viewModel = createViewModel()
viewModel.trySendAction(VaultAction.LogoutAccountClick(accountSummary))
verify { authRepository.logout(userId = accountUserId) }
}
@Suppress("MaxLineLength")
@Test
fun `on SwitchAccountClick when result is NoChange should try to switch to the given account and set isSwitchingAccounts to false`() =