mirror of
https://github.com/LiveFastEatTrashRaccoon/RaccoonForLemmy.git
synced 2025-02-09 04:38:42 +01:00
chore: domain identity tests (#633)
This commit is contained in:
parent
577667347a
commit
3b58d89b30
@ -43,9 +43,12 @@ kotlin {
|
|||||||
implementation(projects.domain.lemmy.data)
|
implementation(projects.domain.lemmy.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val commonTest by getting {
|
val androidUnitTest by getting {
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(kotlin("test"))
|
implementation(libs.kotlinx.coroutines.test)
|
||||||
|
implementation(kotlin("test-junit"))
|
||||||
|
implementation(libs.mockk)
|
||||||
|
implementation(projects.core.testutils)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.provider.ServiceProvider
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.TemporaryKeyStore
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class DefaultApiConfigurationRepositoryTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
private val serviceProvider = mockk<ServiceProvider>(relaxUnitFun = true) {
|
||||||
|
every { currentInstance } returns "lemmy.world"
|
||||||
|
}
|
||||||
|
private val keyStore = mockk<TemporaryKeyStore>(relaxUnitFun = true) {
|
||||||
|
every { get(any(), any<String>()) } returns ""
|
||||||
|
}
|
||||||
|
private val sut = DefaultApiConfigurationRepository(
|
||||||
|
serviceProvider = serviceProvider,
|
||||||
|
keyStore = keyStore,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenChangeInstance_thenValueIsUpdated() {
|
||||||
|
val resBefore = sut.instance.value
|
||||||
|
assertEquals("", resBefore)
|
||||||
|
|
||||||
|
sut.changeInstance("feddit.it")
|
||||||
|
|
||||||
|
verify {
|
||||||
|
serviceProvider.changeInstance("feddit.it")
|
||||||
|
keyStore.save(any(), "feddit.it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.dto.LoginForm
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.dto.LoginResponse
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.provider.ServiceProvider
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.service.AuthService
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import de.jensklingenberg.ktorfit.Response
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class DefaultAuthRepositoryTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
private val authService = mockk<AuthService>()
|
||||||
|
private val serviceProvider = mockk<ServiceProvider>(relaxUnitFun = true) {
|
||||||
|
every { auth } returns authService
|
||||||
|
}
|
||||||
|
|
||||||
|
private val sut = DefaultAuthRepository(
|
||||||
|
services = serviceProvider,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenLogin_thenResultIsAsExpected() = runTest {
|
||||||
|
val loginData = LoginResponse(token = "", registrationCreated = false, verifyEmailSent = true)
|
||||||
|
val fakeResponse = mockk<Response<LoginResponse>> {
|
||||||
|
every { isSuccessful } returns true
|
||||||
|
every { body() } returns loginData
|
||||||
|
}
|
||||||
|
coEvery { authService.login(any()) } returns fakeResponse
|
||||||
|
val res = sut.login("username", "password")
|
||||||
|
|
||||||
|
assertTrue(res.isSuccess)
|
||||||
|
|
||||||
|
val resultData = res.getOrThrow()
|
||||||
|
assertEquals(loginData, resultData)
|
||||||
|
coVerify {
|
||||||
|
authService.login(LoginForm("username", "password"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.AccountModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.utils.network.NetworkManager
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.UserModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository
|
||||||
|
import io.mockk.Called
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
|
||||||
|
class DefaultIdentityRepositoryTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
private val accountRepository = mockk<AccountRepository>()
|
||||||
|
private val userRepository = mockk<UserRepository> {
|
||||||
|
coEvery { getModeratedCommunities(any(), any()) } returns emptyList()
|
||||||
|
}
|
||||||
|
private val siteRepository = mockk<SiteRepository>()
|
||||||
|
private val networkManager = mockk<NetworkManager> {
|
||||||
|
coEvery { isNetworkAvailable() } returns true
|
||||||
|
}
|
||||||
|
|
||||||
|
private val sut = DefaultIdentityRepository(
|
||||||
|
accountRepository = accountRepository,
|
||||||
|
userRepository = userRepository,
|
||||||
|
siteRepository = siteRepository,
|
||||||
|
networkManager = networkManager,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNetworkAvailableAndUserLogged_whenStartup_thenResultIsAsExpected() = runTest {
|
||||||
|
val fakeToken = "fake-token"
|
||||||
|
val fakeAccount = mockk<AccountModel> {
|
||||||
|
every { jwt } returns fakeToken
|
||||||
|
}
|
||||||
|
val fakeUser = UserModel(id = 1)
|
||||||
|
coEvery { accountRepository.getActive() } returns fakeAccount
|
||||||
|
coEvery { siteRepository.getCurrentUser(any()) } returns fakeUser
|
||||||
|
|
||||||
|
sut.startup()
|
||||||
|
|
||||||
|
val token = sut.authToken.value
|
||||||
|
assertEquals(fakeToken, token)
|
||||||
|
val isLogged = sut.isLogged.value
|
||||||
|
assertTrue(isLogged == true)
|
||||||
|
assertEquals(fakeUser, sut.cachedUser)
|
||||||
|
|
||||||
|
coVerify {
|
||||||
|
accountRepository.getActive()
|
||||||
|
networkManager.isNetworkAvailable()
|
||||||
|
siteRepository.getCurrentUser("fake-token")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNetworkUnavailable_whenStartup_thenResultIsAsExpected() = runTest {
|
||||||
|
coEvery { networkManager.isNetworkAvailable() } returns false
|
||||||
|
val fakeToken = "fake-token"
|
||||||
|
val fakeAccount = mockk<AccountModel> {
|
||||||
|
every { jwt } returns fakeToken
|
||||||
|
}
|
||||||
|
val fakeUser = UserModel(id = 1)
|
||||||
|
coEvery { accountRepository.getActive() } returns fakeAccount
|
||||||
|
coEvery { siteRepository.getCurrentUser(any()) } returns fakeUser
|
||||||
|
|
||||||
|
sut.startup()
|
||||||
|
|
||||||
|
val token = sut.authToken.value
|
||||||
|
assertEquals(fakeToken, token)
|
||||||
|
val isLogged = sut.isLogged.value
|
||||||
|
assertNull(isLogged)
|
||||||
|
|
||||||
|
coVerify {
|
||||||
|
accountRepository.getActive()
|
||||||
|
networkManager.isNetworkAvailable()
|
||||||
|
siteRepository wasNot Called
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNotUserLogged_whenStartup_thenResultIsAsExpected() = runTest {
|
||||||
|
coEvery { accountRepository.getActive() } returns null
|
||||||
|
|
||||||
|
sut.startup()
|
||||||
|
|
||||||
|
val token = sut.authToken.value
|
||||||
|
assertEquals("", token)
|
||||||
|
val isLogged = sut.isLogged.value
|
||||||
|
assertTrue(isLogged == false)
|
||||||
|
assertNull(sut.cachedUser)
|
||||||
|
|
||||||
|
coVerify {
|
||||||
|
accountRepository.getActive()
|
||||||
|
networkManager wasNot Called
|
||||||
|
siteRepository wasNot Called
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.usecase
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.AccountModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DefaultDeleteAccountUseCaseTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
|
||||||
|
private val accountRepository = mockk<AccountRepository>(relaxUnitFun = true)
|
||||||
|
private val sut = DefaultDeleteAccountUseCase(
|
||||||
|
accountRepository = accountRepository,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenExecute_thenInteractionsAreAsExpected() = runTest {
|
||||||
|
val account = AccountModel(id = 1, username = "test", jwt = "fake-token", instance = "test")
|
||||||
|
sut(account)
|
||||||
|
|
||||||
|
coVerify { accountRepository.delete(1) }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,183 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.usecase
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.dto.LoginResponse
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.AccountModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.SettingsModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.CommunitySortRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.ApiConfigurationRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.AuthRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class DefaultLoginUseCaseTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
|
||||||
|
private val authRepository = mockk<AuthRepository>()
|
||||||
|
private val apiConfigurationRepository = mockk<ApiConfigurationRepository>(relaxUnitFun = true) {
|
||||||
|
every { instance } returns MutableStateFlow("")
|
||||||
|
}
|
||||||
|
private val identityRepository = mockk<IdentityRepository>(relaxUnitFun = true)
|
||||||
|
private val accountRepository = mockk<AccountRepository>(relaxUnitFun = true)
|
||||||
|
private val settingsRepository = mockk<SettingsRepository>(relaxUnitFun = true)
|
||||||
|
private val siteRepository = mockk<SiteRepository>(relaxUnitFun = true)
|
||||||
|
private val communitySortRepository = mockk<CommunitySortRepository>(relaxUnitFun = true)
|
||||||
|
private val sut = DefaultLoginUseCase(
|
||||||
|
authRepository = authRepository,
|
||||||
|
apiConfigurationRepository = apiConfigurationRepository,
|
||||||
|
identityRepository = identityRepository,
|
||||||
|
accountRepository = accountRepository,
|
||||||
|
settingsRepository = settingsRepository,
|
||||||
|
siteRepository = siteRepository,
|
||||||
|
communitySortRepository = communitySortRepository,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNewAccount_whenExecute_thenInteractionsAreAsExpected() = runTest {
|
||||||
|
val accountId = 1L
|
||||||
|
val anonymousSettings = SettingsModel()
|
||||||
|
coEvery {
|
||||||
|
authRepository.login(any(), any())
|
||||||
|
} returns Result.success(LoginResponse("fake-token", registrationCreated = false, verifyEmailSent = true))
|
||||||
|
coEvery {
|
||||||
|
siteRepository.getAccountSettings(any())
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getBy(any(), any())
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getActive()
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.createAccount(any())
|
||||||
|
} returns accountId
|
||||||
|
coEvery {
|
||||||
|
settingsRepository.getSettings(any())
|
||||||
|
} returns anonymousSettings
|
||||||
|
|
||||||
|
val res = sut("fake-instance", "fake-username", "fake-password")
|
||||||
|
|
||||||
|
assertTrue(res.isSuccess)
|
||||||
|
coVerify {
|
||||||
|
apiConfigurationRepository.changeInstance("fake-instance")
|
||||||
|
authRepository.login("fake-username", "fake-password")
|
||||||
|
siteRepository.getAccountSettings("fake-token")
|
||||||
|
accountRepository.getBy("fake-username", "fake-instance")
|
||||||
|
accountRepository.createAccount(withArg {
|
||||||
|
assertEquals("fake-username", it.username)
|
||||||
|
})
|
||||||
|
accountRepository.setActive(accountId, true)
|
||||||
|
settingsRepository.createSettings(anonymousSettings, accountId)
|
||||||
|
settingsRepository.changeCurrentSettings(anonymousSettings)
|
||||||
|
communitySortRepository.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenPreviousDifferentAccount_whenExecute_thenInteractionsAreAsExpected() = runTest {
|
||||||
|
val accountId = 2L
|
||||||
|
val oldAccountId = 1L
|
||||||
|
val anonymousSettings = SettingsModel()
|
||||||
|
coEvery {
|
||||||
|
authRepository.login(any(), any())
|
||||||
|
} returns Result.success(LoginResponse("fake-token", registrationCreated = false, verifyEmailSent = true))
|
||||||
|
coEvery {
|
||||||
|
siteRepository.getAccountSettings(any())
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getBy(any(), any())
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getActive()
|
||||||
|
} returns AccountModel(
|
||||||
|
id = oldAccountId,
|
||||||
|
username = "old-username",
|
||||||
|
instance = "old-instance",
|
||||||
|
jwt = "old-token"
|
||||||
|
)
|
||||||
|
coEvery {
|
||||||
|
accountRepository.createAccount(any())
|
||||||
|
} returns accountId
|
||||||
|
coEvery {
|
||||||
|
settingsRepository.getSettings(any())
|
||||||
|
} returns anonymousSettings
|
||||||
|
|
||||||
|
val res = sut("fake-instance", "fake-username", "fake-password")
|
||||||
|
|
||||||
|
assertTrue(res.isSuccess)
|
||||||
|
coVerify {
|
||||||
|
apiConfigurationRepository.changeInstance("fake-instance")
|
||||||
|
authRepository.login("fake-username", "fake-password")
|
||||||
|
siteRepository.getAccountSettings("fake-token")
|
||||||
|
accountRepository.getBy("fake-username", "fake-instance")
|
||||||
|
accountRepository.createAccount(withArg {
|
||||||
|
assertEquals("fake-username", it.username)
|
||||||
|
})
|
||||||
|
accountRepository.setActive(oldAccountId, false)
|
||||||
|
accountRepository.setActive(accountId, true)
|
||||||
|
settingsRepository.createSettings(anonymousSettings, accountId)
|
||||||
|
settingsRepository.changeCurrentSettings(anonymousSettings)
|
||||||
|
communitySortRepository.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenPreviousExistingAccount_whenExecute_thenInteractionsAreAsExpected() = runTest {
|
||||||
|
val accountId = 2L
|
||||||
|
val oldSettings = SettingsModel(id = 1)
|
||||||
|
coEvery {
|
||||||
|
authRepository.login(any(), any())
|
||||||
|
} returns Result.success(LoginResponse("fake-token", registrationCreated = false, verifyEmailSent = true))
|
||||||
|
coEvery {
|
||||||
|
siteRepository.getAccountSettings(any())
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getBy(any(), any())
|
||||||
|
} returns AccountModel(
|
||||||
|
id = accountId,
|
||||||
|
username = "old-username",
|
||||||
|
instance = "old-instance",
|
||||||
|
jwt = "old-token"
|
||||||
|
)
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getActive()
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.createAccount(any())
|
||||||
|
} returns accountId
|
||||||
|
coEvery {
|
||||||
|
settingsRepository.getSettings(accountId)
|
||||||
|
} returns oldSettings
|
||||||
|
|
||||||
|
val res = sut("fake-instance", "fake-username", "fake-password")
|
||||||
|
|
||||||
|
assertTrue(res.isSuccess)
|
||||||
|
coVerify {
|
||||||
|
apiConfigurationRepository.changeInstance("fake-instance")
|
||||||
|
authRepository.login("fake-username", "fake-password")
|
||||||
|
siteRepository.getAccountSettings("fake-token")
|
||||||
|
accountRepository.getBy("fake-username", "fake-instance")
|
||||||
|
accountRepository.setActive(accountId, true)
|
||||||
|
settingsRepository.changeCurrentSettings(oldSettings)
|
||||||
|
communitySortRepository.clear()
|
||||||
|
}
|
||||||
|
coVerify(inverse = true) {
|
||||||
|
accountRepository.createAccount(any())
|
||||||
|
settingsRepository.createSettings(any(), any())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.usecase
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenter
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterEvent
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.AccountModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.SettingsModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.CommunitySortRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DefaultLogoutUseCaseTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
private val identityRepository = mockk<IdentityRepository>(relaxUnitFun = true)
|
||||||
|
private val accountRepository = mockk<AccountRepository>(relaxUnitFun = true)
|
||||||
|
private val settingsRepository = mockk<SettingsRepository>(relaxUnitFun = true)
|
||||||
|
private val notificationCenter = mockk<NotificationCenter>(relaxUnitFun = true)
|
||||||
|
private val communitySortRepository = mockk<CommunitySortRepository>(relaxUnitFun = true)
|
||||||
|
private val sut = DefaultLogoutUseCase(
|
||||||
|
identityRepository = identityRepository,
|
||||||
|
accountRepository = accountRepository,
|
||||||
|
settingsRepository = settingsRepository,
|
||||||
|
notificationCenter = notificationCenter,
|
||||||
|
communitySortRepository = communitySortRepository,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenNewAccount_whenExecute_thenInteractionsAreAsExpected() = runTest {
|
||||||
|
val accountId = 1L
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getActive()
|
||||||
|
} returns AccountModel(
|
||||||
|
id = accountId,
|
||||||
|
instance = "fake-instance",
|
||||||
|
username = "fake-username",
|
||||||
|
jwt = "fake-token",
|
||||||
|
)
|
||||||
|
val anonymousSettings = SettingsModel()
|
||||||
|
coEvery {
|
||||||
|
settingsRepository.getSettings(any())
|
||||||
|
} returns anonymousSettings
|
||||||
|
|
||||||
|
sut()
|
||||||
|
|
||||||
|
coVerify {
|
||||||
|
notificationCenter.send(ofType(NotificationCenterEvent.ResetHome::class))
|
||||||
|
notificationCenter.send(ofType(NotificationCenterEvent.ResetExplore::class))
|
||||||
|
notificationCenter.send(ofType(NotificationCenterEvent.Logout::class))
|
||||||
|
identityRepository.clearToken()
|
||||||
|
accountRepository.setActive(accountId, false)
|
||||||
|
settingsRepository.changeCurrentSettings(anonymousSettings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.usecase
|
||||||
|
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.api.provider.ServiceProvider
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenter
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterEvent
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.AccountModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.data.SettingsModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.CommunitySortRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.core.testutils.DispatcherTestRule
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DefaultSwitchAccountUseCaseTest {
|
||||||
|
@get:Rule
|
||||||
|
val dispatcherTestRule = DispatcherTestRule()
|
||||||
|
|
||||||
|
|
||||||
|
private val identityRepository = mockk<IdentityRepository>(relaxUnitFun = true)
|
||||||
|
private val accountRepository = mockk<AccountRepository>(relaxUnitFun = true)
|
||||||
|
private val settingsRepository = mockk<SettingsRepository>(relaxUnitFun = true)
|
||||||
|
private val serviceProvider = mockk<ServiceProvider>(relaxUnitFun = true)
|
||||||
|
private val notificationCenter = mockk<NotificationCenter>(relaxUnitFun = true)
|
||||||
|
private val communitySortRepository = mockk<CommunitySortRepository>(relaxUnitFun = true)
|
||||||
|
private val sut = DefaultSwitchAccountUseCase(
|
||||||
|
identityRepository = identityRepository,
|
||||||
|
accountRepository = accountRepository,
|
||||||
|
settingsRepository = settingsRepository,
|
||||||
|
serviceProvider = serviceProvider,
|
||||||
|
notificationCenter = notificationCenter,
|
||||||
|
communitySortRepository = communitySortRepository,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenExecute_thenInteractionsAreAsExpected() = runTest {
|
||||||
|
val accountId = 2L
|
||||||
|
val oldAccountId = 1L
|
||||||
|
val newAccount = AccountModel(
|
||||||
|
id = accountId,
|
||||||
|
username = "new-username",
|
||||||
|
instance = "new-instance",
|
||||||
|
jwt = "new-token"
|
||||||
|
)
|
||||||
|
val oldAccount = AccountModel(
|
||||||
|
id = oldAccountId,
|
||||||
|
username = "old-username",
|
||||||
|
instance = "old-instance",
|
||||||
|
jwt = "old-token"
|
||||||
|
)
|
||||||
|
val oldSettings = SettingsModel(id = 1)
|
||||||
|
val newSettings = SettingsModel(id = 2)
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getBy(any(), any())
|
||||||
|
} returns null
|
||||||
|
coEvery {
|
||||||
|
accountRepository.getActive()
|
||||||
|
} returns oldAccount
|
||||||
|
coEvery {
|
||||||
|
settingsRepository.getSettings(oldAccountId)
|
||||||
|
} returns oldSettings
|
||||||
|
coEvery {
|
||||||
|
settingsRepository.getSettings(accountId)
|
||||||
|
} returns newSettings
|
||||||
|
|
||||||
|
sut(newAccount)
|
||||||
|
|
||||||
|
coVerify {
|
||||||
|
accountRepository.setActive(oldAccountId, false)
|
||||||
|
accountRepository.setActive(accountId, true)
|
||||||
|
settingsRepository.getSettings(accountId)
|
||||||
|
settingsRepository.changeCurrentSettings(newSettings)
|
||||||
|
communitySortRepository.clear()
|
||||||
|
notificationCenter.send(ofType(NotificationCenterEvent.Logout::class))
|
||||||
|
identityRepository.storeToken("new-token")
|
||||||
|
identityRepository.refreshLoggedState()
|
||||||
|
serviceProvider.changeInstance("new-instance")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -50,44 +50,43 @@ include(":feature:search")
|
|||||||
include(":feature:settings")
|
include(":feature:settings")
|
||||||
|
|
||||||
include(":unit:about")
|
include(":unit:about")
|
||||||
|
include(":unit:accountsettings")
|
||||||
include(":unit:ban")
|
include(":unit:ban")
|
||||||
include(":unit:chat")
|
include(":unit:chat")
|
||||||
|
include(":unit:choosecolor")
|
||||||
|
include(":unit:choosefont")
|
||||||
include(":unit:communitydetail")
|
include(":unit:communitydetail")
|
||||||
include(":unit:communityinfo")
|
include(":unit:communityinfo")
|
||||||
|
include(":unit:configurecontentview")
|
||||||
|
include(":unit:configureswipeactions")
|
||||||
include(":unit:createcomment")
|
include(":unit:createcomment")
|
||||||
include(":unit:createpost")
|
include(":unit:createpost")
|
||||||
include(":unit:createreport")
|
include(":unit:createreport")
|
||||||
|
include(":unit:drafts")
|
||||||
include(":unit:drawer")
|
include(":unit:drawer")
|
||||||
|
include(":unit:editcommunity")
|
||||||
|
include(":unit:explore")
|
||||||
|
include(":unit:filteredcontents")
|
||||||
include(":unit:instanceinfo")
|
include(":unit:instanceinfo")
|
||||||
include(":unit:login")
|
include(":unit:login")
|
||||||
include(":unit:manageaccounts")
|
include(":unit:manageaccounts")
|
||||||
|
include(":unit:manageban")
|
||||||
include(":unit:managesubscriptions")
|
include(":unit:managesubscriptions")
|
||||||
|
include(":unit:mentions")
|
||||||
|
include(":unit:messages")
|
||||||
|
include(":unit:modlog")
|
||||||
include(":unit:multicommunity")
|
include(":unit:multicommunity")
|
||||||
include(":unit:myaccount")
|
include(":unit:myaccount")
|
||||||
include(":unit:postdetail")
|
include(":unit:postdetail")
|
||||||
include(":unit:postlist")
|
include(":unit:postlist")
|
||||||
|
include(":unit:rawcontent")
|
||||||
include(":unit:remove")
|
include(":unit:remove")
|
||||||
|
include(":unit:replies")
|
||||||
include(":unit:reportlist")
|
include(":unit:reportlist")
|
||||||
include(":unit:saveditems")
|
include(":unit:saveditems")
|
||||||
include(":unit:selectcommunity")
|
include(":unit:selectcommunity")
|
||||||
|
include(":unit:selectinstance")
|
||||||
include(":unit:userdetail")
|
include(":unit:userdetail")
|
||||||
|
include(":unit:userinfo")
|
||||||
include(":unit:web")
|
include(":unit:web")
|
||||||
include(":unit:zoomableimage")
|
include(":unit:zoomableimage")
|
||||||
include(":unit:replies")
|
|
||||||
include(":unit:mentions")
|
|
||||||
include(":unit:messages")
|
|
||||||
include(":unit:modlog")
|
|
||||||
include(":unit:userinfo")
|
|
||||||
include(":unit:rawcontent")
|
|
||||||
include(":unit:accountsettings")
|
|
||||||
include(":unit:manageban")
|
|
||||||
include(":unit:choosecolor")
|
|
||||||
include(":unit:selectinstance")
|
|
||||||
include(":unit:choosefont")
|
|
||||||
include(":unit:choosecolor")
|
|
||||||
include(":unit:configureswipeactions")
|
|
||||||
include(":unit:configurecontentview")
|
|
||||||
include(":unit:drafts")
|
|
||||||
include(":unit:filteredcontents")
|
|
||||||
include(":unit:explore")
|
|
||||||
include(":unit:editcommunity")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user