Skip to content

Commit

Permalink
test: add jellyseerr use case tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Divinelink committed Jul 10, 2024
1 parent 5a51fd0 commit 39729a7
Show file tree
Hide file tree
Showing 8 changed files with 511 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import javax.inject.Singleton

@Singleton
class SessionStorage @Inject constructor(
private val storage: PreferenceStorage,
private val encryptedStorage: EncryptedStorage,
val storage: PreferenceStorage,
val encryptedStorage: EncryptedStorage,
) {
val sessionId: String?
get() = encryptedStorage.sessionId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.divinelink.core.domain.jellyseerr
import com.divinelink.core.commons.di.IoDispatcher
import com.divinelink.core.commons.domain.FlowUseCase
import com.divinelink.core.data.jellyseerr.repository.JellyseerrRepository
import com.divinelink.core.datastore.PreferenceStorage
import com.divinelink.core.datastore.SessionStorage
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
Expand All @@ -14,13 +13,12 @@ import javax.inject.Inject

open class LogoutJellyseerrUseCase @Inject constructor(
private val repository: JellyseerrRepository,
private val storage: PreferenceStorage,
private val sessionStorage: SessionStorage,
@IoDispatcher val dispatcher: CoroutineDispatcher,
) : FlowUseCase<Unit, String>(dispatcher) {

override fun execute(parameters: Unit): Flow<Result<String>> = flow {
val address = storage.jellyseerrAddress.first()
val address = sessionStorage.storage.jellyseerrAddress.first()
if (address == null) {
emit(Result.failure(Exception("No address found.")))
return@flow
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.divinelink.core.domain.jellyseerr

import com.divinelink.core.commons.domain.data
import com.divinelink.core.datastore.PreferenceStorage
import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod
import com.divinelink.core.testing.MainDispatcherRule
import com.divinelink.core.testing.factories.model.jellyseerr.JellyseerrAccountDetailsFactory
import com.divinelink.core.testing.repository.TestJellyseerrRepository
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.Rule
import org.junit.Test

class GetJellyseerrDetailsUseCaseTest {

private lateinit var preferenceStorage: PreferenceStorage

private val repository = TestJellyseerrRepository()

@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val testDispatcher = mainDispatcherRule.testDispatcher

@Test
fun `test get jellyseerr account details with null storage data`() = runTest {
preferenceStorage = FakePreferenceStorage()

repository.mockGetJellyseerrAccountDetails(JellyseerrAccountDetailsFactory.jellyseerr())

val useCase = GetJellyseerrDetailsUseCase(
storage = preferenceStorage,
repository = repository.mock,
dispatcher = testDispatcher,
)

val result = useCase.invoke(Unit).first()

assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).isInstanceOf(Exception::class.java)
}

@Test
fun `test get jellyseerr account details with storage data`() = runTest {
preferenceStorage = FakePreferenceStorage(
jellyseerrAddress = "http://localhost:8096",
jellyseerrAccount = "jellyseerrAccount",
jellyseerrSignInMethod = JellyseerrLoginMethod.JELLYSEERR.name,
)

repository.mockGetJellyseerrAccountDetails(JellyseerrAccountDetailsFactory.jellyseerr())

val useCase = GetJellyseerrDetailsUseCase(
storage = preferenceStorage,
repository = repository.mock,
dispatcher = testDispatcher,
)

val result = useCase.invoke(Unit).first()

assertThat(result.isSuccess).isTrue()
assertThat(result.data).isEqualTo(JellyseerrAccountDetailsFactory.jellyseerr())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.divinelink.core.domain.jellyseerr

import com.divinelink.core.datastore.PreferenceStorage
import com.divinelink.core.model.Password
import com.divinelink.core.model.Username
import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod
import com.divinelink.core.model.jellyseerr.JellyseerrLoginParams
import com.divinelink.core.testing.MainDispatcherRule
import com.divinelink.core.testing.factories.model.jellyseerr.JellyseerrAccountDetailsFactory
import com.divinelink.core.testing.repository.TestJellyseerrRepository
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.Rule
import org.junit.Test

class LoginJellyseerrUseCaseTest {

private lateinit var preferenceStorage: PreferenceStorage

private val repository = TestJellyseerrRepository()

@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val testDispatcher = mainDispatcherRule.testDispatcher

@Test
fun `test loginJellyseerr with null parameters throws exception`() = runTest {
preferenceStorage = FakePreferenceStorage()

val useCase = LoginJellyseerrUseCase(
repository = repository.mock,
storage = preferenceStorage,
dispatcher = testDispatcher,
)

useCase.invoke(null).collect {
assertThat(it.isFailure).isTrue()
assertThat(it.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java)
}
}

@Test
fun `test loginJellyseerr with Jellyfin login method`() = runTest {
preferenceStorage = FakePreferenceStorage()

repository.mockSignInWithJellyfin(Result.success(JellyseerrAccountDetailsFactory.jellyfin()))

val useCase = LoginJellyseerrUseCase(
repository = repository.mock,
storage = preferenceStorage,
dispatcher = testDispatcher,
)

useCase.invoke(
JellyseerrLoginParams(
username = Username("jellyfinUsername"),
password = Password("password"),
address = "http://localhost:8096",
signInMethod = JellyseerrLoginMethod.JELLYFIN,
),
).collect {
assertThat(it.isSuccess).isTrue()
assertThat(it.getOrNull()).isEqualTo(JellyseerrAccountDetailsFactory.jellyfin())
assertThat(preferenceStorage.jellyseerrAccount.first()).isEqualTo("jellyfinUsername")
assertThat(preferenceStorage.jellyseerrAddress.first()).isEqualTo("http://localhost:8096")
assertThat(
preferenceStorage.jellyseerrSignInMethod.first(),
).isEqualTo(JellyseerrLoginMethod.JELLYFIN.name)
}
}

@Test
fun `test loginJellyseerr with Jellyseerr login method`() = runTest {
preferenceStorage = FakePreferenceStorage()

repository.mockSignInWithJellyseerr(
Result.success(JellyseerrAccountDetailsFactory.jellyseerr()),
)

val useCase = LoginJellyseerrUseCase(
repository = repository.mock,
storage = preferenceStorage,
dispatcher = testDispatcher,
)

useCase.invoke(
JellyseerrLoginParams(
username = Username("jellyseerrUsername"),
password = Password("password"),
address = "http://localhost:8096",
signInMethod = JellyseerrLoginMethod.JELLYSEERR,
),
).collect {
assertThat(it.isSuccess).isTrue()
assertThat(it.getOrNull()).isEqualTo(JellyseerrAccountDetailsFactory.jellyseerr())
assertThat(preferenceStorage.jellyseerrAccount.first()).isEqualTo("jellyseerrUsername")
assertThat(preferenceStorage.jellyseerrAddress.first()).isEqualTo("http://localhost:8096")
assertThat(
preferenceStorage.jellyseerrSignInMethod.first(),
).isEqualTo(JellyseerrLoginMethod.JELLYSEERR.name)
}
}

@Test
fun `test loginJellyseerr with Jellyseerr login method and error`() = runTest {
preferenceStorage = FakePreferenceStorage()

repository.mockSignInWithJellyseerr(Result.failure(Exception("error")))

val useCase = LoginJellyseerrUseCase(
repository = repository.mock,
storage = preferenceStorage,
dispatcher = testDispatcher,
)

useCase.invoke(
JellyseerrLoginParams(
username = Username("jellyseerrUsername"),
password = Password("password"),
address = "http://localhost:8096",
signInMethod = JellyseerrLoginMethod.JELLYSEERR,
),
).collect {
assertThat(it.isFailure).isTrue()
}
}

@Test
fun `test loginJellyseerr with Jellyfin login method and error`() = runTest {
preferenceStorage = FakePreferenceStorage()

repository.mockSignInWithJellyfin(Result.failure(Exception("error")))

val useCase = LoginJellyseerrUseCase(
repository = repository.mock,
storage = preferenceStorage,
dispatcher = testDispatcher,
)

useCase.invoke(
JellyseerrLoginParams(
username = Username("jellyfinUsername"),
password = Password("password"),
address = "http://localhost:8096",
signInMethod = JellyseerrLoginMethod.JELLYFIN,
),
).collect {
assertThat(it.isFailure).isTrue()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.divinelink.core.domain.jellyseerr

import com.divinelink.core.datastore.SessionStorage
import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod
import com.divinelink.core.testing.MainDispatcherRule
import com.divinelink.core.testing.factories.storage.SessionStorageFactory
import com.divinelink.core.testing.factories.storage.SessionStorageFactory.toWzd
import com.divinelink.core.testing.repository.TestJellyseerrRepository
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test

class LogoutJellyseerrUseCaseTest {

private lateinit var sessionStorage: SessionStorage

private val repository = TestJellyseerrRepository()

@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val testDispatcher = mainDispatcherRule.testDispatcher

@Test
fun `test logout jellyseerr with null address`() = runTest {
sessionStorage = SessionStorageFactory.empty()

val useCase = LogoutJellyseerrUseCase(
repository = repository.mock,
sessionStorage = sessionStorage,
dispatcher = testDispatcher,
)

val result = useCase.invoke(Unit).first()

assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).isInstanceOf(Exception::class.java)
assertThat(result.exceptionOrNull()?.message).isEqualTo("No address found.")
}

@Test
fun `test logout jellyseerr with success`() = runTest {
sessionStorage = SessionStorageFactory.empty().toWzd {
withJellyseerrAddress("http://localhost:8096")
}

repository.mockLogout(Result.success(Unit))

val useCase = LogoutJellyseerrUseCase(
repository = repository.mock,
sessionStorage = sessionStorage,
dispatcher = testDispatcher,
)

val result = useCase.invoke(Unit).first()

assertThat(result.isSuccess).isTrue()
assertThat(result.getOrNull()).isEqualTo("http://localhost:8096")
}

@Test
fun `test logout jellyseerr with success clear jellyseerr session`() = runTest {
sessionStorage = SessionStorageFactory.empty().toWzd {
withJellyseerrAddress("http://localhost:8096")
withJellyseerrAccount("jellyseerrAccount")
withJellyseerrSignInMethod(JellyseerrLoginMethod.JELLYSEERR.name)
withJellyseerrAuthCookie("jellyseerrAuthCookie")
}

assertThat(sessionStorage.storage.jellyseerrAddress.first()).isEqualTo("http://localhost:8096")
assertThat(sessionStorage.storage.jellyseerrAccount.first()).isEqualTo("jellyseerrAccount")
assertThat(sessionStorage.storage.jellyseerrSignInMethod.first()).isEqualTo(
JellyseerrLoginMethod.JELLYSEERR.name,
)
assertThat(sessionStorage.encryptedStorage.jellyseerrAuthCookie).isEqualTo(
"jellyseerrAuthCookie",
)

repository.mockLogout(Result.success(Unit))

val useCase = LogoutJellyseerrUseCase(
repository = repository.mock,
sessionStorage = sessionStorage,
dispatcher = testDispatcher,
)

val result = useCase.invoke(Unit).first()

assertThat(result.isSuccess).isTrue()

repository.verifyClearJellyseerrAccountDetails()

assertThat(sessionStorage.storage.jellyseerrAddress.first()).isNull()
assertThat(sessionStorage.storage.jellyseerrAccount.first()).isNull()
assertThat(sessionStorage.storage.jellyseerrSignInMethod.first()).isNull()
assertThat(sessionStorage.encryptedStorage.jellyseerrAuthCookie).isNull()
}

@Test
fun `test logout jellyseerr with failure`() = runTest {
sessionStorage = SessionStorageFactory.empty().toWzd {
withJellyseerrAddress("http://localhost:8096")
}

repository.mockLogout(Result.failure(Exception("Logout failed.")))

val useCase = LogoutJellyseerrUseCase(
repository = repository.mock,
sessionStorage = sessionStorage,
dispatcher = testDispatcher,
)

val result = useCase.invoke(Unit).first()

assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).isInstanceOf(Exception::class.java)
assertThat(result.exceptionOrNull()?.message).isEqualTo("Logout failed.")
}
}
Loading

0 comments on commit 39729a7

Please sign in to comment.