Skip to content

Commit

Permalink
100% Verify coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 25, 2024
1 parent 5dd9f06 commit 41742c2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/vonage/client/kt/Verify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Verify(private val verify2Client: Verify2Client) {

fun isValidVerificationCode(requestId: String, code: String): Boolean {
try {
verify2Client.checkVerificationCode(UUID.fromString(requestId), code)
checkVerificationCode(requestId, code)
return true
} catch (ex: VerifyResponseException) {
if (ex.statusCode == 400 || ex.statusCode == 410) {
Expand Down
12 changes: 6 additions & 6 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ abstract class AbstractTest {

mockRequest(HttpMethod.POST, expectedUrl,
contentType = if (expectedRequestParams != null) ContentType.APPLICATION_JSON else null,
accept = if (expectedResponseParams != null) ContentType.APPLICATION_JSON else null,
accept = if (expectedResponseParams != null && status < 400) ContentType.APPLICATION_JSON else null,
AuthType.JWT, expectedRequestParams
).mockReturn(status, expectedResponseParams)

Expand All @@ -131,22 +131,22 @@ abstract class AbstractTest {
}

protected inline fun <reified E: VonageApiResponseException> assertApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Unit) {
url: String, requestMethod: HttpMethod, actualCall: () -> Any) {

assert402ApiResponseException<E>(url, requestMethod, actualCall)
assert429ApiResponseException<E>(url, requestMethod, actualCall)
}

protected inline fun <reified E: VonageApiResponseException> assertApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Unit, status: Int,
url: String, requestMethod: HttpMethod, actualCall: () -> Any, status: Int,
errorType: String, title: String, detail: String, instance: String): E {

mockRequest(requestMethod, url).mockReturn(status, mapOf(
"type" to errorType, "title" to title,
"detail" to detail, "instance" to instance
))

val exception = assertThrows<E>(actualCall)
val exception = assertThrows<E> { actualCall.invoke() }

assertEquals(status, exception.statusCode)
assertEquals(URI.create(errorType), exception.type)
Expand All @@ -157,7 +157,7 @@ abstract class AbstractTest {
}

protected inline fun <reified E: VonageApiResponseException> assert402ApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Unit): E =
url: String, requestMethod: HttpMethod, actualCall: () -> Any): E =

assertApiResponseException(url, requestMethod, actualCall, 402,
"https://developer.nexmo.com/api-errors/#low-balance",
Expand All @@ -167,7 +167,7 @@ abstract class AbstractTest {
)

protected inline fun <reified E: VonageApiResponseException> assert429ApiResponseException(
url: String, requestMethod: HttpMethod, actualCall: () -> Unit): E =
url: String, requestMethod: HttpMethod, actualCall: () -> Any): E =
assertApiResponseException(url, requestMethod, actualCall, 429,
"https://www.developer.vonage.com/api-errors#throttled",
"Rate Limit Hit",
Expand Down
35 changes: 30 additions & 5 deletions src/test/kotlin/com/vonage/client/kt/VerifyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import com.vonage.client.verify2.VerifyResponseException
import org.junit.jupiter.api.Test
import java.net.URI
import java.util.UUID
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.*

class VerifyTest : AbstractTest() {
private val verifyClient = vonageClient.verify
Expand All @@ -31,8 +29,9 @@ class VerifyTest : AbstractTest() {
private val fromEmail = "bob@example.org"
private val checkUrl = "https://api.nexmo.com/v2/verify/$requestId/silent-auth/redirect"
private val redirectUrl = "https://acme-app.com/sa/redirect"
private val checkCodeRequestParams = mapOf("code" to code)

private fun assertVerifyResponseException(url: String, requestMethod: HttpMethod, actualCall: () -> Unit) {
private fun assertVerifyResponseException(url: String, requestMethod: HttpMethod, actualCall: () -> Any) {
assertApiResponseException<VerifyResponseException>(url, requestMethod, actualCall)
if (url.contains(requestId)) {
assertApiResponseException<VerifyResponseException>(url, requestMethod, actualCall,
Expand Down Expand Up @@ -178,10 +177,36 @@ class VerifyTest : AbstractTest() {

@Test
fun `check code`() {
mockJsonJwtPost(requestIdUrl, expectedRequestParams = mapOf("code" to code))
mockJsonJwtPost(requestIdUrl, checkCodeRequestParams)
verifyClient.checkVerificationCode(requestId, code)
assertVerifyResponseException(requestIdUrl, HttpMethod.POST) {
verifyClient.checkVerificationCode(requestId, code)
}
}

@Test
fun `is valid verification code`() {
val call: () -> Boolean = {verifyClient.isValidVerificationCode(requestId, code)}

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 200)
assertTrue(call.invoke())

val title = "Invalid Code"

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 400, mapOf(
"title" to title,
"type" to "https://www.developer.vonage.com/api-errors/verify#invalid-code",
"detail" to "The code you provided does not match the expected value."
))
assertFalse(call.invoke())

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 410, mapOf(
"title" to title,
"type" to "https://www.developer.vonage.com/api-errors/verify#expired",
"detail" to "An incorrect code has been provided too many times. Workflow terminated."
))
assertFalse(call.invoke())

assertVerifyResponseException(requestIdUrl, HttpMethod.POST, call)
}
}

0 comments on commit 41742c2

Please sign in to comment.