Skip to content

Commit

Permalink
chore: trying to fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaflaten committed Nov 28, 2024
1 parent 743be82 commit 80ac09c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.sykdig.digitalisering.papirsykmelding
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import kotlinx.coroutines.runBlocking
import no.nav.sykdig.IntegrationTest
import no.nav.sykdig.config.AadRestTemplateConfiguration
import no.nav.sykdig.digitalisering.SykDigOppgaveService
import no.nav.sykdig.digitalisering.papirsykmelding.api.model.AvvisSykmeldingRequest
import no.nav.sykdig.digitalisering.papirsykmelding.api.model.PapirManuellOppgave
Expand All @@ -20,11 +21,19 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mockito
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.jwt.Jwt
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.client.MockRestServiceServer
import org.springframework.test.web.client.match.MockRestRequestMatchers.method
import org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo
import org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess
import org.springframework.web.client.RestTemplate
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.OffsetDateTime
Expand All @@ -44,35 +53,46 @@ class NasjonalOppgaveServiceTest : IntegrationTest() {
@MockBean
lateinit var oppgaveSecurityService: OppgaveSecurityService

@Autowired
@Qualifier("smregisteringRestTemplate")
private lateinit var restTemplate: RestTemplate

@BeforeEach
fun setUp() {
// mockJwtAuthentication()
mockJwtAuthentication()
val mockServer = MockRestServiceServer.createServer(restTemplate)

mockServer.expect(requestTo("http://localhost:8081/azureator/token"))
.andExpect(method(HttpMethod.POST))
.andRespond(withSuccess("{\"access_token\": \"dummy-token\"}", MediaType.APPLICATION_JSON))

nasjonalOppgaveRepository.deleteAll()
}


// @Test
// fun `avvis oppgave blir oppdatert og lagra i DB`() = runBlocking {
// val oppgaveId = 123
// val request = mapper.writeValueAsString(AvvisSykmeldingRequest(reason = "MANGLENDE_DIAGNOSE"))
// val originalOppgave = nasjonalOppgaveService.lagreOppgave(testDataPapirManuellOppgave())
// Mockito.`when`(sykdigOppgaveService.getOppgave(oppgaveId.toString())).thenReturn(testDataOppgaveDbModel(oppgaveId))
//// Mockito.doNothing().`when`(journalpostService).ferdigstillNasjonalAvvistOppgave(
//// oppgaveId,
//// "authorization",
//// "navEnhet",
//// "navEpost",
//// "avvisningsgrunn",
//// "veilederIdent"
//// )
@Test
fun `avvis oppgave blir oppdatert og lagra i DB`() = runBlocking {
val oppgaveId = 123
val request = mapper.writeValueAsString(AvvisSykmeldingRequest(reason = "MANGLENDE_DIAGNOSE"))
val originalOppgave = nasjonalOppgaveService.lagreOppgave(testDataPapirManuellOppgave())
Mockito.`when`(sykdigOppgaveService.getOppgave(oppgaveId.toString())).thenReturn(testDataOppgaveDbModel(oppgaveId))
// Mockito.doNothing().`when`(journalpostService).ferdigstillNasjonalAvvistOppgave(
// oppgaveId,
// "authorization",
// "navEnhet",
// "navEpost",
// "avvisningsgrunn",
// "veilederIdent"
// )
// Mockito.`when`(oppgaveSecurityService.getNavIdent()).thenReturn(Veileder("veilederIdent"))
// assertTrue(originalOppgave.avvisningsgrunn == null)
// val avvistOppgave = nasjonalOppgaveService.avvisOppgave(oppgaveId, request, "auth streng", "enhet")
// assertEquals(testDataNasjonalManuellOppgaveDAO(null, "456", oppgaveId).oppgaveId, avvistOppgave.body?.oppgaveId ?: 123 )
// assertTrue(avvistOppgave.body?.avvisningsgrunn == "MANGLENDE_DIAGNOSE")
// assertEquals(avvistOppgave.body?.id, originalOppgave.id)
//
// }
Mockito.`when`(oppgaveSecurityService.getNavEmail()).thenReturn("veilederIdent")
assertTrue(originalOppgave.avvisningsgrunn == null)
val avvistOppgave = nasjonalOppgaveService.avvisOppgave(oppgaveId, request, "auth streng", "enhet")
assertEquals(testDataNasjonalManuellOppgaveDAO(null, "456", oppgaveId).oppgaveId, avvistOppgave.body?.oppgaveId ?: 123 )
assertTrue(avvistOppgave.body?.avvisningsgrunn == "MANGLENDE_DIAGNOSE")
assertEquals(avvistOppgave.body?.id, originalOppgave.id)

}


@Test
Expand Down Expand Up @@ -203,15 +223,15 @@ class NasjonalOppgaveServiceTest : IntegrationTest() {
)
}

// fun mockJwtAuthentication() {
// val jwt = Jwt.withTokenValue("dummy-token")
// .header("alg", "none")
// .claim("sub", "test-user")
// .claim("scope", "test-scope")
// .build()
// val authentication = JwtAuthenticationToken(jwt)
// val securityContext = SecurityContextHolder.createEmptyContext()
// securityContext.authentication = authentication
// SecurityContextHolder.setContext(securityContext)
// }
fun mockJwtAuthentication() {
val jwt = Jwt.withTokenValue("dummy-token")
.header("alg", "none")
.claim("sub", "test-user")
.claim("scope", "test-scope")
.build()
val authentication = JwtAuthenticationToken(jwt)
val securityContext = SecurityContextHolder.createEmptyContext()
securityContext.authentication = authentication
SecurityContextHolder.setContext(securityContext)
}
}
3 changes: 3 additions & 0 deletions src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ helsenett.url: http://syfohelsenettproxy
smtss.url: http://smtss
regel.url: http://regel

mock-oauth2-server:
port: 8080

no.nav.security.jwt:
issuer:
azureator:
Expand Down

0 comments on commit 80ac09c

Please sign in to comment.