Skip to content

Commit 76a78a7

Browse files
Jorgen-5Jorgen-5
andauthored
Get active group from Authetication in security rule (#439)
* Get active group from Authetication in security rule * Add auth to test * Format --------- Co-authored-by: Jorgen-5 <rlj@ssb.no>
1 parent e37c6ec commit 76a78a7

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/main/kotlin/no/ssb/metadata/vardef/security/VariableOwnerSecurityRule.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import io.micronaut.web.router.RouteAttributes
1414
import io.micronaut.web.router.RouteMatch
1515
import jakarta.inject.Singleton
1616
import no.ssb.metadata.vardef.constants.ACTIVE_GROUP
17-
import no.ssb.metadata.vardef.constants.LABID_ACTIVE_GROUP
1817
import no.ssb.metadata.vardef.constants.VARIABLE_DEFINITION_ID_PATH_VARIABLE
1918
import no.ssb.metadata.vardef.models.Owner
2019
import no.ssb.metadata.vardef.models.SavedVariableDefinition
@@ -118,11 +117,8 @@ class VariableOwnerSecurityRule(
118117
// The next call is blocking, so we need to run it on another thread
119118
.publishOn(Schedulers.boundedElastic())
120119
.map { definitionId ->
121-
// Get active group from either query params or authentication claims
122-
val activeGroup =
123-
request.parameters
124-
.getFirst(ACTIVE_GROUP)
125-
.orElseGet { authentication.attributes[LABID_ACTIVE_GROUP] as? String }
120+
// Get active group from authentication attributes
121+
val activeGroup = authentication.attributes[ACTIVE_GROUP] as String?
126122
if (activeGroup == null) {
127123
logger.info("No active group found in request or authentication claims. Request: $request")
128124
return@map false

src/test/kotlin/no/ssb/metadata/vardef/controllers/validityperiods/CreateTests.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,29 @@ class CreateTests : BaseVardefTest() {
422422
.then()
423423
.statusCode(HttpStatus.BAD_REQUEST.code)
424424
}
425+
426+
@Test
427+
fun `create new validity period with active group as paramater and labid token`(spec: RequestSpecification) {
428+
spec
429+
.given()
430+
.contentType(ContentType.JSON)
431+
.body(allMandatoryFieldsChanged())
432+
.queryParam(ACTIVE_GROUP, "other-group-developers")
433+
.`when`()
434+
.post("/variable-definitions/${INCOME_TAX_VP1_P1.definitionId}/validity-periods")
435+
.then()
436+
.statusCode(201)
437+
val lastPatchInSecondToLastValidityPeriod =
438+
validityPeriods
439+
.getAsMap(INCOME_TAX_VP1_P1.definitionId)
440+
.let { it.values.elementAt(it.values.size - 2) }
441+
?.last()
442+
val lastPatch = patches.latest(INCOME_TAX_VP1_P1.definitionId)
443+
assertThat(
444+
lastPatch.validUntil,
445+
).isNull()
446+
assertThat(
447+
lastPatchInSecondToLastValidityPeriod?.validUntil,
448+
).isEqualTo(lastPatch.validFrom.minusDays(1))
449+
}
425450
}

src/test/kotlin/no/ssb/metadata/vardef/security/KeycloakTokenSupportTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.junit.jupiter.params.provider.MethodSource
1616
import java.util.stream.Stream
1717

1818
@MicronautTest
19-
class KeycloakTokenSupportTest {
19+
class KeycloakTokenSupportTest : BaseVardefTest() {
2020
@ParameterizedTest
2121
@MethodSource("variableCreatorOperations")
2222
@MethodSource("variableOwnerOperations")
@@ -138,6 +138,8 @@ class KeycloakTokenSupportTest {
138138
}
139139
spec
140140
.given()
141+
.auth()
142+
.oauth2(JwtTokenHelper.jwtTokenSigned().parsedString)
141143
.queryParam(ACTIVE_GROUP, "play-foeniks-a-developers")
142144
.`when`()
143145
.request(method, path)

0 commit comments

Comments
 (0)