From 41742c207d05c5f90910fb65773c545cb81df38a Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Tue, 25 Jun 2024 14:12:14 +0100 Subject: [PATCH] 100% Verify coverage --- .../kotlin/com/vonage/client/kt/Verify.kt | 2 +- .../com/vonage/client/kt/AbstractTest.kt | 12 +++---- .../kotlin/com/vonage/client/kt/VerifyTest.kt | 35 ++++++++++++++++--- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/vonage/client/kt/Verify.kt b/src/main/kotlin/com/vonage/client/kt/Verify.kt index 5512d47..68308a1 100644 --- a/src/main/kotlin/com/vonage/client/kt/Verify.kt +++ b/src/main/kotlin/com/vonage/client/kt/Verify.kt @@ -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) { diff --git a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt index 9a8bff6..22cb2ac 100644 --- a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt @@ -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) @@ -131,14 +131,14 @@ abstract class AbstractTest { } protected inline fun assertApiResponseException( - url: String, requestMethod: HttpMethod, actualCall: () -> Unit) { + url: String, requestMethod: HttpMethod, actualCall: () -> Any) { assert402ApiResponseException(url, requestMethod, actualCall) assert429ApiResponseException(url, requestMethod, actualCall) } protected inline fun 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( @@ -146,7 +146,7 @@ abstract class AbstractTest { "detail" to detail, "instance" to instance )) - val exception = assertThrows(actualCall) + val exception = assertThrows { actualCall.invoke() } assertEquals(status, exception.statusCode) assertEquals(URI.create(errorType), exception.type) @@ -157,7 +157,7 @@ abstract class AbstractTest { } protected inline fun 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", @@ -167,7 +167,7 @@ abstract class AbstractTest { ) protected inline fun 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", diff --git a/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt b/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt index 08437e6..60a6d84 100644 --- a/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/VerifyTest.kt @@ -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 @@ -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(url, requestMethod, actualCall) if (url.contains(requestId)) { assertApiResponseException(url, requestMethod, actualCall, @@ -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) + } } \ No newline at end of file