BIT-1094: Store default URI match detection type (#747)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 45592a7369
commit 8a16672b4d
14 changed files with 283 additions and 39 deletions
@@ -4,6 +4,7 @@ import androidx.core.content.edit
import app.cash.turbine.test
import com.x8bit.bitwarden.data.platform.base.FakeSharedPreferences
import com.x8bit.bitwarden.data.platform.datasource.network.di.PlatformNetworkModule
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppLanguage
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
@@ -74,6 +75,10 @@ class SettingsDiskSourceTest {
userId = userId,
vaultTimeoutAction = VaultTimeoutAction.LOCK,
)
settingsDiskSource.storeDefaultUriMatchType(
userId = userId,
uriMatchType = UriMatchType.REGULAR_EXPRESSION,
)
settingsDiskSource.storePullToRefreshEnabled(
userId = userId,
isPullToRefreshEnabled = true,
@@ -99,6 +104,7 @@ class SettingsDiskSourceTest {
assertNull(settingsDiskSource.getVaultTimeoutInMinutes(userId = userId))
assertNull(settingsDiskSource.getVaultTimeoutAction(userId = userId))
assertNull(settingsDiskSource.getDefaultUriMatchType(userId = userId))
assertNull(settingsDiskSource.getPullToRefreshEnabled(userId = userId))
assertNull(settingsDiskSource.getInlineAutofillEnabled(userId = userId))
assertNull(settingsDiskSource.getBlockedAutofillUris(userId = userId))
@@ -379,6 +385,66 @@ class SettingsDiskSourceTest {
assertNull(fakeSharedPreferences.getString(vaultTimeoutActionKey, null))
}
@Test
fun `getDefaultUriMatchType when values are present should pull from SharedPreferences`() {
val defaultUriMatchTypeBaseKey = "bwPreferencesStorage:defaultUriMatch"
val mockUserId = "mockUserId"
val uriMatchType = UriMatchType.REGULAR_EXPRESSION
fakeSharedPreferences
.edit {
putInt(
"${defaultUriMatchTypeBaseKey}_$mockUserId",
3,
)
}
val actual = settingsDiskSource.getDefaultUriMatchType(userId = mockUserId)
assertEquals(
uriMatchType,
actual,
)
}
@Test
fun `getDefaultUriMatchType when values are absent should return null`() {
val mockUserId = "mockUserId"
assertNull(settingsDiskSource.getDefaultUriMatchType(userId = mockUserId))
}
@Test
fun `storeDefaultUriMatchType for non-null values should update SharedPreferences`() {
val defaultUriMatchTypeBaseKey = "bwPreferencesStorage:defaultUriMatch"
val mockUserId = "mockUserId"
val uriMatchType = UriMatchType.REGULAR_EXPRESSION
settingsDiskSource.storeDefaultUriMatchType(
userId = mockUserId,
uriMatchType = uriMatchType,
)
val actual = fakeSharedPreferences.getInt(
"${defaultUriMatchTypeBaseKey}_$mockUserId",
0,
)
assertEquals(
3,
actual,
)
}
@Test
fun `storeDefaultUriMatchType for null values should clear SharedPreferences`() {
val defaultUriMatchTypeBaseKey = "bwPreferencesStorage:defaultUriMatch"
val mockUserId = "mockUserId"
val defaultUriMatchTypeKey = "${defaultUriMatchTypeBaseKey}_$mockUserId"
fakeSharedPreferences.edit {
putInt(defaultUriMatchTypeKey, 3)
}
assertTrue(fakeSharedPreferences.contains(defaultUriMatchTypeKey))
settingsDiskSource.storeDefaultUriMatchType(
userId = mockUserId,
uriMatchType = null,
)
assertFalse(fakeSharedPreferences.contains(defaultUriMatchTypeKey))
}
@Test
fun `getPullToRefreshEnabled when values are present should pull from SharedPreferences`() {
val pullToRefreshBaseKey = "bwPreferencesStorage:syncOnRefresh"
@@ -1,6 +1,7 @@
package com.x8bit.bitwarden.data.platform.datasource.disk.util
import com.x8bit.bitwarden.data.platform.datasource.disk.SettingsDiskSource
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppLanguage
@@ -36,7 +37,7 @@ class FakeSettingsDiskSource : SettingsDiskSource {
private val storedLastSyncTime = mutableMapOf<String, Instant?>()
private val storedVaultTimeoutActions = mutableMapOf<String, VaultTimeoutAction?>()
private val storedVaultTimeoutInMinutes = mutableMapOf<String, Int?>()
private val storedUriMatchTypes = mutableMapOf<String, UriMatchType?>()
private val storedPullToRefreshEnabled = mutableMapOf<String, Boolean?>()
private val storedInlineAutofillEnabled = mutableMapOf<String, Boolean?>()
private val storedBlockedAutofillUris = mutableMapOf<String, List<String>?>()
@@ -74,6 +75,7 @@ class FakeSettingsDiskSource : SettingsDiskSource {
override fun clearData(userId: String) {
storedVaultTimeoutActions.remove(userId)
storedVaultTimeoutInMinutes.remove(userId)
storedUriMatchTypes.remove(userId)
storedPullToRefreshEnabled.remove(userId)
storedInlineAutofillEnabled.remove(userId)
storedBlockedAutofillUris.remove(userId)
@@ -124,6 +126,16 @@ class FakeSettingsDiskSource : SettingsDiskSource {
getMutableVaultTimeoutActionsFlow(userId = userId).tryEmit(vaultTimeoutAction)
}
override fun getDefaultUriMatchType(userId: String): UriMatchType? =
storedUriMatchTypes[userId]
override fun storeDefaultUriMatchType(
userId: String,
uriMatchType: UriMatchType?,
) {
storedUriMatchTypes[userId] = uriMatchType
}
override fun getPullToRefreshEnabled(userId: String): Boolean? =
storedPullToRefreshEnabled[userId]
@@ -9,6 +9,7 @@ import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager
import com.x8bit.bitwarden.data.platform.datasource.disk.util.FakeSettingsDiskSource
import com.x8bit.bitwarden.data.platform.manager.AppForegroundManager
import com.x8bit.bitwarden.data.platform.manager.model.AppForegroundState
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeout
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction
import com.x8bit.bitwarden.data.platform.util.asSuccess
@@ -376,6 +377,39 @@ class SettingsRepositoryTest {
}
}
@Test
fun `defaultUriMatchType should pull from and update SettingsDiskSource`() {
fakeAuthDiskSource.userState = null
assertEquals(
UriMatchType.DOMAIN,
settingsRepository.defaultUriMatchType,
)
val userId = "userId"
fakeAuthDiskSource.userState = MOCK_USER_STATE
// Updates to the disk source change the repository value
UriMatchType.entries.forEach { uriMatchType ->
fakeSettingsDiskSource.storeDefaultUriMatchType(
userId = userId,
uriMatchType = uriMatchType,
)
assertEquals(
uriMatchType,
settingsRepository.defaultUriMatchType,
)
}
// Updates to the repository value change the disk source
UriMatchType.entries.forEach { uriMatchType ->
settingsRepository.defaultUriMatchType = uriMatchType
assertEquals(
uriMatchType,
fakeSettingsDiskSource.getDefaultUriMatchType(userId = userId),
)
}
}
@Suppress("MaxLineLength")
@Test
fun `isUnlockWithPinEnabled should return a value that tracks the existence of an encrypted PIN for the current user`() {
@@ -14,6 +14,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
@@ -246,16 +247,16 @@ class AutoFillScreenTest : BaseComposeTest() {
@Test
fun `default URI match detection add login should be updated on or off according to state`() {
composeTestRule
.onNodeWithText("Default")
.onNodeWithText("Base domain")
.assertExists()
composeTestRule
.onNodeWithText("Starts with")
.assertDoesNotExist()
mutableStateFlow.update {
it.copy(uriDetectionMethod = AutoFillState.UriDetectionMethod.STARTS_WITH)
it.copy(uriDetectionMethod = UriMatchType.STARTS_WITH)
}
composeTestRule
.onNodeWithText("Default")
.onNodeWithText("Base domain")
.assertDoesNotExist()
composeTestRule
.onNodeWithText("Starts with")
@@ -295,5 +296,5 @@ private val DEFAULT_STATE: AutoFillState = AutoFillState(
isAutoFillServicesEnabled = false,
isCopyTotpAutomaticallyEnabled = false,
isUseInlineAutoFillEnabled = false,
uriDetectionMethod = AutoFillState.UriDetectionMethod.DEFAULT,
uriDetectionMethod = UriMatchType.DOMAIN,
)
@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.autofill
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import io.mockk.every
@@ -21,6 +22,8 @@ class AutoFillViewModelTest : BaseViewModelTest() {
private val settingsRepository: SettingsRepository = mockk() {
every { isInlineAutofillEnabled } returns true
every { isInlineAutofillEnabled = any() } just runs
every { defaultUriMatchType } returns UriMatchType.DOMAIN
every { defaultUriMatchType = any() } just runs
every { isAutofillEnabledStateFlow } returns mutableIsAutofillEnabledStateFlow
every { disableAutofill() } just runs
}
@@ -36,7 +39,7 @@ class AutoFillViewModelTest : BaseViewModelTest() {
mutableIsAutofillEnabledStateFlow.value = true
val state = DEFAULT_STATE.copy(
isAutoFillServicesEnabled = true,
uriDetectionMethod = AutoFillState.UriDetectionMethod.REGULAR_EXPRESSION,
uriDetectionMethod = UriMatchType.REGULAR_EXPRESSION,
)
val viewModel = createViewModel(state = state)
assertEquals(state, viewModel.stateFlow.value)
@@ -140,17 +143,15 @@ class AutoFillViewModelTest : BaseViewModelTest() {
}
@Test
fun `on UriDetectionMethodSelect should emit ShowToast`() = runTest {
fun `on UriDetectionMethodSelect should update the state and save the new value to settings`() {
val viewModel = createViewModel()
val method = AutoFillState.UriDetectionMethod.EXACT
viewModel.eventFlow.test {
viewModel.trySendAction(AutoFillAction.UriDetectionMethodSelect(method))
assertEquals(AutoFillEvent.ShowToast("Not yet implemented.".asText()), awaitItem())
}
val method = UriMatchType.EXACT
viewModel.trySendAction(AutoFillAction.UriDetectionMethodSelect(method))
assertEquals(
DEFAULT_STATE.copy(uriDetectionMethod = method),
viewModel.stateFlow.value,
)
verify { settingsRepository.defaultUriMatchType = method }
}
@Test
@@ -175,5 +176,5 @@ private val DEFAULT_STATE: AutoFillState = AutoFillState(
isAutoFillServicesEnabled = false,
isCopyTotpAutomaticallyEnabled = false,
isUseInlineAutoFillEnabled = true,
uriDetectionMethod = AutoFillState.UriDetectionMethod.DEFAULT,
uriDetectionMethod = UriMatchType.DOMAIN,
)
@@ -0,0 +1,27 @@
package com.x8bit.bitwarden.ui.platform.feature.settings.autofill.util
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.ui.platform.base.util.asText
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class UriMatchTypeExtensionsTest {
@Test
fun `displayLabel should return the correct value for each type`() {
mapOf(
UriMatchType.DOMAIN to R.string.base_domain.asText(),
UriMatchType.HOST to R.string.host.asText(),
UriMatchType.STARTS_WITH to R.string.starts_with.asText(),
UriMatchType.REGULAR_EXPRESSION to R.string.reg_ex.asText(),
UriMatchType.EXACT to R.string.exact.asText(),
UriMatchType.NEVER to R.string.never.asText(),
)
.forEach { (type, label) ->
assertEquals(
label,
type.displayLabel,
)
}
}
}