Skip to content

Commit

Permalink
fix: e2e http-response expect extension functions
Browse files Browse the repository at this point in the history
Signed-off-by: mikeplotean <mike@walt.id>
  • Loading branch information
mikeplotean committed Aug 13, 2024
1 parent fa39a60 commit 8db32f3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import E2ETestWebService.loadResource

import id.walt.crypto.utils.JsonUtils.toJsonElement
import id.walt.issuer.issuance.IssuanceRequest
import id.walt.oid4vc.data.*
Expand All @@ -15,13 +14,11 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlin.io.encoding.ExperimentalEncodingApi
import kotlin.test.*
import kotlin.test.assertEquals


class AuthorizationCodeFlow(private val client: HttpClient) {

@OptIn(ExperimentalEncodingApi::class)
fun testIssuerAPI() = runBlocking {
lateinit var offerUrl: String
lateinit var issuerState: String
Expand All @@ -32,15 +29,16 @@ class AuthorizationCodeFlow(private val client: HttpClient) {
//
val issuanceRequestIdToken =
Json.decodeFromString<IssuanceRequest>(loadResource("issuance/openbadgecredential-issuance-request-with-authorization-code-flow-and-id-token.json"))
issuerApi.issue(issuanceRequestIdToken) {
issuerApi.jwt(issuanceRequestIdToken) {
offerUrl = it
println("offer: $offerUrl")
}

var offerUrlParams = Url(offerUrl).parameters.toMap()
var offerObj = CredentialOfferRequest.fromHttpParameters(offerUrlParams)
var credOffer = client.get(offerObj.credentialOfferUri!!).body<JsonObject>()
issuerState = credOffer["grants"]!!.jsonObject["authorization_code"]!!.jsonObject["issuer_state"]!!.jsonPrimitive.content
issuerState =
credOffer["grants"]!!.jsonObject["authorization_code"]!!.jsonObject["issuer_state"]!!.jsonPrimitive.content
var authorizationRequest = AuthorizationRequest(
issuerState = issuerState,
clientId = "did:key:xzy",
Expand All @@ -60,7 +58,7 @@ class AuthorizationCodeFlow(private val client: HttpClient) {
type = "openid_credential",
locations = listOf(credOffer["credential_issuer"]!!.jsonPrimitive.content),
format = CredentialFormat.jwt_vc,
types = listOf("VerifiableCredential","OpenBadgeCredential")
types = listOf("VerifiableCredential", "OpenBadgeCredential")
)
)
)
Expand All @@ -77,23 +75,24 @@ class AuthorizationCodeFlow(private val client: HttpClient) {
//
val issuanceRequestVpToken =
Json.decodeFromString<IssuanceRequest>(loadResource("issuance/openbadgecredential-issuance-request-with-authorization-code-flow-and-vp-token.json"))
issuerApi.issue(issuanceRequestVpToken) {
issuerApi.jwt(issuanceRequestVpToken) {
offerUrl = it
println("offer: $offerUrl")
}

offerUrlParams = Url(offerUrl).parameters.toMap()
offerObj = CredentialOfferRequest.fromHttpParameters(offerUrlParams)
credOffer = client.get(offerObj.credentialOfferUri!!).body<JsonObject>()
issuerState = credOffer["grants"]!!.jsonObject["authorization_code"]!!.jsonObject["issuer_state"]!!.jsonPrimitive.content
issuerState =
credOffer["grants"]!!.jsonObject["authorization_code"]!!.jsonObject["issuer_state"]!!.jsonPrimitive.content
authorizationRequest = authorizationRequest.copy(
issuerState = issuerState,
authorizationDetails = listOf(
AuthorizationDetails(
type = "openid_credential",
locations = listOf(credOffer["credential_issuer"]!!.jsonPrimitive.content),
format = CredentialFormat.jwt_vc,
types = listOf("VerifiableCredential","OpenBadgeCredential")
types = listOf("VerifiableCredential", "OpenBadgeCredential")
)
)
)
Expand All @@ -111,23 +110,24 @@ class AuthorizationCodeFlow(private val client: HttpClient) {
//
val issuanceRequestPwd =
Json.decodeFromString<IssuanceRequest>(loadResource("issuance/openbadgecredential-issuance-request-with-authorization-code-flow-and-pwd.json"))
issuerApi.issue(issuanceRequestPwd) {
issuerApi.jwt(issuanceRequestPwd) {
offerUrl = it
println("offer: $offerUrl")
}

offerUrlParams = Url(offerUrl).parameters.toMap()
offerObj = CredentialOfferRequest.fromHttpParameters(offerUrlParams)
credOffer = client.get(offerObj.credentialOfferUri!!).body<JsonObject>()
issuerState = credOffer["grants"]!!.jsonObject["authorization_code"]!!.jsonObject["issuer_state"]!!.jsonPrimitive.content
issuerState =
credOffer["grants"]!!.jsonObject["authorization_code"]!!.jsonObject["issuer_state"]!!.jsonPrimitive.content
authorizationRequest = authorizationRequest.copy(
issuerState = issuerState,
authorizationDetails = listOf(
AuthorizationDetails(
type = "openid_credential",
locations = listOf(credOffer["credential_issuer"]!!.jsonPrimitive.content),
format = CredentialFormat.jwt_vc,
types = listOf("VerifiableCredential","OpenBadgeCredential")
types = listOf("VerifiableCredential", "OpenBadgeCredential")
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class E2ESdJwtTest(
selectedCredentials = listOf(newCredential.id),
disclosures = newCredential.disclosures?.let { mapOf(newCredential.id to listOf(it)) },
),
expectStatus = HttpResponse::expectFailure,
expectStatus = expectFailure,
)

sessionApi.get(verificationId) {
Expand Down
14 changes: 7 additions & 7 deletions waltid-services/waltid-e2e-tests/src/test/kotlin/E2ETest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,17 @@ class E2ETest {
fun String.expectLooksLikeJwt(): String =
also { assert(startsWith("ey") && count { it == '.' } == 2) { "Does not look like JWT" } }

suspend fun HttpResponse.expectSuccess(): HttpResponse = also {
assert(status.isSuccess()) { "HTTP status is non-successful for response: $it, body is ${it.bodyAsText()}" }

val expectSuccess: suspend HttpResponse.() -> HttpResponse = {
assert(this.status.isSuccess()) { "HTTP status is non-successful for response: $this, body is ${this.bodyAsText()}" }; this
}

fun HttpResponse.expectRedirect(): HttpResponse = also {
assert(status == HttpStatusCode.Found) { "HTTP status is non-successful" }
val expectRedirect: HttpResponse.() -> HttpResponse = {
assert(this.status == HttpStatusCode.Found) { "HTTP status is non-successful" }; this
}

//todo: temporary
fun HttpResponse.expectFailure(): HttpResponse = also {
assert(!status.isSuccess()) { "HTTP status is successful" }
val expectFailure: HttpResponse.() -> HttpResponse = {
assert(!status.isSuccess()) { "HTTP status is successful" }; this
}

fun JsonElement.tryGetData(key: String): JsonElement? = key.split('.').let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class ExchangeApi(private val client: HttpClient) {
}.expectSuccess().apply {
val matched = body<List<WalletCredential>>()
assert(matched.size == expectedCredentialIds.size) { "presentation definition should match $expectedCredentialIds credential(s), but have ${matched.size}" }
assert(matched.map { it.id }.containsAll(expectedCredentialIds)) { "matched credentials does not contain all of the expected ones" }
assert(matched.map { it.id }
.containsAll(expectedCredentialIds)) { "matched credentials does not contain all of the expected ones" }
output?.invoke(matched)
}
}
Expand All @@ -86,7 +87,7 @@ class ExchangeApi(private val client: HttpClient) {
suspend fun usePresentationRequest(
wallet: UUID,
request: UsePresentationRequest,
expectStatus: HttpResponse.() -> HttpResponse = HttpResponse::expectSuccess
expectStatus: suspend HttpResponse.() -> HttpResponse = expectSuccess
) = test("/wallet-api/wallet/{wallet}/exchange/usePresentationRequest - present credentials") {
client.post("/wallet-api/wallet/$wallet/exchange/usePresentationRequest") {
setBody(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ import id.walt.credentials.vc.vcs.W3CVC
import id.walt.crypto.keys.KeyGenerationRequest
import id.walt.crypto.keys.KeySerialization
import id.walt.crypto.keys.KeyType
import id.walt.crypto.keys.jwk.JWKKey
import id.walt.crypto.utils.JsonUtils.toJsonElement
import id.walt.issuer.issuance.IssuanceExamples
import id.walt.issuer.issuance.IssuanceRequest
import id.walt.issuer.issuance.createCredentialOfferUri
import id.walt.issuer.lspPotential.LspPotentialIssuanceInterop
import id.walt.oid4vc.data.AuthenticationMethod
import id.walt.oid4vc.data.CredentialFormat
import id.walt.oid4vc.data.CredentialOffer
import id.walt.oid4vc.data.OpenIDProviderMetadata
import id.walt.oid4vc.data.dif.VCFormat
import id.walt.oid4vc.requests.AuthorizationRequest
import id.walt.verifier.lspPotential.LspPotentialVerificationInterop
import id.walt.webwallet.db.models.WalletCredential
import id.walt.webwallet.db.models.WalletDid
import id.walt.webwallet.web.controllers.UsePresentationRequest
import io.ktor.client.*
import io.ktor.client.call.*
Expand Down Expand Up @@ -202,7 +196,7 @@ class LspPotentialWallet(val client: HttpClient, val walletId: String) {
// === find matching credential ===
val matchingCreds = client.post("/wallet-api/wallet/$walletId/exchange/matchCredentialsForPresentationDefinition") {
setBody(parsedRequest.presentationDefinition!!)
}.expectSuccess().let { response -> response.body<List<WalletCredential>>()}
}.expectSuccess().body<List<WalletCredential>>()
assertNotEquals(0, matchingCreds.size)

client.post("/wallet-api/wallet/$walletId/exchange/usePresentationRequest") {
Expand Down

0 comments on commit 8db32f3

Please sign in to comment.