Skip to content

Commit

Permalink
test: 100% branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 13, 2025
1 parent da3b751 commit 4f29b94
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/vonage/client/kt/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class Messages internal constructor(private val client: MessagesClient) {
* - **500**: Internal server error.
*/
fun send(message: MessageRequest, sandbox: Boolean = false): UUID =
(if (sandbox) client.useSandboxEndpoint()
else client.useRegularEndpoint()).sendMessage(message).messageUuid
(if (sandbox) client.useSandboxEndpoint() else client.useRegularEndpoint())
.sendMessage(message).messageUuid

/**
* Call this method to update an existing message.
Expand Down
36 changes: 25 additions & 11 deletions src/main/kotlin/com/vonage/client/kt/Vonage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ package com.vonage.client.kt
import com.vonage.client.HttpConfig
import com.vonage.client.VonageClient

/**
* Denotes the version of the Vonage Kotlin SDK being used, in SemVer format.
*/
const val VONAGE_KOTLIN_SDK_VERSION = "1.1.2"

/**
* The non-overridable user agent string used by the SDK.
*/
private const val SDK_USER_AGENT = "vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION"

/**
Expand Down Expand Up @@ -173,27 +180,34 @@ class Vonage(config: VonageClient.Builder.() -> Unit) {
* - **VONAGE_PRIVATE_KEY_PATH**: Path to the private key file of the application.
*/
fun VonageClient.Builder.authFromEnv(): VonageClient.Builder {
val apiKey = System.getenv("VONAGE_API_KEY")
val apiSecret = System.getenv("VONAGE_API_SECRET")
val signatureSecret = System.getenv("VONAGE_SIGNATURE_SECRET")
val applicationId = System.getenv("VONAGE_APPLICATION_ID")
val privateKeyPath = System.getenv("VONAGE_PRIVATE_KEY_PATH")
if (apiKey != null) apiKey(apiKey)
if (apiSecret != null) apiSecret(apiSecret)
if (signatureSecret != null) signatureSecret(signatureSecret)
if (applicationId != null) applicationId(applicationId)
if (privateKeyPath != null) privateKeyPath(privateKeyPath)
setFromEnvIfNotNull("VONAGE_API_KEY") { apiKey(it) }
setFromEnvIfNotNull("VONAGE_API_SECRET") { apiSecret(it) }
setFromEnvIfNotNull("VONAGE_SIGNATURE_SECRET") { signatureSecret(it) }
setFromEnvIfNotNull("VONAGE_APPLICATION_ID") { applicationId(it) }
setFromEnvIfNotNull("VONAGE_PRIVATE_KEY_PATH") { privateKeyPath(it) }
setFromEnvIfNotNull("VONAGE_PRIVATE_KEY_CONTENTS") { privateKeyContents(it) }
return this
}

/**
* Calls the setter function with the value of the environment variable if it is not null.
*
* @param envVar The name of the environment variable.
* @param setter The setter function to call with the value of the environment variable.
*/
private fun setFromEnvIfNotNull(envVar: String, setter: (String) -> Unit) {
val value = System.getenv(envVar)
if (value != null) setter(value)
}

/**
* Optional HTTP configuration options for the client.
*
* @param init The config options lambda.
* @return The builder.
*/
fun VonageClient.Builder.httpConfig(init: HttpConfig.Builder.() -> Unit): VonageClient.Builder {
// Workaround to prevent the default prefix being overriden
// Workaround to prevent the default prefix being overridden
val applied = HttpConfig.builder().apply(init)
val customUa = applied.build().customUserAgent
val adjustedUa = if (customUa.isNullOrBlank()) SDK_USER_AGENT else "$SDK_USER_AGENT $customUa"
Expand Down
17 changes: 16 additions & 1 deletion src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class MessagesTest : AbstractTest() {
private val caption = "Additional text to accompany the media"
private val captionMap = mapOf("caption" to caption)


private fun testSend(expectedBodyParams: Map<String, Any>, req: MessageRequest) {
mockPost(expectedUrl = sendUrl, status = 202, authType = authType,
expectedRequestParams = expectedBodyParams,
Expand Down Expand Up @@ -661,4 +660,20 @@ class MessagesTest : AbstractTest() {
assertEquals(networkCode, parsed.destinationNetworkCode)
assertEquals(smsCount, parsed.smsTotalCount)
}

@Test
fun `send sandbox real request fails with 401`() {
try {
client.send(
smsText { from(altNumber); to(toNumber); text(text) },
sandbox = true
)
}
catch (ex: MessageResponseException) {
assertEquals(401, ex.statusCode)
assertNotNull(ex.title)
assertNotNull(ex.detail)
assertNotNull(ex.instance)
}
}
}
18 changes: 18 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/SmsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,22 @@ class SmsTest : AbstractTest() {
assertEquals("Number De-activated.", response[offset].errorText)
assertEquals(OK, response[++offset].status)
}

@Test
fun `wasSuccessfullySent with empty response returns false`() {
mockPostQueryParams(sendUrl,
expectedRequestParams = mapOf(
"from" to from, "to" to toNumber,
"text" to text, "type" to "text",
),
authType = AuthType.API_KEY_SECRET_HEADER,
expectedResponseParams = mapOf(
"message-count" to "-1",
"messages" to emptyList<SmsSubmissionResponseMessage>()
)
)
val response = client.sendText(from, toNumber, text)
assertNotNull(response)
assertFalse(response.wasSuccessfullySent())
}
}

0 comments on commit 4f29b94

Please sign in to comment.