mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-1527: Handle attachment downloads (#894)
This commit is contained in:
committed by
Álison Fernandes
parent
6e945a4385
commit
8bb754f85b
+25
@@ -9,6 +9,7 @@ import com.x8bit.bitwarden.data.vault.datasource.network.model.FileUploadType
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.ShareCipherJsonRequest
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.UpdateCipherCollectionsJsonRequest
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.UpdateCipherResponseJson
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockAttachment
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockAttachmentJsonRequest
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockAttachmentJsonResponse
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCipher
|
||||
@@ -245,6 +246,19 @@ class CiphersServiceTest : BaseServiceTest() {
|
||||
result.getOrThrow(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCipherAttachment should return the correct response`() = runTest {
|
||||
server.enqueue(MockResponse().setBody(GET_CIPHER_ATTACHMENT_SUCCESS_JSON))
|
||||
val result = ciphersService.getCipherAttachment(
|
||||
cipherId = "mockId-1",
|
||||
attachmentId = "mockId-1",
|
||||
)
|
||||
assertEquals(
|
||||
createMockAttachment(number = 1),
|
||||
result.getOrThrow(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupMockUri(
|
||||
@@ -455,3 +469,14 @@ private const val UPDATE_CIPHER_INVALID_JSON = """
|
||||
"validationErrors": null
|
||||
}
|
||||
"""
|
||||
|
||||
private const val GET_CIPHER_ATTACHMENT_SUCCESS_JSON = """
|
||||
{
|
||||
"fileName": "mockFileName-1",
|
||||
"size": 1,
|
||||
"sizeName": "mockSizeName-1",
|
||||
"id": "mockId-1",
|
||||
"url": "mockUrl-1",
|
||||
"key": "mockKey-1"
|
||||
}
|
||||
"""
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.x8bit.bitwarden.data.vault.datasource.network.service
|
||||
|
||||
import com.x8bit.bitwarden.data.platform.base.BaseServiceTest
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.api.DownloadApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import retrofit2.create
|
||||
|
||||
class DownloadServiceTest : BaseServiceTest() {
|
||||
private val downloadApi: DownloadApi = retrofit.create()
|
||||
|
||||
private val downloadService: DownloadService = DownloadServiceImpl(
|
||||
downloadApi = downloadApi,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `getDataStream should return a raw stream RespondBody`() = runTest {
|
||||
server.enqueue(
|
||||
MockResponse()
|
||||
.setResponseCode(200)
|
||||
.setBody("Bitwarden")
|
||||
.setHeader("Content-Type", "application/stream"),
|
||||
)
|
||||
val url = "/test-url"
|
||||
val result = downloadService.getDataStream(url)
|
||||
assertTrue(result.isSuccess)
|
||||
assertEquals("Bitwarden", String(result.getOrThrow().bytes()))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.x8bit.bitwarden.data.vault.datasource.sdk
|
||||
|
||||
import com.bitwarden.core.Attachment
|
||||
import com.bitwarden.core.AttachmentEncryptResult
|
||||
import com.bitwarden.core.AttachmentView
|
||||
import com.bitwarden.core.Cipher
|
||||
@@ -661,6 +662,42 @@ class VaultSdkSourceTest {
|
||||
verify { sdkClientManager.getOrCreateClient(userId = userId) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `File decrypt should call SDK and return a Result with correct data`() = runBlocking {
|
||||
val userId = "userId"
|
||||
val mockCipher = mockk<Cipher>()
|
||||
val mockAttachment = mockk<Attachment>()
|
||||
val expectedResult = Unit
|
||||
coEvery {
|
||||
clientVault.attachments().decryptFile(
|
||||
cipher = mockCipher,
|
||||
attachment = mockAttachment,
|
||||
encryptedFilePath = "encrypted_path",
|
||||
decryptedFilePath = "decrypted_path",
|
||||
)
|
||||
} just runs
|
||||
val result = vaultSdkSource.decryptFile(
|
||||
userId = userId,
|
||||
cipher = mockCipher,
|
||||
attachment = mockAttachment,
|
||||
encryptedFilePath = "encrypted_path",
|
||||
decryptedFilePath = "decrypted_path",
|
||||
)
|
||||
assertEquals(
|
||||
expectedResult.asSuccess(),
|
||||
result,
|
||||
)
|
||||
coVerify {
|
||||
clientVault.attachments().decryptFile(
|
||||
cipher = mockCipher,
|
||||
attachment = mockAttachment,
|
||||
encryptedFilePath = "encrypted_path",
|
||||
decryptedFilePath = "decrypted_path",
|
||||
)
|
||||
}
|
||||
verify { sdkClientManager.getOrCreateClient(userId = userId) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `encryptPasswordHistory should call SDK and return a Result with correct data`() =
|
||||
runBlocking {
|
||||
|
||||
+461
-146
@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.vault.repository
|
||||
import android.net.Uri
|
||||
import app.cash.turbine.test
|
||||
import app.cash.turbine.turbineScope
|
||||
import com.bitwarden.core.Attachment
|
||||
import com.bitwarden.core.Cipher
|
||||
import com.bitwarden.core.CipherView
|
||||
import com.bitwarden.core.CollectionView
|
||||
@@ -79,6 +80,7 @@ import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSendView
|
||||
import com.x8bit.bitwarden.data.vault.manager.FileManager
|
||||
import com.x8bit.bitwarden.data.vault.manager.TotpCodeManager
|
||||
import com.x8bit.bitwarden.data.vault.manager.VaultLockManager
|
||||
import com.x8bit.bitwarden.data.vault.manager.model.DownloadResult
|
||||
import com.x8bit.bitwarden.data.vault.manager.model.VerificationCodeItem
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.CreateAttachmentResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.CreateCipherResult
|
||||
@@ -89,6 +91,7 @@ import com.x8bit.bitwarden.data.vault.repository.model.DeleteCipherResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.DeleteFolderResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.DeleteSendResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.DomainsData
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.DownloadAttachmentResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.GenerateTotpResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.RemovePasswordSendResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.RestoreCipherResult
|
||||
@@ -130,6 +133,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import retrofit2.HttpException
|
||||
import java.io.File
|
||||
import java.net.UnknownHostException
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
@@ -3326,6 +3330,312 @@ class VaultRepositoryTest {
|
||||
assertEquals(CreateAttachmentResult.Success(mockCipherView), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `downloadAttachment with missing attachment should return Failure`() = runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
|
||||
val attachmentId = "mockId-1"
|
||||
val attachment = mockk<Attachment> {
|
||||
every { id } returns attachmentId
|
||||
}
|
||||
val cipher = mockk<Cipher> {
|
||||
every { attachments } returns emptyList()
|
||||
every { id } returns "mockId-1"
|
||||
}
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
} returns cipher.asSuccess()
|
||||
|
||||
assertEquals(
|
||||
DownloadAttachmentResult.Failure,
|
||||
vaultRepository.downloadAttachment(
|
||||
cipherView = cipherView,
|
||||
attachmentId = attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
}
|
||||
coVerify(exactly = 0) {
|
||||
ciphersService.getCipherAttachment(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `downloadAttachment with failed attachment details request should return Failure`() =
|
||||
runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
|
||||
val attachmentId = "mockId-1"
|
||||
val attachment = mockk<Attachment> {
|
||||
every { id } returns attachmentId
|
||||
}
|
||||
val cipher = mockk<Cipher> {
|
||||
every { attachments } returns listOf(attachment)
|
||||
every { id } returns "mockId-1"
|
||||
}
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
} returns cipher.asSuccess()
|
||||
|
||||
coEvery {
|
||||
ciphersService.getCipherAttachment(any(), any())
|
||||
} returns Throwable().asFailure()
|
||||
|
||||
assertEquals(
|
||||
DownloadAttachmentResult.Failure,
|
||||
vaultRepository.downloadAttachment(
|
||||
cipherView = cipherView,
|
||||
attachmentId = attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
ciphersService.getCipherAttachment(
|
||||
cipherId = requireNotNull(cipherView.id),
|
||||
attachmentId = attachmentId,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `downloadAttachment with attachment details missing url should return Failure`() = runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
|
||||
val attachmentId = "mockId-1"
|
||||
val attachment = mockk<Attachment> {
|
||||
every { id } returns attachmentId
|
||||
}
|
||||
val cipher = mockk<Cipher> {
|
||||
every { attachments } returns listOf(attachment)
|
||||
every { id } returns "mockId-1"
|
||||
}
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
} returns cipher.asSuccess()
|
||||
|
||||
val response = mockk<SyncResponseJson.Cipher.Attachment> {
|
||||
every { url } returns null
|
||||
}
|
||||
coEvery {
|
||||
ciphersService.getCipherAttachment(any(), any())
|
||||
} returns response.asSuccess()
|
||||
|
||||
assertEquals(
|
||||
DownloadAttachmentResult.Failure,
|
||||
vaultRepository.downloadAttachment(
|
||||
cipherView = cipherView,
|
||||
attachmentId = attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
ciphersService.getCipherAttachment(
|
||||
cipherId = requireNotNull(cipherView.id),
|
||||
attachmentId = attachmentId,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `downloadAttachment with failed download should return Failure`() = runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
|
||||
val attachmentId = "mockId-1"
|
||||
val attachment = mockk<Attachment> {
|
||||
every { id } returns attachmentId
|
||||
}
|
||||
val cipher = mockk<Cipher> {
|
||||
every { attachments } returns listOf(attachment)
|
||||
every { id } returns "mockId-1"
|
||||
}
|
||||
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
} returns cipher.asSuccess()
|
||||
|
||||
val response = mockk<SyncResponseJson.Cipher.Attachment> {
|
||||
every { url } returns "https://bitwarden.com"
|
||||
}
|
||||
coEvery {
|
||||
ciphersService.getCipherAttachment(any(), any())
|
||||
} returns response.asSuccess()
|
||||
|
||||
coEvery {
|
||||
fileManager.downloadFileToCache(any())
|
||||
} returns DownloadResult.Failure
|
||||
|
||||
assertEquals(
|
||||
DownloadAttachmentResult.Failure,
|
||||
vaultRepository.downloadAttachment(
|
||||
cipherView = cipherView,
|
||||
attachmentId = attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
ciphersService.getCipherAttachment(
|
||||
cipherId = requireNotNull(cipherView.id),
|
||||
attachmentId = attachmentId,
|
||||
)
|
||||
fileManager.downloadFileToCache("https://bitwarden.com")
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `downloadAttachment with failed decryption should delete file and return Failure`() =
|
||||
runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
|
||||
val attachmentId = "mockId-1"
|
||||
val attachment = mockk<Attachment> {
|
||||
every { id } returns attachmentId
|
||||
}
|
||||
val cipher = mockk<Cipher> {
|
||||
every { attachments } returns listOf(attachment)
|
||||
every { id } returns "mockId-1"
|
||||
}
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
} returns cipher.asSuccess()
|
||||
|
||||
val response = mockk<SyncResponseJson.Cipher.Attachment> {
|
||||
every { url } returns "https://bitwarden.com"
|
||||
}
|
||||
coEvery {
|
||||
ciphersService.getCipherAttachment(any(), any())
|
||||
} returns response.asSuccess()
|
||||
|
||||
val file = mockk<File> {
|
||||
every { path } returns "path/to/encrypted/file"
|
||||
every { delete() } returns true
|
||||
}
|
||||
coEvery {
|
||||
fileManager.downloadFileToCache(any())
|
||||
} returns DownloadResult.Success(file)
|
||||
|
||||
coEvery {
|
||||
vaultSdkSource.decryptFile(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipher = cipher,
|
||||
attachment = attachment,
|
||||
encryptedFilePath = "path/to/encrypted/file",
|
||||
decryptedFilePath = "path/to/encrypted/file_decrypted",
|
||||
)
|
||||
} returns Throwable().asFailure()
|
||||
|
||||
assertEquals(
|
||||
DownloadAttachmentResult.Failure,
|
||||
vaultRepository.downloadAttachment(
|
||||
cipherView = cipherView,
|
||||
attachmentId = attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
ciphersService.getCipherAttachment(
|
||||
cipherId = requireNotNull(cipherView.id),
|
||||
attachmentId = attachmentId,
|
||||
)
|
||||
fileManager.downloadFileToCache("https://bitwarden.com")
|
||||
vaultSdkSource.decryptFile(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipher = cipher,
|
||||
attachment = attachment,
|
||||
encryptedFilePath = "path/to/encrypted/file",
|
||||
decryptedFilePath = "path/to/encrypted/file_decrypted",
|
||||
)
|
||||
}
|
||||
verify(exactly = 1) {
|
||||
file.delete()
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `downloadAttachment with successful decryption should delete file and return Success`() =
|
||||
runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
|
||||
val attachmentId = "mockId-1"
|
||||
val attachment = mockk<Attachment> {
|
||||
every { id } returns attachmentId
|
||||
}
|
||||
val cipher = mockk<Cipher> {
|
||||
every { attachments } returns listOf(attachment)
|
||||
every { id } returns "mockId-1"
|
||||
}
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
} returns cipher.asSuccess()
|
||||
|
||||
val response = mockk<SyncResponseJson.Cipher.Attachment> {
|
||||
every { url } returns "https://bitwarden.com"
|
||||
}
|
||||
coEvery {
|
||||
ciphersService.getCipherAttachment(any(), any())
|
||||
} returns response.asSuccess()
|
||||
|
||||
val file = mockk<File> {
|
||||
every { path } returns "path/to/encrypted/file"
|
||||
every { delete() } returns true
|
||||
}
|
||||
coEvery {
|
||||
fileManager.downloadFileToCache(any())
|
||||
} returns DownloadResult.Success(file)
|
||||
|
||||
coEvery {
|
||||
vaultSdkSource.decryptFile(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipher = cipher,
|
||||
attachment = attachment,
|
||||
encryptedFilePath = "path/to/encrypted/file",
|
||||
decryptedFilePath = "path/to/encrypted/file_decrypted",
|
||||
)
|
||||
} returns Unit.asSuccess()
|
||||
|
||||
assertEquals(
|
||||
DownloadAttachmentResult.Success(
|
||||
file = File("path/to/encrypted/file_decrypted"),
|
||||
),
|
||||
vaultRepository.downloadAttachment(
|
||||
cipherView = cipherView,
|
||||
attachmentId = attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultSdkSource.encryptCipher(MOCK_USER_STATE.activeUserId, cipherView)
|
||||
ciphersService.getCipherAttachment(
|
||||
cipherId = requireNotNull(cipherView.id),
|
||||
attachmentId = attachmentId,
|
||||
)
|
||||
fileManager.downloadFileToCache("https://bitwarden.com")
|
||||
vaultSdkSource.decryptFile(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipher = cipher,
|
||||
attachment = attachment,
|
||||
encryptedFilePath = "path/to/encrypted/file",
|
||||
decryptedFilePath = "path/to/encrypted/file_decrypted",
|
||||
)
|
||||
}
|
||||
verify(exactly = 1) {
|
||||
file.delete()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `generateTotp with no active user should return GenerateTotpResult Error`() =
|
||||
runTest {
|
||||
@@ -3901,180 +4211,185 @@ class VaultRepositoryTest {
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `syncCipherUpsertFlow create with local cipher with no common collections should do nothing`() = runTest {
|
||||
val number = 1
|
||||
val cipherId = "mockId-$number"
|
||||
fun `syncCipherUpsertFlow create with local cipher with no common collections should do nothing`() =
|
||||
runTest {
|
||||
val number = 1
|
||||
val cipherId = "mockId-$number"
|
||||
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
val cipherView = createMockCipherView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCipherList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipherList = listOf(createMockSdkCipher(number = number)),
|
||||
)
|
||||
} returns listOf(cipherView).asSuccess()
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
val cipherView = createMockCipherView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCipherList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipherList = listOf(createMockSdkCipher(number = number)),
|
||||
)
|
||||
} returns listOf(cipherView).asSuccess()
|
||||
|
||||
val ciphersFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Cipher>>()
|
||||
setupVaultDiskSourceFlows(ciphersFlow = ciphersFlow)
|
||||
val ciphersFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Cipher>>()
|
||||
setupVaultDiskSourceFlows(ciphersFlow = ciphersFlow)
|
||||
|
||||
vaultRepository.ciphersStateFlow.test {
|
||||
// Populate and consume items related to the ciphers flow
|
||||
awaitItem()
|
||||
ciphersFlow.tryEmit(listOf(createMockCipher(number = number)))
|
||||
awaitItem()
|
||||
vaultRepository.ciphersStateFlow.test {
|
||||
// Populate and consume items related to the ciphers flow
|
||||
awaitItem()
|
||||
ciphersFlow.tryEmit(listOf(createMockCipher(number = number)))
|
||||
awaitItem()
|
||||
|
||||
mutableSyncCipherUpsertFlow.tryEmit(
|
||||
SyncCipherUpsertData(
|
||||
cipherId = cipherId,
|
||||
revisionDate = ZonedDateTime.now(),
|
||||
isUpdate = false,
|
||||
collectionIds = null,
|
||||
organizationId = null,
|
||||
),
|
||||
)
|
||||
mutableSyncCipherUpsertFlow.tryEmit(
|
||||
SyncCipherUpsertData(
|
||||
cipherId = cipherId,
|
||||
revisionDate = ZonedDateTime.now(),
|
||||
isUpdate = false,
|
||||
collectionIds = null,
|
||||
organizationId = null,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
ciphersService.getCipher(any())
|
||||
vaultDiskSource.saveCipher(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
ciphersService.getCipher(any())
|
||||
vaultDiskSource.saveCipher(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `syncCipherUpsertFlow create with local cipher, and with common collections, should make a request and save cipher to disk`() = runTest {
|
||||
val number = 1
|
||||
val cipherId = "mockId-$number"
|
||||
fun `syncCipherUpsertFlow create with local cipher, and with common collections, should make a request and save cipher to disk`() =
|
||||
runTest {
|
||||
val number = 1
|
||||
val cipherId = "mockId-$number"
|
||||
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
val cipherView = createMockCipherView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCipherList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipherList = listOf(createMockSdkCipher(number = number)),
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
val cipherView = createMockCipherView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCipherList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipherList = listOf(createMockSdkCipher(number = number)),
|
||||
)
|
||||
} returns listOf(cipherView).asSuccess()
|
||||
val collectionView = createMockCollectionView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCollectionList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
collectionList = listOf(createMockSdkCollection(number = number)),
|
||||
)
|
||||
} returns listOf(collectionView).asSuccess()
|
||||
|
||||
val cipher: SyncResponseJson.Cipher = mockk()
|
||||
coEvery {
|
||||
ciphersService.getCipher(cipherId)
|
||||
} returns cipher.asSuccess()
|
||||
coEvery {
|
||||
vaultDiskSource.saveCipher(userId = MOCK_USER_STATE.activeUserId, cipher = cipher)
|
||||
} just runs
|
||||
|
||||
val ciphersFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Cipher>>()
|
||||
val collectionsFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Collection>>()
|
||||
setupVaultDiskSourceFlows(
|
||||
ciphersFlow = ciphersFlow,
|
||||
collectionsFlow = collectionsFlow,
|
||||
)
|
||||
} returns listOf(cipherView).asSuccess()
|
||||
val collectionView = createMockCollectionView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCollectionList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
collectionList = listOf(createMockSdkCollection(number = number)),
|
||||
)
|
||||
} returns listOf(collectionView).asSuccess()
|
||||
|
||||
val cipher: SyncResponseJson.Cipher = mockk()
|
||||
coEvery {
|
||||
ciphersService.getCipher(cipherId)
|
||||
} returns cipher.asSuccess()
|
||||
coEvery {
|
||||
vaultDiskSource.saveCipher(userId = MOCK_USER_STATE.activeUserId, cipher = cipher)
|
||||
} just runs
|
||||
turbineScope {
|
||||
val ciphersStateFlow = vaultRepository.ciphersStateFlow.testIn(backgroundScope)
|
||||
val collectionsStateFlow =
|
||||
vaultRepository.collectionsStateFlow.testIn(backgroundScope)
|
||||
|
||||
val ciphersFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Cipher>>()
|
||||
val collectionsFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Collection>>()
|
||||
setupVaultDiskSourceFlows(
|
||||
ciphersFlow = ciphersFlow,
|
||||
collectionsFlow = collectionsFlow,
|
||||
)
|
||||
// Populate and consume items related to the ciphers flow
|
||||
ciphersStateFlow.awaitItem()
|
||||
ciphersFlow.tryEmit(listOf(createMockCipher(number = number)))
|
||||
ciphersStateFlow.awaitItem()
|
||||
|
||||
turbineScope {
|
||||
val ciphersStateFlow = vaultRepository.ciphersStateFlow.testIn(backgroundScope)
|
||||
val collectionsStateFlow = vaultRepository.collectionsStateFlow.testIn(backgroundScope)
|
||||
// Populate and consume items related to the collections flow
|
||||
collectionsStateFlow.awaitItem()
|
||||
collectionsFlow.tryEmit(listOf(createMockCollection(number = number)))
|
||||
collectionsStateFlow.awaitItem()
|
||||
|
||||
// Populate and consume items related to the ciphers flow
|
||||
ciphersStateFlow.awaitItem()
|
||||
ciphersFlow.tryEmit(listOf(createMockCipher(number = number)))
|
||||
ciphersStateFlow.awaitItem()
|
||||
mutableSyncCipherUpsertFlow.tryEmit(
|
||||
SyncCipherUpsertData(
|
||||
cipherId = cipherId,
|
||||
revisionDate = ZonedDateTime.now(),
|
||||
isUpdate = false,
|
||||
collectionIds = listOf("mockId-1"),
|
||||
organizationId = "mock-id",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// Populate and consume items related to the collections flow
|
||||
collectionsStateFlow.awaitItem()
|
||||
collectionsFlow.tryEmit(listOf(createMockCollection(number = number)))
|
||||
collectionsStateFlow.awaitItem()
|
||||
|
||||
mutableSyncCipherUpsertFlow.tryEmit(
|
||||
SyncCipherUpsertData(
|
||||
cipherId = cipherId,
|
||||
revisionDate = ZonedDateTime.now(),
|
||||
isUpdate = false,
|
||||
collectionIds = listOf("mockId-1"),
|
||||
organizationId = "mock-id",
|
||||
),
|
||||
)
|
||||
coVerify(exactly = 1) {
|
||||
ciphersService.getCipher(any())
|
||||
vaultDiskSource.saveCipher(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
ciphersService.getCipher(any())
|
||||
vaultDiskSource.saveCipher(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `syncCipherUpsertFlow update with no local cipher, but with common collections, should make a request save cipher to disk`() = runTest {
|
||||
val number = 1
|
||||
val cipherId = "mockId-$number"
|
||||
fun `syncCipherUpsertFlow update with no local cipher, but with common collections, should make a request save cipher to disk`() =
|
||||
runTest {
|
||||
val number = 1
|
||||
val cipherId = "mockId-$number"
|
||||
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCipherList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipherList = listOf(),
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCipherList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
cipherList = listOf(),
|
||||
)
|
||||
} returns listOf<CipherView>().asSuccess()
|
||||
val collectionView = createMockCollectionView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCollectionList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
collectionList = listOf(createMockSdkCollection(number = number)),
|
||||
)
|
||||
} returns listOf(collectionView).asSuccess()
|
||||
|
||||
val cipher: SyncResponseJson.Cipher = mockk()
|
||||
coEvery {
|
||||
ciphersService.getCipher(cipherId)
|
||||
} returns cipher.asSuccess()
|
||||
coEvery {
|
||||
vaultDiskSource.saveCipher(userId = MOCK_USER_STATE.activeUserId, cipher = cipher)
|
||||
} just runs
|
||||
|
||||
val ciphersFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Cipher>>()
|
||||
val collectionsFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Collection>>()
|
||||
setupVaultDiskSourceFlows(
|
||||
ciphersFlow = ciphersFlow,
|
||||
collectionsFlow = collectionsFlow,
|
||||
)
|
||||
} returns listOf<CipherView>().asSuccess()
|
||||
val collectionView = createMockCollectionView(number = number)
|
||||
coEvery {
|
||||
vaultSdkSource.decryptCollectionList(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
collectionList = listOf(createMockSdkCollection(number = number)),
|
||||
)
|
||||
} returns listOf(collectionView).asSuccess()
|
||||
|
||||
val cipher: SyncResponseJson.Cipher = mockk()
|
||||
coEvery {
|
||||
ciphersService.getCipher(cipherId)
|
||||
} returns cipher.asSuccess()
|
||||
coEvery {
|
||||
vaultDiskSource.saveCipher(userId = MOCK_USER_STATE.activeUserId, cipher = cipher)
|
||||
} just runs
|
||||
turbineScope {
|
||||
val ciphersStateFlow = vaultRepository.ciphersStateFlow.testIn(backgroundScope)
|
||||
val collectionsStateFlow =
|
||||
vaultRepository.collectionsStateFlow.testIn(backgroundScope)
|
||||
|
||||
val ciphersFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Cipher>>()
|
||||
val collectionsFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Collection>>()
|
||||
setupVaultDiskSourceFlows(
|
||||
ciphersFlow = ciphersFlow,
|
||||
collectionsFlow = collectionsFlow,
|
||||
)
|
||||
// Populate and consume items related to the ciphers flow
|
||||
ciphersStateFlow.awaitItem()
|
||||
ciphersFlow.tryEmit(listOf())
|
||||
ciphersStateFlow.awaitItem()
|
||||
|
||||
turbineScope {
|
||||
val ciphersStateFlow = vaultRepository.ciphersStateFlow.testIn(backgroundScope)
|
||||
val collectionsStateFlow = vaultRepository.collectionsStateFlow.testIn(backgroundScope)
|
||||
// Populate and consume items related to the collections flow
|
||||
collectionsStateFlow.awaitItem()
|
||||
collectionsFlow.tryEmit(listOf(createMockCollection(number = number)))
|
||||
collectionsStateFlow.awaitItem()
|
||||
|
||||
// Populate and consume items related to the ciphers flow
|
||||
ciphersStateFlow.awaitItem()
|
||||
ciphersFlow.tryEmit(listOf())
|
||||
ciphersStateFlow.awaitItem()
|
||||
mutableSyncCipherUpsertFlow.tryEmit(
|
||||
SyncCipherUpsertData(
|
||||
cipherId = cipherId,
|
||||
revisionDate = ZonedDateTime.now(),
|
||||
isUpdate = true,
|
||||
collectionIds = listOf("mockId-1"),
|
||||
organizationId = "mock-id",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// Populate and consume items related to the collections flow
|
||||
collectionsStateFlow.awaitItem()
|
||||
collectionsFlow.tryEmit(listOf(createMockCollection(number = number)))
|
||||
collectionsStateFlow.awaitItem()
|
||||
|
||||
mutableSyncCipherUpsertFlow.tryEmit(
|
||||
SyncCipherUpsertData(
|
||||
cipherId = cipherId,
|
||||
revisionDate = ZonedDateTime.now(),
|
||||
isUpdate = true,
|
||||
collectionIds = listOf("mockId-1"),
|
||||
organizationId = "mock-id",
|
||||
),
|
||||
)
|
||||
coVerify(exactly = 1) {
|
||||
ciphersService.getCipher(any())
|
||||
vaultDiskSource.saveCipher(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
ciphersService.getCipher(any())
|
||||
vaultDiskSource.saveCipher(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `syncCipherUpsertFlow update with no local cipher should do nothing`() = runTest {
|
||||
val number = 1
|
||||
|
||||
+194
-1
@@ -60,7 +60,7 @@ class VaultItemScreenTest : BaseComposeTest() {
|
||||
private var onNavigateToMoveToOrganizationItemId: String? = null
|
||||
private var onNavigateToAttachmentsId: String? = null
|
||||
|
||||
private val intentManager = mockk<IntentManager>()
|
||||
private val intentManager = mockk<IntentManager>(relaxed = true)
|
||||
|
||||
private val mutableEventFlow = bufferedMutableSharedFlow<VaultItemEvent>()
|
||||
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
|
||||
@@ -135,6 +135,15 @@ class VaultItemScreenTest : BaseComposeTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToSelectAttachmentSaveLocation should invoke createAttachmentChooserIntent`() {
|
||||
mutableEventFlow.tryEmit(VaultItemEvent.NavigateToSelectAttachmentSaveLocation("test.mp4"))
|
||||
|
||||
verify(exactly = 1) {
|
||||
intentManager.createAttachmentChooserIntent("test.mp4")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `basic dialog should be displayed according to state`() {
|
||||
val message = "message"
|
||||
@@ -311,6 +320,179 @@ class VaultItemScreenTest : BaseComposeTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `attachments should be displayed according to state`() {
|
||||
DEFAULT_VIEW_STATES
|
||||
.forEach { typeState ->
|
||||
mutableStateFlow.update { it.copy(viewState = typeState) }
|
||||
composeTestRule.onNodeWithTextAfterScroll("Attachments").assertIsDisplayed()
|
||||
composeTestRule.onNodeWithTextAfterScroll("test.mp4").assertIsDisplayed()
|
||||
composeTestRule.onNodeWithTextAfterScroll("11 MB").assertIsDisplayed()
|
||||
|
||||
mutableStateFlow.update { currentState ->
|
||||
updateCommonContent(currentState) { copy(attachments = emptyList()) }
|
||||
}
|
||||
|
||||
composeTestRule.assertScrollableNodeDoesNotExist("Attachments")
|
||||
composeTestRule.assertScrollableNodeDoesNotExist("test.mp4")
|
||||
composeTestRule.assertScrollableNodeDoesNotExist("11 MB")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `attachment download click for non-premium users should show an error dialog`() {
|
||||
mutableStateFlow.update { currentState ->
|
||||
currentState.copy(
|
||||
viewState = EMPTY_LOGIN_VIEW_STATE.copy(
|
||||
common = EMPTY_COMMON.copy(
|
||||
attachments = listOf(
|
||||
VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = false,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
composeTestRule.onNodeWithContentDescriptionAfterScroll("Download").performClick()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText(
|
||||
"A premium membership is required to use this feature.",
|
||||
)
|
||||
.filterToOne(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Ok")
|
||||
.filterToOne(hasAnyAncestor(isDialog()))
|
||||
.performClick()
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `attachment download click for large downloads should show a prompt and dismiss when clicking No`() {
|
||||
mutableStateFlow.update { currentState ->
|
||||
currentState.copy(
|
||||
viewState = EMPTY_LOGIN_VIEW_STATE.copy(
|
||||
common = EMPTY_COMMON.copy(
|
||||
attachments = listOf(
|
||||
VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = true,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
composeTestRule.onNodeWithContentDescriptionAfterScroll("Download").performClick()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText(
|
||||
"This attachment is 11 MB in size. Are you sure you want to download it onto " +
|
||||
"your device?",
|
||||
)
|
||||
.filterToOne(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText("No")
|
||||
.filterToOne(hasAnyAncestor(isDialog()))
|
||||
.performClick()
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `attachment download click for large downloads should show a prompt and emit AttachmentDownloadClick`() {
|
||||
val attachment = VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = true,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
)
|
||||
mutableStateFlow.update { currentState ->
|
||||
currentState.copy(
|
||||
viewState = EMPTY_LOGIN_VIEW_STATE.copy(
|
||||
common = EMPTY_COMMON.copy(
|
||||
attachments = listOf(attachment),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
composeTestRule.onNodeWithContentDescriptionAfterScroll("Download").performClick()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText(
|
||||
"This attachment is 11 MB in size. Are you sure you want to download it onto " +
|
||||
"your device?",
|
||||
)
|
||||
.filterToOne(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Yes")
|
||||
.filterToOne(hasAnyAncestor(isDialog()))
|
||||
.performClick()
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
|
||||
verify {
|
||||
viewModel.trySendAction(VaultItemAction.Common.AttachmentDownloadClick(attachment))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `attachment download click for smaller downloads should emit AttachmentDownloadClick`() {
|
||||
val attachment = VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "9 MB",
|
||||
isLargeFile = false,
|
||||
isDownloadAllowed = true,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
)
|
||||
mutableStateFlow.update { currentState ->
|
||||
currentState.copy(
|
||||
viewState = EMPTY_LOGIN_VIEW_STATE.copy(
|
||||
common = EMPTY_COMMON.copy(
|
||||
attachments = listOf(attachment),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
composeTestRule.onNodeWithContentDescriptionAfterScroll("Download").performClick()
|
||||
|
||||
composeTestRule.assertNoDialogExists()
|
||||
|
||||
verify {
|
||||
viewModel.trySendAction(VaultItemAction.Common.AttachmentDownloadClick(attachment))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on show hidden field click should send HiddenFieldVisibilityClicked`() {
|
||||
val textField = VaultItemState.ViewState.Content.Common.Custom.HiddenField(
|
||||
@@ -1913,6 +2095,16 @@ private val DEFAULT_COMMON: VaultItemState.ViewState.Content.Common =
|
||||
),
|
||||
),
|
||||
requiresReprompt = true,
|
||||
attachments = listOf(
|
||||
VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = true,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
private val DEFAULT_LOGIN: VaultItemState.ViewState.Content.ItemType.Login =
|
||||
@@ -1970,6 +2162,7 @@ private val EMPTY_COMMON: VaultItemState.ViewState.Content.Common =
|
||||
notes = null,
|
||||
customFields = emptyList(),
|
||||
requiresReprompt = true,
|
||||
attachments = emptyList(),
|
||||
)
|
||||
|
||||
private val EMPTY_LOGIN_TYPE: VaultItemState.ViewState.Content.ItemType.Login =
|
||||
|
||||
+308
-1
@@ -1,5 +1,6 @@
|
||||
package com.x8bit.bitwarden.ui.vault.feature.item
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import app.cash.turbine.turbineScope
|
||||
@@ -13,9 +14,11 @@ import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardMan
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
|
||||
import com.x8bit.bitwarden.data.vault.manager.FileManager
|
||||
import com.x8bit.bitwarden.data.vault.manager.model.VerificationCodeItem
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.DeleteCipherResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.DownloadAttachmentResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.RestoreCipherResult
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
@@ -42,6 +45,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import java.time.Instant
|
||||
|
||||
@Suppress("LargeClass")
|
||||
@@ -61,6 +65,8 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
every { getVaultItemStateFlow(VAULT_ITEM_ID) } returns mutableVaultItemFlow
|
||||
}
|
||||
|
||||
private val mockFileManager: FileManager = mockk()
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mockkStatic(CipherView::toViewState)
|
||||
@@ -744,6 +750,7 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
val loginViewState = VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(
|
||||
isEmpty = true,
|
||||
isPremiumUser = true,
|
||||
).copy(
|
||||
requiresReprompt = false,
|
||||
customFields = listOf(hiddenField),
|
||||
@@ -772,7 +779,7 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
assertEquals(
|
||||
loginState.copy(
|
||||
viewState = loginViewState.copy(
|
||||
common = createCommonContent(isEmpty = true).copy(
|
||||
common = createCommonContent(isEmpty = true, isPremiumUser = true).copy(
|
||||
requiresReprompt = false,
|
||||
customFields = listOf(hiddenField.copy(isVisible = true)),
|
||||
),
|
||||
@@ -994,6 +1001,291 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on AttachmentDownloadClick should prompt for password if required`() =
|
||||
runTest {
|
||||
val loginViewState = createViewState(
|
||||
common = DEFAULT_COMMON.copy(requiresReprompt = true),
|
||||
)
|
||||
val mockCipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(
|
||||
isPremiumUser = true,
|
||||
totpCodeItemData = null,
|
||||
)
|
||||
} returns loginViewState
|
||||
}
|
||||
mutableVaultItemFlow.value = DataState.Loaded(data = mockCipherView)
|
||||
mutableAuthCodeItemFlow.value = DataState.Loaded(data = null)
|
||||
val loginState = DEFAULT_STATE.copy(viewState = loginViewState)
|
||||
val viewModel = createViewModel(state = loginState)
|
||||
|
||||
val attachment = VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = false,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
)
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(
|
||||
loginState,
|
||||
awaitItem(),
|
||||
)
|
||||
|
||||
viewModel.trySendAction(
|
||||
VaultItemAction.Common.AttachmentDownloadClick(attachment),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
loginState.copy(
|
||||
dialog = VaultItemState.DialogState.MasterPasswordDialog(
|
||||
action = PasswordRepromptAction.AttachmentDownloadClick(attachment),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
vaultRepo.downloadAttachment(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `on AttachmentDownloadClick should show loading dialog, attempt to download an attachment, and display an error dialog on failure`() =
|
||||
runTest {
|
||||
val loginViewState = createViewState(
|
||||
common = DEFAULT_COMMON.copy(requiresReprompt = false),
|
||||
)
|
||||
val mockCipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(
|
||||
isPremiumUser = true,
|
||||
totpCodeItemData = null,
|
||||
)
|
||||
} returns loginViewState
|
||||
}
|
||||
mutableVaultItemFlow.value = DataState.Loaded(data = mockCipherView)
|
||||
mutableAuthCodeItemFlow.value = DataState.Loaded(data = null)
|
||||
val loginState = DEFAULT_STATE.copy(viewState = loginViewState)
|
||||
val viewModel = createViewModel(state = loginState)
|
||||
|
||||
val attachment = VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = false,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
)
|
||||
|
||||
coEvery {
|
||||
vaultRepo.downloadAttachment(any(), any())
|
||||
} returns DownloadAttachmentResult.Failure
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(
|
||||
loginState,
|
||||
awaitItem(),
|
||||
)
|
||||
|
||||
viewModel.trySendAction(
|
||||
VaultItemAction.Common.AttachmentDownloadClick(attachment),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
loginState.copy(dialog = VaultItemState.DialogState.Loading(R.string.downloading.asText())),
|
||||
awaitItem(),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
loginState.copy(
|
||||
dialog = VaultItemState.DialogState.Generic(
|
||||
R.string.unable_to_download_file.asText(),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultRepo.downloadAttachment(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `on AttachmentDownloadClick should show loading dialog, attempt to download an attachment, and emit NavigateToSelectAttachmentSaveLocation on success`() =
|
||||
runTest {
|
||||
val loginViewState = createViewState(
|
||||
common = DEFAULT_COMMON.copy(requiresReprompt = false),
|
||||
)
|
||||
val mockCipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(
|
||||
isPremiumUser = true,
|
||||
totpCodeItemData = null,
|
||||
)
|
||||
} returns loginViewState
|
||||
}
|
||||
mutableVaultItemFlow.value = DataState.Loaded(data = mockCipherView)
|
||||
mutableAuthCodeItemFlow.value = DataState.Loaded(data = null)
|
||||
val loginState = DEFAULT_STATE.copy(viewState = loginViewState)
|
||||
val viewModel = createViewModel(state = loginState)
|
||||
|
||||
val attachment = VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = false,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
)
|
||||
|
||||
val file = mockk<File>()
|
||||
coEvery {
|
||||
vaultRepo.downloadAttachment(any(), any())
|
||||
} returns DownloadAttachmentResult.Success(file)
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(
|
||||
loginState,
|
||||
awaitItem(),
|
||||
)
|
||||
|
||||
viewModel.trySendAction(
|
||||
VaultItemAction.Common.AttachmentDownloadClick(attachment),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
loginState.copy(
|
||||
dialog = VaultItemState.DialogState.Loading(
|
||||
R.string.downloading.asText(),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
|
||||
viewModel.eventFlow.test {
|
||||
assertEquals(
|
||||
VaultItemEvent.NavigateToSelectAttachmentSaveLocation("test.mp4"),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultRepo.downloadAttachment(any(), any())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `on AttachmentFileLocationReceive success should hide loading dialog, copy file, delete file, and show toast`() =
|
||||
runTest {
|
||||
val file = mockk<File>()
|
||||
val viewModel = createViewModel(state = DEFAULT_STATE, tempAttachmentFile = file)
|
||||
|
||||
coEvery {
|
||||
mockFileManager.deleteFile(any())
|
||||
} just runs
|
||||
|
||||
val uri = mockk<Uri>()
|
||||
coEvery {
|
||||
mockFileManager.fileToUri(uri, file)
|
||||
} returns true
|
||||
|
||||
viewModel.trySendAction(
|
||||
VaultItemAction.Common.AttachmentFileLocationReceive(uri),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE,
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
|
||||
viewModel.eventFlow.test {
|
||||
assertEquals(
|
||||
VaultItemEvent.ShowToast(R.string.save_attachment_success.asText()),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
mockFileManager.deleteFile(file)
|
||||
mockFileManager.fileToUri(uri, file)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `on AttachmentFileLocationReceive failure should hide loading dialog, copy file, delete file, and show dialog`() =
|
||||
runTest {
|
||||
val file = mockk<File>()
|
||||
val viewModel = createViewModel(state = DEFAULT_STATE, tempAttachmentFile = file)
|
||||
|
||||
coEvery {
|
||||
mockFileManager.deleteFile(any())
|
||||
} just runs
|
||||
|
||||
val uri = mockk<Uri>()
|
||||
coEvery {
|
||||
mockFileManager.fileToUri(uri, file)
|
||||
} returns false
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
viewModel.trySendAction(
|
||||
VaultItemAction.Common.AttachmentFileLocationReceive(uri),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE,
|
||||
awaitItem(),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
dialog = VaultItemState.DialogState.Generic(
|
||||
R.string.unable_to_save_attachment.asText(),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
mockFileManager.deleteFile(file)
|
||||
mockFileManager.fileToUri(uri, file)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on NoAttachmentFileLocationReceive failure should show dialog`() {
|
||||
val file = mockk<File>()
|
||||
val viewModel = createViewModel(state = DEFAULT_STATE, tempAttachmentFile = file)
|
||||
|
||||
coEvery {
|
||||
mockFileManager.deleteFile(any())
|
||||
} just runs
|
||||
|
||||
viewModel.trySendAction(VaultItemAction.Common.NoAttachmentFileLocationReceive)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
dialog = VaultItemState.DialogState.Generic(
|
||||
R.string.unable_to_save_attachment.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
|
||||
coVerify { mockFileManager.deleteFile(file) }
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -1488,20 +1780,25 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("LongParameterList")
|
||||
private fun createViewModel(
|
||||
state: VaultItemState?,
|
||||
vaultItemId: String = VAULT_ITEM_ID,
|
||||
bitwardenClipboardManager: BitwardenClipboardManager = clipboardManager,
|
||||
authRepository: AuthRepository = authRepo,
|
||||
vaultRepository: VaultRepository = vaultRepo,
|
||||
fileManager: FileManager = mockFileManager,
|
||||
tempAttachmentFile: File? = null,
|
||||
): VaultItemViewModel = VaultItemViewModel(
|
||||
savedStateHandle = SavedStateHandle().apply {
|
||||
set("state", state)
|
||||
set("vault_item_id", vaultItemId)
|
||||
set("tempAttachmentFile", tempAttachmentFile)
|
||||
},
|
||||
clipboardManager = bitwardenClipboardManager,
|
||||
authRepository = authRepository,
|
||||
vaultRepository = vaultRepository,
|
||||
fileManager = fileManager,
|
||||
)
|
||||
|
||||
private fun createViewState(
|
||||
@@ -1619,6 +1916,16 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
),
|
||||
requiresReprompt = true,
|
||||
currentCipher = createMockCipherView(number = 1),
|
||||
attachments = listOf(
|
||||
VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = true,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
private val DEFAULT_VIEW_STATE: VaultItemState.ViewState.Content =
|
||||
|
||||
+16
-9
@@ -38,7 +38,8 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = false).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = false, isPremiumUser = true)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = createLoginContent(isEmpty = false),
|
||||
),
|
||||
viewState,
|
||||
@@ -62,7 +63,8 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = false).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = false, isPremiumUser = isPremiumUser)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = createLoginContent(isEmpty = false).copy(isPremiumUser = isPremiumUser),
|
||||
),
|
||||
viewState,
|
||||
@@ -79,7 +81,7 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = true).copy(
|
||||
common = createCommonContent(isEmpty = true, isPremiumUser = true).copy(
|
||||
currentCipher = cipherView,
|
||||
),
|
||||
type = createLoginContent(isEmpty = true),
|
||||
@@ -98,7 +100,8 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = false).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = false, isPremiumUser = true)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = createIdentityContent(isEmpty = false),
|
||||
),
|
||||
viewState,
|
||||
@@ -115,7 +118,8 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = true).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = true, isPremiumUser = true)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = createIdentityContent(isEmpty = true),
|
||||
),
|
||||
viewState,
|
||||
@@ -142,7 +146,8 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = false).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = false, isPremiumUser = true)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = createIdentityContent(
|
||||
isEmpty = false,
|
||||
identityName = "Mx middleName",
|
||||
@@ -174,7 +179,7 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = false).copy(
|
||||
common = createCommonContent(isEmpty = false, isPremiumUser = true).copy(
|
||||
currentCipher = cipherView.copy(
|
||||
identity = cipherView.identity?.copy(
|
||||
address1 = null,
|
||||
@@ -209,7 +214,8 @@ class CipherViewExtensionsTest {
|
||||
|
||||
assertEquals(
|
||||
VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = false).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = false, isPremiumUser = true)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = VaultItemState.ViewState.Content.ItemType.SecureNote,
|
||||
),
|
||||
viewState,
|
||||
@@ -226,7 +232,8 @@ class CipherViewExtensionsTest {
|
||||
)
|
||||
|
||||
val expectedState = VaultItemState.ViewState.Content(
|
||||
common = createCommonContent(isEmpty = true).copy(currentCipher = cipherView),
|
||||
common = createCommonContent(isEmpty = true, isPremiumUser = true)
|
||||
.copy(currentCipher = cipherView),
|
||||
type = VaultItemState.ViewState.Content.ItemType.SecureNote,
|
||||
)
|
||||
|
||||
|
||||
+27
-2
@@ -1,5 +1,6 @@
|
||||
package com.x8bit.bitwarden.ui.vault.feature.item.util
|
||||
|
||||
import com.bitwarden.core.AttachmentView
|
||||
import com.bitwarden.core.CipherRepromptType
|
||||
import com.bitwarden.core.CipherType
|
||||
import com.bitwarden.core.CipherView
|
||||
@@ -86,7 +87,17 @@ fun createCipherView(type: CipherType, isEmpty: Boolean): CipherView =
|
||||
edit = false,
|
||||
viewPassword = false,
|
||||
localData = null,
|
||||
attachments = null,
|
||||
attachments = listOf(
|
||||
AttachmentView(
|
||||
id = "attachment-id",
|
||||
sizeName = "11 MB",
|
||||
size = "11000000",
|
||||
url = "https://example.com",
|
||||
fileName = "test.mp4",
|
||||
key = "key",
|
||||
),
|
||||
)
|
||||
.takeUnless { isEmpty },
|
||||
fields = listOf(
|
||||
FieldView(
|
||||
name = "text",
|
||||
@@ -132,7 +143,10 @@ fun createCipherView(type: CipherType, isEmpty: Boolean): CipherView =
|
||||
revisionDate = Instant.ofEpochSecond(1_000L),
|
||||
)
|
||||
|
||||
fun createCommonContent(isEmpty: Boolean): VaultItemState.ViewState.Content.Common =
|
||||
fun createCommonContent(
|
||||
isEmpty: Boolean,
|
||||
isPremiumUser: Boolean,
|
||||
): VaultItemState.ViewState.Content.Common =
|
||||
if (isEmpty) {
|
||||
VaultItemState.ViewState.Content.Common(
|
||||
name = "mockName",
|
||||
@@ -140,6 +154,7 @@ fun createCommonContent(isEmpty: Boolean): VaultItemState.ViewState.Content.Comm
|
||||
notes = null,
|
||||
customFields = emptyList(),
|
||||
requiresReprompt = true,
|
||||
attachments = emptyList(),
|
||||
)
|
||||
} else {
|
||||
VaultItemState.ViewState.Content.Common(
|
||||
@@ -172,6 +187,16 @@ fun createCommonContent(isEmpty: Boolean): VaultItemState.ViewState.Content.Comm
|
||||
),
|
||||
),
|
||||
requiresReprompt = true,
|
||||
attachments = listOf(
|
||||
VaultItemState.ViewState.Content.Common.AttachmentItem(
|
||||
id = "attachment-id",
|
||||
displaySize = "11 MB",
|
||||
isLargeFile = true,
|
||||
isDownloadAllowed = isPremiumUser,
|
||||
url = "https://example.com",
|
||||
title = "test.mp4",
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user