Skip to content

Commit

Permalink
Add Tests for ClaimsParser
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLydonKing committed Jan 2, 2024
1 parent 1364175 commit 99313ed
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object ClaimsParser {
*/
def getExpiration(jwt: Jwt): Instant = {
getClaim(jwt, "exp") match {
case Some(expiration) => Instant.ofEpochSecond(expiration.toString.toLong)
case Some(expiration) => Instant.parse(expiration.toString)
case None => throw new Exception("Expiration not found")
}
}
Expand All @@ -110,7 +110,7 @@ object ClaimsParser {
*/
def getIssueTime(jwt: Jwt): Instant = {
getClaim(jwt, "iat") match {
case Some(issueTime) => Instant.ofEpochSecond(issueTime.toString.toLong)
case Some(issueTime) => Instant.parse(issueTime.toString)
case None => throw new Exception("Issue time not found")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.loginclient.authorization

import io.jsonwebtoken.Jwts
import org.springframework.security.oauth2.jwt.Jwt
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.time.Instant
import java.util
import java.util.Date

class ClaimsParserTest extends AnyFlatSpec with Matchers {

private val subject = "testUser"
private val expiration = Date.from(Instant.now().plus(java.time.Duration.ofHours(1)))
private val issuedAt = Date.from(Instant.now())
private val groupsClaim: util.ArrayList[String] = new util.ArrayList()
private val email = "[email protected]"
private val displayName = "Test User"
private val tokenType = "access"

groupsClaim.add("group1")
groupsClaim.add("group2")

private val token = Jwts.builder().compact()

private val jwt = Jwt.withTokenValue(token)
.header("alg", "none")
.subject(subject)
.expiresAt(expiration.toInstant)
.issuedAt(issuedAt.toInstant)
.claim("groups", groupsClaim)
.claim("email", email)
.claim("displayname", displayName)
.claim("type", tokenType)build()

it should "return a subject that equals 'testUser'" in {
val sub = ClaimsParser.getSubject(jwt)
assert(sub.equals(subject))
}

it should "return a list of groups that contain 'group1', 'group2')" in {
val groups = ClaimsParser.getGroups(jwt)
assert(groups == List("group1", "group2"))
}

it should "return a expiration date that should be within an hour of testing" in {
val exp = ClaimsParser.getExpiration(jwt)
assert(exp.equals(expiration.toInstant))
}

it should "return the set time of issue" in {
val issueTime = ClaimsParser.getIssueTime(jwt)
assert(issueTime.equals(issuedAt.toInstant))
}

it should "return the email address" in {
val email = ClaimsParser.getEmail(jwt)
assert(email.equals(email))
}

it should "return the display name" in {
val displayName = ClaimsParser.getDisplayName(jwt)
assert(displayName.equals(displayName))
}

it should "return the token type" in {
val tokenType = ClaimsParser.getTokenType(jwt)
assert(tokenType.equals(tokenType))
}
}
6 changes: 6 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,18 @@ object Dependencies {
jwtDecoder,
bouncyCastle,

jjwtApi,
jjwtImpl,
jjwtJackson,

jsonParser,

requests,

springBootWeb,
springBootSecurity,

scalaTest
)

def exampleDependencies: Seq[ModuleID] = Seq(
Expand Down

0 comments on commit 99313ed

Please sign in to comment.