mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-1282: Add UI for Vault Sync (#740)
This commit is contained in:
committed by
Álison Fernandes
parent
376278e97a
commit
de99c36b20
+31
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.Instant
|
||||
|
||||
class SettingsRepositoryTest {
|
||||
private val autofillManager: AutofillManager = mockk {
|
||||
@@ -119,6 +120,36 @@ class SettingsRepositoryTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultLastSync should pull from and update SettingsDiskSource`() {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
assertNull(settingsRepository.vaultLastSync)
|
||||
val instant = Instant.ofEpochMilli(1_698_408_000_000L)
|
||||
|
||||
// Updates to the disk source change the repository value.
|
||||
fakeSettingsDiskSource.storeLastSyncTime(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
lastSyncTime = instant,
|
||||
)
|
||||
assertEquals(instant, settingsRepository.vaultLastSync)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultLastSyncStateFlow should react to changes in SettingsDiskSource`() = runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
val instant = Instant.ofEpochMilli(1_698_408_000_000L)
|
||||
settingsRepository
|
||||
.vaultLastSyncStateFlow
|
||||
.test {
|
||||
assertNull(awaitItem())
|
||||
fakeSettingsDiskSource.storeLastSyncTime(
|
||||
userId = MOCK_USER_STATE.activeUserId,
|
||||
lastSyncTime = instant,
|
||||
)
|
||||
assertEquals(instant, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isIconLoadingDisabled should pull from and update SettingsDiskSource`() {
|
||||
assertFalse(settingsRepository.isIconLoadingDisabled)
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.x8bit.bitwarden.data.auth.datasource.disk.util.FakeAuthDiskSource
|
||||
import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams
|
||||
import com.x8bit.bitwarden.data.auth.util.KdfParamsConstants.DEFAULT_PBKDF2_ITERATIONS
|
||||
import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager
|
||||
import com.x8bit.bitwarden.data.platform.datasource.disk.SettingsDiskSource
|
||||
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
|
||||
@@ -95,14 +96,20 @@ import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.net.UnknownHostException
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
|
||||
@Suppress("LargeClass")
|
||||
class VaultRepositoryTest {
|
||||
|
||||
private val clock: Clock = Clock.fixed(
|
||||
Instant.parse("2023-10-27T12:00:00Z"),
|
||||
ZoneOffset.UTC,
|
||||
)
|
||||
private val dispatcherManager: DispatcherManager = FakeDispatcherManager()
|
||||
private val fileManager: FileManager = mockk()
|
||||
private val fakeAuthDiskSource = FakeAuthDiskSource()
|
||||
private val settingsDiskSource = mockk<SettingsDiskSource>()
|
||||
private val syncService: SyncService = mockk()
|
||||
private val sendsService: SendsService = mockk()
|
||||
private val ciphersService: CiphersService = mockk()
|
||||
@@ -132,10 +139,12 @@ class VaultRepositoryTest {
|
||||
vaultDiskSource = vaultDiskSource,
|
||||
vaultSdkSource = vaultSdkSource,
|
||||
authDiskSource = fakeAuthDiskSource,
|
||||
settingsDiskSource = settingsDiskSource,
|
||||
vaultLockManager = vaultLockManager,
|
||||
dispatcherManager = dispatcherManager,
|
||||
totpCodeManager = totpCodeManager,
|
||||
fileManager = fileManager,
|
||||
clock = clock,
|
||||
)
|
||||
|
||||
@BeforeEach
|
||||
@@ -412,6 +421,9 @@ class VaultRepositoryTest {
|
||||
vault = mockSyncResponse,
|
||||
)
|
||||
} just runs
|
||||
every {
|
||||
settingsDiskSource.storeLastSyncTime(MOCK_USER_STATE.activeUserId, clock.instant())
|
||||
} just runs
|
||||
|
||||
vaultRepository.sync()
|
||||
|
||||
@@ -1051,6 +1063,9 @@ class VaultRepositoryTest {
|
||||
vault = mockSyncResponse,
|
||||
)
|
||||
} just runs
|
||||
every {
|
||||
settingsDiskSource.storeLastSyncTime(MOCK_USER_STATE.activeUserId, clock.instant())
|
||||
} just runs
|
||||
coEvery {
|
||||
vaultSdkSource.decryptSendList(
|
||||
userId = userId,
|
||||
@@ -2217,6 +2232,9 @@ class VaultRepositoryTest {
|
||||
vault = mockSyncResponse,
|
||||
)
|
||||
} just runs
|
||||
every {
|
||||
settingsDiskSource.storeLastSyncTime(MOCK_USER_STATE.activeUserId, clock.instant())
|
||||
} just runs
|
||||
|
||||
val stateFlow = MutableStateFlow<DataState<List<VerificationCodeItem>>>(
|
||||
DataState.Loading,
|
||||
|
||||
+42
-3
@@ -2,9 +2,11 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.other
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
@@ -14,11 +16,22 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
|
||||
class OtherViewModelTest : BaseViewModelTest() {
|
||||
private val clock: Clock = Clock.fixed(
|
||||
Instant.parse("2023-10-27T12:00:00Z"),
|
||||
ZoneOffset.UTC,
|
||||
)
|
||||
private val mutablePullToRefreshStateFlow = MutableStateFlow(false)
|
||||
private val instant: Instant = Instant.parse("2023-10-26T12:00:00Z")
|
||||
private val mutableVaultLastSyncStateFlow = MutableStateFlow(instant)
|
||||
private val settingsRepository = mockk<SettingsRepository> {
|
||||
every { getPullToRefreshEnabledFlow() } returns mutablePullToRefreshStateFlow
|
||||
every { vaultLastSyncStateFlow } returns mutableVaultLastSyncStateFlow
|
||||
every { vaultLastSync } returns instant
|
||||
}
|
||||
private val vaultRepository = mockk<VaultRepository>()
|
||||
|
||||
@@ -103,13 +116,38 @@ class OtherViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultLastSyncReceive should sync repo`() = runTest {
|
||||
val newSyncTime = Instant.parse("2023-10-27T12:00:00Z")
|
||||
val viewModel = createViewModel()
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(DEFAULT_STATE, awaitItem())
|
||||
mutableVaultLastSyncStateFlow.tryEmit(newSyncTime)
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
lastSyncTime = "10/27/2023 12:00 PM",
|
||||
dialogState = null,
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on SyncNowButtonClick should sync repo`() = runTest {
|
||||
every { vaultRepository.sync() } just runs
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(DEFAULT_STATE, awaitItem())
|
||||
viewModel.trySendAction(OtherAction.SyncNowButtonClick)
|
||||
expectNoEvents()
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
dialogState = OtherState.DialogState.Loading(
|
||||
message = R.string.syncing.asText(),
|
||||
),
|
||||
),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
verify { vaultRepository.sync() }
|
||||
}
|
||||
@@ -117,6 +155,7 @@ class OtherViewModelTest : BaseViewModelTest() {
|
||||
private fun createViewModel(
|
||||
state: OtherState? = null,
|
||||
) = OtherViewModel(
|
||||
clock = clock,
|
||||
settingsRepo = settingsRepository,
|
||||
vaultRepo = vaultRepository,
|
||||
savedStateHandle = SavedStateHandle().apply {
|
||||
@@ -129,7 +168,7 @@ class OtherViewModelTest : BaseViewModelTest() {
|
||||
allowScreenCapture = false,
|
||||
allowSyncOnRefresh = false,
|
||||
clearClipboardFrequency = OtherState.ClearClipboardFrequency.DEFAULT,
|
||||
lastSyncTime = "5/14/2023 4:52 PM",
|
||||
lastSyncTime = "10/26/2023 12:00 PM",
|
||||
dialogState = null,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user