Skip to content

Commit

Permalink
tweaks and simple version tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpbw2 committed Oct 3, 2024
1 parent 7ab88ee commit 95dfe5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

private const val CIPHER_KEY_ENCRYPTION_KEY = "enableCipherKeyEncryption"
private const val CIPHER_KEY_ENC_MIN_SERVER_VERSION = "2024.2.0"

/**
* Primary implementation of [FeatureFlagManager].
Expand All @@ -17,8 +18,13 @@ class FeatureFlagManagerImpl(
) : FeatureFlagManager {

override val sdkFeatureFlags: Map<String, Boolean>
get() = mapOf(CIPHER_KEY_ENCRYPTION_KEY to
isServerVersionAtLeast(serverConfigRepository, "2024.2.0"))
get() = mapOf(
CIPHER_KEY_ENCRYPTION_KEY to
isServerVersionAtLeast(
serverConfigRepository.getLocalServerConfig(),
CIPHER_KEY_ENC_MIN_SERVER_VERSION,
),
)

override fun <T : Any> getFeatureFlagFlow(key: FlagKey<T>): Flow<T> =
serverConfigRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.x8bit.bitwarden.data.platform.util

import com.x8bit.bitwarden.data.platform.repository.ServerConfigRepository
import com.x8bit.bitwarden.data.platform.datasource.disk.model.ServerConfig
import kotlin.text.split
import kotlin.text.toIntOrNull

Expand All @@ -10,9 +10,8 @@ private const val SUFFIX_SEPARATOR = "-"
/**
* Checks if the server version is greater than another provided version, returns true if it is.
*/
fun isServerVersionAtLeast(serverConfig: ServerConfigRepository, version: String): Boolean {
fun isServerVersionAtLeast(serverConfig: ServerConfig?, version: String): Boolean {
val serverVersion = serverConfig
.getLocalServerConfig()
?.serverData
?.version

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.x8bit.bitwarden.data.platform.datasource.network.model.ConfigResponse
import com.x8bit.bitwarden.data.platform.datasource.network.model.ConfigResponseJson.ServerJson
import com.x8bit.bitwarden.data.platform.manager.model.FlagKey
import com.x8bit.bitwarden.data.platform.repository.util.FakeServerConfigRepository
import com.x8bit.bitwarden.data.platform.util.isServerVersionAtLeast
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonPrimitive
import org.junit.Test
Expand All @@ -33,6 +34,30 @@ class FeatureFlagManagerTest {
assertEquals(expected, actual)
}

@Test
fun `server version is at least supplied version`() {
val result =
isServerVersionAtLeast(
fakeServerConfigRepository.getLocalServerConfig(),
"2024.2.0",
)

// This relies on the fake server version being "2024.7.0"
assertTrue(result)
}

@Test
fun `server version is not at least supplied version`() {
val result =
isServerVersionAtLeast(
fakeServerConfigRepository.getLocalServerConfig(),
"2024.12.0-suffix",
)

// This relies on the fake server version being "2024.7.0"
assertFalse(result)
}

@Test
fun `ServerConfigRepository flow with value should trigger new flags`() = runTest {
fakeServerConfigRepository.serverConfigValue = null
Expand Down

0 comments on commit 95dfe5e

Please sign in to comment.