Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add JWTTokenGenerator test #2784

Merged
merged 4 commits into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/test/kotlin/net/atos/client/util/JWTTokenGeneratorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* SPDX-FileCopyrightText: 2025 Lifely
* SPDX-License-Identifier: EUPL-1.2+
*/

package net.atos.client.util

import com.auth0.jwt.JWT
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.mockk.checkUnnecessaryStub
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import net.atos.zac.authentication.LoggedInUser
import net.atos.zac.identity.model.getFullName

class JWTTokenGeneratorTest : BehaviorSpec({

beforeEach {
checkUnnecessaryStub()
}

Given("client id and secret and no logged-in user") {
val clientId = "clientId"
val clientSecret = "clientSecret"

When("token is generated") {
val token = JWTTokenGenerator.generate(clientId, clientSecret, null)

Then("it should be a valid JWT token") {
val tokenParts = token.split(" ")
tokenParts[0] shouldBe "Bearer"
val decodedToken = JWT.decode(tokenParts[1])
with(decodedToken) {
issuer shouldBe clientId
getHeaderClaim("client_identifier").asString() shouldBe clientId
}
}
}
}

Given("client credentials and logged-in user") {
val clientId = "clientId"
val clientSecret = "clientSecret"
val loggedInUser = mockk<LoggedInUser>()

every { loggedInUser.id } returns "id"
mockkStatic(LoggedInUser::getFullName)
every { loggedInUser.getFullName() } returns "fullName"

When("token is generated") {
val token = JWTTokenGenerator.generate(clientId, clientSecret, loggedInUser)

Then("it should be a valid JWT token") {
val tokenParts = token.split(" ")
tokenParts[0] shouldBe "Bearer"
val decodedToken = JWT.decode(tokenParts[1])
with(decodedToken) {
issuer shouldBe clientId
getHeaderClaim("client_identifier").asString() shouldBe clientId
getClaim("user_id").asString() shouldBe "id"
getClaim("user_representation").asString() shouldBe "fullName"
}
}
}
}
})
Loading