Add the underlying support for deleting a Cipher (#587)

This commit is contained in:
David Perez authored and Álison Fernandes committed 2024-06-20 17:08:07 +01:00
1 parent b29af03b27
commit 1f0a1bba6f
13 files changed
+129 -1

No files matched your search

@@ -82,6 +82,18 @@ class VaultDiskSourceTest {
}
}
@Test
fun `DeleteCipher should call deleteCipher`() = runTest {
assertFalse(ciphersDao.deleteCipherCalled)
ciphersDao.storedCiphers.add(CIPHER_ENTITY)
assertEquals(1, ciphersDao.storedCiphers.size)
vaultDiskSource.deleteCipher(USER_ID, CIPHER_1.id)
assertTrue(ciphersDao.deleteCipherCalled)
assertEquals(emptyList<CipherEntity>(), ciphersDao.storedCiphers)
}
@Test
fun `saveCollection should call insertCollection`() = runTest {
assertFalse(collectionsDao.insertCollectionCalled)
@@ -9,6 +9,7 @@ class FakeCiphersDao : CiphersDao {
val storedCiphers = mutableListOf<CipherEntity>()
var deleteCipherCalled: Boolean = false
var deleteCiphersCalled: Boolean = false
var insertCiphersCalled: Boolean = false
@@ -26,6 +27,14 @@ class FakeCiphersDao : CiphersDao {
return count
}
override suspend fun deleteCipher(userId: String, cipherId: String): Int {
deleteCipherCalled = true
val count = storedCiphers.count { it.userId == userId && it.id == cipherId }
storedCiphers.removeAll { it.userId == userId && it.id == cipherId }
ciphersFlow.tryEmit(storedCiphers.toList())
return count
}
override fun getAllCiphers(userId: String): Flow<List<CipherEntity>> =
ciphersFlow.map { ciphers -> ciphers.filter { it.userId == userId } }
@@ -7,8 +7,8 @@ import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCipher
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCipherJsonRequest
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import retrofit2.create
class CiphersServiceTest : BaseServiceTest() {
@@ -63,6 +63,14 @@ class CiphersServiceTest : BaseServiceTest() {
result.getOrThrow(),
)
}
@Test
fun `deleteCipher should execute the delete cipher API`() = runTest {
server.enqueue(MockResponse().setResponseCode(200))
val cipherId = "cipherId"
val result = ciphersService.deleteCipher(cipherId = cipherId)
assertEquals(Unit, result.getOrThrow())
}
}
private const val CREATE_UPDATE_CIPHER_SUCCESS_JSON = """
@@ -49,6 +49,7 @@ import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSendView
import com.x8bit.bitwarden.data.vault.manager.VaultLockManager
import com.x8bit.bitwarden.data.vault.repository.model.CreateCipherResult
import com.x8bit.bitwarden.data.vault.repository.model.CreateSendResult
import com.x8bit.bitwarden.data.vault.repository.model.DeleteCipherResult
import com.x8bit.bitwarden.data.vault.repository.model.DeleteSendResult
import com.x8bit.bitwarden.data.vault.repository.model.RemovePasswordSendResult
import com.x8bit.bitwarden.data.vault.repository.model.SendData
@@ -1390,6 +1391,36 @@ class VaultRepositoryTest {
assertEquals(UpdateCipherResult.Success, result)
}
@Suppress("MaxLineLength")
@Test
fun `deleteCipher with ciphersService deleteCipher failure should return DeleteCipherResult Error`() =
runTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
val cipherId = "mockId-1"
coEvery {
ciphersService.deleteCipher(cipherId = cipherId)
} returns Throwable("Fail").asFailure()
val result = vaultRepository.deleteCipher(cipherId)
assertEquals(DeleteCipherResult.Error, result)
}
@Suppress("MaxLineLength")
@Test
fun `deleteCipher with ciphersService deleteCipher success should return DeleteCipherResult success`() =
runTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
val userId = "mockId-1"
val cipherId = "mockId-1"
coEvery { ciphersService.deleteCipher(cipherId = cipherId) } returns Unit.asSuccess()
coEvery { vaultDiskSource.deleteCipher(userId, cipherId) } just runs
val result = vaultRepository.deleteCipher(cipherId)
assertEquals(DeleteCipherResult.Success, result)
}
@Test
fun `createSend with encryptSend failure should return CreateSendResult failure`() =
runTest {