Skip to content

Commit

Permalink
refactor!: ExistingRequest for Verify v2
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Aug 5, 2024
1 parent abbd8f7 commit 9c41319
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
41 changes: 19 additions & 22 deletions src/main/kotlin/com/vonage/client/kt/Verify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,32 @@ class Verify(private val verify2Client: Verify2Client) {
VerificationRequest.builder().brand(brand).apply(init).build()
)

fun cancelVerification(requestId: UUID): Unit = verify2Client.cancelVerification(requestId)
inner class ExistingRequest internal constructor(private val requestId: UUID) {

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

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

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

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

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

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

fun isValidVerificationCode(requestId: UUID, code: String): Boolean {
try {
checkVerificationCode(requestId, code)
return true
} catch (ex: VerifyResponseException) {
if (ex.statusCode == 400 || ex.statusCode == 410) {
return false
} else {
throw ex
fun isValidVerificationCode(code: String): Boolean {
try {
checkVerificationCode(code)
return true
} catch (ex: VerifyResponseException) {
if (ex.statusCode == 400 || ex.statusCode == 410) {
return false
} else {
throw ex
}
}
}
}

fun request(requestId: UUID): ExistingRequest = ExistingRequest(requestId)

fun request(requestId: String): ExistingRequest = request(UUID.fromString(requestId))
}

fun VerificationRequest.Builder.silentAuth(
Expand Down
24 changes: 11 additions & 13 deletions src/test/kotlin/com/vonage/client/kt/VerifyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class VerifyTest : AbstractTest() {
private val requestIdStr = "c11236f4-00bf-4b89-84ba-88b25df97315"
private val requestId = UUID.fromString(requestIdStr)
private val requestIdUrl = "$baseUrl/$requestIdStr"
private val existingRequest = verifyClient.request(requestIdStr)
private val timeout = 60
private val fraudCheck = false
private val sandbox = true
Expand Down Expand Up @@ -171,37 +172,34 @@ class VerifyTest : AbstractTest() {
@Test
fun `cancel verification`() {
mockDelete(requestIdUrl)
verifyClient.cancelVerification(requestIdStr)
verifyClient.cancelVerification(requestId)
existingRequest.cancel()
assertVerifyResponseException(requestIdUrl, HttpMethod.DELETE) {
verifyClient.cancelVerification(requestIdStr)
existingRequest.cancel()
}
}

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

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

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

existingRequest.checkVerificationCode(code)

val title = "Invalid Code"

Expand All @@ -221,7 +219,7 @@ class VerifyTest : AbstractTest() {

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

0 comments on commit 9c41319

Please sign in to comment.