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

Fix #1810: Renaming the admin profile creates new one #2051

Merged
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 @@ -95,6 +95,9 @@ class ProfileManagementController @Inject constructor(
/** Indicates that the given profileId is not associated with an admin. */
class ProfileNotAdminException(msg: String) : Exception(msg)

/** Indicates that the Profile already has admin. */
class ProfileAlreadyHasAdminException(msg: String) : Exception(msg)

/** Indicates that the there is not device settings currently. */
class DeviceSettingsNotFoundException(msg: String) : Exception(msg)

Expand All @@ -112,7 +115,8 @@ class ProfileManagementController @Inject constructor(
FAILED_TO_GENERATE_GRAVATAR,
FAILED_TO_DELETE_DIR,
PROFILE_NOT_FOUND,
PROFILE_NOT_ADMIN
PROFILE_NOT_ADMIN,
PROFILE_ALREADY_HAS_ADMIN
}

// TODO(#272): Remove init block when storeDataAsync is fixed
Expand Down Expand Up @@ -199,6 +203,12 @@ class ProfileManagementController @Inject constructor(
if (!isNameUnique(name, it)) {
return@storeDataWithCustomChannelAsync Pair(it, ProfileActionStatus.PROFILE_NAME_NOT_UNIQUE)
}
if (isAdmin && alreadyHasAdmin(it)) {
return@storeDataWithCustomChannelAsync Pair(
it,
ProfileActionStatus.PROFILE_ALREADY_HAS_ADMIN
)
}

val nextProfileId = it.nextProfileId
val profileDir = directoryManagementUtil.getOrCreateDir(nextProfileId.toString())
Expand Down Expand Up @@ -662,6 +672,11 @@ class ProfileManagementController @Inject constructor(
"ProfileId ${profileId?.internalId} does not match an existing admin"
)
)
ProfileActionStatus.PROFILE_ALREADY_HAS_ADMIN -> AsyncResult.failed(
ProfileAlreadyHasAdminException(
"Profile cannot be an admin"
)
)
}
}

Expand All @@ -675,6 +690,15 @@ class ProfileManagementController @Inject constructor(
return true
}

private fun alreadyHasAdmin(profileDatabase: ProfileDatabase): Boolean {
profileDatabase.profilesMap.values.forEach {
if (it.isAdmin) {
return true
}
}
return false
}

private fun saveImageToInternalStorage(avatarImagePath: Uri, profileDir: File): String? {
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, avatarImagePath)
val fileName = avatarImagePath.pathSegments.last()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,33 @@ class ProfileManagementControllerTest {
assertThat(wasProfileEverAdded).isTrue()
}

@Test
fun testAddAdminProfile_addAnotherAdminProfile_checkSecondAdminProfileWasNotAdded() {
profileManagementController.addProfile(
name = "Rohit",
pin = "12345",
avatarImagePath = null,
allowDownloadAccess = true,
colorRgb = -10710042,
isAdmin = true
).toLiveData().observeForever(mockUpdateResultObserver)
testCoroutineDispatchers.runCurrent()

profileManagementController.addProfile(
name = "Ben",
pin = "12345",
avatarImagePath = null,
allowDownloadAccess = true,
colorRgb = -10710042,
isAdmin = true
).toLiveData().observeForever(mockUpdateResultObserver)
testCoroutineDispatchers.runCurrent()

verifyUpdateFailed()
assertThat(updateResultCaptor.value.getErrorOrNull()).hasMessageThat()
.contains("Profile cannot be an admin")
}

@Test
fun testDeviceSettings_addAdminProfile_getDefaultDeviceSettings_isSuccessful() {
profileManagementController.addProfile(
Expand Down