From 32f9b1bde635a61eba5207102b5f2a0fb2776a1f Mon Sep 17 00:00:00 2001 From: David Perez Date: Fri, 16 Feb 2024 16:12:38 -0600 Subject: [PATCH] Use the clock in the Generator Repo (#1032) --- .../repository/GeneratorRepositoryImpl.kt | 7 ++-- .../di/GeneratorRepositoryModule.kt | 3 ++ .../repository/GeneratorRepositoryTest.kt | 36 ++++++------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryImpl.kt index dc8f59765..6b4300499 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryImpl.kt @@ -40,7 +40,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.launch -import java.time.Instant +import java.time.Clock import javax.inject.Singleton import kotlin.math.max @@ -50,6 +50,7 @@ import kotlin.math.max @Singleton @Suppress("LongParameterList") class GeneratorRepositoryImpl( + private val clock: Clock, private val generatorSdkSource: GeneratorSdkSource, private val generatorDiskSource: GeneratorDiskSource, private val authDiskSource: AuthDiskSource, @@ -119,7 +120,7 @@ class GeneratorRepositoryImpl( onSuccess = { generatedPassword -> val passwordHistoryView = PasswordHistoryView( password = generatedPassword, - lastUsedDate = Instant.now(), + lastUsedDate = clock.instant(), ) if (shouldSave) { @@ -141,7 +142,7 @@ class GeneratorRepositoryImpl( onSuccess = { generatedPassphrase -> val passwordHistoryView = PasswordHistoryView( password = generatedPassphrase, - lastUsedDate = Instant.now(), + lastUsedDate = clock.instant(), ) scope.launch { storePasswordHistory(passwordHistoryView) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/di/GeneratorRepositoryModule.kt b/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/di/GeneratorRepositoryModule.kt index 9c88a3fe5..f43ca13f5 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/di/GeneratorRepositoryModule.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/tools/generator/repository/di/GeneratorRepositoryModule.kt @@ -13,6 +13,7 @@ import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import java.time.Clock import javax.inject.Singleton /** @@ -25,6 +26,7 @@ object GeneratorRepositoryModule { @Provides @Singleton fun provideGeneratorRepository( + clock: Clock, generatorSdkSource: GeneratorSdkSource, generatorDiskSource: GeneratorDiskSource, authDiskSource: AuthDiskSource, @@ -33,6 +35,7 @@ object GeneratorRepositoryModule { dispatcherManager: DispatcherManager, policyManager: PolicyManager, ): GeneratorRepository = GeneratorRepositoryImpl( + clock = clock, generatorSdkSource = generatorSdkSource, generatorDiskSource = generatorDiskSource, authDiskSource = authDiskSource, diff --git a/app/src/test/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryTest.kt index dfbe21046..a7bc6c8c4 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/tools/generator/repository/GeneratorRepositoryTest.kt @@ -44,27 +44,29 @@ import io.mockk.coVerify import io.mockk.every import io.mockk.just import io.mockk.mockk -import io.mockk.mockkStatic import io.mockk.runs -import io.mockk.unmockkStatic import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json import kotlinx.serialization.json.encodeToJsonElement import kotlinx.serialization.json.jsonObject -import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Test +import java.time.Clock import java.time.Instant +import java.time.ZoneOffset @Suppress("LargeClass") class GeneratorRepositoryTest { + private val fixedClock: Clock = Clock.fixed( + Instant.parse("2021-01-01T00:00:00Z"), + ZoneOffset.UTC, + ) private val mutableUserStateFlow = MutableStateFlow(null) - private val generatorSdkSource: GeneratorSdkSource = mockk() private val generatorDiskSource: GeneratorDiskSource = mockk() private val authDiskSource: AuthDiskSource = mockk { @@ -77,6 +79,7 @@ class GeneratorRepositoryTest { private val policyManager: PolicyManager = mockk() private val repository = GeneratorRepositoryImpl( + clock = fixedClock, generatorSdkSource = generatorSdkSource, generatorDiskSource = generatorDiskSource, authDiskSource = authDiskSource, @@ -86,20 +89,10 @@ class GeneratorRepositoryTest { policyManager = policyManager, ) - @AfterEach - fun tearDown() { - unmockkStatic(Instant::class) - } - @Suppress("MaxLineLength") @Test fun `generatePassword should emit Success result and store the generated password when shouldSave is true`() = runTest { - val fixedInstant = Instant.parse("2021-01-01T00:00:00Z") - - mockkStatic(Instant::class) - every { Instant.now() } returns fixedInstant - val userId = "testUserId" val request = PasswordGeneratorRequest( lowercase = true, @@ -116,7 +109,7 @@ class GeneratorRepositoryTest { val generatedPassword = "GeneratedPassword123!" val encryptedPasswordHistory = PasswordHistory( password = generatedPassword, - lastUsedDate = Instant.now(), + lastUsedDate = fixedClock.instant(), ) coEvery { authDiskSource.userState?.activeUserId } returns userId @@ -147,11 +140,6 @@ class GeneratorRepositoryTest { @Test fun `generatePassword should emit Success result but not store the generated password when shouldSave is false`() = runTest { - val fixedInstant = Instant.parse("2021-01-01T00:00:00Z") - - mockkStatic(Instant::class) - every { Instant.now() } returns fixedInstant - val userId = "testUserId" val request = PasswordGeneratorRequest( lowercase = true, @@ -168,7 +156,7 @@ class GeneratorRepositoryTest { val generatedPassword = "GeneratedPassword123!" val encryptedPasswordHistory = PasswordHistory( password = generatedPassword, - lastUsedDate = Instant.now(), + lastUsedDate = fixedClock.instant(), ) coEvery { authDiskSource.userState?.activeUserId } returns userId @@ -219,10 +207,6 @@ class GeneratorRepositoryTest { @Test fun `generatePassphrase should emit Success result and store the generated passphrase`() = runTest { - val fixedInstant = Instant.parse("2021-01-01T00:00:00Z") - mockkStatic(Instant::class) - every { Instant.now() } returns fixedInstant - val userId = "testUserId" val request = PassphraseGeneratorRequest( numWords = 5.toUByte(), @@ -233,7 +217,7 @@ class GeneratorRepositoryTest { val generatedPassphrase = "Generated-Passphrase-123" val encryptedPasswordHistory = PasswordHistory( password = generatedPassphrase, - lastUsedDate = Instant.now(), + lastUsedDate = fixedClock.instant(), ) coEvery { authDiskSource.userState?.activeUserId } returns userId