2024-01-22 18:08:28 +03:00
|
|
|
package com.x8bit.bitwarden
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.CaptchaCallbackTokenResult
|
2024-02-21 01:55:23 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.DuoCallbackTokenResult
|
2024-01-23 06:29:03 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.SsoCallbackResult
|
2024-01-22 18:08:28 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.getCaptchaCallbackTokenResult
|
2024-02-21 01:55:23 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.getDuoCallbackTokenResult
|
2024-01-23 06:29:03 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.getSsoCallbackResult
|
2024-02-15 23:54:46 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.util.YubiKeyResult
|
|
|
|
import com.x8bit.bitwarden.data.auth.util.getYubiKeyResultOrNull
|
2024-01-22 18:08:28 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
|
|
|
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 io.mockk.verify
|
|
|
|
import org.junit.jupiter.api.AfterEach
|
|
|
|
import org.junit.jupiter.api.BeforeEach
|
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
|
2024-02-16 01:11:57 +03:00
|
|
|
class AuthCallbackViewModelTest : BaseViewModelTest() {
|
2024-01-22 18:08:28 +03:00
|
|
|
private val authRepository = mockk<AuthRepository> {
|
|
|
|
every { setCaptchaCallbackTokenResult(any()) } just runs
|
2024-01-23 06:29:03 +03:00
|
|
|
every { setSsoCallbackResult(any()) } just runs
|
2024-02-21 01:55:23 +03:00
|
|
|
every { setDuoCallbackTokenResult(any()) } just runs
|
2024-02-15 23:54:46 +03:00
|
|
|
every { setYubiKeyResult(any()) } just runs
|
2024-01-22 18:08:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
fun setUp() {
|
2024-02-15 23:54:46 +03:00
|
|
|
mockkStatic(
|
|
|
|
Intent::getYubiKeyResultOrNull,
|
|
|
|
Intent::getCaptchaCallbackTokenResult,
|
2024-02-21 01:55:23 +03:00
|
|
|
Intent::getDuoCallbackTokenResult,
|
2024-02-15 23:54:46 +03:00
|
|
|
Intent::getSsoCallbackResult,
|
|
|
|
)
|
2024-01-22 18:08:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
fun tearDown() {
|
2024-02-15 23:54:46 +03:00
|
|
|
unmockkStatic(
|
|
|
|
Intent::getYubiKeyResultOrNull,
|
|
|
|
Intent::getCaptchaCallbackTokenResult,
|
2024-02-21 01:55:23 +03:00
|
|
|
Intent::getDuoCallbackTokenResult,
|
2024-02-15 23:54:46 +03:00
|
|
|
Intent::getSsoCallbackResult,
|
|
|
|
)
|
2024-01-22 18:08:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2024-02-15 23:54:46 +03:00
|
|
|
fun `on IntentReceive with captcha host should call setCaptchaCallbackToken`() {
|
2024-01-22 18:08:28 +03:00
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
2024-02-15 23:54:46 +03:00
|
|
|
val captchaCallbackTokenResult = CaptchaCallbackTokenResult.Success(token = "mockk_token")
|
|
|
|
every { mockIntent.getCaptchaCallbackTokenResult() } returns captchaCallbackTokenResult
|
2024-02-21 01:55:23 +03:00
|
|
|
every { mockIntent.getDuoCallbackTokenResult() } returns null
|
2024-02-15 23:54:46 +03:00
|
|
|
every { mockIntent.getYubiKeyResultOrNull() } returns null
|
|
|
|
every { mockIntent.getSsoCallbackResult() } returns null
|
|
|
|
|
2024-02-16 01:11:57 +03:00
|
|
|
viewModel.trySendAction(AuthCallbackAction.IntentReceive(intent = mockIntent))
|
2024-02-15 23:54:46 +03:00
|
|
|
verify(exactly = 1) {
|
|
|
|
authRepository.setCaptchaCallbackTokenResult(tokenResult = captchaCallbackTokenResult)
|
2024-01-22 18:08:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 01:55:23 +03:00
|
|
|
@Test
|
|
|
|
fun `on IntentReceive with duo host should call setDuoCallbackToken`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val duoCallbackTokenResult = DuoCallbackTokenResult.Success(token = "mockk_token")
|
|
|
|
every { mockIntent.getCaptchaCallbackTokenResult() } returns null
|
|
|
|
every { mockIntent.getDuoCallbackTokenResult() } returns duoCallbackTokenResult
|
|
|
|
every { mockIntent.getYubiKeyResultOrNull() } returns null
|
|
|
|
every { mockIntent.getSsoCallbackResult() } returns null
|
|
|
|
|
|
|
|
viewModel.trySendAction(AuthCallbackAction.IntentReceive(intent = mockIntent))
|
|
|
|
verify(exactly = 1) {
|
|
|
|
authRepository.setDuoCallbackTokenResult(tokenResult = duoCallbackTokenResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 06:29:03 +03:00
|
|
|
@Test
|
2024-02-15 23:54:46 +03:00
|
|
|
fun `on IntentReceive with sso host should call setSsoCallbackResult`() {
|
2024-01-23 06:29:03 +03:00
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
2024-02-15 23:54:46 +03:00
|
|
|
val sseCallbackResult = SsoCallbackResult.Success(
|
2024-01-23 06:29:03 +03:00
|
|
|
state = "mockk_state",
|
|
|
|
code = "mockk_code",
|
|
|
|
)
|
2024-02-15 23:54:46 +03:00
|
|
|
every { mockIntent.getSsoCallbackResult() } returns sseCallbackResult
|
|
|
|
every { mockIntent.getYubiKeyResultOrNull() } returns null
|
|
|
|
every { mockIntent.getCaptchaCallbackTokenResult() } returns null
|
2024-02-21 01:55:23 +03:00
|
|
|
every { mockIntent.getDuoCallbackTokenResult() } returns null
|
2024-02-15 23:54:46 +03:00
|
|
|
|
2024-02-16 01:11:57 +03:00
|
|
|
viewModel.trySendAction(AuthCallbackAction.IntentReceive(intent = mockIntent))
|
2024-02-15 23:54:46 +03:00
|
|
|
verify(exactly = 1) {
|
|
|
|
authRepository.setSsoCallbackResult(result = sseCallbackResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with Yubi Key Result should call setYubiKeyResult`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val yubiKeyResult = mockk<YubiKeyResult>()
|
|
|
|
every { mockIntent.getYubiKeyResultOrNull() } returns yubiKeyResult
|
|
|
|
every { mockIntent.getCaptchaCallbackTokenResult() } returns null
|
2024-02-21 01:55:23 +03:00
|
|
|
every { mockIntent.getDuoCallbackTokenResult() } returns null
|
2024-02-15 23:54:46 +03:00
|
|
|
every { mockIntent.getSsoCallbackResult() } returns null
|
|
|
|
|
2024-02-16 01:11:57 +03:00
|
|
|
viewModel.trySendAction(AuthCallbackAction.IntentReceive(intent = mockIntent))
|
2024-02-15 23:54:46 +03:00
|
|
|
verify(exactly = 1) {
|
|
|
|
authRepository.setYubiKeyResult(yubiKeyResult)
|
2024-01-23 06:29:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:11:57 +03:00
|
|
|
private fun createViewModel() = AuthCallbackViewModel(
|
2024-01-22 18:08:28 +03:00
|
|
|
authRepository = authRepository,
|
|
|
|
)
|
|
|
|
}
|