Update the TwoFactorRequired response to be able to parse multiple payload types (#1018)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent d6513a1ef7
commit cb20a6d690
6 changed files with 42 additions and 15 deletions
@@ -18,6 +18,9 @@ import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import okhttp3.mockwebserver.MockResponse
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
@@ -195,8 +198,8 @@ private const val TWO_FACTOR_BODY_JSON = """
"""
private val TWO_FACTOR_BODY = GetTokenResponseJson.TwoFactorRequired(
authMethodsData = mapOf(
TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"),
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.EMAIL to JsonObject(mapOf("Email" to JsonPrimitive("ex***@email.com"))),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
),
ssoToken = "exampleToken",
captchaToken = "BWCaptchaBypass_ABCXYZ",
@@ -2,16 +2,21 @@ package com.x8bit.bitwarden.data.auth.datasource.network.util
import com.x8bit.bitwarden.data.auth.datasource.network.model.GetTokenResponseJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.TwoFactorAuthMethod
import org.junit.jupiter.api.Test
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class TwoFactorRequiredExtensionTest {
@Test
fun `availableAuthMethods returns the expected value`() {
val subject = GetTokenResponseJson.TwoFactorRequired(
authMethodsData = mapOf(
TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"),
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.EMAIL to JsonObject(
mapOf("Email" to JsonPrimitive("ex***@email.com")),
),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
),
captchaToken = null,
ssoToken = null,
@@ -30,8 +35,10 @@ class TwoFactorRequiredExtensionTest {
fun `twoFactorDisplayEmail returns the expected value`() {
val subject = GetTokenResponseJson.TwoFactorRequired(
authMethodsData = mapOf(
TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"),
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.EMAIL to JsonObject(
mapOf("Email" to JsonPrimitive("ex***@email.com")),
),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
),
captchaToken = null,
ssoToken = null,
@@ -43,7 +50,7 @@ class TwoFactorRequiredExtensionTest {
fun `twoFactorDisplayEmail returns the expected value when null`() {
val subject = GetTokenResponseJson.TwoFactorRequired(
authMethodsData = mapOf(
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
),
captchaToken = null,
ssoToken = null,
@@ -55,8 +62,10 @@ class TwoFactorRequiredExtensionTest {
fun `preferredAuthMethod returns the expected value`() {
val subject = GetTokenResponseJson.TwoFactorRequired(
authMethodsData = mapOf(
TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"),
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.EMAIL to JsonObject(
mapOf("Email" to JsonPrimitive("ex***@email.com")),
),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
),
captchaToken = null,
ssoToken = null,
@@ -104,6 +104,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.junit.jupiter.api.AfterEach
@@ -4151,8 +4154,10 @@ class AuthRepositoryTest {
private const val USER_ID_2 = "b9d32ec0-6497-4582-9798-b350f53bfa02"
private val ORGANIZATIONS = listOf(createMockOrganization(number = 0))
private val TWO_FACTOR_AUTH_METHODS_DATA = mapOf(
TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"),
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.EMAIL to JsonObject(
mapOf("Email" to JsonPrimitive("ex***@email.com")),
),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
)
private val PRE_LOGIN_SUCCESS = PreLoginResponseJson(
kdfParams = PreLoginResponseJson.KdfParams.Pbkdf2(iterations = 1u),
@@ -22,6 +22,9 @@ import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
@@ -417,8 +420,10 @@ class TwoFactorLoginViewModelTest : BaseViewModelTest() {
companion object {
private val TWO_FACTOR_AUTH_METHODS_DATA = mapOf(
TwoFactorAuthMethod.EMAIL to mapOf("Email" to "ex***@email.com"),
TwoFactorAuthMethod.AUTHENTICATOR_APP to mapOf("Email" to null),
TwoFactorAuthMethod.EMAIL to JsonObject(
mapOf("Email" to JsonPrimitive("ex***@email.com")),
),
TwoFactorAuthMethod.AUTHENTICATOR_APP to JsonObject(mapOf("Email" to JsonNull)),
)
private val TWO_FACTOR_RESPONSE =
GetTokenResponseJson.TwoFactorRequired(