Skip to content

Commit

Permalink
[BTD-556] New role added
Browse files Browse the repository at this point in the history
  • Loading branch information
paddynski-moj committed Feb 26, 2025
1 parent 772a1f3 commit 85e5eaa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Keys.KEY_LEARNERS
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Keys.KEY_MATCHING
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.PERMISSIONS
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLES
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLE_LEARNERS
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLE_MATCHING

Expand Down Expand Up @@ -38,8 +38,8 @@ class OpenApiConfiguration(buildProperties: BuildProperties) {
SecurityScheme().addBearerJwtRequirement(ROLE_MATCHING),
),
)
.addSecurityItem(SecurityRequirement().addList(KEY_LEARNERS, PERMISSIONS[ROLE_LEARNERS]))
.addSecurityItem(SecurityRequirement().addList(KEY_MATCHING, PERMISSIONS[ROLE_MATCHING]))
.addSecurityItem(SecurityRequirement().addList(KEY_LEARNERS, ROLES[ROLE_LEARNERS]))
.addSecurityItem(SecurityRequirement().addList(KEY_MATCHING, ROLES[ROLE_MATCHING]))
}

private fun SecurityScheme.addBearerJwtRequirement(role: String): SecurityScheme = type(SecurityScheme.Type.HTTP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Roles {
private const val READ = "read"
private const val WRITE = "write"

val PERMISSIONS = mapOf(
val ROLES = mapOf(
ROLE_LEARNERS to listOf(READ),
ROLE_MATCHING to listOf(READ, WRITE),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import io.swagger.v3.parser.OpenAPIV3Parser
import net.minidev.json.JSONArray
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.http.MediaType
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Keys.KEY_LEARNERS
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Keys.KEY_MATCHING
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLES
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLE_LEARNERS
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLE_MATCHING

class OpenApiDocsTest : IntegrationTestBase() {
@LocalServerPort
Expand Down Expand Up @@ -82,22 +83,33 @@ class OpenApiDocsTest : IntegrationTestBase() {
}
}

@ParameterizedTest
@CsvSource(value = ["$KEY_LEARNERS, $ROLE_LEARNERS"])
fun `the security scheme is setup for bearer tokens`(key: String, role: String) {
webTestClient.get()
@Test
fun `the security scheme is setup for bearer tokens`() {
val body = webTestClient.get()
.uri("/v3/api-docs")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("$.components.securitySchemes.$key.type").isEqualTo("http")
.jsonPath("$.components.securitySchemes.$key.scheme").isEqualTo("bearer")
.jsonPath("$.components.securitySchemes.$key.description").value<String> {
assertThat(it).contains(role)
}
.jsonPath("$.components.securitySchemes.$key.bearerFormat").isEqualTo("JWT")
.jsonPath("$.security[0].$key").isEqualTo(JSONArray().apply { this.add("read") })

val keys = mapOf(
KEY_LEARNERS to ROLE_LEARNERS,
KEY_MATCHING to ROLE_MATCHING,
)

var index = 0
keys.forEach { entry ->
val key = entry.key
val role = entry.value
body
.jsonPath("$.components.securitySchemes.$key.type").isEqualTo("http")
.jsonPath("$.components.securitySchemes.$key.scheme").isEqualTo("bearer")
.jsonPath("$.components.securitySchemes.$key.description").value<String> {
assertThat(it).contains(role)
}
.jsonPath("$.components.securitySchemes.$key.bearerFormat").isEqualTo("JWT")
.jsonPath("$.security[${index++}].$key").isEqualTo(JSONArray().apply { this.addAll(ROLES.get(role) ?: listOf()) })
}
}

@Test
Expand Down

0 comments on commit 85e5eaa

Please sign in to comment.