-
Notifications
You must be signed in to change notification settings - Fork 11
fix(profiles): gate picker management affordances on server authorization #220
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
Open
Daicaa
wants to merge
1
commit into
Silo-Server:main
Choose a base branch
from
Daicaa:fix/picker-profile-management-gating
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...n/org/siloserver/silo/android/ui/screens/profiles/ProfileSelectionManagementGatingTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| package org.siloserver.silo.android.ui.screens.profiles | ||
|
|
||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.engine.mock.MockEngine | ||
| import io.ktor.client.engine.mock.respond | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
| import kotlinx.coroutines.test.advanceUntilIdle | ||
| import kotlinx.coroutines.test.resetMain | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlinx.coroutines.test.setMain | ||
| import org.siloserver.silo.model.auth.User | ||
| import org.siloserver.silo.model.profile.Profile | ||
| import org.siloserver.silo.network.ApiResult | ||
| import org.siloserver.silo.network.TokenManagerImpl | ||
| import org.siloserver.silo.network.api.ProfileApi | ||
| import org.siloserver.silo.repository.ProfileRepository | ||
| import kotlin.test.AfterTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertNull | ||
| import kotlin.test.assertTrue | ||
|
|
||
| /** | ||
| * Picker-time management gating. The server authorizes profile management | ||
| * for admin accounts OR when the acting profile is the household primary — | ||
| * and the phone keeps the acting profile across "Switch Profile", so both | ||
| * arms matter here. The picker must not offer create/edit/delete to anyone | ||
| * else — except the add tile on an empty grid, which the server exempts | ||
| * (first-profile bootstrap). | ||
| */ | ||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| class ProfileSelectionManagementGatingTest { | ||
| @AfterTest | ||
| fun tearDown() { | ||
| Dispatchers.resetMain() | ||
| } | ||
|
|
||
| private fun user(role: String) = User( | ||
| id = 1, | ||
| username = "someone", | ||
| email = "someone@example.com", | ||
| role = role, | ||
| ) | ||
|
|
||
| @Test | ||
| fun `admin account gets management affordances`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository(listOf(Profile(id = "p1", name = "One"))), | ||
| currentUserProvider = { user("admin") }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertTrue(viewModel.uiState.value.canManageProfiles) | ||
| assertTrue(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| @Test | ||
| fun `non-admin account with no acting profile gets no management affordances`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository(listOf(Profile(id = "p1", name = "One"))), | ||
| currentUserProvider = { user("user") }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertFalse(viewModel.uiState.value.canManageProfiles) | ||
| assertFalse(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| /** | ||
| * The regression the first cut of this gating introduced: phone keeps the | ||
| * acting profile across "Switch Profile", and the picker is the ONLY | ||
| * route to create/edit — a non-admin household owner acting as the | ||
| * primary is authorized by the server and must keep the affordances. | ||
| */ | ||
| @Test | ||
| fun `non-admin acting as the primary profile keeps management affordances`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository( | ||
| profiles = listOf( | ||
| Profile(id = "owner", name = "Owner", isPrimary = true), | ||
| Profile(id = "kid", name = "Kid"), | ||
| ), | ||
| activeProfileId = "owner", | ||
| ), | ||
| currentUserProvider = { user("user") }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertTrue(viewModel.uiState.value.canManageProfiles) | ||
| assertTrue(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| @Test | ||
| fun `non-admin acting as a non-primary profile gets no management affordances`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository( | ||
| profiles = listOf( | ||
| Profile(id = "owner", name = "Owner", isPrimary = true), | ||
| Profile(id = "kid", name = "Kid"), | ||
| ), | ||
| activeProfileId = "kid", | ||
| ), | ||
| currentUserProvider = { user("user") }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertFalse(viewModel.uiState.value.canManageProfiles) | ||
| assertFalse(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| @Test | ||
| fun `unresolved user fails closed`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository(listOf(Profile(id = "p1", name = "One"))), | ||
| currentUserProvider = { null }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertFalse(viewModel.uiState.value.canManageProfiles) | ||
| assertFalse(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| @Test | ||
| fun `empty grid keeps the add tile for first-profile bootstrap`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository(emptyList()), | ||
| currentUserProvider = { user("user") }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertFalse(viewModel.uiState.value.canManageProfiles) | ||
| assertTrue(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| /** Bootstrap must survive a dead `/me`: fresh account, user unresolved. */ | ||
| @Test | ||
| fun `empty grid with unresolved user still shows the add tile`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository(emptyList()), | ||
| currentUserProvider = { null }, | ||
| ) | ||
| advanceUntilIdle() | ||
|
|
||
| assertFalse(viewModel.uiState.value.canManageProfiles) | ||
| assertTrue(viewModel.uiState.value.canAddProfile) | ||
| } | ||
|
|
||
| @Test | ||
| fun `revoking the grant on reload leaves manage mode and closes the delete dialog`() = runTest { | ||
| Dispatchers.setMain(UnconfinedTestDispatcher(testScheduler)) | ||
| var currentUser: User? = user("admin") | ||
| val profile = Profile(id = "p1", name = "One") | ||
| val viewModel = ProfileSelectionViewModel( | ||
| profileRepository = FixedProfileRepository(listOf(profile)), | ||
| currentUserProvider = { currentUser }, | ||
| ) | ||
| advanceUntilIdle() | ||
| viewModel.toggleManageMode() | ||
| viewModel.requestDeleteProfile(profile) | ||
| assertTrue(viewModel.uiState.value.isManageMode) | ||
| assertNotNull(viewModel.uiState.value.deleteDialogProfile) | ||
|
|
||
| currentUser = null | ||
| viewModel.loadProfiles() | ||
| advanceUntilIdle() | ||
|
|
||
| // Otherwise the "Done" toggle disappears while its mode stays on, | ||
| // and the pending delete confirmation stays open and actionable. | ||
| assertFalse(viewModel.uiState.value.canManageProfiles) | ||
| assertFalse(viewModel.uiState.value.isManageMode) | ||
| assertNull(viewModel.uiState.value.deleteDialogProfile) | ||
| } | ||
| } | ||
|
|
||
| private class FixedProfileRepository( | ||
| private val profiles: List<Profile>, | ||
| private val activeProfileId: String? = null, | ||
| ) : ProfileRepository( | ||
| profileApi = ProfileApi(HttpClient(MockEngine { respond("{}") })), | ||
| tokenManager = TokenManagerImpl(), | ||
| ) { | ||
| override suspend fun listProfiles(): ApiResult<List<Profile>> = ApiResult.Success(profiles) | ||
|
|
||
| override suspend fun getActiveProfileId(): String? = activeProfileId | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.