Skip to content

Commit

Permalink
ETTERLATTE-API tester for tokenx/selvbetjening (#6587)
Browse files Browse the repository at this point in the history
* Lage tester for tokenx/selvbetjening

* Fjern unødvendig claim
  • Loading branch information
sebassonav authored Dec 9, 2024
1 parent c3a362b commit 319c01e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.etterlatte
import io.ktor.server.application.install
import no.nav.etterlatte.behandling.sak.behandlingSakRoutes
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.common.appName
import no.nav.etterlatte.libs.common.logging.sikkerLoggOppstart
import no.nav.etterlatte.libs.ktor.initialisering.initEmbeddedServer
import no.nav.etterlatte.libs.ktor.initialisering.run
Expand Down Expand Up @@ -30,6 +31,7 @@ class Server(
samordningVedtakRoute(
samordningVedtakService = applicationContext.samordningVedtakService,
config = applicationContext.config,
appname = appName()!!,
)

barnepensjonVedtakRoute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import no.nav.etterlatte.libs.ktor.token.hentTokenClaimsForIssuerName
fun Route.samordningVedtakRoute(
samordningVedtakService: SamordningVedtakService,
config: Config,
appname: String,
) {
route("api/vedtak") {
install(MaskinportenScopeAuthorizationPlugin) {
Expand Down Expand Up @@ -117,7 +118,7 @@ fun Route.samordningVedtakRoute(
accessPolicyRolesEllerAdGrupper = setOf("les-oms-vedtak", config.getString("roller.pensjon-saksbehandler"))
issuers = setOf(Issuer.AZURE.issuerName)
}
install(SelvbetjeningAuthorizationPlugin) {
install(selvbetjeningAuthorizationPlugin(appname)) {
validator = { fnr, borger -> borger.value == fnr.value }
issuer = Issuer.TOKENX.issuerName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.ktor.server.application.log
import io.ktor.server.auth.AuthenticationChecked
import io.ktor.server.auth.principal
import io.ktor.server.request.receive
import no.nav.etterlatte.libs.common.appName
import no.nav.etterlatte.libs.common.feilhaandtering.IkkeTillattException
import no.nav.etterlatte.libs.common.logging.getCorrelationId
import no.nav.etterlatte.libs.common.person.Folkeregisteridentifikator
Expand All @@ -17,7 +16,7 @@ import no.nav.security.token.support.v2.TokenValidationContextPrincipal
/**
* Sjekk av at bruker kun spør etter egne data
*/
val SelvbetjeningAuthorizationPlugin =
fun selvbetjeningAuthorizationPlugin(appname: String) =
createRouteScopedPlugin(
name = "SelvbetjeningAuthorizationPlugin",
createConfiguration = ::PluginConfiguration,
Expand All @@ -30,7 +29,7 @@ val SelvbetjeningAuthorizationPlugin =
if (principal.context.issuers.contains(issuer)) {
val subject = principal.context.getClaims(pluginConfig.issuer).subject
val fnr =
when (appName()?.lowercase()) {
when (appname.lowercase()) {
"etterlatte-samordning-vedtak" -> call.fnr
"etterlatte-api" -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package no.nav.etterlatte.samordning.vedtak
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import io.kotest.matchers.shouldBe
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.parameter
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
Expand All @@ -19,6 +22,7 @@ import io.mockk.mockk
import no.nav.etterlatte.ktor.runServerWithConfig
import no.nav.etterlatte.ktor.startRandomPort
import no.nav.etterlatte.libs.common.person.Folkeregisteridentifikator
import no.nav.etterlatte.libs.ktor.route.FoedselsnummerDTO
import no.nav.etterlatte.libs.ktor.token.APP
import no.nav.etterlatte.libs.ktor.token.Claims
import no.nav.etterlatte.libs.ktor.token.Issuer
Expand Down Expand Up @@ -185,7 +189,7 @@ class SamordningVedtakRouteTest {
@Nested
inner class PensjonApi {
private val virkFom = LocalDate.now()
private val fnr = "01448203510"
private val fnr = "06076941937"

@BeforeEach
fun before() {
Expand Down Expand Up @@ -273,14 +277,135 @@ class SamordningVedtakRouteTest {
}
}

private fun ApplicationTestBuilder.samordningVedtakApi() {
@Nested
inner class SamordningApi {
private val virkFom = LocalDate.now()
private val fnr = "06237240748"

@BeforeEach
fun before() {
config = config(mockOAuth2Server.config.httpServer.port(), Issuer.TOKENX.issuerName)
}

@Test
fun `Skal gi bad request hvis appnavn er etterlatte-samordning-vedtak`() {
coEvery {
samordningVedtakService.hentVedtaksliste(
fomDato = virkFom,
fnr = Folkeregisteridentifikator.of(fnr),
context = PensjonContext,
)
} returns
listOf(opprettSamordningVedtakDto())

testApplication {
val client = samordningVedtakApi("etterlatte-samordning-vedtak")
val response =
client.post("/api/pensjon/vedtak") {
parameter("fomDato", virkFom)
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Authorization, "Bearer ${tokenxtoken(fnr)}")
setBody(FoedselsnummerDTO(fnr))
}

response.status shouldBe HttpStatusCode.BadRequest
coVerify(exactly = 0) {
samordningVedtakService.hentVedtaksliste(
fomDato = virkFom,
fnr = Folkeregisteridentifikator.of(fnr),
context = PensjonContext,
)
}
}
}

@Test
fun `skal gi 400 BAD request, med gyldig token selvbetjening tokenx hvis fnr mangler i body`() {
coEvery {
samordningVedtakService.hentVedtaksliste(
fomDato = virkFom,
fnr = Folkeregisteridentifikator.of(fnr),
context = PensjonContext,
)
} returns
listOf(opprettSamordningVedtakDto())

testApplication {
val client = samordningVedtakApi("etterlatte-api")
val response =
client.post("/api/pensjon/vedtak") {
parameter("fomDato", virkFom)
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Authorization, "Bearer ${tokenxtoken(fnr)}")
}

response.status shouldBe HttpStatusCode.BadRequest
coVerify(exactly = 0) {
samordningVedtakService.hentVedtaksliste(
fomDato = virkFom,
fnr = Folkeregisteridentifikator.of(fnr),
context = PensjonContext,
)
}
}
}

@Test
fun `skal gi 200 med gyldig token selvbetjening tokenx og fnr i body`() {
coEvery {
samordningVedtakService.hentVedtaksliste(
fomDato = virkFom,
fnr = Folkeregisteridentifikator.of(fnr),
context = PensjonContext,
)
} returns
listOf(opprettSamordningVedtakDto())

testApplication {
val client = samordningVedtakApi("etterlatte-api")
val response =
client.post("/api/pensjon/vedtak") {
parameter("fomDato", virkFom)
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Authorization, "Bearer ${tokenxtoken(fnr)}")
setBody(FoedselsnummerDTO(fnr))
}

response.status shouldBe HttpStatusCode.OK
coVerify {
samordningVedtakService.hentVedtaksliste(
fomDato = virkFom,
fnr = Folkeregisteridentifikator.of(fnr),
context = PensjonContext,
)
}
}
}

private fun tokenxtoken(fnr: String): String {
val claimSet =
tokenMedClaims(
mapOf(
Claims.sub to fnr,
),
)

return mockOAuth2Server
.issueToken(
issuerId = Issuer.TOKENX.issuerName,
claims = claimSet.allClaims,
).serialize()
}
}

private fun ApplicationTestBuilder.samordningVedtakApi(appname: String? = null): HttpClient =
runServerWithConfig(applicationConfig = config) {
samordningVedtakRoute(
samordningVedtakService = samordningVedtakService,
config = config,
appname = appname ?: "etterlatte-samordning-vedtak",
)
}
}

@AfterEach
fun afterEach() {
Expand Down

0 comments on commit 319c01e

Please sign in to comment.