Skip to content

Commit

Permalink
La til tester for tilgangskontroll klient og fikset bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsmsa committed Dec 16, 2024
1 parent eb40ca9 commit a2042f5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
1 change: 1 addition & 0 deletions apps/tilgangskontroll/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
testImplementation(libs.test.kotest.assertionsCore)
testImplementation(libs.test.mockOauth2Server)
testImplementation(libs.test.mockk.core)
testImplementation(project(":lib:tilgangskontroll-client"))
}
sourceSets {
main {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.kotest.common.runBlocking
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import io.ktor.client.call.body
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.request.bearerAuth
Expand All @@ -23,8 +24,11 @@ import io.ktor.server.auth.authenticate
import io.ktor.server.routing.routing
import io.ktor.server.testing.testApplication
import no.nav.paw.config.hoplite.loadNaisOrLocalConfiguration
import no.nav.paw.error.model.Data
import no.nav.paw.tilgangskontroll.api.models.TilgangskontrollRequestV1
import no.nav.paw.tilgangskontroll.api.models.TilgangskontrollResponseV1
import no.nav.paw.tilgangskontroll.client.TilgangskontrollClientConfig
import no.nav.paw.tilgangskontroll.client.tilgangsTjenesteForAnsatte
import no.nav.paw.tilgangskontroll.ktorserver.AuthProvider
import no.nav.paw.tilgangskontroll.ktorserver.AuthProviderConfig
import no.nav.paw.tilgangskontroll.ktorserver.AuthProviders
Expand All @@ -48,15 +52,7 @@ class TilgangskontrollTest: FreeSpec({
mockOAuthServer.shutdown()
}
val map = ConcurrentHashMap<Triple<EntraId, Identitetsnummer, Tilgang>, Boolean>()
val service = object: TilgangsTjenesteForAnsatte {
override suspend fun harAnsattTilgangTilPerson(
navIdent: EntraId,
identitetsnummer: Identitetsnummer,
tilgang: Tilgang
): Boolean {
return map[Triple(navIdent, identitetsnummer, tilgang)] ?: false
}
}
val service = tilgangsTjenesteMock(map)

"Verifiser applikasjonsflyt".config(enabled = true) {
val ansatt = NavAnsatt(UUID.randomUUID(), "Z123")
Expand All @@ -76,46 +72,53 @@ class TilgangskontrollTest: FreeSpec({
}
}
val client = createClient {
defaultRequest {
bearerAuth(token.serialize())
headers {
append(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) {
jackson {
registerKotlinModule()
registerModule(JavaTimeModule())
}
}
}
client.post("/api/v1/tilgang") {
setBody(TilgangskontrollRequestV1(
identitetsnummer = person.value,
navAnsattId = ansatt.azureId,
tilgang = TilgangskontrollRequestV1.Tilgang.LESE
))
} should { response ->
response.status shouldBe HttpStatusCode.OK
val body = runBlocking { response.body<TilgangskontrollResponseV1>() }
body.harTilgang shouldBe true
val tilgangskontrollKlient = tilgangsTjenesteForAnsatte(
httpClient = client,
config = TilgangskontrollClientConfig(
uri = "",
scope = "MOCK"
),
tokenProvider = { token.serialize() }
)
tilgangskontrollKlient.harAnsattTilgangTilPerson(
navIdent = no.nav.paw.model.EntraId(ansatt.azureId),
identitetsnummer = no.nav.paw.model.Identitetsnummer(person.value),
tilgang = no.nav.paw.tilgangskontroll.client.Tilgang.LESE
) should { response ->
response.shouldBeInstanceOf<Data<Boolean>>()
response.data shouldBe true
}
client.post("/api/v1/tilgang") {
setBody(TilgangskontrollRequestV1(
identitetsnummer = person.value,
navAnsattId = ansatt.azureId,
tilgang = TilgangskontrollRequestV1.Tilgang.SKRIVE
))
} should { response ->
response.status shouldBe HttpStatusCode.OK
val body = runBlocking { response.body<TilgangskontrollResponseV1>() }
body.harTilgang shouldBe false
tilgangskontrollKlient.harAnsattTilgangTilPerson(
navIdent = no.nav.paw.model.EntraId(ansatt.azureId),
identitetsnummer = no.nav.paw.model.Identitetsnummer(person.value),
tilgang = no.nav.paw.tilgangskontroll.client.Tilgang.SKRIVE
) should { response ->
response.shouldBeInstanceOf<Data<Boolean>>()
response.data shouldBe false
}
}
}

})

private fun tilgangsTjenesteMock(map: ConcurrentHashMap<Triple<EntraId, Identitetsnummer, Tilgang>, Boolean>) =
object : TilgangsTjenesteForAnsatte {
override suspend fun harAnsattTilgangTilPerson(
navIdent: EntraId,
identitetsnummer: Identitetsnummer,
tilgang: Tilgang
): Boolean {
return map[Triple(navIdent, identitetsnummer, tilgang)] ?: false
}
}


fun Application.configureAuthentication(
oAuth2Server: MockOAuth2Server,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import no.nav.paw.model.EntraId
import no.nav.paw.model.Identitetsnummer
import no.nav.paw.tilgangskontroll.server.models.TilgangskontrollRequestV1
import no.nav.paw.error.model.Response
import no.nav.paw.error.model.map
import no.nav.paw.tilgangskontroll.server.models.TilgangskontrollResponseV1
import java.util.*

interface TilgangsTjenesteForAnsatte {
Expand All @@ -40,15 +42,15 @@ private class TilgangsTjenesteForAnsatteImpl(
config: TilgangskontrollClientConfig,
tokenPrivder: (String) -> String
) : TilgangsTjenesteForAnsatte {
private val apiTilgangV1 = config.apiTilgangV1().toURL()
private val apiTilgangV1 = config.apiTilgangV1()
private val tokenProvider = { tokenPrivder(config.scope) }

override suspend fun harAnsattTilgangTilPerson(
navIdent: EntraId,
identitetsnummer: Identitetsnummer,
tilgang: Tilgang
): Response<Boolean> {
val response = httpClient.post(apiTilgangV1) {
val response = httpClient.post(apiTilgangV1.toString()) {
bearerAuth(tokenProvider())
contentType(ContentType.Application.Json)
setBody(TilgangskontrollRequestV1(
Expand All @@ -57,7 +59,8 @@ private class TilgangsTjenesteForAnsatteImpl(
tilgang = tilgang.toApi()
))
}
return mapResponse(response)
return mapResponse<TilgangskontrollResponseV1>(response)
.map { it.harTilgang }
}
}

Expand Down

0 comments on commit a2042f5

Please sign in to comment.