Skip to content

Commit

Permalink
refactor: Created tests for jvm and js in their respective folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe Maas committed Oct 27, 2024
1 parent a987de5 commit 88d43fb
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,117 +1,7 @@
package com.sphereon.oid.fed.client.trustchain

import com.sphereon.oid.fed.client.FederationClient
import com.sphereon.oid.fed.client.crypto.ICryptoCallbackService
import com.sphereon.oid.fed.client.fetch.IFetchCallbackService
import com.sphereon.oid.fed.client.service.DefaultCallbacks
import com.sphereon.oid.fed.openapi.models.Jwk
import io.ktor.client.*
import io.ktor.client.call.body
import io.ktor.client.engine.mock.*
import io.ktor.client.request.get
import io.ktor.http.*
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
expect class PlatformCallback

class PlatformCallback : IFetchCallbackService {
expect class CryptoCallbackService

override suspend fun getHttpClient(): HttpClient {
return HttpClient(MockEngine { request ->
val responseContent = mockResponses.find { it[0] == request.url.toString() }?.get(1)
?: error("Unhandled ${request.url}")

respond(
content = responseContent,
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/entity-statement+jwt")
)
})
}

override suspend fun fetchStatement(endpoint: String): String {
return getHttpClient().get(endpoint) {
headers {
append(HttpHeaders.Accept, "application/entity-statement+jwt")
}
}.body()
}
}

class CryptoCallbackServiceCallback : ICryptoCallbackService {
override suspend fun verify(jwt: String, jwk: Jwk): Boolean {
return true
}
}

class TrustChainTest() {
@Test
fun buildTrustChain() = runTest {
val fetchService = PlatformCallback()
DefaultCallbacks.setFetchServiceDefault(fetchService)
val cryptoService = CryptoCallbackServiceCallback()
DefaultCallbacks.setCryptoServiceDefault(cryptoService)
val trustChainService = DefaultTrustChainImpl(null, null)
DefaultCallbacks.setTrustChainServiceDefault(trustChainService)

val client = FederationClient()

val trustChain = client.resolveTrustChain(
"https://spid.wbss.it/Spid/oidc/rp/ipasv_lt",
arrayOf("https://oidc.registry.servizicie.interno.gov.it")
)

assertNotNull(trustChain)

assertEquals(4, trustChain.size)

assertEquals(
trustChain[0],
mockResponses.find { it[0] == "https://spid.wbss.it/Spid/oidc/rp/ipasv_lt/.well-known/openid-federation" }
?.get(1)
)

assertEquals(
trustChain[1],
mockResponses.find { it[0] == "https://spid.wbss.it/Spid/oidc/sa/fetch?sub=https://spid.wbss.it/Spid/oidc/rp/ipasv_lt" }
?.get(1)
)

assertEquals(
trustChain[2],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/fetch?sub=https://spid.wbss.it/Spid/oidc/sa" }
?.get(1)
)

assertEquals(
trustChain[3],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/.well-known/openid-federation" }
?.get(1)
)

val trustChain2 = client.resolveTrustChain(
"https://spid.wbss.it/Spid/oidc/sa",
arrayOf("https://oidc.registry.servizicie.interno.gov.it")
)

assertNotNull(trustChain2)
assertEquals(trustChain2.size, 3)
assertEquals(
trustChain2[0],
mockResponses.find { it[0] == "https://spid.wbss.it/Spid/oidc/sa/.well-known/openid-federation" }?.get(1)
)

assertEquals(
trustChain2[1],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/fetch?sub=https://spid.wbss.it/Spid/oidc/sa" }
?.get(1)
)

assertEquals(
trustChain2[2],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/.well-known/openid-federation" }
?.get(1)
)
}
}
expect class TrustChainTest()
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class PlatformCallbackJS : IFetchCallbackServiceJS {
actual class PlatformCallback : IFetchCallbackServiceJS {

private val FETCH_SERVICE_JS_SCOPE = "FetchServiceTestJS"

Expand Down Expand Up @@ -51,18 +51,18 @@ class PlatformCallbackJS : IFetchCallbackServiceJS {
}
}

class CryptoCallbackServiceCallbackJS : ICryptoCallbackServiceJS {
actual class CryptoCallbackService : ICryptoCallbackServiceJS {
override fun verify(jwt: String, jwk: Jwk): Promise<Boolean> {
return Promise.resolve(true)
}
}

class TrustChainTestJS() {
actual class TrustChainTest {
@Test
fun buildTrustChain() = runTest {
val fetchService = PlatformCallbackJS()
val fetchService = PlatformCallback()
DefaultCallbacks.setFetchServiceDefault(fetchService)
val cryptoService = CryptoCallbackServiceCallbackJS()
val cryptoService = CryptoCallbackService()
DefaultCallbacks.setCryptoServiceDefault(cryptoService)
val trustChainService = DefaultTrustChainJSImpl()
DefaultCallbacks.setTrustChainServiceDefault(trustChainService)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.sphereon.oid.fed.client.trustchain

import com.sphereon.oid.fed.client.FederationClient
import com.sphereon.oid.fed.client.crypto.ICryptoCallbackService
import com.sphereon.oid.fed.client.fetch.IFetchCallbackService
import com.sphereon.oid.fed.client.service.DefaultCallbacks
import com.sphereon.oid.fed.openapi.models.Jwk
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.MockEngine.Companion.invoke
import io.ktor.client.engine.mock.respond
import io.ktor.client.request.get
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.headers
import io.ktor.http.headersOf
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

actual class PlatformCallback : IFetchCallbackService {

override suspend fun getHttpClient(): HttpClient {
return HttpClient(MockEngine { request ->
val responseContent = mockResponses.find { it[0] == request.url.toString() }?.get(1)
?: error("Unhandled ${request.url}")

respond(
content = responseContent,
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/entity-statement+jwt")
)
})
}

override suspend fun fetchStatement(endpoint: String): String {
return getHttpClient().get(endpoint) {
headers {
append(HttpHeaders.Accept, "application/entity-statement+jwt")
}
}.body()
}
}

actual class CryptoCallbackService : ICryptoCallbackService {
override suspend fun verify(jwt: String, jwk: Jwk): Boolean {
return true
}
}

actual class TrustChainTest {
@Test
fun buildTrustChain() = runTest {
val fetchService = PlatformCallback()
DefaultCallbacks.setFetchServiceDefault(fetchService)
val cryptoService = CryptoCallbackService()
DefaultCallbacks.setCryptoServiceDefault(cryptoService)
val trustChainService = DefaultTrustChainImpl(null, null)
DefaultCallbacks.setTrustChainServiceDefault(trustChainService)

val client = FederationClient()

val trustChain = client.resolveTrustChain(
"https://spid.wbss.it/Spid/oidc/rp/ipasv_lt",
arrayOf("https://oidc.registry.servizicie.interno.gov.it")
)

assertNotNull(trustChain)

assertEquals(4, trustChain.size)

assertEquals(
trustChain[0],
mockResponses.find { it[0] == "https://spid.wbss.it/Spid/oidc/rp/ipasv_lt/.well-known/openid-federation" }
?.get(1)
)

assertEquals(
trustChain[1],
mockResponses.find { it[0] == "https://spid.wbss.it/Spid/oidc/sa/fetch?sub=https://spid.wbss.it/Spid/oidc/rp/ipasv_lt" }
?.get(1)
)

assertEquals(
trustChain[2],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/fetch?sub=https://spid.wbss.it/Spid/oidc/sa" }
?.get(1)
)

assertEquals(
trustChain[3],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/.well-known/openid-federation" }
?.get(1)
)

val trustChain2 = client.resolveTrustChain(
"https://spid.wbss.it/Spid/oidc/sa",
arrayOf("https://oidc.registry.servizicie.interno.gov.it")
)

assertNotNull(trustChain2)
assertEquals(trustChain2.size, 3)
assertEquals(
trustChain2[0],
mockResponses.find { it[0] == "https://spid.wbss.it/Spid/oidc/sa/.well-known/openid-federation" }?.get(1)
)

assertEquals(
trustChain2[1],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/fetch?sub=https://spid.wbss.it/Spid/oidc/sa" }
?.get(1)
)

assertEquals(
trustChain2[2],
mockResponses.find { it[0] == "https://oidc.registry.servizicie.interno.gov.it/.well-known/openid-federation" }
?.get(1)
)
}
}

0 comments on commit 88d43fb

Please sign in to comment.