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

This commit is contained in:
David Perez 2024-02-15 10:28:34 -06:00 committed by Álison Fernandes
parent d6513a1ef7
commit cb20a6d690
6 changed files with 42 additions and 15 deletions

View file

@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.auth.datasource.network.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
/**
* Models response bodies from the get token request.
@ -121,7 +122,7 @@ sealed class GetTokenResponseJson {
@Serializable
data class TwoFactorRequired(
@SerialName("TwoFactorProviders2")
val authMethodsData: Map<TwoFactorAuthMethod, Map<String, String?>?>,
val authMethodsData: Map<TwoFactorAuthMethod, JsonObject?>,
@SerialName("CaptchaBypassToken")
val captchaToken: String?,

View file

@ -2,6 +2,8 @@ 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 kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonPrimitive
/**
* Return the list of two-factor auth methods available to the user.
@ -34,4 +36,6 @@ val GetTokenResponseJson.TwoFactorRequired?.twoFactorDisplayEmail: String
?.authMethodsData
?.get(TwoFactorAuthMethod.EMAIL)
?.get("Email")
?.jsonPrimitive
?.contentOrNull
?: ""

View file

@ -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",

View file

@ -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,

View file

@ -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),

View file

@ -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(