Skip to content

Commit

Permalink
test: add unit tests for KeycloakUserService
Browse files Browse the repository at this point in the history
  • Loading branch information
alarv committed May 31, 2024
1 parent d2b64cf commit 545c971
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ dependencies {
testImplementation("org.testcontainers:postgresql:1.19.8")
testImplementation("com.github.dasniko:testcontainers-keycloak:3.3.1")
testImplementation("org.testcontainers:junit-jupiter:1.19.8")
// mockK
testImplementation("io.mockk:mockk:1.13.11")

}

springBoot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "keycloak")
data class KeycloakConfig(
val serverUrl: String = "",
var realm: String = "",
var clientId: String = "",
var clientSecret: String = "",
val serverUrl: String,
var realm: String,
var clientId: String,
var clientSecret: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.jaqpot.api.auth.keycloak

import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.keycloak.admin.client.Keycloak
import org.keycloak.representations.idm.UserRepresentation
import org.springframework.test.util.ReflectionTestUtils

class KeycloakUserServiceTest {

private val keycloakConfig = KeycloakConfig(
"serverUrl",
"realm",
"clientId",
"clientSecret",
)

private val userRepresentation: UserRepresentation = mockk()

private lateinit var keycloakClient: Keycloak
private lateinit var keycloakUserService: KeycloakUserService

@BeforeEach
fun setup() {
every { userRepresentation.id } returns "id"
every { userRepresentation.firstName } returns "firstName"
every { userRepresentation.lastName } returns "lastName"
keycloakClient = mockk()
keycloakUserService = KeycloakUserService(keycloakConfig)
ReflectionTestUtils.setField(keycloakUserService, "keycloakClient", keycloakClient)
}

@Test
fun getUserById() {
every { keycloakClient.realm("realm").users().get("id").toRepresentation() } returns userRepresentation
val user = keycloakUserService.getUserById("id")

assertEquals(user.name, "firstName lastName")

}

@Test
fun getUserByUsername() {
every {
keycloakClient.realm("realm").users().searchByUsername("username", true)
} returns listOf(userRepresentation)
val user = keycloakUserService.getUserByUsername("username")

assertEquals(user.name, "firstName lastName")
}

@Test
fun getUserByEmail() {
every {
keycloakClient.realm("realm").users().searchByEmail("email", true)
} returns listOf(userRepresentation)
val user = keycloakUserService.getUserByEmail("email")

assertEquals(user.name, "firstName lastName")
}
}

0 comments on commit 545c971

Please sign in to comment.