[PM-10930] Fix password generator policies (#3853)

This commit is contained in:
aj-rosado 2024-09-10 16:31:58 +02:00 committed by GitHub
parent 95240a7ce3
commit 4e69ed57e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 4 deletions

View file

@ -7,6 +7,11 @@ import com.x8bit.bitwarden.data.vault.datasource.network.model.PolicyTypeJson
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
private val JSON = Json {
ignoreUnknownKeys = true
explicitNulls = false
}
/** /**
* Maps the given [SyncResponseJson.Profile.Organization] to an [Organization]. * Maps the given [SyncResponseJson.Profile.Organization] to an [Organization].
*/ */
@ -31,21 +36,22 @@ fun List<SyncResponseJson.Profile.Organization>.toOrganizations(): List<Organiza
*/ */
val SyncResponseJson.Policy.policyInformation: PolicyInformation? val SyncResponseJson.Policy.policyInformation: PolicyInformation?
get() = data?.toString()?.let { get() = data?.toString()?.let {
when (type) { when (type) {
PolicyTypeJson.MASTER_PASSWORD -> { PolicyTypeJson.MASTER_PASSWORD -> {
Json.decodeFromStringOrNull<PolicyInformation.MasterPassword>(it) JSON.decodeFromStringOrNull<PolicyInformation.MasterPassword>(it)
} }
PolicyTypeJson.PASSWORD_GENERATOR -> { PolicyTypeJson.PASSWORD_GENERATOR -> {
Json.decodeFromStringOrNull<PolicyInformation.PasswordGenerator>(it) JSON.decodeFromStringOrNull<PolicyInformation.PasswordGenerator>(it)
} }
PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT -> { PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT -> {
Json.decodeFromStringOrNull<PolicyInformation.VaultTimeout>(it) JSON.decodeFromStringOrNull<PolicyInformation.VaultTimeout>(it)
} }
PolicyTypeJson.SEND_OPTIONS -> { PolicyTypeJson.SEND_OPTIONS -> {
Json.decodeFromStringOrNull<PolicyInformation.SendOptions>(it) JSON.decodeFromStringOrNull<PolicyInformation.SendOptions>(it)
} }
else -> null else -> null

View file

@ -95,6 +95,8 @@ class PolicyManagerImpl(
): Boolean = ): Boolean =
if (policyType == PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT) { if (policyType == PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT) {
organization.type == OrganizationType.OWNER organization.type == OrganizationType.OWNER
} else if (policyType == PolicyTypeJson.PASSWORD_GENERATOR) {
false
} else { } else {
(organization.type == OrganizationType.OWNER || (organization.type == OrganizationType.OWNER ||
organization.type == OrganizationType.ADMIN) || organization.type == OrganizationType.ADMIN) ||

View file

@ -139,6 +139,34 @@ class PolicyManagerTest {
assertTrue(policyManager.getActivePolicies(type = PolicyTypeJson.MASTER_PASSWORD).isEmpty()) assertTrue(policyManager.getActivePolicies(type = PolicyTypeJson.MASTER_PASSWORD).isEmpty())
} }
@Test
fun `getActivePolicies returns active and applied PasswordGenerator policies`() {
val userState: UserStateJson = mockk {
every { activeUserId } returns USER_ID
}
every { authDiskSource.userState } returns userState
every {
authDiskSource.getOrganizations(USER_ID)
} returns listOf(
createMockOrganization(
number = 3,
isEnabled = true,
shouldUsePolicies = true,
),
)
every {
authDiskSource.getPolicies(USER_ID)
} returns listOf(
createMockPolicy(
organizationId = "mockId-3",
isEnabled = true,
type = PolicyTypeJson.PASSWORD_GENERATOR,
),
)
assertTrue(policyManager.getActivePolicies(type = PolicyTypeJson.PASSWORD_GENERATOR).any())
}
} }
private const val USER_ID = "userId" private const val USER_ID = "userId"