Skip to content

Commit

Permalink
✅ Add failure corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
prime2do committed Dec 8, 2023
1 parent fe38ccf commit 96184f8
Showing 1 changed file with 164 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand All @@ -41,6 +42,7 @@ class EmailVerificationViewModelTest {
private val validEmail = "[email protected]"
private val invalidEmail = "test"
private val emptyEmail = ""
private val wrongEmail = "[email protected]"
private val signup = "signup"
private val resetPassword = "reset_password"
private val sourceList = arrayListOf(signup, resetPassword)
Expand Down Expand Up @@ -322,6 +324,106 @@ class EmailVerificationViewModelTest {
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)
}

@Test
fun `should fail code send when bad request occurs`() {
val sendCodeRequest = AuthSendCodeRequest(wrongEmail)
val errorResponseBody = "Error message or JSON here".toResponseBody(null)
val errorResponse: Response<Void> = Response.error(400, errorResponseBody)

coEvery { authRepository.sendCodeForSignup(sendCodeRequest) } returns flowOf(
errorResponse
)
coEvery { authRepository.sendCodeForResetPassword(sendCodeRequest) } returns flowOf(
errorResponse
)
coEvery { responseHandler.parseErrorResponse(any()).key } returns "email"
viewModel.setSource("signup")
viewModel.setEmail(wrongEmail)
viewModel.sendCode()

coVerify { authRepository.sendCodeForSignup(sendCodeRequest) }

assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.EMAIL)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.wrong_email)
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)

viewModel.setSource("reset_password")
viewModel.setEmail(wrongEmail)
viewModel.sendCode()

coVerify { authRepository.sendCodeForResetPassword(sendCodeRequest) }

assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.EMAIL)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.wrong_email)
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)
}

@Test
fun `should fail code send when internet connection fails`() {
val validSendCodeRequest = AuthSendCodeRequest(validEmail)
val errorResponseBody = "Error message or JSON here".toResponseBody(null)
val errorResponse: Response<Void> = Response.error(600, errorResponseBody)

coEvery { authRepository.sendCodeForSignup(validSendCodeRequest) } returns flowOf(
errorResponse
)
coEvery { authRepository.sendCodeForResetPassword(validSendCodeRequest) } returns flowOf(
errorResponse
)
viewModel.setSource("signup")
viewModel.setEmail(validEmail)
viewModel.sendCode()

coVerify { authRepository.sendCodeForSignup(validSendCodeRequest) }

assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.CONNECTION)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.connection_error)
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)

viewModel.setSource("reset_password")
viewModel.setEmail(validEmail)
viewModel.sendCode()

coVerify { authRepository.sendCodeForResetPassword(validSendCodeRequest) }

assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.CONNECTION)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.connection_error)
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)
}

@Test
fun `should fail code send when unknown error occurs`() {
val validSendCodeRequest = AuthSendCodeRequest(validEmail)
val errorResponseBody = "Error message or JSON here".toResponseBody(null)
val errorResponse: Response<Void> = Response.error(404, errorResponseBody)

coEvery { authRepository.sendCodeForSignup(validSendCodeRequest) } returns flowOf(
errorResponse
)
coEvery { authRepository.sendCodeForResetPassword(validSendCodeRequest) } returns flowOf(
errorResponse
)
viewModel.setSource("signup")
viewModel.setEmail(validEmail)
viewModel.sendCode()

coVerify { authRepository.sendCodeForSignup(validSendCodeRequest) }

assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.UNKNOWN)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.unknown_error)
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)

viewModel.setSource("reset_password")
viewModel.setEmail(validEmail)
viewModel.sendCode()

coVerify { authRepository.sendCodeForResetPassword(validSendCodeRequest) }

assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.UNKNOWN)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.unknown_error)
assertEquals(viewModel.uiState.value.isCodeSuccessfullySent, false)
}

@Test
fun `should fail verification when source is invalid`() {
val navigateCallback: (String) -> Unit = mockk(relaxed = true)
Expand All @@ -337,7 +439,6 @@ class EmailVerificationViewModelTest {
assertEquals(viewModel.uiState.value.isValidCode, false)
}

// ResetPassword
@Test
fun `should fail verification when internet connection fails`() {
val navigateCallback: (String) -> Unit = mockk(relaxed = true)
Expand All @@ -346,9 +447,25 @@ class EmailVerificationViewModelTest {
val validEmailVerificationRequest = AuthVerifyEmailRequest(
validEmail, validCode
)
val errorResponseBody = "Error message or JSON here".toResponseBody(null)
val errorResponse: Response<Void> = Response.error(600, errorResponseBody)
coEvery { authRepository.verifyEmailForSignup(validEmailVerificationRequest) } returns flowOf(
errorResponse
)
coEvery { authRepository.verifyEmailForResetPassword(validEmailVerificationRequest) } returns flowOf(
Resource.error("unknown", accessToken)
)
viewModel.setSource("signup")
viewModel.setEmail(validEmail)
viewModel.setCode(validCode)

viewModel.verifyEmail(navigateCallback)

coVerify { authRepository.verifyEmailForSignup(validEmailVerificationRequest) }

assertEquals(viewModel.uiState.value.isValidCode, false)
assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.CONNECTION)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.connection_error)

viewModel.setSource("reset_password")
viewModel.setEmail(validEmail)
Expand All @@ -360,30 +477,72 @@ class EmailVerificationViewModelTest {

assertEquals(viewModel.uiState.value.isValidCode, false)
assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.CONNECTION)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.connection_error)
}

// ResetPassword
@Test
fun `should fail verification when code is wrong`() {
val navigateCallback: (String) -> Unit = mockk(relaxed = true)
val accessToken = AccessToken("access")

val validEmailVerificationRequest = AuthVerifyEmailRequest(
val emailVerificationRequest = AuthVerifyEmailRequest(
validEmail, wrongCode
)
coEvery { authRepository.verifyEmailForResetPassword(validEmailVerificationRequest) } returns flowOf(
val errorResponseBody = "Error message or JSON here".toResponseBody(null)
val errorResponse: Response<Void> = Response.error(400, errorResponseBody)
coEvery { authRepository.verifyEmailForSignup(emailVerificationRequest) } returns flowOf(
errorResponse
)
coEvery { authRepository.verifyEmailForResetPassword(emailVerificationRequest) } returns flowOf(
Resource.error("", accessToken)
)
viewModel.setSource("signup")
viewModel.setEmail(validEmail)
viewModel.setCode(wrongCode)

viewModel.verifyEmail(navigateCallback)

coVerify { authRepository.verifyEmailForSignup(emailVerificationRequest) }

assertEquals(viewModel.uiState.value.isValidCode, false)
assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.CODE)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.wrong_code)

viewModel.setSource("reset_password")
viewModel.setEmail(validEmail)
viewModel.setCode(wrongCode)

viewModel.verifyEmail(navigateCallback)

coVerify { authRepository.verifyEmailForResetPassword(validEmailVerificationRequest) }
coVerify { authRepository.verifyEmailForResetPassword(emailVerificationRequest) }

assertEquals(viewModel.uiState.value.isValidCode, false)
assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.CODE)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.wrong_code)
}

// Signup
@Test
fun `should fail verification when unknown error occurs`() {
val navigateCallback: (String) -> Unit = mockk(relaxed = true)
val validEmailVerificationRequest = AuthVerifyEmailRequest(
validEmail, validCode
)
val errorResponseBody = "Error message or JSON here".toResponseBody(null)
val errorResponse: Response<Void> = Response.error(404, errorResponseBody)
coEvery { authRepository.verifyEmailForSignup(validEmailVerificationRequest) } returns flowOf(
errorResponse
)
viewModel.setSource("signup")
viewModel.setEmail(validEmail)
viewModel.setCode(validCode)

viewModel.verifyEmail(navigateCallback)

coVerify { authRepository.verifyEmailForSignup(validEmailVerificationRequest) }

assertEquals(viewModel.uiState.value.isValidCode, false)
assertEquals(viewModel.uiState.value.error?.type, EmailVerificationErrorType.UNKNOWN)
assertEquals(viewModel.uiState.value.error?.messageId, R.string.unknown_error)
}
}

0 comments on commit 96184f8

Please sign in to comment.