From 7c93da01f10ce384ca62af2b4ddc356df5e52f48 Mon Sep 17 00:00:00 2001 From: mrflick72 Date: Sat, 21 Oct 2023 17:03:54 +0200 Subject: [PATCH] tests for the engine itself --- .../login/workflow/LoginWorkflowEngine.kt | 2 - .../workflow/LoginWorkflowEngineController.kt | 6 ++- .../login/workflow/LoginWorkflowHandler.kt | 2 - .../login/workflow/LoginWorkflowEngineTest.kt | 50 +++++++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 src/test/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineTest.kt diff --git a/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngine.kt b/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngine.kt index 6c0c69ee..4a1ef4a0 100644 --- a/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngine.kt +++ b/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngine.kt @@ -4,7 +4,6 @@ import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import jakarta.servlet.http.HttpSession import org.springframework.security.core.Authentication -import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.web.authentication.AuthenticationSuccessHandler import java.util.* import kotlin.jvm.optionals.getOrElse @@ -23,7 +22,6 @@ class CompositeLoginWorkflowEngine( response: HttpServletResponse, authentication: Authentication ) { - SecurityContextHolder.getContext().authentication = authentication this.defaultSuccessHandler.onAuthenticationSuccess(request, response, authentication) } diff --git a/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineController.kt b/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineController.kt index 05c160fc..6cca4809 100644 --- a/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineController.kt +++ b/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineController.kt @@ -11,8 +11,12 @@ import org.springframework.security.web.authentication.SavedRequestAwareAuthenti import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.GetMapping +const val LOGIN_ENGINE_BROKER_PAGE = "/login-workflow" + @Controller -class LoginWorkflowEngineController(private val engine: LoginWorkflowEngine) { +class LoginWorkflowEngineController( + private val engine: LoginWorkflowEngine +) { private val logger = LoggerFactory.getLogger(LoginWorkflowEngineController::class.java) diff --git a/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowHandler.kt b/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowHandler.kt index 9645092a..4389f073 100644 --- a/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowHandler.kt +++ b/src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowHandler.kt @@ -4,8 +4,6 @@ import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse -const val LOGIN_ENGINE_BROKER_PAGE = "/login-workflow" - interface LoginWorkflowHandler { fun view(): String diff --git a/src/test/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineTest.kt b/src/test/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineTest.kt new file mode 100644 index 00000000..4f66d0db --- /dev/null +++ b/src/test/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineTest.kt @@ -0,0 +1,50 @@ +package com.vauthenticator.server.login.workflow + +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import io.mockk.just +import io.mockk.runs +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpSession +import org.junit.jupiter.api.Assertions.assertSame +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.security.web.authentication.AuthenticationSuccessHandler + +@ExtendWith(MockKExtension::class) +class LoginWorkflowEngineTest { + + @MockK + lateinit var defaultSuccessHandler: AuthenticationSuccessHandler + + @MockK + lateinit var firstLoginWorkflowHandler: LoginWorkflowHandler + + @MockK + lateinit var secondLoginWorkflowHandler: LoginWorkflowHandler + + @MockK + lateinit var session: HttpSession + + @MockK + lateinit var request: HttpServletRequest + + @Test + fun `when a login workflow runs`() { + val loginWorkflowHandlers = listOf( + firstLoginWorkflowHandler, + secondLoginWorkflowHandler + ) + + every { session.getAttribute("CompositeLoginWorkflowEngine_index") } returns 0 andThen 1 andThen 2 + every { session.setAttribute("CompositeLoginWorkflowEngine_index", 1) } just runs + every { session.setAttribute("CompositeLoginWorkflowEngine_index", 2) } just runs + + val uut = CompositeLoginWorkflowEngine(loginWorkflowHandlers, defaultSuccessHandler) + + assertSame(firstLoginWorkflowHandler, uut.workflowsNextHop(session)) + assertSame(secondLoginWorkflowHandler, uut.workflowsNextHop(session)) + assertSame(DefaultLoginWorkflowHandler, uut.workflowsNextHop(session)) + } +} \ No newline at end of file