BIT-500 Add View Item Screen (#299)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 0abc8886a6
commit bd2cd54d47
13 changed files with 1890 additions and 12 deletions
@@ -48,4 +48,6 @@ private const val VAULT_ITEM_ID = "vault_item_id"
private val DEFAULT_STATE: VaultItemState = VaultItemState(
vaultItemId = VAULT_ITEM_ID,
viewState = VaultItemState.ViewState.Loading,
dialog = null,
)
@@ -2,13 +2,31 @@ package com.x8bit.bitwarden.ui.vault.feature.item
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.bitwarden.core.CipherView
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.DataState
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class VaultItemViewModelTest : BaseViewModelTest() {
private val mutableVaultItemFlow = MutableStateFlow<DataState<CipherView?>>(DataState.Loading)
private val mutableUserStateFlow = MutableStateFlow<UserState?>(DEFAULT_USER_STATE)
private val authRepo: AuthRepository = mockk {
every { userStateFlow } returns mutableUserStateFlow
}
private val vaultRepo: VaultRepository = mockk {
every { getVaultItemStateFlow(VAULT_ITEM_ID) } returns mutableVaultItemFlow
}
@Test
fun `initial state should be correct when not set`() {
val viewModel = createViewModel(state = null)
@@ -17,7 +35,11 @@ class VaultItemViewModelTest : BaseViewModelTest() {
@Test
fun `initial state should be correct when set`() {
val state = DEFAULT_STATE.copy(vaultItemId = "something_different")
val differentVaultItemId = "something_different"
every {
vaultRepo.getVaultItemStateFlow(differentVaultItemId)
} returns MutableStateFlow<DataState<CipherView?>>(DataState.Loading)
val state = DEFAULT_STATE.copy(vaultItemId = differentVaultItemId)
val viewModel = createViewModel(state = state)
assertEquals(state, viewModel.stateFlow.value)
}
@@ -34,11 +56,15 @@ class VaultItemViewModelTest : BaseViewModelTest() {
private fun createViewModel(
state: VaultItemState? = DEFAULT_STATE,
vaultItemId: String = VAULT_ITEM_ID,
authRepository: AuthRepository = authRepo,
vaultRepository: VaultRepository = vaultRepo,
): VaultItemViewModel = VaultItemViewModel(
savedStateHandle = SavedStateHandle().apply {
set("state", state)
set("vault_item_id", vaultItemId)
},
authRepository = authRepository,
vaultRepository = vaultRepository,
)
}
@@ -46,4 +72,20 @@ private const val VAULT_ITEM_ID = "vault_item_id"
private val DEFAULT_STATE: VaultItemState = VaultItemState(
vaultItemId = VAULT_ITEM_ID,
viewState = VaultItemState.ViewState.Loading,
dialog = null,
)
private val DEFAULT_USER_STATE: UserState = UserState(
activeUserId = "user_id_1",
accounts = listOf(
UserState.Account(
userId = "user_id_1",
name = "Bit",
email = "bitwarden@gmail.com",
avatarColorHex = "#ff00ff",
isPremium = true,
isVaultUnlocked = true,
),
),
)
@@ -0,0 +1,233 @@
package com.x8bit.bitwarden.ui.vault.feature.item.util
import com.bitwarden.core.CipherRepromptType
import com.bitwarden.core.CipherType
import com.bitwarden.core.CipherView
import com.bitwarden.core.FieldType
import com.bitwarden.core.FieldView
import com.bitwarden.core.LoginUriView
import com.bitwarden.core.LoginView
import com.bitwarden.core.PasswordHistoryView
import com.x8bit.bitwarden.ui.vault.feature.item.VaultItemState
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.time.Instant
import java.util.TimeZone
class CipherViewExtensionsTest {
@BeforeEach
fun setup() {
// Setting the timezone so the tests pass consistently no matter the environment.
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
}
@AfterEach
fun tearDown() {
// Clearing the timezone after the test.
TimeZone.setDefault(null)
}
@Test
fun `toViewState should transform full CipherView into ViewState Login Content with premium`() {
val viewState = DEFAULT_FULL_LOGIN_CIPHER_VIEW.toViewState(isPremiumUser = true)
assertEquals(DEFAULT_FULL_LOGIN_VIEW_STATE, viewState)
}
@Suppress("MaxLineLength")
@Test
fun `toViewState should transform full CipherView into ViewState Login Content without premium`() {
val isPremiumUser = false
val viewState = DEFAULT_FULL_LOGIN_CIPHER_VIEW.toViewState(isPremiumUser = isPremiumUser)
assertEquals(DEFAULT_FULL_LOGIN_VIEW_STATE.copy(isPremiumUser = isPremiumUser), viewState)
}
@Test
fun `toViewState should transform empty CipherView into ViewState Login Content`() {
val viewState = DEFAULT_EMPTY_LOGIN_CIPHER_VIEW.toViewState(isPremiumUser = true)
assertEquals(DEFAULT_EMPTY_LOGIN_VIEW_STATE, viewState)
}
}
val DEFAULT_FULL_LOGIN_VIEW: LoginView = LoginView(
username = "username",
password = "password",
passwordRevisionDate = Instant.ofEpochSecond(1_000L),
uris = listOf(
LoginUriView(
uri = "www.example.com",
match = null,
),
),
totp = "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example",
autofillOnPageLoad = false,
)
val DEFAULT_EMPTY_LOGIN_VIEW: LoginView = LoginView(
username = null,
password = null,
passwordRevisionDate = null,
uris = emptyList(),
totp = null,
autofillOnPageLoad = false,
)
val DEFAULT_FULL_LOGIN_CIPHER_VIEW: CipherView = CipherView(
id = null,
organizationId = null,
folderId = null,
collectionIds = emptyList(),
key = null,
name = "login cipher",
notes = "Lots of notes",
type = CipherType.LOGIN,
login = DEFAULT_FULL_LOGIN_VIEW,
identity = null,
card = null,
secureNote = null,
favorite = false,
reprompt = CipherRepromptType.PASSWORD,
organizationUseTotp = false,
edit = false,
viewPassword = false,
localData = null,
attachments = null,
fields = listOf(
FieldView(
name = "text",
value = "value",
type = FieldType.TEXT,
linkedId = null,
),
FieldView(
name = "hidden",
value = "value",
type = FieldType.HIDDEN,
linkedId = null,
),
FieldView(
name = "boolean",
value = "true",
type = FieldType.BOOLEAN,
linkedId = null,
),
FieldView(
name = "linked username",
value = null,
type = FieldType.LINKED,
linkedId = 100U,
),
FieldView(
name = "linked password",
value = null,
type = FieldType.LINKED,
linkedId = 101U,
),
),
passwordHistory = listOf(
PasswordHistoryView(
password = "old_password",
lastUsedDate = Instant.ofEpochSecond(1_000L),
),
),
creationDate = Instant.ofEpochSecond(1_000L),
deletedDate = null,
revisionDate = Instant.ofEpochSecond(1_000L),
)
val DEFAULT_EMPTY_LOGIN_CIPHER_VIEW: CipherView = CipherView(
id = null,
organizationId = null,
folderId = null,
collectionIds = emptyList(),
key = null,
name = "login cipher",
notes = null,
type = CipherType.LOGIN,
login = DEFAULT_EMPTY_LOGIN_VIEW,
identity = null,
card = null,
secureNote = null,
favorite = false,
reprompt = CipherRepromptType.PASSWORD,
organizationUseTotp = false,
edit = false,
viewPassword = false,
localData = null,
attachments = null,
fields = null,
passwordHistory = null,
creationDate = Instant.ofEpochSecond(1_000L),
deletedDate = null,
revisionDate = Instant.ofEpochSecond(1_000L),
)
val DEFAULT_FULL_LOGIN_VIEW_STATE: VaultItemState.ViewState.Content.Login =
VaultItemState.ViewState.Content.Login(
name = "login cipher",
lastUpdated = "1/1/70 12:16 AM",
passwordHistoryCount = 1,
notes = "Lots of notes",
isPremiumUser = true,
customFields = listOf(
VaultItemState.ViewState.Content.Custom.TextField(
name = "text",
value = "value",
isCopyable = true,
),
VaultItemState.ViewState.Content.Custom.HiddenField(
name = "hidden",
value = "value",
isCopyable = true,
isVisible = false,
),
VaultItemState.ViewState.Content.Custom.BooleanField(
name = "boolean",
value = true,
),
VaultItemState.ViewState.Content.Custom.LinkedField(
name = "linked username",
id = 100U,
),
VaultItemState.ViewState.Content.Custom.LinkedField(
name = "linked password",
id = 101U,
),
),
requiresReprompt = true,
username = "username",
passwordData = VaultItemState.ViewState.Content.PasswordData(
password = "password",
isVisible = false,
),
uris = listOf(
VaultItemState.ViewState.Content.UriData(
uri = "www.example.com",
isCopyable = true,
isLaunchable = true,
),
),
passwordRevisionDate = "1/1/70 12:16 AM",
totp = "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example",
)
val DEFAULT_EMPTY_LOGIN_VIEW_STATE: VaultItemState.ViewState.Content.Login =
VaultItemState.ViewState.Content.Login(
name = "login cipher",
lastUpdated = "1/1/70 12:16 AM",
passwordHistoryCount = null,
notes = null,
isPremiumUser = true,
customFields = emptyList(),
requiresReprompt = true,
username = null,
passwordData = null,
uris = emptyList(),
passwordRevisionDate = null,
totp = null,
)