@@ -6,14 +6,13 @@ import io.kotest.matchers.shouldBe
6
6
import io.ktor.client.request.HttpRequestBuilder
7
7
import io.ktor.client.request.get
8
8
import io.ktor.client.request.header
9
+ import io.ktor.client.request.post
10
+ import io.ktor.client.request.setBody
9
11
import io.ktor.client.statement.bodyAsText
10
12
import io.ktor.http.HttpHeaders
11
13
import io.ktor.http.HttpMethod
12
14
import io.ktor.http.HttpStatusCode
13
- import io.ktor.server.testing.TestApplicationEngine
14
- import io.ktor.server.testing.handleRequest
15
15
import io.ktor.server.testing.setBody
16
- import io.ktor.server.testing.withTestApplication
17
16
import io.mockk.coEvery
18
17
import io.mockk.coVerify
19
18
import io.mockk.mockk
@@ -32,7 +31,7 @@ import no.nav.dagpenger.inntekt.oppslag.Person
32
31
import no.nav.dagpenger.inntekt.oppslag.PersonNotFoundException
33
32
import no.nav.dagpenger.inntekt.oppslag.PersonOppslag
34
33
import no.nav.dagpenger.inntekt.serder.jacksonObjectMapper
35
- import no.nav.dagpenger.inntekt.v1.TestApplication.handleAuthenticatedAzureAdRequest
34
+ import no.nav.dagpenger.inntekt.v1.TestApplication.autentisert
36
35
import no.nav.dagpenger.inntekt.v1.TestApplication.mockInntektApi
37
36
import no.nav.dagpenger.inntekt.v1.TestApplication.testOAuthToken
38
37
import no.nav.dagpenger.inntekt.v1.TestApplication.withMockAuthServerAndTestApplication
@@ -188,105 +187,153 @@ internal class InntektRouteSpec {
188
187
189
188
@Test
190
189
fun `skal ikke autentisere på v2 hvis ikke auth token er med ` () =
191
- testApp {
192
- handleRequest(HttpMethod .Post , klassifisertInntektPathV2) {
193
- addHeader(HttpHeaders .ContentType , " application/json" )
194
- setBody(validJson)
195
- }.apply {
196
- assertEquals(HttpStatusCode .Unauthorized , response.status())
197
- }
190
+ withMockAuthServerAndTestApplication(
191
+ mockInntektApi(
192
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
193
+ personOppslag = personOppslagMock,
194
+ ),
195
+ ) {
196
+ val response =
197
+ client.post(klassifisertInntektPathV2) {
198
+ header(HttpHeaders .ContentType , " application/json" )
199
+ setBody(validJson)
200
+ }
201
+
202
+ assertEquals(HttpStatusCode .Unauthorized , response.status)
198
203
}
199
204
200
205
@Test
201
206
fun `skal autentisere på v2 hvis auth token er med ` () =
202
- testApp {
203
- handleAuthenticatedAzureAdRequest(HttpMethod .Post , klassifisertInntektPathV2) {
204
- addHeader(HttpHeaders .ContentType , " application/json" )
205
- setBody(validJson)
206
- }.apply {
207
- assertEquals(HttpStatusCode .OK , response.status())
208
- }
207
+ withMockAuthServerAndTestApplication(
208
+ mockInntektApi(
209
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
210
+ personOppslag = personOppslagMock,
211
+ ),
212
+ ) {
213
+ val response =
214
+ autentisert(
215
+ klassifisertInntektPathV2,
216
+ httpMethod = HttpMethod .Post ,
217
+ body = validJson,
218
+ callId = callId,
219
+ )
220
+
221
+ assertEquals(HttpStatusCode .OK , response.status)
209
222
}
210
223
211
224
@Test
212
225
fun `Klassifisert inntekt API specification test - Should match json field names and formats` () =
213
- testApp {
214
- handleAuthenticatedAzureAdRequest(HttpMethod .Post , klassifisertInntektPathV2) {
215
- addHeader(HttpHeaders .ContentType , " application/json" )
216
- addHeader(" X-Request-Id" , callId)
217
- setBody(validJson)
218
- }.apply {
219
- assertEquals(HttpStatusCode .OK , response.status())
220
- coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getBehandlingsInntekt(inntektParametre, callId) }
221
- coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getKlassifisertInntekt(inntektParametre, callId) }
222
- }
226
+ withMockAuthServerAndTestApplication(
227
+ mockInntektApi(
228
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
229
+ personOppslag = personOppslagMock,
230
+ ),
231
+ ) {
232
+ val response =
233
+ autentisert(
234
+ klassifisertInntektPathV2,
235
+ httpMethod = HttpMethod .Post ,
236
+ body = validJson,
237
+ callId = callId,
238
+ )
239
+
240
+ assertEquals(HttpStatusCode .OK , response.status)
241
+ coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getBehandlingsInntekt(inntektParametre, callId) }
242
+ coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getKlassifisertInntekt(inntektParametre, callId) }
223
243
}
224
244
225
245
@Test
226
246
fun `Klassifisert Requests with fødselsnummer works and does store data` () =
227
- testApp {
228
- handleAuthenticatedAzureAdRequest(HttpMethod .Post , klassifisertInntektPathV2) {
229
- addHeader(HttpHeaders .ContentType , " application/json" )
230
- addHeader(" X-Request-Id" , callId)
231
- setBody(validJsonWithFnr)
232
- }.apply {
233
- assertEquals(HttpStatusCode .OK , response.status())
234
- coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getBehandlingsInntekt(fnrParametre, callId) }
235
- coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getSpesifisertInntekt(fnrParametre, callId) }
236
- coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getKlassifisertInntekt(fnrParametre, callId) }
237
- }
247
+ withMockAuthServerAndTestApplication(
248
+ mockInntektApi(
249
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
250
+ personOppslag = personOppslagMock,
251
+ ),
252
+ ) {
253
+ val response =
254
+ autentisert(
255
+ klassifisertInntektPathV2,
256
+ httpMethod = HttpMethod .Post ,
257
+ body = validJsonWithFnr,
258
+ callId = callId,
259
+ )
260
+ assertEquals(HttpStatusCode .OK , response.status)
261
+ coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getBehandlingsInntekt(fnrParametre, callId) }
262
+ coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getSpesifisertInntekt(fnrParametre, callId) }
263
+ coVerify(exactly = 1 ) { behandlingsInntektsGetterMock.getKlassifisertInntekt(fnrParametre, callId) }
238
264
}
239
265
240
266
@Test
241
267
fun `Klassifisert Requests with vedtakId as string works and does store data` () =
242
- testApp {
243
- handleAuthenticatedAzureAdRequest(HttpMethod .Post , klassifisertInntektPathV2) {
244
- addHeader(HttpHeaders .ContentType , " application/json" )
245
- addHeader(" X-Request-Id" , callId)
246
- setBody(validJsonWithVedtakIdAsUlid)
247
- }.apply {
248
- assertEquals(HttpStatusCode .OK , response.status())
249
- coVerify(exactly = 1 ) {
250
- behandlingsInntektsGetterMock.getBehandlingsInntekt(
251
- vedtakIdUlidParametre,
252
- callId,
253
- )
254
- }
255
- coVerify(exactly = 1 ) {
256
- behandlingsInntektsGetterMock.getSpesifisertInntekt(
257
- vedtakIdUlidParametre,
258
- callId,
259
- )
260
- }
261
- coVerify(exactly = 1 ) {
262
- behandlingsInntektsGetterMock.getKlassifisertInntekt(
263
- vedtakIdUlidParametre,
264
- callId,
265
- )
266
- }
268
+ withMockAuthServerAndTestApplication(
269
+ mockInntektApi(
270
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
271
+ personOppslag = personOppslagMock,
272
+ ),
273
+ ) {
274
+ val response =
275
+ autentisert(
276
+ klassifisertInntektPathV2,
277
+ httpMethod = HttpMethod .Post ,
278
+ body = validJsonWithVedtakIdAsUlid,
279
+ callId = callId,
280
+ )
281
+
282
+ assertEquals(HttpStatusCode .OK , response.status)
283
+ coVerify(exactly = 1 ) {
284
+ behandlingsInntektsGetterMock.getBehandlingsInntekt(
285
+ vedtakIdUlidParametre,
286
+ callId,
287
+ )
288
+ }
289
+ coVerify(exactly = 1 ) {
290
+ behandlingsInntektsGetterMock.getSpesifisertInntekt(
291
+ vedtakIdUlidParametre,
292
+ callId,
293
+ )
294
+ }
295
+ coVerify(exactly = 1 ) {
296
+ behandlingsInntektsGetterMock.getKlassifisertInntekt(
297
+ vedtakIdUlidParametre,
298
+ callId,
299
+ )
267
300
}
268
301
}
269
302
270
303
@Test
271
304
fun `Klassifisert request fails on post request with missing fields` () =
272
- testApp {
273
- handleAuthenticatedAzureAdRequest(HttpMethod .Post , klassifisertInntektPathV2) {
274
- addHeader(HttpHeaders .ContentType , " application/json" )
275
- setBody(jsonMissingFields)
276
- }.apply {
277
- assertEquals(HttpStatusCode .BadRequest , response.status())
278
- }
305
+ withMockAuthServerAndTestApplication(
306
+ mockInntektApi(
307
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
308
+ personOppslag = personOppslagMock,
309
+ ),
310
+ ) {
311
+ val response =
312
+ autentisert(
313
+ klassifisertInntektPathV2,
314
+ httpMethod = HttpMethod .Post ,
315
+ body = jsonMissingFields,
316
+ callId = callId,
317
+ )
318
+ assertEquals(HttpStatusCode .BadRequest , response.status)
279
319
}
280
320
281
321
@Test
282
322
fun `Klassifisert request fails on post request with unknown person` () =
283
- testApp {
284
- handleAuthenticatedAzureAdRequest(HttpMethod .Post , klassifisertInntektPathV2) {
285
- addHeader(HttpHeaders .ContentType , " application/json" )
286
- setBody(jsonUkjentPerson)
287
- }.apply {
288
- assertEquals(HttpStatusCode .BadRequest , response.status())
289
- }
323
+ withMockAuthServerAndTestApplication(
324
+ mockInntektApi(
325
+ behandlingsInntektsGetter = behandlingsInntektsGetterMock,
326
+ personOppslag = personOppslagMock,
327
+ ),
328
+ ) {
329
+ val response =
330
+ autentisert(
331
+ klassifisertInntektPathV2,
332
+ httpMethod = HttpMethod .Post ,
333
+ body = jsonUkjentPerson,
334
+ callId = callId,
335
+ )
336
+ assertEquals(HttpStatusCode .BadRequest , response.status)
290
337
}
291
338
292
339
@Test
@@ -324,15 +371,6 @@ internal class InntektRouteSpec {
324
371
ikkeInntektIdResponse.status shouldBe HttpStatusCode .BadRequest
325
372
}
326
373
327
- private fun testApp (callback : TestApplicationEngine .() -> Unit ) {
328
- withTestApplication(
329
- mockInntektApi(
330
- behandlingsInntektsGetter = behandlingsInntektsGetterMock,
331
- personOppslag = personOppslagMock,
332
- ),
333
- ) { callback() }
334
- }
335
-
336
374
private fun HttpRequestBuilder.autentisert () {
337
375
header(HttpHeaders .Authorization , " Bearer $testOAuthToken " )
338
376
}
0 commit comments