Skip to content

Commit

Permalink
IS-1947: Add endpoint to assign dialogmote to other veileder
Browse files Browse the repository at this point in the history
  • Loading branch information
vetlesolgaard committed Jan 31, 2024
1 parent 525a42e commit 5860ce8
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class DialogmoteService(
}
}

fun overtaMoter(veilederIdent: String, dialogmoter: List<Dialogmote>) {
fun tildelMoter(veilederIdent: String, dialogmoter: List<Dialogmote>) {
database.connection.use { connection ->
dialogmoter.forEach { dialogmote ->
connection.updateMoteTildeltVeileder(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.nav.syfo.dialogmote.api.domain

import java.util.UUID

data class TildelDialogmoterDTO(
val veilederIdent: String,
val dialogmoteUuids: List<UUID>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const val dialogmoteApiMoteFerdigstillPath = "/ferdigstill"
const val dialogmoteApiMoteEndreFerdigstiltPath = "/endreferdigstilt"
const val dialogmoteApiMoteTidStedPath = "/tidsted"
const val dialogmoteActionsApiOvertaPath = "/overta"
const val dialogmoteTildelPath = "/tildel"

fun Route.registerDialogmoteActionsApiV2(
dialogmoteService: DialogmoteService,
Expand Down Expand Up @@ -176,7 +177,7 @@ fun Route.registerDialogmoteActionsApiV2(
callId
)
) {
dialogmoteService.overtaMoter(getNAVIdentFromToken(token), dialogmoter)
dialogmoteService.tildelMoter(getNAVIdentFromToken(token), dialogmoter)
call.respond(HttpStatusCode.OK)
} else {
val accessDeniedMessage = "Denied Veileder access to Dialogmøter for Person with PersonIdent"
Expand All @@ -189,5 +190,37 @@ fun Route.registerDialogmoteActionsApiV2(
call.respond(HttpStatusCode.BadRequest, e.message ?: illegalArgumentMessage)
}
}

post(dialogmoteTildelPath) {
val callId = getCallId()
try {
val token = getBearerHeader()
?: throw IllegalArgumentException("No Authorization header supplied")
val (veilederIdent, dialogmoteUuids) = call.receive<TildelDialogmoterDTO>()
if (dialogmoteUuids.isEmpty()) {
throw IllegalArgumentException("No dialogmoteUuids supplied")
}

val dialogmoter = dialogmoteUuids.map { dialogmoteService.getDialogmote(it) }

if (dialogmoteTilgangService.hasAccessToAllDialogmotePersons(
personIdentList = dialogmoter.map { it.arbeidstaker.personIdent },
token,
callId
)
) {
dialogmoteService.tildelMoter(veilederIdent, dialogmoter)
call.respond(HttpStatusCode.OK)
} else {
val accessDeniedMessage = "Denied veileder access to dialogmøter for person with PersonIdent"
log.warn("$accessDeniedMessage, {}", callIdArgument(callId))
call.respond(HttpStatusCode.Forbidden, accessDeniedMessage)
}
} catch (e: IllegalArgumentException) {
val illegalArgumentMessage = "Could not tildele dialogmøter"
log.error("$illegalArgumentMessage: {}, {}", e.message, callId)
call.respond(HttpStatusCode.BadRequest, e.message ?: illegalArgumentMessage)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ package no.nav.syfo.dialogmote.api.v2

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.TestApplicationEngine
import io.ktor.server.testing.handleRequest
import io.ktor.server.testing.setBody
import io.ktor.http.*
import io.ktor.server.testing.*
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.justRun
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
package no.nav.syfo.dialogmote.api.v2

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.ktor.http.*
import io.ktor.server.testing.*
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.justRun
import io.mockk.mockk
import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptExternal
import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptStatusEnum
import no.altinn.services.serviceengine.correspondence._2009._10.ICorrespondenceAgencyExternalBasic
import no.nav.syfo.brev.esyfovarsel.EsyfovarselProducer
import no.nav.syfo.brev.esyfovarsel.NarmesteLederHendelse
import no.nav.syfo.dialogmote.api.domain.DialogmoteDTO
import no.nav.syfo.dialogmote.api.domain.TildelDialogmoterDTO
import no.nav.syfo.dialogmote.database.createNewDialogmoteWithReferences
import no.nav.syfo.testhelper.*
import no.nav.syfo.testhelper.generator.generateNewDialogmote
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTO
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.*

class TildelDialogmoteApiV2Spek : Spek({

val objectMapper: ObjectMapper = configuredJacksonMapper()

describe(TildelDialogmoteApiV2Spek::class.java.simpleName) {
with(TestApplicationEngine()) {
start()

val externalMockEnvironment = ExternalMockEnvironment.getInstance()
val database = externalMockEnvironment.database

val esyfovarselHendelse = mockk<NarmesteLederHendelse>(relaxed = true)
val esyfovarselProducerMock = mockk<EsyfovarselProducer>(relaxed = true)

val altinnMock = mockk<ICorrespondenceAgencyExternalBasic>()

val veilederCallerIdent = UserConstants.VEILEDER_IDENT
val veilederIdentTildelesMoter = UserConstants.VEILEDER_IDENT_2
val veilederCallerToken = generateJWTNavIdent(
externalMockEnvironment.environment.aadAppClient,
externalMockEnvironment.wellKnownVeilederV2.issuer,
veilederCallerIdent,
)

application.testApiModule(
externalMockEnvironment = externalMockEnvironment,
altinnMock = altinnMock,
esyfovarselProducer = esyfovarselProducerMock,
)

beforeEachGroup {
database.dropData()
}
beforeEachTest {
val altinnResponse = ReceiptExternal()
altinnResponse.receiptStatusCode = ReceiptStatusEnum.OK

justRun { esyfovarselProducerMock.sendVarselToEsyfovarsel(esyfovarselHendelse) }
clearMocks(altinnMock)
every {
altinnMock.insertCorrespondenceBasicV2(any(), any(), any(), any(), any())
} returns altinnResponse
}
afterGroup { database.dropData() }

describe("Tildel dialogmoter") {
val urlMote = "$dialogmoteApiV2Basepath$dialogmoteApiPersonIdentUrlPath"
val urlMoterEnhet = "$dialogmoteApiV2Basepath$dialogmoteApiEnhetUrlPath/${UserConstants.ENHET_NR.value}"
val urlTildelMote = "$dialogmoteApiV2Basepath$dialogmoteTildelPath"
val newDialogmoteDTO = generateNewDialogmoteDTO(UserConstants.ARBEIDSTAKER_FNR)
val newDialogmoteDTOAnnenArbeidstaker = generateNewDialogmoteDTO(UserConstants.ARBEIDSTAKER_ANNEN_FNR)

describe("Happy path") {
it("should tildele dialogmoter if request is successful") {
val createdDialogmoterUuids = mutableListOf<UUID>()

with(
handleRequest(HttpMethod.Post, urlMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(objectMapper.writeValueAsString(newDialogmoteDTO))
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
}
with(
handleRequest(HttpMethod.Post, urlMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(objectMapper.writeValueAsString(newDialogmoteDTOAnnenArbeidstaker))
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
}

with(
handleRequest(HttpMethod.Get, urlMoterEnhet) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
val dialogmoteList = objectMapper.readValue<List<DialogmoteDTO>>(response.content!!)

dialogmoteList.size shouldBeEqualTo 2
dialogmoteList.any { dialogmoteDTO -> dialogmoteDTO.tildeltVeilederIdent == veilederCallerIdent } shouldBeEqualTo true
dialogmoteList.any { dialogmoteDTO -> dialogmoteDTO.tildeltVeilederIdent == veilederIdentTildelesMoter } shouldBeEqualTo false

createdDialogmoterUuids.addAll(dialogmoteList.map { UUID.fromString(it.uuid) })
}

with(
handleRequest(HttpMethod.Post, urlTildelMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(
objectMapper.writeValueAsString(
TildelDialogmoterDTO(
veilederIdent = veilederIdentTildelesMoter,
dialogmoteUuids = createdDialogmoterUuids
)
)
)
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
}

with(
handleRequest(HttpMethod.Get, urlMoterEnhet) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
val dialogmoteList = objectMapper.readValue<List<DialogmoteDTO>>(response.content!!)

dialogmoteList.size shouldBeEqualTo 2
dialogmoteList.all { dialogmoteDTO -> dialogmoteDTO.tildeltVeilederIdent == veilederIdentTildelesMoter } shouldBeEqualTo true
dialogmoteList.all { dialogmoteDTO -> dialogmoteDTO.tildeltVeilederIdent == veilederCallerIdent } shouldBeEqualTo false
}
}
}

describe("Unhappy paths") {
it("should return status Unauthorized if no token is supplied") {
with(
handleRequest(HttpMethod.Post, urlTildelMote) {
}
) {
response.status() shouldBeEqualTo HttpStatusCode.Unauthorized
}
}

it("should return status Bad Request if no dialogmoteUuids supplied") {
with(
handleRequest(HttpMethod.Post, urlTildelMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(
objectMapper.writeValueAsString(
TildelDialogmoterDTO(
veilederIdent = veilederIdentTildelesMoter,
dialogmoteUuids = emptyList()
)
)
)
}
) {
response.status() shouldBeEqualTo HttpStatusCode.BadRequest
}
}

it("should return status Forbidden if denied access to dialogmøte person") {
val createdDialogmoterUuids = mutableListOf<UUID>()

val newDialogmoteNoVeilederAccess =
generateNewDialogmote(UserConstants.ARBEIDSTAKER_VEILEDER_NO_ACCESS)
database.connection.use { connection ->
val (dialogmoteIdPair) = connection.createNewDialogmoteWithReferences(
newDialogmote = newDialogmoteNoVeilederAccess
)
createdDialogmoterUuids.add(dialogmoteIdPair.second)
}

with(
handleRequest(HttpMethod.Post, urlTildelMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(
objectMapper.writeValueAsString(
TildelDialogmoterDTO(
veilederIdent = veilederIdentTildelesMoter,
dialogmoteUuids = createdDialogmoterUuids,
)
)
)
}
) {
response.status() shouldBeEqualTo HttpStatusCode.Forbidden
}
}

it("should return status Forbidden if contains dialogmøte with denied access to person") {
val createdDialogmoterUuids = mutableListOf<UUID>()

with(
handleRequest(HttpMethod.Post, urlMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(objectMapper.writeValueAsString(newDialogmoteDTO))
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
}

with(
handleRequest(HttpMethod.Get, urlMoterEnhet) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
}
) {
response.status() shouldBeEqualTo HttpStatusCode.OK
val dialogmoteList = objectMapper.readValue<List<DialogmoteDTO>>(response.content!!)

dialogmoteList.size shouldBeEqualTo 1

createdDialogmoterUuids.addAll(dialogmoteList.map { UUID.fromString(it.uuid) })
}

val newDialogmoteNoVeilederAccess =
generateNewDialogmote(UserConstants.ARBEIDSTAKER_VEILEDER_NO_ACCESS)
database.connection.use { connection ->
val (dialogmoteIdPair) = connection.createNewDialogmoteWithReferences(
newDialogmote = newDialogmoteNoVeilederAccess
)
createdDialogmoterUuids.add(dialogmoteIdPair.second)
}

with(
handleRequest(HttpMethod.Post, urlTildelMote) {
addHeader(HttpHeaders.Authorization, bearerHeader(veilederCallerToken))
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(
objectMapper.writeValueAsString(
TildelDialogmoterDTO(
veilederIdent = veilederIdentTildelesMoter,
dialogmoteUuids = createdDialogmoterUuids
)
)
)
}
) {
response.status() shouldBeEqualTo HttpStatusCode.Forbidden
}
}
}
}
}
}
})

0 comments on commit 5860ce8

Please sign in to comment.