[BWA-57] Define feature flag disk source (#175)

This commit is contained in:
Patrick Honkonen
2024-08-29 10:22:14 -04:00
committed by GitHub
parent 3c74a342d1
commit d0203eedc4
7 changed files with 311 additions and 0 deletions
@@ -0,0 +1,20 @@
package com.bitwarden.authenticator.data.platform.datasource.disk
import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration
import kotlinx.coroutines.flow.Flow
/**
* Primary access point for feature flag configuration.
*/
interface FeatureFlagDiskSource {
/**
* The currently persisted [FeatureFlagsConfiguration].
*/
var featureFlagsConfiguration: FeatureFlagsConfiguration?
/**
* Emits updates to track [FeatureFlagsConfiguration]. This will replay the last known value.
*/
val featureFlagsConfigurationFlow: Flow<FeatureFlagsConfiguration?>
}
@@ -0,0 +1,42 @@
package com.bitwarden.authenticator.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.authenticator.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY
import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration
import com.bitwarden.authenticator.data.platform.repository.util.bufferedMutableSharedFlow
import com.bitwarden.authenticator.data.platform.util.decodeFromStringOrNull
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onSubscription
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
private const val KEY_FEATURE_FLAGS = "$BASE_KEY:featureFlags"
/**
* Primary implementation of [FeatureFlagDiskSource].
*/
class FeatureFlagDiskSourceImpl(
sharedPreferences: SharedPreferences,
private val json: Json,
) : BaseDiskSource(sharedPreferences = sharedPreferences),
FeatureFlagDiskSource {
private val mutableFeatureFlagsConfigurationFlow =
bufferedMutableSharedFlow<FeatureFlagsConfiguration?>(replay = 1)
override val featureFlagsConfigurationFlow: Flow<FeatureFlagsConfiguration?>
get() = mutableFeatureFlagsConfigurationFlow.onSubscription {
emit(featureFlagsConfiguration)
}
override var featureFlagsConfiguration: FeatureFlagsConfiguration?
get() = getString(key = KEY_FEATURE_FLAGS)
?.let { json.decodeFromStringOrNull(it) }
set(value) {
putString(
key = KEY_FEATURE_FLAGS,
value = value.let { json.encodeToString(it) },
)
mutableFeatureFlagsConfigurationFlow.tryEmit(value)
}
}
@@ -2,12 +2,15 @@ package com.bitwarden.authenticator.data.platform.datasource.disk.di
import android.content.SharedPreferences
import com.bitwarden.authenticator.data.platform.datasource.di.UnencryptedPreferences
import com.bitwarden.authenticator.data.platform.datasource.disk.FeatureFlagDiskSource
import com.bitwarden.authenticator.data.platform.datasource.disk.FeatureFlagDiskSourceImpl
import com.bitwarden.authenticator.data.platform.datasource.disk.SettingsDiskSource
import com.bitwarden.authenticator.data.platform.datasource.disk.SettingsDiskSourceImpl
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import javax.inject.Singleton
@Module
@@ -20,4 +23,15 @@ object PlatformDiskModule {
@UnencryptedPreferences sharedPreferences: SharedPreferences,
): SettingsDiskSource =
SettingsDiskSourceImpl(sharedPreferences = sharedPreferences)
@Provides
@Singleton
fun provideFeatureFlagDiskSource(
@UnencryptedPreferences sharedPreferences: SharedPreferences,
json: Json,
): FeatureFlagDiskSource =
FeatureFlagDiskSourceImpl(
sharedPreferences = sharedPreferences,
json = json,
)
}
@@ -0,0 +1,14 @@
package com.bitwarden.authenticator.data.platform.datasource.disk.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonPrimitive
/**
* Models the state of feature flags.
*/
@Serializable
data class FeatureFlagsConfiguration(
@SerialName("featureFlags")
val featureFlags: Map<String, JsonPrimitive>,
)
@@ -0,0 +1,40 @@
package com.bitwarden.authenticator.data.platform.manager.model
/**
* Represents feature flags used by the application.
*
* @property name The string value of the given key.
* @property defaultValue The value to be used if the flags value cannot be determined or is not remotely configured.
* @property isRemotelyConfigured Indicates if the flag should respect the network value or not.
*/
sealed class FeatureFlag<out T : Any>(
val name: String,
val defaultValue: T,
val isRemotelyConfigured: Boolean,
)
/**
* Models feature flags that are managed locally. E.g., [isRemotelyConfigured] is `false`.
*/
sealed class LocalFeatureFlag<out T : Any>(
name: String,
defaultValue: T,
) : FeatureFlag<T>(name, defaultValue, isRemotelyConfigured = false) {
/**
* Indicates the state of Bitwarden authentication.
*/
data object BitwardenAuthenticationEnabled : LocalFeatureFlag<Boolean>(
name = "bitwarden-authentication-enabled",
defaultValue = false,
)
}
/**
* Models feature flags that are managed remotely. E.g., [isRemotelyConfigured] is `true`.
*/
sealed class RemoteFeatureFlag<out T : Any>(
name: String,
defaultValue: T,
) : FeatureFlag<T>(name, defaultValue, isRemotelyConfigured = true)
@@ -0,0 +1,109 @@
package com.bitwarden.authenticator.data.platform.base
import android.content.SharedPreferences
/**
* A faked implementation of [SharedPreferences] that is backed by an internal, memory-based map.
*/
class FakeSharedPreferences : SharedPreferences {
private val sharedPreferences: MutableMap<String, Any?> = mutableMapOf()
private val listeners = mutableSetOf<SharedPreferences.OnSharedPreferenceChangeListener>()
override fun contains(key: String): Boolean =
sharedPreferences.containsKey(key)
override fun edit(): SharedPreferences.Editor = Editor()
override fun getAll(): Map<String, *> = sharedPreferences
override fun getBoolean(key: String, defaultValue: Boolean): Boolean =
getValue(key, defaultValue)
override fun getFloat(key: String, defaultValue: Float): Float =
getValue(key, defaultValue)
override fun getInt(key: String, defaultValue: Int): Int =
getValue(key, defaultValue)
override fun getLong(key: String, defaultValue: Long): Long =
getValue(key, defaultValue)
override fun getString(key: String, defaultValue: String?): String? =
getValue(key, defaultValue)
override fun getStringSet(key: String, defaultValue: Set<String>?): Set<String>? =
getValue(key, defaultValue)
override fun registerOnSharedPreferenceChangeListener(
listener: SharedPreferences.OnSharedPreferenceChangeListener,
) {
listeners += listener
}
override fun unregisterOnSharedPreferenceChangeListener(
listener: SharedPreferences.OnSharedPreferenceChangeListener,
) {
listeners -= listener
}
private inline fun <reified T> getValue(
key: String,
defaultValue: T,
): T = sharedPreferences[key] as? T ?: defaultValue
inner class Editor : SharedPreferences.Editor {
private val pendingSharedPreferences = sharedPreferences.toMutableMap()
override fun apply() {
sharedPreferences.apply {
clear()
putAll(pendingSharedPreferences)
// Notify listeners
listeners.forEach { listener ->
pendingSharedPreferences.keys.forEach { key ->
listener.onSharedPreferenceChanged(this@FakeSharedPreferences, key)
}
}
}
}
override fun clear(): SharedPreferences.Editor =
apply { pendingSharedPreferences.clear() }
override fun commit(): Boolean {
apply()
return true
}
override fun putBoolean(key: String, value: Boolean): SharedPreferences.Editor =
putValue(key, value)
override fun putFloat(key: String, value: Float): SharedPreferences.Editor =
putValue(key, value)
override fun putInt(key: String, value: Int): SharedPreferences.Editor =
putValue(key, value)
override fun putLong(key: String, value: Long): SharedPreferences.Editor =
putValue(key, value)
override fun putString(key: String, value: String?): SharedPreferences.Editor =
putValue(key, value)
override fun putStringSet(key: String, value: Set<String>?): SharedPreferences.Editor =
putValue(key, value)
override fun remove(key: String): SharedPreferences.Editor =
apply { pendingSharedPreferences.remove(key) }
private inline fun <reified T> putValue(
key: String,
value: T,
): SharedPreferences.Editor = apply {
value
?.let { pendingSharedPreferences[key] = it }
?: pendingSharedPreferences.remove(key)
}
}
}
@@ -0,0 +1,72 @@
package com.bitwarden.authenticator.data.platform.datasource.disk
import androidx.core.content.edit
import app.cash.turbine.test
import com.bitwarden.authenticator.data.platform.base.FakeSharedPreferences
import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration
import com.bitwarden.authenticator.data.platform.datasource.network.di.PlatformNetworkModule
import com.bitwarden.authenticator.data.platform.manager.model.LocalFeatureFlag
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonPrimitive
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
class FeatureFlagDiskSourceTest {
private val json = PlatformNetworkModule.providesJson()
private val fakeSharedPreferences = FakeSharedPreferences()
private val featureFlagDiskSource = FeatureFlagDiskSourceImpl(
sharedPreferences = fakeSharedPreferences,
json = json,
)
@Test
fun `featureFlagsConfiguration should pull from and update SharedPreferences`() {
val featureFlagsConfigKey = "bwPreferencesStorage:featureFlags"
assertNull(featureFlagDiskSource.featureFlagsConfiguration)
assertNull(fakeSharedPreferences.getString(featureFlagsConfigKey, null))
featureFlagDiskSource.featureFlagsConfiguration = FEATURE_FLAGS_CONFIGURATION
assertEquals(
json.parseToJsonElement(
FEATURE_FLAGS_CONFIGURATION_JSON
),
json.parseToJsonElement(
fakeSharedPreferences.getString(featureFlagsConfigKey, null)!!
),
)
fakeSharedPreferences.edit { putString(featureFlagsConfigKey, null) }
assertNull(featureFlagDiskSource.featureFlagsConfiguration)
}
@Test
fun `featureFlagsConfigFlow should react to changes in featureFlagsConfig`() = runTest {
featureFlagDiskSource.featureFlagsConfigurationFlow.test {
assertNull(featureFlagDiskSource.featureFlagsConfiguration)
assertNull(awaitItem())
featureFlagDiskSource.featureFlagsConfiguration = FEATURE_FLAGS_CONFIGURATION
assertEquals(FEATURE_FLAGS_CONFIGURATION, awaitItem())
}
}
}
private const val FEATURE_FLAGS_CONFIGURATION_JSON = """
{
"featureFlags" : {
"bitwarden-authentication-enabled" : true
}
}
"""
private val FEATURE_FLAGS_CONFIGURATION = FeatureFlagsConfiguration(
featureFlags = mapOf(
LocalFeatureFlag.BitwardenAuthenticationEnabled.name to JsonPrimitive(true)
)
)