Skip to content

Commit

Permalink
Test generateToken
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Aug 26, 2024
1 parent a6f6333 commit 993a032
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/VideoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package com.vonage.client.kt

import com.auth0.jwt.JWT
import com.vonage.client.video.*
import java.net.URI
import java.time.Duration
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.time.temporal.Temporal
import java.time.temporal.TemporalUnit
import java.util.*
import kotlin.test.*

Expand Down Expand Up @@ -467,4 +473,51 @@ class VideoTest : AbstractTest() {
sessionId(sessionId); token(token); name(renderName)
})
}

@Test
fun `generate token all parameters`() {
val data = userName
val role = Role.SUBSCRIBER
val ttl = Duration.ofHours(8)
val encoded = existingSession.generateToken {
data(data); role(role); expiryLength(ttl)
initialLayoutClassList(layoutClasses)
}
val decoded = JWT.decode(encoded)
assertNotNull(decoded)
assertNotNull(decoded.id)
assertNotNull(decoded.signature)
val iat = decoded.issuedAtAsInstant
assertTrue(Instant.now().isBefore(iat.plusSeconds(15)))
assertEquals(
iat.plus(ttl).truncatedTo(ChronoUnit.SECONDS),
decoded.expiresAtAsInstant.truncatedTo(ChronoUnit.SECONDS)
)
val claims = decoded.claims
assertNotNull(claims)
assertEquals(applicationId, claims["application_id"]?.asString())
assertEquals("session.connect", claims["scope"]?.asString())
assertEquals(sessionId, claims["session_id"]?.asString())
assertEquals(data, claims["connection_data"]?.asString())
assertEquals(role.name.lowercase(), claims["role"]?.asString())
assertEquals(layoutClasses.joinToString(separator = " "), claims["initial_layout_class_list"]?.asString())
}

@Test
fun `generate token default parameters`() {
val encoded = existingSession.generateToken()
val decoded = JWT.decode(encoded)
assertNotNull(decoded)
assertNotNull(decoded.id)
assertNotNull(decoded.signature)
assertNotNull(decoded.issuedAt)
val expectedExpires = decoded.issuedAtAsInstant.plusSeconds(86400).truncatedTo(ChronoUnit.SECONDS)
assertEquals(expectedExpires, decoded.expiresAtAsInstant?.truncatedTo(ChronoUnit.SECONDS))
val claims = decoded.claims
assertNotNull(claims)
assertEquals(applicationId, claims["application_id"]?.asString())
assertEquals("session.connect", claims["scope"]?.asString())
assertEquals(sessionId, claims["session_id"]?.asString())
assertEquals(Role.PUBLISHER.toString(), claims["role"]?.asString())
}
}

0 comments on commit 993a032

Please sign in to comment.