mirror of
https://github.com/bitwarden/android.git
synced 2026-05-31 01:22:39 -05:00
Add Organizations to UserState.Account (#432)
This commit is contained in:
@@ -32,8 +32,10 @@ import com.x8bit.bitwarden.data.auth.repository.model.LoginResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.PasswordStrengthResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.RegisterResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.SwitchAccountResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserOrganizations
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.CaptchaCallbackTokenResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.toOrganizations
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.toUserState
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.toUserStateJson
|
||||
@@ -143,7 +145,7 @@ class AuthRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `userStateFlow should update with changes to the UserStateJson and VaultState data`() {
|
||||
fun `userStateFlow should update according to changes in its underyling data sources`() {
|
||||
fakeAuthDiskSource.userState = null
|
||||
assertEquals(
|
||||
null,
|
||||
@@ -155,6 +157,7 @@ class AuthRepositoryTest {
|
||||
assertEquals(
|
||||
SINGLE_USER_STATE_1.toUserState(
|
||||
vaultState = VAULT_STATE,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
),
|
||||
repository.userStateFlow.value,
|
||||
@@ -164,6 +167,7 @@ class AuthRepositoryTest {
|
||||
assertEquals(
|
||||
MULTI_USER_STATE.toUserState(
|
||||
vaultState = VAULT_STATE,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
),
|
||||
repository.userStateFlow.value,
|
||||
@@ -174,6 +178,20 @@ class AuthRepositoryTest {
|
||||
assertEquals(
|
||||
MULTI_USER_STATE.toUserState(
|
||||
vaultState = emptyVaultState,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
),
|
||||
repository.userStateFlow.value,
|
||||
)
|
||||
|
||||
fakeAuthDiskSource.storeOrganizations(
|
||||
userId = USER_ID_1,
|
||||
organizations = ORGANIZATIONS,
|
||||
)
|
||||
assertEquals(
|
||||
MULTI_USER_STATE.toUserState(
|
||||
vaultState = emptyVaultState,
|
||||
userOrganizationsList = USER_ORGANIZATIONS,
|
||||
specialCircumstance = null,
|
||||
),
|
||||
repository.userStateFlow.value,
|
||||
@@ -201,6 +219,7 @@ class AuthRepositoryTest {
|
||||
assertNull(repository.specialCircumstance)
|
||||
val initialUserState = SINGLE_USER_STATE_1.toUserState(
|
||||
vaultState = VAULT_STATE,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
)
|
||||
mutableVaultStateFlow.value = VAULT_STATE
|
||||
@@ -1173,6 +1192,7 @@ class AuthRepositoryTest {
|
||||
val originalUserId = USER_ID_1
|
||||
val originalUserState = SINGLE_USER_STATE_1.toUserState(
|
||||
vaultState = VAULT_STATE,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
)
|
||||
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
|
||||
@@ -1203,6 +1223,7 @@ class AuthRepositoryTest {
|
||||
val invalidId = "invalidId"
|
||||
val originalUserState = SINGLE_USER_STATE_1.toUserState(
|
||||
vaultState = VAULT_STATE,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
)
|
||||
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
|
||||
@@ -1231,6 +1252,7 @@ class AuthRepositoryTest {
|
||||
val updatedUserId = USER_ID_2
|
||||
val originalUserState = MULTI_USER_STATE.toUserState(
|
||||
vaultState = VAULT_STATE,
|
||||
userOrganizationsList = emptyList(),
|
||||
specialCircumstance = null,
|
||||
)
|
||||
fakeAuthDiskSource.userState = MULTI_USER_STATE
|
||||
@@ -1494,6 +1516,12 @@ class AuthRepositoryTest {
|
||||
USER_ID_3 to ACCOUNT_3,
|
||||
),
|
||||
)
|
||||
private val USER_ORGANIZATIONS = listOf(
|
||||
UserOrganizations(
|
||||
userId = USER_ID_1,
|
||||
organizations = ORGANIZATIONS.toOrganizations(),
|
||||
),
|
||||
)
|
||||
private val VAULT_STATE = VaultState(
|
||||
unlockedVaultUserIds = setOf(USER_ID_1),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.x8bit.bitwarden.data.auth.repository.util
|
||||
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.util.FakeAuthDiskSource
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.Organization
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserOrganizations
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockOrganization
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class AuthDiskSourceExtensionsTest {
|
||||
private val authDiskSource: AuthDiskSource = FakeAuthDiskSource()
|
||||
|
||||
@Test
|
||||
fun `userOrganizationsList should return data for all available users`() {
|
||||
val mockAccounts = mapOf(
|
||||
"userId1" to mockk<AccountJson>(),
|
||||
"userId2" to mockk<AccountJson>(),
|
||||
"userId3" to mockk<AccountJson>(),
|
||||
)
|
||||
val userStateJson = mockk<UserStateJson>() {
|
||||
every { accounts } returns mockAccounts
|
||||
}
|
||||
authDiskSource.apply {
|
||||
userState = userStateJson
|
||||
storeOrganizations(
|
||||
userId = "userId1",
|
||||
organizations = listOf(createMockOrganization(number = 1)),
|
||||
)
|
||||
storeOrganizations(
|
||||
userId = "userId2",
|
||||
organizations = listOf(createMockOrganization(number = 2)),
|
||||
)
|
||||
storeOrganizations(
|
||||
userId = "userId3",
|
||||
organizations = listOf(createMockOrganization(number = 3)),
|
||||
)
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
listOf(
|
||||
UserOrganizations(
|
||||
userId = "userId1",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "mockId-1",
|
||||
name = "mockName-1",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserOrganizations(
|
||||
userId = "userId2",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "mockId-2",
|
||||
name = "mockName-2",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserOrganizations(
|
||||
userId = "userId3",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "mockId-3",
|
||||
name = "mockName-3",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
authDiskSource.userOrganizationsList,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `userOrganizationsListFlow should emit whenever there are changes to organization data`() =
|
||||
runTest {
|
||||
val mockAccounts = mapOf(
|
||||
"userId1" to mockk<AccountJson>(),
|
||||
"userId2" to mockk<AccountJson>(),
|
||||
"userId3" to mockk<AccountJson>(),
|
||||
)
|
||||
val userStateJson = mockk<UserStateJson>() {
|
||||
every { accounts } returns mockAccounts
|
||||
}
|
||||
authDiskSource.apply {
|
||||
userState = userStateJson
|
||||
storeOrganizations(
|
||||
userId = "userId1",
|
||||
organizations = listOf(createMockOrganization(number = 1)),
|
||||
)
|
||||
}
|
||||
|
||||
authDiskSource.userOrganizationsListFlow.test {
|
||||
assertEquals(
|
||||
listOf(
|
||||
UserOrganizations(
|
||||
userId = "userId1",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "mockId-1",
|
||||
name = "mockName-1",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserOrganizations(
|
||||
userId = "userId2",
|
||||
organizations = emptyList(),
|
||||
),
|
||||
UserOrganizations(
|
||||
userId = "userId3",
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
|
||||
authDiskSource.storeOrganizations(
|
||||
userId = "userId2",
|
||||
organizations = listOf(createMockOrganization(number = 2)),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
listOf(
|
||||
UserOrganizations(
|
||||
userId = "userId1",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "mockId-1",
|
||||
name = "mockName-1",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserOrganizations(
|
||||
userId = "userId2",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "mockId-2",
|
||||
name = "mockName-2",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserOrganizations(
|
||||
userId = "userId3",
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.x8bit.bitwarden.data.auth.repository.util
|
||||
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.Organization
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockOrganization
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class SyncResponseJsonExtensionsTest {
|
||||
@Test
|
||||
fun `toOrganization should output the correct organization`() {
|
||||
assertEquals(
|
||||
Organization(
|
||||
id = "mockId-1",
|
||||
name = "mockName-1",
|
||||
),
|
||||
createMockOrganization(number = 1).toOrganization(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toOrganizations should output the correct list of organizations`() {
|
||||
assertEquals(
|
||||
listOf(
|
||||
Organization(
|
||||
id = "mockId-1",
|
||||
name = "mockName-1",
|
||||
),
|
||||
Organization(
|
||||
id = "mockId-2",
|
||||
name = "mockName-2",
|
||||
),
|
||||
),
|
||||
listOf(
|
||||
createMockOrganization(number = 1),
|
||||
createMockOrganization(number = 2),
|
||||
)
|
||||
.toOrganizations(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.KdfTypeJson
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.Organization
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserOrganizations
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultState
|
||||
@@ -101,6 +103,12 @@ class UserStateJsonExtensionsTest {
|
||||
environment = Environment.Eu,
|
||||
isPremium = false,
|
||||
isVaultUnlocked = true,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -126,6 +134,17 @@ class UserStateJsonExtensionsTest {
|
||||
vaultState = VaultState(
|
||||
unlockedVaultUserIds = setOf("activeUserId"),
|
||||
),
|
||||
userOrganizationsList = listOf(
|
||||
UserOrganizations(
|
||||
userId = "activeUserId",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
specialCircumstance = null,
|
||||
),
|
||||
)
|
||||
@@ -146,6 +165,12 @@ class UserStateJsonExtensionsTest {
|
||||
environment = Environment.Eu,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = false,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
specialCircumstance = UserState.SpecialCircumstance.PendingAccountAddition,
|
||||
@@ -172,6 +197,17 @@ class UserStateJsonExtensionsTest {
|
||||
vaultState = VaultState(
|
||||
unlockedVaultUserIds = emptySet(),
|
||||
),
|
||||
userOrganizationsList = listOf(
|
||||
UserOrganizations(
|
||||
userId = "activeUserId",
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
specialCircumstance = UserState.SpecialCircumstance.PendingAccountAddition,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -70,6 +70,7 @@ class LandingViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -198,6 +199,7 @@ class LandingViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
)
|
||||
val userState = UserState(
|
||||
activeUserId = "activeUserId",
|
||||
|
||||
@@ -123,6 +123,7 @@ class LoginViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -106,6 +106,7 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -341,6 +342,7 @@ private val DEFAULT_USER_STATE = UserState(
|
||||
avatarColorHex = "#aa00aa",
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -37,6 +37,7 @@ class RootNavViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -59,6 +60,7 @@ class RootNavViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = false,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -598,6 +598,7 @@ private val DEFAULT_USER_STATE: UserState = UserState(
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -126,6 +126,7 @@ class VaultViewModelTest : BaseViewModelTest() {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -773,6 +774,7 @@ private val DEFAULT_USER_STATE = UserState(
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
UserState.Account(
|
||||
userId = "lockedUserId",
|
||||
@@ -782,6 +784,7 @@ private val DEFAULT_USER_STATE = UserState(
|
||||
environment = Environment.Us,
|
||||
isPremium = false,
|
||||
isVaultUnlocked = false,
|
||||
organizations = emptyList(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.x8bit.bitwarden.ui.vault.feature.vault.util
|
||||
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.Organization
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.ui.platform.components.model.AccountSummary
|
||||
@@ -51,6 +52,12 @@ class UserStateExtensionsTest {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserState.Account(
|
||||
userId = "lockedUserId",
|
||||
@@ -60,6 +67,12 @@ class UserStateExtensionsTest {
|
||||
environment = Environment.Eu,
|
||||
isPremium = false,
|
||||
isVaultUnlocked = false,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
UserState.Account(
|
||||
userId = "unlockedUserId",
|
||||
@@ -73,6 +86,12 @@ class UserStateExtensionsTest {
|
||||
),
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -100,6 +119,12 @@ class UserStateExtensionsTest {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
)
|
||||
.toAccountSummary(isActive = true),
|
||||
)
|
||||
@@ -125,6 +150,12 @@ class UserStateExtensionsTest {
|
||||
environment = Environment.Us,
|
||||
isPremium = false,
|
||||
isVaultUnlocked = false,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
)
|
||||
.toAccountSummary(isActive = false),
|
||||
)
|
||||
@@ -154,6 +185,12 @@ class UserStateExtensionsTest {
|
||||
environment = Environment.Us,
|
||||
isPremium = true,
|
||||
isVaultUnlocked = true,
|
||||
organizations = listOf(
|
||||
Organization(
|
||||
id = "organizationId",
|
||||
name = "organizationName",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user