Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore name already exists error for update custom list #6573

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.mullvad.mullvadvpn.compose.createEdgeToEdgeComposeExtension
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.EditCustomListNameUiState
import net.mullvad.mullvadvpn.compose.test.EDIT_CUSTOM_LIST_DIALOG_INPUT_TEST_TAG
import net.mullvad.mullvadvpn.lib.model.CustomListName
import net.mullvad.mullvadvpn.lib.model.NameAlreadyExists
import net.mullvad.mullvadvpn.lib.model.UnknownCustomListError
import net.mullvad.mullvadvpn.usecase.customlists.RenameError
Expand Down Expand Up @@ -46,7 +47,10 @@ class EditCustomListNameDialogTest {
fun givenCustomListExistsShouldShowCustomListExitsErrorText() =
composeExtension.use {
// Arrange
val state = EditCustomListNameUiState(error = RenameError(NameAlreadyExists("name")))
val state =
EditCustomListNameUiState(
error = RenameError(NameAlreadyExists(CustomListName.fromString("name")))
)
setContentWithTheme { EditCustomListNameDialog(state = state) }

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class CustomListsRepositoryTest {
// Arrange
val customListId = CustomListId("1")
val customListName = CustomListName.fromString("CUSTOM")
val expectedResult = NameAlreadyExists(customListName.value).left()
val expectedResult = NameAlreadyExists(customListName).left()
val mockSettings: Settings = mockk()
val mockCustomList =
CustomList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ class CustomListActionUseCaseTest {
val action = CustomListAction.Rename(id = customListId, name = name, newName = newName)
coEvery {
mockCustomListsRepository.updateCustomListName(id = customListId, name = newName)
} returns NameAlreadyExists(newName.value).left()
} returns NameAlreadyExists(newName).left()

val expectedError = RenameError(NameAlreadyExists(newName.value)).left()
val expectedError = RenameError(NameAlreadyExists(newName)).left()

// Act
val result = customListActionUseCase(action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ class EditCustomListNameDialogViewModelTest {
fun `when failing to rename a list should update ui state with error`() = runTest {
// Arrange
val customListId = CustomListId("id2")
val customListName = "list2"
val customListName = CustomListName.fromString("list2")
val expectedError = RenameError(NameAlreadyExists(customListName))
val viewModel = createViewModel(customListId, customListName)
val viewModel = createViewModel(customListId, customListName.value)
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
expectedError.left()

// Act, Assert
viewModel.uiState.test {
awaitItem() // Default state
viewModel.updateCustomListName(customListName)
viewModel.updateCustomListName(customListName.value)
assertEquals(expectedError, awaitItem().error)
}
}
Expand All @@ -74,16 +74,16 @@ class EditCustomListNameDialogViewModelTest {
runTest {
// Arrange
val customListId = CustomListId("id")
val customListName = "list"
val customListName = CustomListName.fromString("list")
val expectedError = RenameError(NameAlreadyExists(customListName))
val viewModel = createViewModel(customListId, customListName)
val viewModel = createViewModel(customListId, customListName.value)
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
expectedError.left()

// Act, Assert
viewModel.uiState.test {
awaitItem() // Default state
viewModel.updateCustomListName(customListName)
viewModel.updateCustomListName(customListName.value)
assertEquals(expectedError, awaitItem().error) // Showing error
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import net.mullvad.mullvadvpn.lib.model.GetDeviceListError
import net.mullvad.mullvadvpn.lib.model.GetDeviceStateError
import net.mullvad.mullvadvpn.lib.model.GetVersionInfoError
import net.mullvad.mullvadvpn.lib.model.LoginAccountError
import net.mullvad.mullvadvpn.lib.model.NameAlreadyExists
import net.mullvad.mullvadvpn.lib.model.NewAccessMethodSetting
import net.mullvad.mullvadvpn.lib.model.ObfuscationSettings
import net.mullvad.mullvadvpn.lib.model.Ownership as ModelOwnership
Expand Down Expand Up @@ -492,7 +493,12 @@ class ManagementService(

suspend fun updateCustomList(customList: ModelCustomList): Either<UpdateCustomListError, Unit> =
Either.catch { grpc.updateCustomList(customList.fromDomain()) }
.mapLeft(::UnknownCustomListError)
.mapLeftStatus {
when (it.status.code) {
Status.Code.ALREADY_EXISTS -> NameAlreadyExists(customList.name)
else -> UnknownCustomListError(it)
}
}
.mapEmpty()

suspend fun deleteCustomList(id: CustomListId): Either<DeleteCustomListError, Unit> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ sealed interface UpdateCustomListLocationsError {

sealed interface UpdateCustomListError

data class NameAlreadyExists(val name: String) : UpdateCustomListError, UpdateCustomListNameError
data class NameAlreadyExists(val name: CustomListName) :
UpdateCustomListError, UpdateCustomListNameError

data class UnknownCustomListError(val throwable: Throwable) :
UpdateCustomListError,
Expand Down
Loading