Skip to content

Commit

Permalink
Controller for kontaktinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbb committed Oct 10, 2023
1 parent 2bf689d commit 306c20f
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package no.nav.arbeidsgiver.min_side.kontaktinfo

import org.springframework.web.bind.annotation.*

@RestController
class KontaktinfoController {

@PostMapping("/api/kontaktinfo/v1")
fun getKontaktinfo(@RequestBody requestBody: KontaktinfoRequest): KontaktinfoResponse {
/* TODO tilgangsstyring */
/* TODO hente kofuvi */

return KontaktinfoResponse(
underenhet = null,
hovedenhet = null,
)
}

class KontaktinfoRequest(
val virksomhetsnummer: String,
)

@Suppress("unused")
class Kontaktinfo(
val eposter: List<String>,
val telefonnummer: List<String>,
)

@Suppress("unused")
class KontaktinfoResponse(
/* null hvis ingen tilgang */
val hovedenhet: Kontaktinfo?,

/* null hvis ingen tilgang */
val underenhet: Kontaktinfo?,
)
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package no.nav.arbeidsgiver.min_side.kontaktinfo

import no.nav.arbeidsgiver.min_side.config.SecurityConfig
import no.nav.arbeidsgiver.min_side.controller.AuthenticatedUserHolder
import no.nav.arbeidsgiver.min_side.controller.SecurityMockMvcUtil.Companion.jwtWithPid
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.security.oauth2.jwt.JwtDecoder
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.ResultActionsDsl
import org.springframework.test.web.servlet.post
import org.springframework.test.web.servlet.request.RequestPostProcessor

@MockBean(JwtDecoder::class)
@WebMvcTest(
value = [
KontaktinfoController::class,
SecurityConfig::class,
AuthenticatedUserHolder::class,
],
properties = [
"server.servlet.context-path=/"
]
)
class KontaktinfoControllerTest {
@Autowired
lateinit var mockMvc: MockMvc

@Test
fun protocolFormat() {
mockMvc.kontaktinfo(
content = """{ "virksomhetsnummer": "123456789" }"""
).andExpect {
status { isOk() }
content {
contentType(APPLICATION_JSON)
json("""
{
"hovedenhet": null,
"underenhet": null
}
""")}
}
}

@Test
fun virksomhetsnummerAsNumberFails() {
/* spring's objectmapper konverterer numbers til strings. */
mockMvc.kontaktinfo(
content = """{ "virksomhetsnummer": 123456789 }"""
).andExpect {
status { isOk() }
}
}


@Test
fun wrongJsonInRequest() {
mockMvc.kontaktinfo(
content = """{ }"""
).andExpect {
status { isBadRequest() }
}
}


@Test
fun superflousJsonFields() {
/* spring's objectmapper godtar ekstra felter. */
mockMvc.kontaktinfo(
content = """{ "virksomhetsnummer": "12341234", "garbage": 2 }"""
).andExpect {
status { isOk() }
}
}

@Test
fun disallowAcceptXML() {
mockMvc.kontaktinfo(
content = """{ "virksomhetsnummer": "12341234" }""",
accept = MediaType.APPLICATION_XML
).andExpect {
status { is4xxClientError() }
}
}

private fun MockMvc.kontaktinfo(
contentType: MediaType? = APPLICATION_JSON,
content: String,
auth: RequestPostProcessor = jwtWithPid("42"),
accept: MediaType? = APPLICATION_JSON,
): ResultActionsDsl =
post("/api/kontaktinfo/v1") {
this.contentType = contentType
this.content = content
this.accept = accept
with(auth)
}
}

0 comments on commit 306c20f

Please sign in to comment.