Skip to content

Commit 160d6cb

Browse files
committed
chore: remove redundant code
Signed-off-by: Mirko Mollik <mirko.mollik@eudi.sprind.org>
1 parent ffb193e commit 160d6cb

File tree

4 files changed

+93
-119
lines changed

4 files changed

+93
-119
lines changed

apps/backend/test/presentation/presentation-mdoc.e2e-spec.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Openid4vpClient,
55
} from "@openid4vc/openid4vp";
66
import { CryptoKey } from "jose";
7-
import request from "supertest";
87
import { App } from "supertest/types";
98
import { afterAll, beforeAll, describe, expect, test } from "vitest";
109
import { AuthConfig } from "../../src/shared/utils/webhook/webhook.dto";
@@ -14,6 +13,8 @@ import {
1413
} from "../../src/verifier/oid4vp/dto/presentation-request.dto";
1514
import {
1615
callbacks,
16+
createPresentationRequest,
17+
createTestFetch,
1718
encryptVpToken,
1819
PresentationTestContext,
1920
prepareMdocPresentation,
@@ -28,43 +29,7 @@ describe("Presentation - mDOC Credential", () => {
2829
let issuerCert: string;
2930
let ctx: PresentationTestContext;
3031

31-
const client = new Openid4vpClient({
32-
callbacks: {
33-
...callbacks,
34-
fetch: async (uri: string, init: RequestInit) => {
35-
const path = uri.split(host)[1];
36-
let response: any;
37-
if (init.method === "POST") {
38-
response = await request(app.getHttpServer())
39-
.post(path)
40-
.trustLocalhost()
41-
.send(init.body!);
42-
} else {
43-
response = await request(app.getHttpServer())
44-
.get(path)
45-
.trustLocalhost();
46-
}
47-
return {
48-
ok: true,
49-
text: () => response.text,
50-
json: () => response.body,
51-
status: response.status,
52-
headers: response.headers,
53-
};
54-
},
55-
},
56-
});
57-
58-
/**
59-
* Helper function to create a presentation request
60-
*/
61-
function createPresentationRequest(requestBody: PresentationRequest) {
62-
return request(app.getHttpServer())
63-
.post("/verifier/offer")
64-
.trustLocalhost()
65-
.set("Authorization", `Bearer ${authToken}`)
66-
.send(requestBody);
67-
}
32+
let client: Openid4vpClient;
6833

6934
/**
7035
* Helper function to submit a mDOC presentation
@@ -87,7 +52,11 @@ describe("Presentation - mDOC Credential", () => {
8752
}),
8853
};
8954

90-
const res = await createPresentationRequest(requestBody);
55+
const res = await createPresentationRequest(
56+
app,
57+
authToken,
58+
requestBody,
59+
);
9160

9261
const authRequest = client.parseOpenid4vpAuthorizationRequest({
9362
authorizationRequest: res.body.uri,
@@ -137,6 +106,13 @@ describe("Presentation - mDOC Credential", () => {
137106
host = ctx.host;
138107
privateIssuerKey = ctx.privateIssuerKey;
139108
issuerCert = ctx.issuerCert;
109+
110+
client = new Openid4vpClient({
111+
callbacks: {
112+
...callbacks,
113+
fetch: createTestFetch(app, () => host),
114+
},
115+
});
140116
});
141117

142118
afterAll(async () => {

apps/backend/test/presentation/presentation-sdjwt.e2e-spec.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Openid4vpClient,
55
} from "@openid4vc/openid4vp";
66
import { CryptoKey } from "jose";
7-
import request from "supertest";
87
import { App } from "supertest/types";
98
import { afterAll, beforeAll, describe, expect, test } from "vitest";
109
import { StatusListService } from "../../src/issuer/lifecycle/status/status-list.service";
@@ -15,6 +14,8 @@ import {
1514
} from "../../src/verifier/oid4vp/dto/presentation-request.dto";
1615
import {
1716
callbacks,
17+
createPresentationRequest,
18+
createTestFetch,
1819
encryptVpToken,
1920
PresentationTestContext,
2021
preparePresentation,
@@ -32,43 +33,7 @@ describe("Presentation - SD-JWT Credential", () => {
3233

3334
const credentialConfigId = "pid";
3435

35-
const client = new Openid4vpClient({
36-
callbacks: {
37-
...callbacks,
38-
fetch: async (uri: string, init: RequestInit) => {
39-
const path = uri.split(host)[1];
40-
let response: any;
41-
if (init.method === "POST") {
42-
response = await request(app.getHttpServer())
43-
.post(path)
44-
.trustLocalhost()
45-
.send(init.body!);
46-
} else {
47-
response = await request(app.getHttpServer())
48-
.get(path)
49-
.trustLocalhost();
50-
}
51-
return {
52-
ok: true,
53-
text: () => response.text,
54-
json: () => response.body,
55-
status: response.status,
56-
headers: response.headers,
57-
};
58-
},
59-
},
60-
});
61-
62-
/**
63-
* Helper function to create a presentation request
64-
*/
65-
function createPresentationRequest(requestBody: PresentationRequest) {
66-
return request(app.getHttpServer())
67-
.post("/verifier/offer")
68-
.trustLocalhost()
69-
.set("Authorization", `Bearer ${authToken}`)
70-
.send(requestBody);
71-
}
36+
let client: Openid4vpClient;
7237

7338
/**
7439
* Helper function to submit a complete presentation flow
@@ -91,7 +56,11 @@ describe("Presentation - SD-JWT Credential", () => {
9156
}),
9257
};
9358

94-
const res = await createPresentationRequest(requestBody);
59+
const res = await createPresentationRequest(
60+
app,
61+
authToken,
62+
requestBody,
63+
);
9564

9665
const authRequest = client.parseOpenid4vpAuthorizationRequest({
9766
authorizationRequest: res.body.uri,
@@ -152,6 +121,13 @@ describe("Presentation - SD-JWT Credential", () => {
152121
privateIssuerKey = ctx.privateIssuerKey;
153122
issuerCert = ctx.issuerCert;
154123
statusListService = ctx.statusListService;
124+
125+
client = new Openid4vpClient({
126+
callbacks: {
127+
...callbacks,
128+
fetch: createTestFetch(app, () => host),
129+
},
130+
});
155131
});
156132

157133
afterAll(async () => {

apps/backend/test/presentation/presentation-webhook.e2e-spec.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
} from "@openid4vc/openid4vp";
66
import { CryptoKey } from "jose";
77
import nock from "nock";
8-
import request from "supertest";
98
import { App } from "supertest/types";
109
import { afterAll, beforeAll, describe, expect, test } from "vitest";
1110
import { StatusListService } from "../../src/issuer/lifecycle/status/status-list.service";
@@ -16,6 +15,8 @@ import {
1615
} from "../../src/verifier/oid4vp/dto/presentation-request.dto";
1716
import {
1817
callbacks,
18+
createPresentationRequest,
19+
createTestFetch,
1920
encryptVpToken,
2021
PresentationTestContext,
2122
preparePresentation,
@@ -33,43 +34,7 @@ describe("Presentation - Webhook Integration", () => {
3334

3435
const credentialConfigId = "pid";
3536

36-
const client = new Openid4vpClient({
37-
callbacks: {
38-
...callbacks,
39-
fetch: async (uri: string, init: RequestInit) => {
40-
const path = uri.split(host)[1];
41-
let response: any;
42-
if (init.method === "POST") {
43-
response = await request(app.getHttpServer())
44-
.post(path)
45-
.trustLocalhost()
46-
.send(init.body!);
47-
} else {
48-
response = await request(app.getHttpServer())
49-
.get(path)
50-
.trustLocalhost();
51-
}
52-
return {
53-
ok: true,
54-
text: () => response.text,
55-
json: () => response.body,
56-
status: response.status,
57-
headers: response.headers,
58-
};
59-
},
60-
},
61-
});
62-
63-
/**
64-
* Helper function to create a presentation request
65-
*/
66-
function createPresentationRequest(requestBody: PresentationRequest) {
67-
return request(app.getHttpServer())
68-
.post("/verifier/offer")
69-
.trustLocalhost()
70-
.set("Authorization", `Bearer ${authToken}`)
71-
.send(requestBody);
72-
}
37+
let client: Openid4vpClient;
7338

7439
/**
7540
* Helper function to submit a complete presentation flow
@@ -92,7 +57,11 @@ describe("Presentation - Webhook Integration", () => {
9257
}),
9358
};
9459

95-
const res = await createPresentationRequest(requestBody);
60+
const res = await createPresentationRequest(
61+
app,
62+
authToken,
63+
requestBody,
64+
);
9665

9766
const authRequest = client.parseOpenid4vpAuthorizationRequest({
9867
authorizationRequest: res.body.uri,
@@ -153,6 +122,13 @@ describe("Presentation - Webhook Integration", () => {
153122
privateIssuerKey = ctx.privateIssuerKey;
154123
issuerCert = ctx.issuerCert;
155124
statusListService = ctx.statusListService;
125+
126+
client = new Openid4vpClient({
127+
callbacks: {
128+
...callbacks,
129+
fetch: createTestFetch(app, () => host),
130+
},
131+
});
156132
});
157133

158134
afterAll(async () => {

apps/backend/test/utils.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { INestApplication, ValidationPipe } from "@nestjs/common";
1616
import { ConfigService } from "@nestjs/config";
1717
import { Test, TestingModule } from "@nestjs/testing";
1818
import { CallbackContext, Jwk, SignJwtCallback } from "@openid4vc/oauth2";
19-
import { Openid4vpAuthorizationRequest } from "@openid4vc/openid4vp";
19+
import { ResolvedOpenid4vpAuthorizationRequest } from "@openid4vc/openid4vp";
2020
import { X509Certificate } from "@peculiar/x509";
2121
import { digest, ES256 } from "@sd-jwt/crypto-nodejs";
2222
import { SDJwtVcInstance } from "@sd-jwt/sd-jwt-vc";
@@ -42,6 +42,7 @@ import { CredentialConfigCreate } from "../src/issuer/configuration/credentials/
4242
import { IssuanceDto } from "../src/issuer/configuration/issuance/dto/issuance.dto";
4343
import { StatusListService } from "../src/issuer/lifecycle/status/status-list.service";
4444
import { TrustListCreateDto } from "../src/issuer/trustlist/dto/trust-list-create.dto";
45+
import { PresentationRequest } from "../src/verifier/oid4vp/dto/presentation-request.dto";
4546
import { PresentationConfigCreateDto } from "../src/verifier/presentations/dto/presentation-config-create.dto";
4647
import { DEVICE_JWK, mdocContext } from "./utils-mdoc";
4748

@@ -767,7 +768,7 @@ export async function setupPresentationTestApp(): Promise<PresentationTestContex
767768
export async function encryptVpToken(
768769
vp_token: string,
769770
credentialId: string,
770-
resolved: Openid4vpAuthorizationRequest,
771+
resolved: ResolvedOpenid4vpAuthorizationRequest,
771772
): Promise<string> {
772773
const key = (await importJWK(
773774
resolved.authorizationRequestPayload.client_metadata?.jwks
@@ -787,3 +788,48 @@ export async function encryptVpToken(
787788
.setExpirationTime("2h")
788789
.encrypt(key);
789790
}
791+
792+
/**
793+
* Creates a test fetch function for Openid4vpClient that routes requests through supertest
794+
*/
795+
export function createTestFetch(
796+
app: INestApplication<App>,
797+
getHost: () => string,
798+
) {
799+
return async (uri: string, init: RequestInit) => {
800+
const path = uri.split(getHost())[1];
801+
let response: request.Response;
802+
if (init.method === "POST") {
803+
response = await request(app.getHttpServer())
804+
.post(path)
805+
.trustLocalhost()
806+
.send(init.body!);
807+
} else {
808+
response = await request(app.getHttpServer())
809+
.get(path)
810+
.trustLocalhost();
811+
}
812+
return {
813+
ok: true,
814+
text: () => response.text,
815+
json: () => response.body,
816+
status: response.status,
817+
headers: response.headers,
818+
};
819+
};
820+
}
821+
822+
/**
823+
* Helper function to create a presentation request via the verifier API
824+
*/
825+
export function createPresentationRequest(
826+
app: INestApplication<App>,
827+
authToken: string,
828+
requestBody: PresentationRequest,
829+
) {
830+
return request(app.getHttpServer())
831+
.post("/verifier/offer")
832+
.trustLocalhost()
833+
.set("Authorization", `Bearer ${authToken}`)
834+
.send(requestBody);
835+
}

0 commit comments

Comments
 (0)