diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 61c89f49..420c5cde 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -178,12 +178,6 @@ dependencies { testImplementation(libs.ui.automator) debugImplementation(libs.androidx.test.ktx) - testImplementation(libs.datastore) - testImplementation(libs.datastore.core) - testImplementation(libs.datastore.preferences) - testImplementation(libs.datastore.preferences.core) - testImplementation(libs.encrypted.preferences) - testImplementation(libs.kotlin.test.junit) androidTestImplementation(libs.androidx.compose.ui.test) diff --git a/app/src/test/kotlin/com/andreolas/movierama/base/storage/DataStorePreferenceStorageTest.kt b/app/src/test/kotlin/com/andreolas/movierama/base/storage/DataStorePreferenceStorageTest.kt index ebf13ad7..ad45725f 100644 --- a/app/src/test/kotlin/com/andreolas/movierama/base/storage/DataStorePreferenceStorageTest.kt +++ b/app/src/test/kotlin/com/andreolas/movierama/base/storage/DataStorePreferenceStorageTest.kt @@ -1,43 +1,23 @@ package com.andreolas.movierama.base.storage -import android.content.Context -import androidx.datastore.core.DataStore -import androidx.datastore.preferences.core.PreferenceDataStoreFactory -import androidx.datastore.preferences.core.Preferences -import androidx.datastore.preferences.preferencesDataStoreFile -import androidx.test.platform.app.InstrumentationRegistry import com.divinelink.core.datastore.DataStorePreferenceStorage import com.divinelink.core.designsystem.theme.Theme -import com.divinelink.core.testing.MainDispatcherRule +import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod +import com.divinelink.core.testing.datastore.TestDatastoreFactory +import com.divinelink.core.testing.factories.model.jellyseerr.JellyseerrAccountDetailsFactory import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.flow.first -import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest -import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner -private const val TEST_DATASTORE_NAME: String = "test_datastore" - @RunWith(RobolectricTestRunner::class) class DataStorePreferenceStorageTest { private lateinit var storage: DataStorePreferenceStorage - @get:Rule - val mainDispatcherRule = MainDispatcherRule() - private val testDispatcher = mainDispatcherRule.testDispatcher - - private val testContext: Context = InstrumentationRegistry.getInstrumentation().targetContext - - private val testCoroutineScope = TestScope(testDispatcher) - - private val fakeDataStore: DataStore = - PreferenceDataStoreFactory.create( - scope = testCoroutineScope, - produceFile = { testContext.preferencesDataStoreFile(TEST_DATASTORE_NAME) }, - ) + private val fakeDataStore = TestDatastoreFactory.create() @Test fun `test selectTheme sets selectedTheme`() = runTest { @@ -148,4 +128,72 @@ class DataStorePreferenceStorageTest { assertThat(storage.accountId.first()).isNull() } + + @Test + fun `test setJellyseerrAddress`() = runTest { + storage = DataStorePreferenceStorage(fakeDataStore) + + assertThat(storage.jellyseerrAddress.first()).isEqualTo(null) + + storage.setJellyseerrAddress("http://localhost:5050") + assertThat(storage.jellyseerrAddress.first()).isEqualTo("http://localhost:5050") + } + + @Test + fun `test clearJellyseerrAddress`() = runTest { + storage = DataStorePreferenceStorage(fakeDataStore) + + storage.setJellyseerrAddress("http://localhost:5050") + assertThat(storage.jellyseerrAddress.first()).isEqualTo("http://localhost:5050") + + storage.clearJellyseerrAddress() + assertThat(storage.jellyseerrAddress.first()).isEqualTo(null) + } + + @Test + fun `test setJellyseerrAccount`() = runTest { + storage = DataStorePreferenceStorage(fakeDataStore) + assertThat(storage.jellyseerrAccount.first()).isEqualTo(null) + + val displayName = JellyseerrAccountDetailsFactory.jellyseerr().displayName + + storage.setJellyseerrAccount(displayName) + assertThat(storage.jellyseerrAccount.first()).isEqualTo(displayName) + } + + @Test + fun `test clearJellyseerrAccount`() = runTest { + storage = DataStorePreferenceStorage(fakeDataStore) + val displayName = JellyseerrAccountDetailsFactory.jellyseerr().displayName + + storage.setJellyseerrAccount(displayName) + assertThat(storage.jellyseerrAccount.first()).isEqualTo(displayName) + + storage.clearJellyseerrAccount() + assertThat(storage.jellyseerrAccount.first()).isEqualTo(null) + } + + @Test + fun `test testJellyseerrSignInMethod`() = runTest { + storage = DataStorePreferenceStorage(fakeDataStore) + assertThat(storage.jellyseerrSignInMethod.first()).isEqualTo(null) + + storage.setJellyseerrSignInMethod(JellyseerrLoginMethod.JELLYSEERR.name) + assertThat( + storage.jellyseerrSignInMethod.first(), + ).isEqualTo(JellyseerrLoginMethod.JELLYSEERR.name) + } + + @Test + fun `test clearJellyseerrSignInMethod`() = runTest { + storage = DataStorePreferenceStorage(fakeDataStore) + + storage.setJellyseerrSignInMethod(JellyseerrLoginMethod.JELLYSEERR.name) + assertThat( + storage.jellyseerrSignInMethod.first(), + ).isEqualTo(JellyseerrLoginMethod.JELLYSEERR.name) + + storage.clearJellyseerrSignInMethod() + assertThat(storage.jellyseerrSignInMethod.first()).isEqualTo(null) + } } diff --git a/app/src/test/kotlin/com/andreolas/movierama/session/SessionStorageTest.kt b/app/src/test/kotlin/com/andreolas/movierama/session/SessionStorageTest.kt index 556c7c3a..9ed92781 100644 --- a/app/src/test/kotlin/com/andreolas/movierama/session/SessionStorageTest.kt +++ b/app/src/test/kotlin/com/andreolas/movierama/session/SessionStorageTest.kt @@ -1,9 +1,11 @@ package com.andreolas.movierama.session import com.divinelink.core.datastore.SessionStorage +import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod import com.divinelink.core.testing.storage.FakeEncryptedPreferenceStorage import com.divinelink.core.testing.storage.FakePreferenceStorage import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest import org.junit.After import org.junit.Test @@ -125,4 +127,35 @@ class SessionStorageTest { assertThat(preferenceStorage.accountId.value).isEqualTo("account_id") } + + @Test + fun `test clearJellyseerrSession clears jellyseerr data`() = runTest { + val preferenceStorage = FakePreferenceStorage( + jellyseerrAccount = "Zabaob", + jellyseerrAddress = "http://localhost:5050", + jellyseerrSignInMethod = JellyseerrLoginMethod.JELLYSEERR.name, + ) + val encryptedPreferenceStorage = FakeEncryptedPreferenceStorage( + jellyseerrAuthCookie = "123456789qwertyuiop", + ) + + val sessionStorage = SessionStorage( + preferenceStorage, + encryptedPreferenceStorage, + ) + + assertThat(preferenceStorage.jellyseerrAccount.first()).isEqualTo("Zabaob") + assertThat(preferenceStorage.jellyseerrAddress.first()).isEqualTo("http://localhost:5050") + assertThat(preferenceStorage.jellyseerrSignInMethod.first()).isEqualTo( + JellyseerrLoginMethod.JELLYSEERR.name, + ) + assertThat(encryptedPreferenceStorage.jellyseerrAuthCookie).isEqualTo("123456789qwertyuiop") + + sessionStorage.clearJellyseerrSession() + + assertThat(preferenceStorage.jellyseerrAccount.first()).isNull() + assertThat(preferenceStorage.jellyseerrAddress.first()).isNull() + assertThat(preferenceStorage.jellyseerrSignInMethod.first()).isNull() + assertThat(encryptedPreferenceStorage.jellyseerrAuthCookie).isNull() + } } diff --git a/core/testing/build.gradle.kts b/core/testing/build.gradle.kts index 906c908a..2c4453ae 100644 --- a/core/testing/build.gradle.kts +++ b/core/testing/build.gradle.kts @@ -32,4 +32,10 @@ dependencies { implementation(libs.robolectric) implementation(libs.sqldelight.driver) + + implementation(libs.datastore) + implementation(libs.datastore.core) + implementation(libs.datastore.preferences) + implementation(libs.datastore.preferences.core) + implementation(libs.encrypted.preferences) } diff --git a/core/testing/src/main/kotlin/com/divinelink/core/testing/database/TestJellyseerrAccountDetailsQueries.kt b/core/testing/src/main/kotlin/com/divinelink/core/testing/database/TestJellyseerrAccountDetailsQueries.kt deleted file mode 100644 index d86f9dd1..00000000 --- a/core/testing/src/main/kotlin/com/divinelink/core/testing/database/TestJellyseerrAccountDetailsQueries.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.divinelink.core.testing.database - -import com.divinelink.core.database.JellyseerrAccountDetailsQueries -import org.mockito.kotlin.mock - -class TestJellyseerrAccountDetailsQueries { - val mock: JellyseerrAccountDetailsQueries = mock() -} diff --git a/core/testing/src/main/kotlin/com/divinelink/core/testing/datastore/TestDatastoreFactory.kt b/core/testing/src/main/kotlin/com/divinelink/core/testing/datastore/TestDatastoreFactory.kt new file mode 100644 index 00000000..2f16e795 --- /dev/null +++ b/core/testing/src/main/kotlin/com/divinelink/core/testing/datastore/TestDatastoreFactory.kt @@ -0,0 +1,33 @@ +package com.divinelink.core.testing.datastore + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.PreferenceDataStoreFactory +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.preferencesDataStoreFile +import androidx.test.platform.app.InstrumentationRegistry +import com.divinelink.core.testing.MainDispatcherRule +import kotlinx.coroutines.test.TestScope +import org.junit.Rule + +class TestDatastoreFactory { + + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + private val testDispatcher = mainDispatcherRule.testDispatcher + + private val testCoroutineScope = TestScope(testDispatcher) + + private val testContext: Context = InstrumentationRegistry.getInstrumentation().targetContext + + val datastore: DataStore = PreferenceDataStoreFactory.create( + scope = testCoroutineScope, + produceFile = { testContext.preferencesDataStoreFile(TEST_DATASTORE_NAME) }, + ) + + companion object { + private const val TEST_DATASTORE_NAME: String = "test_datastore" + + fun create(): DataStore = TestDatastoreFactory().datastore + } +} diff --git a/core/testing/src/main/kotlin/com/divinelink/core/testing/factories/storage/SessionStorageFactory.kt b/core/testing/src/main/kotlin/com/divinelink/core/testing/factories/storage/SessionStorageFactory.kt index f1d59200..4e1835b4 100644 --- a/core/testing/src/main/kotlin/com/divinelink/core/testing/factories/storage/SessionStorageFactory.kt +++ b/core/testing/src/main/kotlin/com/divinelink/core/testing/factories/storage/SessionStorageFactory.kt @@ -1,6 +1,7 @@ package com.divinelink.core.testing.factories.storage import com.divinelink.core.datastore.SessionStorage +import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod import com.divinelink.core.testing.storage.FakeEncryptedPreferenceStorage import com.divinelink.core.testing.storage.FakePreferenceStorage @@ -23,7 +24,13 @@ object SessionStorageFactory { fun full() = SessionStorage( storage = FakePreferenceStorage( accountId = "123456789", + jellyseerrAccount = "Zabaob", + jellyseerrAddress = "http://localhost:5050", + jellyseerrSignInMethod = JellyseerrLoginMethod.JELLYSEERR.name, + ), + encryptedStorage = FakeEncryptedPreferenceStorage( + sessionId = "123456789", + jellyseerrAuthCookie = "123456789qwertyuiop", ), - encryptedStorage = FakeEncryptedPreferenceStorage(sessionId = "123456789"), ) } diff --git a/core/testing/src/main/kotlin/com/divinelink/core/testing/usecase/FakeGetJellyseerrDetailsUseCase.kt b/core/testing/src/main/kotlin/com/divinelink/core/testing/usecase/FakeGetJellyseerrDetailsUseCase.kt index e0f3db47..e08c977d 100644 --- a/core/testing/src/main/kotlin/com/divinelink/core/testing/usecase/FakeGetJellyseerrDetailsUseCase.kt +++ b/core/testing/src/main/kotlin/com/divinelink/core/testing/usecase/FakeGetJellyseerrDetailsUseCase.kt @@ -23,7 +23,7 @@ class FakeGetJellyseerrDetailsUseCase { ) } - fun mockSuccess(response: Result) { + fun mockSuccess(response: Result) { whenever(mock.invoke(any())).thenReturn(flowOf(response)) } } diff --git a/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTest.kt b/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTest.kt index 0c09e7bb..8fc47a62 100644 --- a/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTest.kt +++ b/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTest.kt @@ -196,6 +196,21 @@ class JellyseerrSettingsViewModelTest { ) } + @Test + fun `test getJellyseerrAccount with null clear logged in data`() = runTest { + testRobot + .mockJellyseerrAccountDetailsResponse(Result.success(null)) + .buildViewModel() + .assertUiState( + createUiState( + jellyseerrState = JellyseerrState.Initial( + isLoading = false, + address = "", + ), + ), + ) + } + private fun createUiState( snackbarMessage: SnackbarMessage? = null, jellyseerrState: JellyseerrState = JellyseerrState.Initial( diff --git a/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTestRobot.kt b/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTestRobot.kt index 2437a516..3f11cc10 100644 --- a/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTestRobot.kt +++ b/feature/settings/src/test/kotlin/com/divinelink/feature/settings/app/account/jellyseerr/JellyseerrSettingsViewModelTestRobot.kt @@ -29,6 +29,10 @@ class JellyseerrSettingsViewModelTestRobot : ViewModelTestRobot) = apply { + getJellyseerrDetailsUseCase.mockSuccess(response) + } + fun onUserAddressChange(address: String) = apply { viewModel.onJellyseerrInteraction(JellyseerrInteraction.OnAddressChange(address)) }