-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IS-2258: Add behandler system api for isfrisktilarbeid
- Loading branch information
1 parent
315bc4f
commit 67c6ab6
Showing
7 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/no/nav/syfo/behandler/api/BehandlerSystemApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package no.nav.syfo.behandler.api | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import no.nav.syfo.application.api.APISystemConsumerAccessService | ||
import no.nav.syfo.behandler.BehandlerService | ||
import no.nav.syfo.behandler.domain.* | ||
import no.nav.syfo.util.* | ||
import java.util.* | ||
|
||
const val behandlerSystemApiPath = "/api/system/v1/behandler" | ||
|
||
fun Route.registerBehandlerSystemApi( | ||
behandlerService: BehandlerService, | ||
apiConsumerAccessService: APISystemConsumerAccessService, | ||
authorizedApplicationNameList: List<String>, | ||
) { | ||
route(behandlerSystemApiPath) { | ||
get("/{$behandlerRefParam}") { | ||
val token = getBearerHeader() | ||
?: throw IllegalArgumentException("No Authorization header supplied") | ||
apiConsumerAccessService.validateSystemConsumerApplicationClientId( | ||
authorizedApplicationNameList = authorizedApplicationNameList, | ||
token = token, | ||
) | ||
|
||
val behandlerRef = UUID.fromString(this.call.parameters[behandlerRefParam]) | ||
val behandler = behandlerService.getBehandler( | ||
behandlerRef = behandlerRef | ||
) | ||
|
||
if (behandler != null) { | ||
call.respond(behandler.toBehandlerDTO(behandlerType = null)) | ||
} else { | ||
call.respond(HttpStatusCode.NotFound) | ||
} | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/test/kotlin/no/nav/syfo/behandler/api/BehandlerSystemApiSpek.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package no.nav.syfo.behandler.api | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import io.ktor.http.* | ||
import io.ktor.server.testing.* | ||
import no.nav.syfo.testhelper.* | ||
import no.nav.syfo.testhelper.UserConstants.ARBEIDSTAKER_FNR | ||
import no.nav.syfo.testhelper.UserConstants.PARTNERID | ||
import no.nav.syfo.testhelper.generator.generateBehandler | ||
import no.nav.syfo.util.bearerHeader | ||
import no.nav.syfo.util.configuredJacksonMapper | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.specification.describe | ||
import java.util.UUID | ||
|
||
class BehandlerSystemApiSpek : Spek({ | ||
val objectMapper: ObjectMapper = configuredJacksonMapper() | ||
|
||
with(TestApplicationEngine()) { | ||
start() | ||
|
||
val externalMockEnvironment = ExternalMockEnvironment.instance | ||
val database = externalMockEnvironment.database | ||
application.testApiModule(externalMockEnvironment = externalMockEnvironment) | ||
|
||
afterEachTest { | ||
database.dropData() | ||
} | ||
|
||
describe(BehandlerSystemApiSpek::class.java.simpleName) { | ||
|
||
val validToken = generateJWTSystem( | ||
audience = externalMockEnvironment.environment.aadAppClient, | ||
azp = isfrisktilarbeidClientId, | ||
issuer = externalMockEnvironment.wellKnownInternalAzureAD.issuer, | ||
) | ||
|
||
describe("Get behandler for behandlerRef") { | ||
val behandlerRef = UUID.randomUUID() | ||
val url = "$behandlerSystemApiPath/$behandlerRef" | ||
val behandler = generateBehandler( | ||
behandlerRef = behandlerRef, | ||
partnerId = PARTNERID, | ||
) | ||
describe("Happy path") { | ||
it("should return behandler for behandlerRef") { | ||
database.createBehandlerForArbeidstaker( | ||
behandler = behandler, | ||
arbeidstakerPersonident = ARBEIDSTAKER_FNR, | ||
) | ||
with( | ||
handleRequest(HttpMethod.Get, url) { | ||
addHeader(HttpHeaders.Authorization, bearerHeader(validToken)) | ||
} | ||
) { | ||
val behandlerDTO = | ||
objectMapper.readValue<BehandlerDTO>(response.content!!) | ||
behandlerDTO.behandlerRef shouldBeEqualTo behandlerRef.toString() | ||
behandlerDTO.fornavn shouldBeEqualTo "Dana" | ||
} | ||
} | ||
} | ||
describe("Unhappy path") { | ||
it("should return status NotFound for non-matching behandlerRef") { | ||
with( | ||
handleRequest(HttpMethod.Get, url) { | ||
addHeader(HttpHeaders.Authorization, bearerHeader(validToken)) | ||
} | ||
) { | ||
response.status() shouldBeEqualTo HttpStatusCode.NotFound | ||
} | ||
} | ||
it("should return status Unauthorized if no token is supplied") { | ||
with( | ||
handleRequest(HttpMethod.Get, url) {} | ||
) { | ||
response.status() shouldBeEqualTo HttpStatusCode.Unauthorized | ||
} | ||
} | ||
it("should return status BadRequest if invalid behandlerRef") { | ||
with( | ||
handleRequest(HttpMethod.Get, "$behandlerPath/123abc") { | ||
addHeader(HttpHeaders.Authorization, bearerHeader(validToken)) | ||
} | ||
) { | ||
response.status() shouldBeEqualTo HttpStatusCode.BadRequest | ||
} | ||
} | ||
it("should return status Forbidden if wrong consumer azp") { | ||
val invalidValidToken = generateJWTSystem( | ||
audience = externalMockEnvironment.environment.aadAppClient, | ||
azp = testSyfooppfolgingsplanserviceClientId, | ||
issuer = externalMockEnvironment.wellKnownInternalAzureAD.issuer, | ||
) | ||
|
||
with( | ||
handleRequest(HttpMethod.Get, url) { | ||
addHeader(HttpHeaders.Authorization, bearerHeader(invalidValidToken)) | ||
} | ||
) { | ||
response.status() shouldBeEqualTo HttpStatusCode.Forbidden | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters