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-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-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
|
|
|
|
|
|
|
|
class WebAuthCallbackViewModelTest : BaseViewModelTest() {
|
|
|
|
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-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,
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
every { mockIntent.getYubiKeyResultOrNull() } returns null
|
|
|
|
every { mockIntent.getSsoCallbackResult() } returns null
|
|
|
|
|
|
|
|
viewModel.trySendAction(WebAuthCallbackAction.IntentReceive(intent = mockIntent))
|
|
|
|
verify(exactly = 1) {
|
|
|
|
authRepository.setCaptchaCallbackTokenResult(tokenResult = captchaCallbackTokenResult)
|
2024-01-22 18:08:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
viewModel.trySendAction(WebAuthCallbackAction.IntentReceive(intent = mockIntent))
|
|
|
|
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
|
|
|
|
every { mockIntent.getSsoCallbackResult() } returns null
|
|
|
|
|
|
|
|
viewModel.trySendAction(WebAuthCallbackAction.IntentReceive(intent = mockIntent))
|
|
|
|
verify(exactly = 1) {
|
|
|
|
authRepository.setYubiKeyResult(yubiKeyResult)
|
2024-01-23 06:29:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:08:28 +03:00
|
|
|
private fun createViewModel() = WebAuthCallbackViewModel(
|
|
|
|
authRepository = authRepository,
|
|
|
|
)
|
|
|
|
}
|