mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-1246, BIT-1250: Show correct permission-related errors when editing (#482)
This commit is contained in:
committed by
Álison Fernandes
parent
8476e55b5a
commit
1e996fcbbe
@@ -13,7 +13,7 @@ import retrofit2.Retrofit
|
||||
*/
|
||||
abstract class BaseServiceTest {
|
||||
|
||||
private val json = PlatformNetworkModule.providesJson()
|
||||
protected val json = PlatformNetworkModule.providesJson()
|
||||
|
||||
protected val server = MockWebServer().apply { start() }
|
||||
|
||||
|
||||
+40
-11
@@ -2,6 +2,7 @@ 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.CiphersApi
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.UpdateCipherResponseJson
|
||||
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
|
||||
@@ -15,6 +16,7 @@ class CiphersServiceTest : BaseServiceTest() {
|
||||
|
||||
private val ciphersService: CiphersService = CiphersServiceImpl(
|
||||
ciphersApi = ciphersApi,
|
||||
json = json,
|
||||
)
|
||||
|
||||
@Test
|
||||
@@ -30,17 +32,37 @@ class CiphersServiceTest : BaseServiceTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `updateCipher should return the correct response`() = runTest {
|
||||
server.enqueue(MockResponse().setBody(CREATE_UPDATE_CIPHER_SUCCESS_JSON))
|
||||
val result = ciphersService.updateCipher(
|
||||
cipherId = "cipher-id-1",
|
||||
body = createMockCipherJsonRequest(number = 1),
|
||||
)
|
||||
assertEquals(
|
||||
createMockCipher(number = 1),
|
||||
result.getOrThrow(),
|
||||
)
|
||||
}
|
||||
fun `updateCipher with success response should return a Success with the correct cipher`() =
|
||||
runTest {
|
||||
server.enqueue(MockResponse().setBody(CREATE_UPDATE_CIPHER_SUCCESS_JSON))
|
||||
val result = ciphersService.updateCipher(
|
||||
cipherId = "cipher-id-1",
|
||||
body = createMockCipherJsonRequest(number = 1),
|
||||
)
|
||||
assertEquals(
|
||||
UpdateCipherResponseJson.Success(
|
||||
cipher = createMockCipher(number = 1),
|
||||
),
|
||||
result.getOrThrow(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `updateCipher with an invalid response should return an Invalid with the correct data`() =
|
||||
runTest {
|
||||
server.enqueue(MockResponse().setResponseCode(400).setBody(UPDATE_CIPHER_INVALID_JSON))
|
||||
val result = ciphersService.updateCipher(
|
||||
cipherId = "cipher-id-1",
|
||||
body = createMockCipherJsonRequest(number = 1),
|
||||
)
|
||||
assertEquals(
|
||||
UpdateCipherResponseJson.Invalid(
|
||||
message = "You do not have permission to edit this.",
|
||||
validationErrors = null,
|
||||
),
|
||||
result.getOrThrow(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private const val CREATE_UPDATE_CIPHER_SUCCESS_JSON = """
|
||||
@@ -134,3 +156,10 @@ private const val CREATE_UPDATE_CIPHER_SUCCESS_JSON = """
|
||||
"key": "mockKey-1"
|
||||
}
|
||||
"""
|
||||
|
||||
private const val UPDATE_CIPHER_INVALID_JSON = """
|
||||
{
|
||||
"message": "You do not have permission to edit this.",
|
||||
"validationErrors": null
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.x8bit.bitwarden.data.platform.util.asFailure
|
||||
import com.x8bit.bitwarden.data.platform.util.asSuccess
|
||||
import com.x8bit.bitwarden.data.vault.datasource.disk.VaultDiskSource
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.UpdateCipherResponseJson
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCipher
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCipherJsonRequest
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockCollection
|
||||
@@ -1746,12 +1747,12 @@ class VaultRepositoryTest {
|
||||
cipherView = mockCipherView,
|
||||
)
|
||||
|
||||
assertEquals(UpdateCipherResult.Error, result)
|
||||
assertEquals(UpdateCipherResult.Error(errorMessage = null), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Suppress("MaxLineLength")
|
||||
fun `updateCipher with ciphersService updateCipher failure should return UpdateCipherResult failure`() =
|
||||
fun `updateCipher with ciphersService updateCipher failure should return UpdateCipherResult Error with a null message`() =
|
||||
runTest {
|
||||
val cipherId = "cipherId1234"
|
||||
val mockCipherView = createMockCipherView(number = 1)
|
||||
@@ -1770,12 +1771,12 @@ class VaultRepositoryTest {
|
||||
cipherView = mockCipherView,
|
||||
)
|
||||
|
||||
assertEquals(UpdateCipherResult.Error, result)
|
||||
assertEquals(UpdateCipherResult.Error(errorMessage = null), result)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Suppress("MaxLineLength")
|
||||
fun `updateCipher with ciphersService updateCipher success should return UpdateCipherResult success`() =
|
||||
fun `updateCipher with ciphersService updateCipher Invalid response should return UpdateCipherResult Error with a non-null message`() =
|
||||
runTest {
|
||||
val cipherId = "cipherId1234"
|
||||
val mockCipherView = createMockCipherView(number = 1)
|
||||
@@ -1787,7 +1788,43 @@ class VaultRepositoryTest {
|
||||
cipherId = cipherId,
|
||||
body = createMockCipherJsonRequest(number = 1),
|
||||
)
|
||||
} returns createMockCipher(number = 1).asSuccess()
|
||||
} returns UpdateCipherResponseJson
|
||||
.Invalid(
|
||||
message = "You do not have permission to edit this.",
|
||||
validationErrors = null,
|
||||
)
|
||||
.asSuccess()
|
||||
|
||||
val result = vaultRepository.updateCipher(
|
||||
cipherId = cipherId,
|
||||
cipherView = mockCipherView,
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
UpdateCipherResult.Error(
|
||||
errorMessage = "You do not have permission to edit this.",
|
||||
),
|
||||
result,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Suppress("MaxLineLength")
|
||||
fun `updateCipher with ciphersService updateCipher Success response should return UpdateCipherResult success`() =
|
||||
runTest {
|
||||
val cipherId = "cipherId1234"
|
||||
val mockCipherView = createMockCipherView(number = 1)
|
||||
coEvery {
|
||||
vaultSdkSource.encryptCipher(cipherView = mockCipherView)
|
||||
} returns createMockSdkCipher(number = 1).asSuccess()
|
||||
coEvery {
|
||||
ciphersService.updateCipher(
|
||||
cipherId = cipherId,
|
||||
body = createMockCipherJsonRequest(number = 1),
|
||||
)
|
||||
} returns UpdateCipherResponseJson
|
||||
.Success(cipher = createMockCipher(number = 1))
|
||||
.asSuccess()
|
||||
coEvery {
|
||||
syncService.sync()
|
||||
} returns Result.success(createMockSyncResponse(1))
|
||||
|
||||
+75
-26
@@ -266,39 +266,88 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `in edit mode, SaveClick createCipher error should emit ShowToast`() = runTest {
|
||||
val cipherView = mockk<CipherView>()
|
||||
val vaultAddEditType = VaultAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID)
|
||||
val stateWithName = createVaultAddItemState(
|
||||
vaultAddEditType = vaultAddEditType,
|
||||
commonContentViewState = createCommonContentViewState(
|
||||
name = "mockName",
|
||||
),
|
||||
)
|
||||
|
||||
every { cipherView.toViewState() } returns stateWithName.viewState
|
||||
coEvery {
|
||||
vaultRepository.updateCipher(DEFAULT_EDIT_ITEM_ID, any())
|
||||
} returns UpdateCipherResult.Error
|
||||
mutableVaultItemFlow.value = DataState.Loaded(cipherView)
|
||||
|
||||
val viewModel = createAddVaultItemViewModel(
|
||||
createSavedStateHandleWithState(
|
||||
state = stateWithName,
|
||||
fun `in edit mode, SaveClick updateCipher error with a null message should show an error dialog with a generic message`() =
|
||||
runTest {
|
||||
val cipherView = mockk<CipherView>()
|
||||
val vaultAddEditType = VaultAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID)
|
||||
val stateWithName = createVaultAddItemState(
|
||||
vaultAddEditType = vaultAddEditType,
|
||||
),
|
||||
)
|
||||
commonContentViewState = createCommonContentViewState(
|
||||
name = "mockName",
|
||||
),
|
||||
)
|
||||
|
||||
every { cipherView.toViewState() } returns stateWithName.viewState
|
||||
coEvery {
|
||||
vaultRepository.updateCipher(DEFAULT_EDIT_ITEM_ID, any())
|
||||
} returns UpdateCipherResult.Error(errorMessage = null)
|
||||
mutableVaultItemFlow.value = DataState.Loaded(cipherView)
|
||||
|
||||
val viewModel = createAddVaultItemViewModel(
|
||||
createSavedStateHandleWithState(
|
||||
state = stateWithName,
|
||||
vaultAddEditType = vaultAddEditType,
|
||||
),
|
||||
)
|
||||
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
|
||||
assertEquals(VaultAddEditEvent.ShowToast("Save Item Failure".asText()), awaitItem())
|
||||
|
||||
assertEquals(
|
||||
stateWithName.copy(
|
||||
dialog = VaultAddEditState.DialogState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
coVerify(exactly = 1) {
|
||||
vaultRepository.updateCipher(DEFAULT_EDIT_ITEM_ID, any())
|
||||
}
|
||||
}
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
vaultRepository.updateCipher(DEFAULT_EDIT_ITEM_ID, any())
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `in edit mode, SaveClick updateCipher error with a non-null message should show an error dialog with that message`() =
|
||||
runTest {
|
||||
val cipherView = mockk<CipherView>()
|
||||
val vaultAddEditType = VaultAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID)
|
||||
val stateWithName = createVaultAddItemState(
|
||||
vaultAddEditType = vaultAddEditType,
|
||||
commonContentViewState = createCommonContentViewState(
|
||||
name = "mockName",
|
||||
),
|
||||
)
|
||||
val errorMessage = "You do not have permission to edit this."
|
||||
|
||||
every { cipherView.toViewState() } returns stateWithName.viewState
|
||||
coEvery {
|
||||
vaultRepository.updateCipher(DEFAULT_EDIT_ITEM_ID, any())
|
||||
} returns UpdateCipherResult.Error(errorMessage = errorMessage)
|
||||
mutableVaultItemFlow.value = DataState.Loaded(cipherView)
|
||||
|
||||
val viewModel = createAddVaultItemViewModel(
|
||||
createSavedStateHandleWithState(
|
||||
state = stateWithName,
|
||||
vaultAddEditType = vaultAddEditType,
|
||||
),
|
||||
)
|
||||
|
||||
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
|
||||
|
||||
assertEquals(
|
||||
stateWithName.copy(
|
||||
dialog = VaultAddEditState.DialogState.Error(
|
||||
message = errorMessage.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
coVerify(exactly = 1) {
|
||||
vaultRepository.updateCipher(DEFAULT_EDIT_ITEM_ID, any())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Saving item with an empty name field will cause a dialog to show up`() = runTest {
|
||||
|
||||
Reference in New Issue
Block a user