diff --git a/build.gradle.kts b/build.gradle.kts index 43b637e..95f3b94 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/src/main/kotlin/org/jaqpot/api/auth/keycloak/KeycloakConfig.kt b/src/main/kotlin/org/jaqpot/api/auth/keycloak/KeycloakConfig.kt index 3150025..5c9ed6a 100644 --- a/src/main/kotlin/org/jaqpot/api/auth/keycloak/KeycloakConfig.kt +++ b/src/main/kotlin/org/jaqpot/api/auth/keycloak/KeycloakConfig.kt @@ -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, ) diff --git a/src/test/kotlin/org/jaqpot/api/auth/keycloak/KeycloakUserServiceTest.kt b/src/test/kotlin/org/jaqpot/api/auth/keycloak/KeycloakUserServiceTest.kt new file mode 100644 index 0000000..e7bbd92 --- /dev/null +++ b/src/test/kotlin/org/jaqpot/api/auth/keycloak/KeycloakUserServiceTest.kt @@ -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") + } +}