From 4e69ed57e8aaadcefed54063e0dcd7f924f85548 Mon Sep 17 00:00:00 2001 From: aj-rosado <109146700+aj-rosado@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:31:58 +0200 Subject: [PATCH] [PM-10930] Fix password generator policies (#3853) --- .../util/SyncResponseJsonExtensions.kt | 14 +++++++--- .../platform/manager/PolicyManagerImpl.kt | 2 ++ .../platform/manager/PolicyManagerTest.kt | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/SyncResponseJsonExtensions.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/SyncResponseJsonExtensions.kt index d22d5804b..c124a1363 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/SyncResponseJsonExtensions.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/SyncResponseJsonExtensions.kt @@ -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 kotlinx.serialization.json.Json +private val JSON = Json { + ignoreUnknownKeys = true + explicitNulls = false +} + /** * Maps the given [SyncResponseJson.Profile.Organization] to an [Organization]. */ @@ -31,21 +36,22 @@ fun List.toOrganizations(): List { - Json.decodeFromStringOrNull(it) + JSON.decodeFromStringOrNull(it) } PolicyTypeJson.PASSWORD_GENERATOR -> { - Json.decodeFromStringOrNull(it) + JSON.decodeFromStringOrNull(it) } PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT -> { - Json.decodeFromStringOrNull(it) + JSON.decodeFromStringOrNull(it) } PolicyTypeJson.SEND_OPTIONS -> { - Json.decodeFromStringOrNull(it) + JSON.decodeFromStringOrNull(it) } else -> null diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerImpl.kt index 12f025546..55c658e00 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerImpl.kt @@ -95,6 +95,8 @@ class PolicyManagerImpl( ): Boolean = if (policyType == PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT) { organization.type == OrganizationType.OWNER + } else if (policyType == PolicyTypeJson.PASSWORD_GENERATOR) { + false } else { (organization.type == OrganizationType.OWNER || organization.type == OrganizationType.ADMIN) || diff --git a/app/src/test/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerTest.kt index ec8262729..27e6ccc26 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/platform/manager/PolicyManagerTest.kt @@ -139,6 +139,34 @@ class PolicyManagerTest { 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"