-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1364175
commit 99313ed
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
clientLibrary/src/test/scala/za/co/absa/loginclient/authorization/ClaimsParserTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters