diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/RegisterResponseJson.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/RegisterResponseJson.kt index bc5bceb66..bff331194 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/RegisterResponseJson.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/RegisterResponseJson.kt @@ -46,7 +46,6 @@ sealed class RegisterResponseJson { /** * Represents the json body of an invalid register request. * - * @param message * @param validationErrors a map where each value is a list of error messages for each key. * The values in the array should be used for display to the user, since the keys tend to come * back as nonsense. (eg: empty string key) @@ -54,18 +53,17 @@ sealed class RegisterResponseJson { @Serializable data class Invalid( @SerialName("message") - val message: String?, + private val invalidMessage: String? = null, + + @SerialName("Message") + private val errorMessage: String? = null, @SerialName("validationErrors") val validationErrors: Map>?, - ) : RegisterResponseJson() - - /** - * A different register error with a message. - */ - @Serializable - data class Error( - @SerialName("Message") - val message: String?, - ) : RegisterResponseJson() + ) : RegisterResponseJson() { + /** + * A generic error message. + */ + val message: String? get() = invalidMessage ?: errorMessage + } } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/SendVerificationEmailResponseJson.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/SendVerificationEmailResponseJson.kt index 12928152c..5d5650993 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/SendVerificationEmailResponseJson.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/model/SendVerificationEmailResponseJson.kt @@ -22,7 +22,6 @@ sealed class SendVerificationEmailResponseJson { /** * Represents the json body of an invalid request. * - * @param message * @param validationErrors a map where each value is a list of error messages for each key. * The values in the array should be used for display to the user, since the keys tend to come * back as nonsense. (eg: empty string key) @@ -30,18 +29,17 @@ sealed class SendVerificationEmailResponseJson { @Serializable data class Invalid( @SerialName("message") - val message: String?, + private val invalidMessage: String? = null, + + @SerialName("Message") + private val errorMessage: String? = null, @SerialName("validationErrors") val validationErrors: Map>?, - ) : SendVerificationEmailResponseJson() - - /** - * A different error with a message. - */ - @Serializable - data class Error( - @SerialName("Message") - val message: String?, - ) : SendVerificationEmailResponseJson() + ) : SendVerificationEmailResponseJson() { + /** + * A generic error message. + */ + val message: String? get() = invalidMessage ?: errorMessage + } } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt index 91d47c6c9..1132ac8b8 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt @@ -43,10 +43,6 @@ class IdentityServiceImpl( codes = listOf(400, 429), json = json, ) - ?: bitwardenError.parseErrorBodyOrNull( - code = 429, - json = json, - ) ?: throw throwable } @@ -121,10 +117,6 @@ class IdentityServiceImpl( codes = listOf(400, 429), json = json, ) - ?: bitwardenError.parseErrorBodyOrNull( - code = 429, - json = json, - ) ?: throw throwable } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt index ce7be191e..1384c2749 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt @@ -816,10 +816,6 @@ class AuthRepositoryImpl( ?: it.message, ) } - - is RegisterResponseJson.Error -> { - RegisterResult.Error(it.message) - } } }, onFailure = { RegisterResult.Error(errorMessage = null) }, diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt index 740b8744a..789dc2e89 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt @@ -22,7 +22,6 @@ import com.x8bit.bitwarden.data.platform.util.asSuccess 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 @@ -41,9 +40,7 @@ class IdentityServiceTest : BaseServiceTest() { private val identityService = IdentityServiceImpl( unauthenticatedIdentityApi = unauthenticatedIdentityApi, - json = Json { - ignoreUnknownKeys = true - }, + json = json, deviceModelProvider = deviceModelProvider, ) @@ -154,7 +151,7 @@ class IdentityServiceTest : BaseServiceTest() { val result = identityService.register(registerRequestBody) assertEquals( RegisterResponseJson.Invalid( - message = "The model state is invalid.", + invalidMessage = "The model state is invalid.", validationErrors = mapOf("" to listOf("Email '' is already taken.")), ), result.getOrThrow(), @@ -167,8 +164,9 @@ class IdentityServiceTest : BaseServiceTest() { server.enqueue(response) val result = identityService.register(registerRequestBody) assertEquals( - RegisterResponseJson.Error( - message = "Slow down! Too many requests. Try again soon.", + RegisterResponseJson.Invalid( + errorMessage = "Slow down! Too many requests. Try again soon.", + validationErrors = null, ), result.getOrThrow(), ) @@ -328,7 +326,7 @@ class IdentityServiceTest : BaseServiceTest() { val result = identityService.registerFinish(registerFinishRequestBody) assertEquals( RegisterResponseJson.Invalid( - message = "The model state is invalid.", + invalidMessage = "The model state is invalid.", validationErrors = mapOf("" to listOf("Email '' is already taken.")), ), result.getOrThrow(), @@ -341,8 +339,9 @@ class IdentityServiceTest : BaseServiceTest() { server.enqueue(response) val result = identityService.registerFinish(registerFinishRequestBody) assertEquals( - RegisterResponseJson.Error( - message = "Slow down! Too many requests. Try again soon.", + RegisterResponseJson.Invalid( + errorMessage = "Slow down! Too many requests. Try again soon.", + validationErrors = null, ), result.getOrThrow(), ) diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt index e1e3aff16..0794bf758 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryTest.kt @@ -3712,7 +3712,9 @@ class AuthRepositoryTest { kdfIterations = DEFAULT_KDF_ITERATIONS.toUInt(), ), ) - } returns RegisterResponseJson.Invalid("message", mapOf()).asSuccess() + } returns RegisterResponseJson + .Invalid(invalidMessage = "message", validationErrors = mapOf()) + .asSuccess() val result = repository.register( email = EMAIL, @@ -3746,7 +3748,7 @@ class AuthRepositoryTest { ) } returns RegisterResponseJson .Invalid( - message = "message", + invalidMessage = "message", validationErrors = mapOf("" to listOf("expected")), ) .asSuccess() @@ -3762,38 +3764,6 @@ class AuthRepositoryTest { assertEquals(RegisterResult.Error(errorMessage = "expected"), result) } - @Test - fun `register returns Error body should return Error with message`() = runTest { - coEvery { identityService.preLogin(EMAIL) } returns PRE_LOGIN_SUCCESS.asSuccess() - coEvery { - identityService.register( - body = RegisterRequestJson( - email = EMAIL, - masterPasswordHash = PASSWORD_HASH, - masterPasswordHint = null, - captchaResponse = null, - key = ENCRYPTED_USER_KEY, - keys = RegisterRequestJson.Keys( - publicKey = PUBLIC_KEY, - encryptedPrivateKey = PRIVATE_KEY, - ), - kdfType = KdfTypeJson.PBKDF2_SHA256, - kdfIterations = DEFAULT_KDF_ITERATIONS.toUInt(), - ), - ) - } returns RegisterResponseJson.Error(message = "message").asSuccess() - - val result = repository.register( - email = EMAIL, - masterPassword = PASSWORD, - masterPasswordHint = null, - captchaToken = null, - shouldCheckDataBreaches = false, - isMasterPasswordStrong = true, - ) - assertEquals(RegisterResult.Error(errorMessage = "message"), result) - } - @Test fun `register with email token Success should return Success`() = runTest { coEvery { identityService.preLogin(EMAIL) } returns PRE_LOGIN_SUCCESS.asSuccess()