Skip to content

Commit

Permalink
tests for the engine itself
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Oct 21, 2023
1 parent 6d9485e commit 7c93da0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +22,6 @@ class CompositeLoginWorkflowEngine(
response: HttpServletResponse,
authentication: Authentication
) {
SecurityContextHolder.getContext().authentication = authentication
this.defaultSuccessHandler.onAuthenticationSuccess(request, response, authentication)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 7c93da0

Please sign in to comment.