PM-21696: Make sure environment is up-to-date (#5209)

This commit is contained in:
David Perez
2025-05-15 21:44:44 +00:00
committed by GitHub
parent 7422efd07a
commit cb0b135429
2 changed files with 20 additions and 2 deletions
@@ -11,6 +11,7 @@ import com.bitwarden.crypto.Kdf
import com.bitwarden.data.datasource.disk.ConfigDiskSource
import com.bitwarden.data.manager.DispatcherManager
import com.bitwarden.data.repository.util.toEnvironmentUrls
import com.bitwarden.data.repository.util.toEnvironmentUrlsOrDefault
import com.bitwarden.network.model.DeleteAccountResponseJson
import com.bitwarden.network.model.GetTokenResponseJson
import com.bitwarden.network.model.IdentityTokenAuthModel
@@ -822,14 +823,25 @@ class AuthRepositoryImpl(
override fun switchAccount(userId: String): SwitchAccountResult {
val currentUserState = authDiskSource.userState ?: return SwitchAccountResult.NoChange
val previousActiveUserId = currentUserState.activeUserId
val updateEnvironment: () -> Unit = {
environmentRepository.environment = currentUserState
.activeAccount
.settings
.environmentUrlData
.toEnvironmentUrlsOrDefault()
}
if (userId == previousActiveUserId) {
// We need to make sure that the environment is set back to the correct spot.
updateEnvironment()
// No switching to do but clear any pending account additions
hasPendingAccountAddition = false
return SwitchAccountResult.NoChange
}
if (userId !in currentUserState.accounts.keys) {
// We need to make sure that the environment is set back to the correct spot.
updateEnvironment()
// The requested user is not currently stored
return SwitchAccountResult.NoChange
}
@@ -5920,6 +5920,7 @@ class AuthRepositoryTest {
firstTimeState = FIRST_TIME_STATE,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeEnvironmentRepository.environment = Environment.Eu
assertEquals(
originalUserState,
repository.userStateFlow.value,
@@ -5936,6 +5937,7 @@ class AuthRepositoryTest {
repository.userStateFlow.value,
)
assertFalse(repository.hasPendingAccountAddition)
assertEquals(Environment.Us, fakeEnvironmentRepository.environment)
}
@Suppress("MaxLineLength")
@@ -5955,6 +5957,7 @@ class AuthRepositoryTest {
firstTimeState = FIRST_TIME_STATE,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeEnvironmentRepository.environment = Environment.Eu
assertEquals(
originalUserState,
repository.userStateFlow.value,
@@ -5969,6 +5972,7 @@ class AuthRepositoryTest {
originalUserState,
repository.userStateFlow.value,
)
assertEquals(Environment.Us, fakeEnvironmentRepository.environment)
}
@Suppress("MaxLineLength")
@@ -5988,6 +5992,7 @@ class AuthRepositoryTest {
firstTimeState = FIRST_TIME_STATE,
)
fakeAuthDiskSource.userState = MULTI_USER_STATE
fakeEnvironmentRepository.environment = Environment.Eu
assertEquals(
originalUserState,
repository.userStateFlow.value,
@@ -6004,6 +6009,7 @@ class AuthRepositoryTest {
repository.userStateFlow.value,
)
assertFalse(repository.hasPendingAccountAddition)
assertEquals(Environment.Eu, fakeEnvironmentRepository.environment)
}
@Test
@@ -7078,7 +7084,7 @@ class AuthRepositoryTest {
private val ACCOUNT_1 = AccountJson(
profile = PROFILE_1,
settings = AccountJson.Settings(
environmentUrlData = null,
environmentUrlData = EnvironmentUrlDataJson.DEFAULT_US,
),
)
private val ACCOUNT_2 = AccountJson(
@@ -7101,7 +7107,7 @@ class AuthRepositoryTest {
creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"),
),
settings = AccountJson.Settings(
environmentUrlData = null,
environmentUrlData = EnvironmentUrlDataJson.DEFAULT_EU,
),
)
private val SINGLE_USER_STATE_1 = UserStateJson(