Skip to content

Commit

Permalink
Add UUID overloads for Verify
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 25, 2024
1 parent b80371f commit b1c1a95
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.0] - 2024-06-25

Initial version, unreleased to public.
Initial version.

### Added
- Messages API
Expand Down
18 changes: 14 additions & 4 deletions src/main/kotlin/com/vonage/client/kt/Verify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ class Verify(private val verify2Client: Verify2Client) {
VerificationRequest.builder().brand(brand).apply(init).build()
)

fun cancelVerification(requestId: String) = verify2Client.cancelVerification(UUID.fromString(requestId))
fun cancelVerification(requestId: UUID) = verify2Client.cancelVerification(requestId)

fun nextWorkflow(requestId: String) = verify2Client.nextWorkflow(UUID.fromString(requestId))
fun cancelVerification(requestId: String) = cancelVerification(UUID.fromString(requestId))

fun nextWorkflow(requestId: UUID) = verify2Client.nextWorkflow(requestId)

fun nextWorkflow(requestId: String) = nextWorkflow(UUID.fromString(requestId))

fun checkVerificationCode(requestId: UUID, code: String) =
verify2Client.checkVerificationCode(requestId, code)

fun checkVerificationCode(requestId: String, code: String) =
verify2Client.checkVerificationCode(UUID.fromString(requestId), code)
checkVerificationCode(UUID.fromString(requestId), code)

fun isValidVerificationCode(requestId: String, code: String): Boolean =
isValidVerificationCode(UUID.fromString(requestId), code)

fun isValidVerificationCode(requestId: String, code: String): Boolean {
fun isValidVerificationCode(requestId: UUID, code: String): Boolean {
try {
checkVerificationCode(requestId, code)
return true
Expand Down
44 changes: 24 additions & 20 deletions src/test/kotlin/com/vonage/client/kt/VerifyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import kotlin.test.*
class VerifyTest : AbstractTest() {
private val verifyClient = vonageClient.verify
private val baseUrl = "/v2/verify"
private val requestId = "c11236f4-00bf-4b89-84ba-88b25df97315"
private val requestIdUrl = "$baseUrl/$requestId"
private val requestIdStr = "c11236f4-00bf-4b89-84ba-88b25df97315"
private val requestId = UUID.fromString(requestIdStr)
private val requestIdUrl = "$baseUrl/$requestIdStr"
private val brand = "Nexmo KT"
private val clientRef = "my-personal-reference"
private val timeout = 60
Expand All @@ -27,17 +28,17 @@ class VerifyTest : AbstractTest() {
private val appHash = "ABC123def45"
private val toEmail = "alice@example.com"
private val fromEmail = "bob@example.org"
private val checkUrl = "https://api.nexmo.com/v2/verify/$requestId/silent-auth/redirect"
private val checkUrl = "https://api.nexmo.com/v2/verify/$requestIdStr/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: () -> Any) {
assertApiResponseException<VerifyResponseException>(url, requestMethod, actualCall)
if (url.contains(requestId)) {
if (url.contains(requestIdStr)) {
assertApiResponseException<VerifyResponseException>(url, requestMethod, actualCall,
404, errorType = "https://developer.vonage.com/api-errors#not-found",
title = "Not Found", instance = "bf0ca0bf927b3b52e3cb03217e1a1ddf",
detail = "Request $requestId was not found or it has been verified already."
detail = "Request $requestIdStr was not found or it has been verified already."
)
if (requestMethod != HttpMethod.DELETE) {
assertApiResponseException<VerifyResponseException>(url, requestMethod, actualCall,
Expand All @@ -64,7 +65,7 @@ class VerifyTest : AbstractTest() {
}
)
),
expectedResponseParams = mapOf("request_id" to requestId) +
expectedResponseParams = mapOf("request_id" to requestIdStr) +
if (channel == Channel.SILENT_AUTH) mapOf("check_url" to checkUrl) else mapOf()
)

Expand All @@ -79,7 +80,7 @@ class VerifyTest : AbstractTest() {
}
}
assertNotNull(response)
assertEquals(UUID.fromString(requestId), response.requestId)
assertEquals(requestId, response.requestId)
if (channel == Channel.SILENT_AUTH) {
assertEquals(URI.create(checkUrl), response.checkUrl)
}
Expand Down Expand Up @@ -136,7 +137,7 @@ class VerifyTest : AbstractTest() {
)
),
expectedResponseParams = mapOf(
"request_id" to requestId,
"request_id" to requestIdStr,
"check_url" to checkUrl
),
status = 202
Expand All @@ -152,44 +153,44 @@ class VerifyTest : AbstractTest() {
}

assertNotNull(response)
assertEquals(UUID.fromString(requestId), response.requestId)
assertEquals(requestId, response.requestId)
assertEquals(URI.create(checkUrl), response.checkUrl)
}

@Test
fun `cancel verification`() {
mockDelete(requestIdUrl)
verifyClient.cancelVerification(requestIdStr)
verifyClient.cancelVerification(requestId)
assertVerifyResponseException(requestIdUrl, HttpMethod.DELETE) {
verifyClient.cancelVerification(requestId)
verifyClient.cancelVerification(requestIdStr)
}
}

@Test
fun `next workflow`() {
val expectedUrl = "$requestIdUrl/next-workflow"
mockJsonJwtPost(expectedUrl)
verifyClient.nextWorkflow(requestIdStr)
verifyClient.nextWorkflow(requestId)
assertVerifyResponseException(expectedUrl, HttpMethod.POST) {
verifyClient.nextWorkflow(requestId)
}
}

@Test
fun `check code`() {
mockJsonJwtPost(requestIdUrl, checkCodeRequestParams)
verifyClient.checkVerificationCode(requestId, code)
assertVerifyResponseException(requestIdUrl, HttpMethod.POST) {
verifyClient.checkVerificationCode(requestId, code)
assertVerifyResponseException(expectedUrl, HttpMethod.POST) {
verifyClient.nextWorkflow(requestIdStr)
}
}

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

mockJsonJwtPost(requestIdUrl, checkCodeRequestParams, 200)
assertTrue(call.invoke())
verifyClient.checkVerificationCode(requestIdStr, code)
verifyClient.checkVerificationCode(requestId, code)
assertTrue(verifyClient.isValidVerificationCode(requestId, code))


val title = "Invalid Code"

Expand All @@ -208,5 +209,8 @@ class VerifyTest : AbstractTest() {
assertFalse(call.invoke())

assertVerifyResponseException(requestIdUrl, HttpMethod.POST, call)
assertVerifyResponseException(requestIdUrl, HttpMethod.POST) {
verifyClient.checkVerificationCode(requestIdStr, code)
}
}
}

0 comments on commit b1c1a95

Please sign in to comment.