-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oppgraderer Altinn client til å bruke maskinporten client
- Loading branch information
Showing
6 changed files
with
155 additions
and
74 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
97 changes: 84 additions & 13 deletions
97
altinn/src/test/kotlin/no/nav/helsearbeidsgiver/inntektsmelding/altinn/AltinnAppTest.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 |
---|---|---|
@@ -1,41 +1,112 @@ | ||
package no.nav.helsearbeidsgiver.inntektsmelding.altinn | ||
|
||
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.ints.shouldBeExactly | ||
import io.kotest.matchers.maps.shouldContainExactly | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldStartWith | ||
import io.mockk.clearAllMocks | ||
import io.mockk.every | ||
import io.mockk.mockkObject | ||
import io.mockk.mockkStatic | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import no.nav.helse.rapids_rivers.RapidApplication | ||
import no.nav.helse.rapids_rivers.testsupport.TestRapid | ||
import no.nav.helsearbeidsgiver.felles.Key | ||
import no.nav.helsearbeidsgiver.felles.fromEnv | ||
import no.nav.helsearbeidsgiver.felles.json.toJson | ||
import no.nav.helsearbeidsgiver.felles.json.toMap | ||
import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.firstMessage | ||
import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.sendJson | ||
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.altinnOrganisasjoner | ||
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.innkommendeMelding | ||
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.toMap | ||
import no.nav.helsearbeidsgiver.maskinporten.TokenResponse | ||
import no.nav.helsearbeidsgiver.utils.json.serializer.set | ||
import no.nav.helsearbeidsgiver.utils.json.toJson | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
|
||
class AltinnAppTest : | ||
FunSpec({ | ||
val testRapid = TestRapid() | ||
lateinit var server: MockWebServer | ||
beforeEach { | ||
testRapid.reset() | ||
clearAllMocks() | ||
server = MockWebServer() | ||
server.start() | ||
val baseUrl = server.url("/").toString() | ||
println("baseUrl== $baseUrl") | ||
} | ||
afterEach { clearAllMocks() } | ||
|
||
test("main") { | ||
test("tester at Altinn client og maskinporten kaller riktig endepunkt og sender riktig data") { | ||
mockkObject(RapidApplication) | ||
every { RapidApplication.create(any()) } returns testRapid | ||
mockEnv() | ||
|
||
val maskinportenToken = TokenResponse("test_token", "Bearer", 3600, "test:test1") | ||
val tokenResponse = Json.encodeToString(TokenResponse.serializer(), maskinportenToken) | ||
server.enqueue( | ||
MockResponse() | ||
.setBody(tokenResponse) | ||
.addHeader("Content-Type", "application/json"), | ||
) | ||
val altinnResponse = Json.encodeToString(altinnOrganisasjoner) | ||
val mockResponse = | ||
MockResponse() | ||
.setBody(altinnResponse) | ||
.addHeader("Content-Type", "application/json") | ||
server.enqueue(mockResponse) | ||
|
||
mockEnv(server) | ||
|
||
assertDoesNotThrow { main() } | ||
|
||
val innkommendeMelding = innkommendeMelding() | ||
|
||
testRapid.sendJson(innkommendeMelding.toMap()) | ||
|
||
testRapid.inspektør.size shouldBeExactly 1 | ||
|
||
val altinnOrgnr = | ||
altinnOrganisasjoner | ||
.mapNotNull { it.orgnr } | ||
.toSet() | ||
|
||
testRapid.firstMessage().toMap() shouldContainExactly | ||
mapOf( | ||
Key.EVENT_NAME to innkommendeMelding.eventName.toJson(), | ||
Key.UUID to innkommendeMelding.transaksjonId.toJson(), | ||
Key.DATA to | ||
innkommendeMelding.data | ||
.plus( | ||
Key.ORG_RETTIGHETER to altinnOrgnr.toJson(String.serializer().set()), | ||
).toJson(), | ||
) | ||
val tokenRequest = server.takeRequest() | ||
tokenRequest.path shouldBe "/token" | ||
tokenRequest.method shouldBe "POST" | ||
|
||
val altinnRequest = server.takeRequest() | ||
altinnRequest.path shouldStartWith "/altinn/reportees/" | ||
altinnRequest.method shouldBe "GET" | ||
} | ||
}) | ||
|
||
private fun mockEnv() { | ||
private fun mockEnv(server: MockWebServer) { | ||
mockkStatic("no.nav.helsearbeidsgiver.felles.EnvUtilsKt") | ||
|
||
every { "ALTINN_URL".fromEnv() } returns "mocked_value" | ||
every { "ALTINN_SERVICE_CODE".fromEnv() } returns "mocked_value" | ||
every { "MASKINPORTEN_TOKEN_ENDPOINT".fromEnv() } returns "mocked_value" | ||
every { "MASKINPORTEN_ISSUER".fromEnv() } returns "mocked_value" | ||
every { "MASKINPORTEN_CLIENT_JWK".fromEnv() } returns "mocked_value" | ||
every { "MASKINPORTEN_CLIENT_ID".fromEnv() } returns "mocked_value" | ||
every { "ALTINN_SCOPE".fromEnv() } returns "mocked_value" | ||
every { "ALTINN_URL".fromEnv() } returns server.url("/altinn").toString() | ||
every { "ALTINN_SERVICE_CODE".fromEnv() } returns "4936" | ||
every { "MASKINPORTEN_TOKEN_ENDPOINT".fromEnv() } returns server.url("/token").toString() | ||
every { "MASKINPORTEN_ISSUER".fromEnv() } returns "https://test.test.no/" | ||
every { "MASKINPORTEN_CLIENT_JWK".fromEnv() } returns generateJWK() | ||
every { "MASKINPORTEN_CLIENT_ID".fromEnv() } returns "TEST_CLIENT_ID" | ||
every { "ALTINN_SCOPE".fromEnv() } returns "test:test/test" | ||
every { "ALTINN_API_KEY".fromEnv() } returns "mocked_value" | ||
every { "KAFKA_BROKERS".fromEnv() } returns "mocked_value" | ||
} | ||
|
||
private fun generateJWK() = RSAKeyGenerator(2048).keyID("test-key-id").generate().toString() |
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
59 changes: 59 additions & 0 deletions
59
altinn/src/test/kotlin/no/nav/helsearbeidsgiver/inntektsmelding/altinn/Mock.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,59 @@ | ||
package no.nav.helsearbeidsgiver.inntektsmelding.altinn | ||
|
||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonNull | ||
import no.nav.helsearbeidsgiver.altinn.AltinnOrganisasjon | ||
import no.nav.helsearbeidsgiver.felles.BehovType | ||
import no.nav.helsearbeidsgiver.felles.EventName | ||
import no.nav.helsearbeidsgiver.felles.Key | ||
import no.nav.helsearbeidsgiver.felles.json.toJson | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.model.Fail | ||
import no.nav.helsearbeidsgiver.utils.json.toJson | ||
import no.nav.helsearbeidsgiver.utils.test.wrapper.genererGyldig | ||
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr | ||
import java.util.UUID | ||
|
||
object Mock { | ||
fun innkommendeMelding(): Melding { | ||
val fnr = Fnr.genererGyldig() | ||
|
||
return Melding( | ||
eventName = EventName.AKTIVE_ORGNR_REQUESTED, | ||
behovType = BehovType.ARBEIDSGIVERE, | ||
transaksjonId = UUID.randomUUID(), | ||
data = | ||
mapOf( | ||
Key.ARBEIDSGIVER_FNR to fnr.toJson(), | ||
), | ||
fnr = fnr, | ||
) | ||
} | ||
|
||
fun Melding.toMap(): Map<Key, JsonElement> = | ||
mapOf( | ||
Key.EVENT_NAME to eventName.toJson(), | ||
Key.BEHOV to behovType.toJson(), | ||
Key.UUID to transaksjonId.toJson(), | ||
Key.DATA to | ||
mapOf( | ||
Key.ARBEIDSGIVER_FNR to fnr.toJson(), | ||
).toJson(), | ||
) | ||
|
||
val fail = | ||
Fail( | ||
feilmelding = "One does not simply walk into Mordor.", | ||
event = EventName.AKTIVE_ORGNR_REQUESTED, | ||
transaksjonId = UUID.randomUUID(), | ||
forespoerselId = null, | ||
utloesendeMelding = JsonNull, | ||
) | ||
|
||
val altinnOrganisasjoner = | ||
setOf( | ||
AltinnOrganisasjon( | ||
navn = "Pippin's Breakfast & Breakfast", | ||
type = "gluttonous", | ||
), | ||
) | ||
} |
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