mirror of
https://github.com/bitwarden/android.git
synced 2024-12-18 07:11:51 +03:00
[PM-10930] Fix password generator policies (#3853)
This commit is contained in:
parent
95240a7ce3
commit
4e69ed57e8
3 changed files with 40 additions and 4 deletions
|
@ -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<SyncResponseJson.Profile.Organization>.toOrganizations(): List<Organiza
|
|||
*/
|
||||
val SyncResponseJson.Policy.policyInformation: PolicyInformation?
|
||||
get() = data?.toString()?.let {
|
||||
|
||||
when (type) {
|
||||
PolicyTypeJson.MASTER_PASSWORD -> {
|
||||
Json.decodeFromStringOrNull<PolicyInformation.MasterPassword>(it)
|
||||
JSON.decodeFromStringOrNull<PolicyInformation.MasterPassword>(it)
|
||||
}
|
||||
|
||||
PolicyTypeJson.PASSWORD_GENERATOR -> {
|
||||
Json.decodeFromStringOrNull<PolicyInformation.PasswordGenerator>(it)
|
||||
JSON.decodeFromStringOrNull<PolicyInformation.PasswordGenerator>(it)
|
||||
}
|
||||
|
||||
PolicyTypeJson.MAXIMUM_VAULT_TIMEOUT -> {
|
||||
Json.decodeFromStringOrNull<PolicyInformation.VaultTimeout>(it)
|
||||
JSON.decodeFromStringOrNull<PolicyInformation.VaultTimeout>(it)
|
||||
}
|
||||
|
||||
PolicyTypeJson.SEND_OPTIONS -> {
|
||||
Json.decodeFromStringOrNull<PolicyInformation.SendOptions>(it)
|
||||
JSON.decodeFromStringOrNull<PolicyInformation.SendOptions>(it)
|
||||
}
|
||||
|
||||
else -> null
|
||||
|
|
|
@ -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) ||
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue