Skip to content

Commit

Permalink
Fix JWT mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
anotheroneofthese committed Aug 28, 2024
1 parent 7bd090e commit ac9c930
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ class OrderController(val orderService: OrderService) {
@AuthenticationPrincipal jwt: JwtAuthenticationToken,
@PathVariable("hostName") hostName: HostName,
@PathVariable("hostOrderId") hostOrderId: String
): ResponseEntity<Order> = orderService.getOrder(jwt, hostName, hostOrderId)
): ResponseEntity<Order> = orderService.getOrder(jwt.name, hostName, hostOrderId)

// TODO - Move this into a utility class
companion object {
fun throwIfHostInvalid(
clientName: String,
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/no/nb/mlt/wls/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import no.nb.mlt.wls.order.payloads.toSynqPayload
import no.nb.mlt.wls.order.repository.OrderRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException
import org.springframework.web.server.ServerErrorException
Expand All @@ -29,10 +28,10 @@ class OrderService(val db: OrderRepository, val synqService: SynqOrderService) {
* Creates an order within the WLS database, and sends it to the appropriate storage systems
*/
suspend fun createOrder(
hostName: String,
clientName: String,
payload: ApiOrderPayload
): ResponseEntity<ApiOrderPayload> {
OrderController.throwIfHostInvalid(hostName, payload.hostName)
OrderController.throwIfHostInvalid(clientName, payload.hostName)
throwIfInvalidPayload(payload)

val existingOrder =
Expand Down Expand Up @@ -81,10 +80,11 @@ class OrderService(val db: OrderRepository, val synqService: SynqOrderService) {
* Gets an order from the WLS database
*/
suspend fun getOrder(
jwt: JwtAuthenticationToken,
clientName: String,
hostName: HostName,
hostOrderId: String
): ResponseEntity<Order> {
OrderController.throwIfHostInvalid(clientName, hostName)
val order =
db.findByHostNameAndHostOrderId(hostName, hostOrderId)
.awaitSingleOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import no.nb.mlt.wls.order.model.ProductLine
import no.nb.mlt.wls.order.payloads.ApiOrderPayload
import no.nb.mlt.wls.order.payloads.toOrder
import no.nb.mlt.wls.order.repository.OrderRepository
import no.nb.mlt.wls.order.service.OrderService
import no.nb.mlt.wls.order.service.SynqOrderService
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
Expand All @@ -30,35 +29,41 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.context.ApplicationContext
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockJwt
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.expectBody
import java.net.URI

// FIXME - Correctly handle JWT in tests
@EnableTestcontainers
@TestInstance(PER_CLASS)
@AutoConfigureWebTestClient
@ExtendWith(MockKExtension::class)
@EnableMongoRepositories("no.nb.mlt.wls")
@SpringBootTest(webEnvironment = RANDOM_PORT)
class OrderControllerTest(
@Autowired val repository: OrderRepository
@Autowired val repository: OrderRepository,
@Autowired val applicationContext: ApplicationContext
) {
@MockkBean
private lateinit var synqOrderService: SynqOrderService

private lateinit var webTestClient: WebTestClient

val clientName: String = HostName.AXIELL.name

@BeforeEach
fun setUp() {
webTestClient =
WebTestClient
.bindToController(OrderController(OrderService(repository, synqOrderService)))
.bindToApplicationContext(applicationContext)
.apply(springSecurity())
.configureClient()
.baseUrl("/v1/order")
.build()
Expand All @@ -67,7 +72,6 @@ class OrderControllerTest(
}

@Test
@WithMockUser
fun `createOrder with valid payload creates order`() =
runTest {
coEvery {
Expand All @@ -76,6 +80,7 @@ class OrderControllerTest(

webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.post()
.uri("/batch/create")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -92,10 +97,10 @@ class OrderControllerTest(
}

@Test
@WithMockUser
fun `createOrder with duplicate payload returns OK`() {
webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.post()
.uri("/batch/create")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -111,10 +116,10 @@ class OrderControllerTest(
}

@Test
@WithMockUser
fun `createOrder payload with different data but same ID returns DB entry`() {
webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.post()
.uri("/batch/create")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -138,6 +143,7 @@ class OrderControllerTest(

webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.post()
.uri("/batch/create")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -156,6 +162,7 @@ class OrderControllerTest(

webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.post()
.uri("/batch/create")
.accept(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit ac9c930

Please sign in to comment.