[PM-11649] Add tests to verify email token

This commit is contained in:
André Bispo
2024-09-04 19:53:44 +01:00
parent 15f72d8ba3
commit 5b438e4535
2 changed files with 110 additions and 0 deletions

View File

@@ -16,6 +16,8 @@ import com.x8bit.bitwarden.data.auth.datasource.network.model.SendVerificationEm
import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceUserDecryptionOptionsJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.TwoFactorAuthMethod
import com.x8bit.bitwarden.data.auth.datasource.network.model.UserDecryptionOptionsJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.VerifyEmailTokenRequestJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.VerifyEmailTokenResponseJson
import com.x8bit.bitwarden.data.platform.base.BaseServiceTest
import com.x8bit.bitwarden.data.platform.util.DeviceModelProvider
import com.x8bit.bitwarden.data.platform.util.asSuccess
@@ -369,6 +371,35 @@ class IdentityServiceTest : BaseServiceTest() {
assertTrue(result.isFailure)
}
@Test
fun `verifyEmailToken should return null when response is empty success`() = runTest {
server.enqueue(MockResponse().setResponseCode(200))
val result = identityService.verifyEmailToken(VERIFY_EMAIL_REQUEST)
assertTrue(result.isSuccess)
}
@Test
fun `verifyEmailToken should return an error when response is an error`() = runTest {
server.enqueue(MockResponse().setResponseCode(400))
val result = identityService.verifyEmailToken(VERIFY_EMAIL_REQUEST)
assertTrue(result.isFailure)
}
@Test
fun `verifyEmailToken failure with expired link should return Invalid`() = runTest {
val response = MockResponse().setResponseCode(400).setBody(EXPIRED_LINK_RESPONSE_JSON)
server.enqueue(response)
val result = identityService.verifyEmailToken(VERIFY_EMAIL_REQUEST)
assertEquals(
@Suppress("MaxLineLength")
VerifyEmailTokenResponseJson.Invalid(
errorMessage = "Expired link. Please restart registration or try logging in. You may already have an account.",
validationErrors = null,
),
result.getOrThrow(),
)
}
companion object {
private const val UNIQUE_APP_ID = "testUniqueAppId"
private const val REFRESH_TOKEN = "refreshToken"
@@ -569,6 +600,14 @@ private const val CAPTCHA_BYPASS_TOKEN_RESPONSE_JSON = """
{
"captchaBypassToken": "mock_token"
}
"""
private const val EXPIRED_LINK_RESPONSE_JSON = """
{
"Object": "error",
"Message": "Expired link. Please restart registration or try logging in. You may already have an account."
}
"""
private val INVALID_LOGIN = GetTokenResponseJson.Invalid(
@@ -582,3 +621,8 @@ private val SEND_VERIFICATION_EMAIL_REQUEST = SendVerificationEmailRequestJson(
name = "Name Example",
receiveMarketingEmails = true,
)
private val VERIFY_EMAIL_REQUEST = VerifyEmailTokenRequestJson(
email = "email@example.com",
emailVerificationToken = "mock_token",
)

View File

@@ -42,6 +42,8 @@ import com.x8bit.bitwarden.data.auth.datasource.network.model.TrustedDeviceUserD
import com.x8bit.bitwarden.data.auth.datasource.network.model.TwoFactorAuthMethod
import com.x8bit.bitwarden.data.auth.datasource.network.model.TwoFactorDataModel
import com.x8bit.bitwarden.data.auth.datasource.network.model.UserDecryptionOptionsJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.VerifyEmailTokenRequestJson
import com.x8bit.bitwarden.data.auth.datasource.network.model.VerifyEmailTokenResponseJson
import com.x8bit.bitwarden.data.auth.datasource.network.service.AccountsService
import com.x8bit.bitwarden.data.auth.datasource.network.service.DevicesService
import com.x8bit.bitwarden.data.auth.datasource.network.service.HaveIBeenPwnedService
@@ -81,6 +83,7 @@ import com.x8bit.bitwarden.data.auth.repository.model.UserOrganizations
import com.x8bit.bitwarden.data.auth.repository.model.ValidatePasswordResult
import com.x8bit.bitwarden.data.auth.repository.model.ValidatePinResult
import com.x8bit.bitwarden.data.auth.repository.model.VaultUnlockType
import com.x8bit.bitwarden.data.auth.repository.model.VerifyEmailTokenResult
import com.x8bit.bitwarden.data.auth.repository.model.VerifyOtpResult
import com.x8bit.bitwarden.data.auth.repository.util.CaptchaCallbackTokenResult
import com.x8bit.bitwarden.data.auth.repository.util.DuoCallbackTokenResult
@@ -5999,6 +6002,69 @@ class AuthRepositoryTest {
)
}
@Test
fun `verifyEmailToken success should return success`() = runTest {
coEvery {
identityService.verifyEmailToken(
VerifyEmailTokenRequestJson(
email = EMAIL,
emailVerificationToken = EMAIL_VERIFICATION_TOKEN,
),
)
} returns VerifyEmailTokenResponseJson.Success.asSuccess()
val result = repository.verifyEmailToken(
email = EMAIL,
emailVerificationToken = EMAIL_VERIFICATION_TOKEN,
)
assertEquals(
VerifyEmailTokenResult.Verified,
result,
)
}
@Test
fun `verifyEmailToken failure with expired link message should return ExpiredLink`() = runTest {
coEvery {
identityService.verifyEmailToken(
VerifyEmailTokenRequestJson(
email = EMAIL,
emailVerificationToken = EMAIL_VERIFICATION_TOKEN,
),
)
} returns Throwable("Expired link").asFailure()
val result = repository.verifyEmailToken(
email = EMAIL,
emailVerificationToken = EMAIL_VERIFICATION_TOKEN,
)
assertEquals(
VerifyEmailTokenResult.LinkExpired,
result,
)
}
@Test
fun `verifyEmailToken generic failure should return error`() = runTest {
coEvery {
identityService.verifyEmailToken(
VerifyEmailTokenRequestJson(
email = EMAIL,
emailVerificationToken = EMAIL_VERIFICATION_TOKEN,
),
)
} returns Throwable("generic fail").asFailure()
val result = repository.verifyEmailToken(
email = EMAIL,
emailVerificationToken = EMAIL_VERIFICATION_TOKEN,
)
assertEquals(
VerifyEmailTokenResult.Error,
result,
)
}
companion object {
private const val UNIQUE_APP_ID = "testUniqueAppId"
private const val NAME = "Example Name"