BIT-431: Add a table to the vault database for folders (#403)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 34101245dd
commit 34e3fbcc04
11 changed files with 351 additions and 20 deletions
@@ -4,9 +4,12 @@ import app.cash.turbine.test
import com.x8bit.bitwarden.data.platform.datasource.network.di.PlatformNetworkModule
import com.x8bit.bitwarden.data.util.assertJsonEquals
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.FakeCiphersDao
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.FakeFoldersDao
import com.x8bit.bitwarden.data.vault.datasource.disk.entity.CipherEntity
import com.x8bit.bitwarden.data.vault.datasource.disk.entity.FolderEntity
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCipher
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockFolder
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
@@ -15,25 +18,29 @@ 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
import java.time.ZonedDateTime
class VaultDiskSourceTest {
private val json = PlatformNetworkModule.providesJson()
private lateinit var ciphersDao: FakeCiphersDao
private lateinit var foldersDao: FakeFoldersDao
private lateinit var vaultDiskSource: VaultDiskSource
@BeforeEach
fun setup() {
ciphersDao = FakeCiphersDao()
foldersDao = FakeFoldersDao()
vaultDiskSource = VaultDiskSourceImpl(
ciphersDao = ciphersDao,
foldersDao = foldersDao,
json = json,
)
}
@Test
fun `getCiphers should emit all dao updates`() = runTest {
fun `getCiphers should emit all CiphersDao updates`() = runTest {
val cipherEntities = listOf(CIPHER_ENTITY)
val ciphers = listOf(CIPHER_1)
@@ -47,33 +54,57 @@ class VaultDiskSourceTest {
}
@Test
fun `replaceVaultData should clear the dao and insert the encoded ciphers`() = runTest {
fun `getFolders should emit all FoldersDao updates`() = runTest {
val folderEntities = listOf(FOLDER_ENTITY)
val folders = listOf(FOLDER_1)
vaultDiskSource
.getFolders(USER_ID)
.test {
assertEquals(emptyList<SyncResponseJson.Folder>(), awaitItem())
foldersDao.insertFolders(folderEntities)
assertEquals(folders, awaitItem())
}
}
@Test
fun `replaceVaultData should clear the daos and insert the new vault data`() = runTest {
assertEquals(ciphersDao.storedCiphers, emptyList<CipherEntity>())
assertEquals(foldersDao.storedFolders, emptyList<FolderEntity>())
vaultDiskSource.replaceVaultData(USER_ID, VAULT_DATA)
assertEquals(1, ciphersDao.storedCiphers.size)
val storedEntity = ciphersDao.storedCiphers.first()
assertEquals(1, foldersDao.storedFolders.size)
// Verify the ciphers dao is updated
val storedCipherEntity = ciphersDao.storedCiphers.first()
// We cannot compare the JSON strings directly because of formatting differences
// So we split that off into its own assertion.
assertEquals(CIPHER_ENTITY.copy(cipherJson = ""), storedEntity.copy(cipherJson = ""))
assertJsonEquals(CIPHER_ENTITY.cipherJson, storedEntity.cipherJson)
assertEquals(CIPHER_ENTITY.copy(cipherJson = ""), storedCipherEntity.copy(cipherJson = ""))
assertJsonEquals(CIPHER_ENTITY.cipherJson, storedCipherEntity.cipherJson)
// Verify the folders dao is updated
assertEquals(listOf(FOLDER_ENTITY), foldersDao.storedFolders)
}
@Test
fun `deleteVaultData should remove all ciphers matching the user ID`() = runTest {
fun `deleteVaultData should remove all vault data matching the user ID`() = runTest {
assertFalse(ciphersDao.deleteCiphersCalled)
assertFalse(foldersDao.deleteFoldersCalled)
vaultDiskSource.deleteVaultData(USER_ID)
assertTrue(ciphersDao.deleteCiphersCalled)
assertTrue(foldersDao.deleteFoldersCalled)
}
}
private const val USER_ID: String = "test_user_id"
private val CIPHER_1: SyncResponseJson.Cipher = createMockCipher(1)
private val FOLDER_1: SyncResponseJson.Folder = createMockFolder(2)
private val VAULT_DATA: SyncResponseJson = SyncResponseJson(
folders = null,
folders = listOf(FOLDER_1),
collections = null,
profile = mockk<SyncResponseJson.Profile> {
every { id } returns USER_ID
@@ -185,3 +216,10 @@ private val CIPHER_ENTITY = CipherEntity(
cipherType = "1",
cipherJson = CIPHER_JSON,
)
private val FOLDER_ENTITY = FolderEntity(
id = "mockId-2",
userId = USER_ID,
name = "mockName-2",
revisionDate = ZonedDateTime.parse("2023-10-27T12:00Z"),
)
@@ -0,0 +1,47 @@
package com.x8bit.bitwarden.data.vault.datasource.disk.convertor
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import java.time.ZonedDateTime
class ZonedDateTimeTypeConverterTest {
@Test
fun `fromTimestamp should return null when value is null`() {
val value: Long? = null
val result = ZonedDateTimeTypeConverter.fromTimestamp(value)
assertNull(result)
}
@Test
fun `fromTimestamp should return correct ZonedDateTime when value is not null`() {
val expected = ZonedDateTime.parse("2023-12-15T20:38:06Z")
val value = expected.toEpochSecond()
val result = ZonedDateTimeTypeConverter.fromTimestamp(value)
assertEquals(expected, result)
}
@Test
fun `toTimestamp should return null when value is null`() {
val value: ZonedDateTime? = null
val result = ZonedDateTimeTypeConverter.toTimestamp(value)
assertNull(result)
}
@Test
fun `toTimestamp should return correct Long when value is not null`() {
val value = ZonedDateTime.parse("2023-12-15T20:38:06Z")
val expected = value.toEpochSecond()
val result = ZonedDateTimeTypeConverter.toTimestamp(value)
assertEquals(expected, result)
}
}
@@ -0,0 +1,54 @@
package com.x8bit.bitwarden.data.vault.datasource.disk.dao
import com.x8bit.bitwarden.data.vault.datasource.disk.entity.FolderEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.map
class FakeFoldersDao : FoldersDao {
val storedFolders = mutableListOf<FolderEntity>()
var deleteFolderCalled: Boolean = false
var deleteFoldersCalled: Boolean = false
private val foldersFlow = MutableSharedFlow<List<FolderEntity>>(
replay = 1,
extraBufferCapacity = Int.MAX_VALUE,
)
init {
foldersFlow.tryEmit(emptyList())
}
override suspend fun deleteAllFolders(userId: String) {
deleteFoldersCalled = true
storedFolders.removeAll { it.userId == userId }
foldersFlow.tryEmit(storedFolders.toList())
}
override suspend fun deleteFolder(userId: String, folderId: String) {
deleteFolderCalled = true
storedFolders.removeAll { it.userId == userId && it.id == folderId }
foldersFlow.tryEmit(storedFolders.toList())
}
override fun getAllFolders(userId: String): Flow<List<FolderEntity>> =
foldersFlow.map { ciphers -> ciphers.filter { it.userId == userId } }
override suspend fun insertFolders(folders: List<FolderEntity>) {
storedFolders.addAll(folders)
foldersFlow.tryEmit(storedFolders.toList())
}
override suspend fun insertFolder(folder: FolderEntity) {
storedFolders.add(folder)
foldersFlow.tryEmit(storedFolders.toList())
}
override suspend fun replaceAllFolders(userId: String, folders: List<FolderEntity>) {
storedFolders.removeAll { it.userId == userId }
storedFolders.addAll(folders)
foldersFlow.tryEmit(storedFolders.toList())
}
}