Skip to content

Commit 6fe7de6

Browse files
committed
test: fix and refactor
1 parent 8e7e9b8 commit 6fe7de6

20 files changed

+104
-83
lines changed

.changeset/tame-buttons-explain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"elysia-openid-client": patch
3+
---
4+
5+
Bump dependencies

__mock__/const.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export const mockLogger = {
4242
fatal: mock(),
4343
};
4444

45+
export const mockOrigin = "http://localhost";
46+
4547
export const mockBaseOptions = {
46-
baseUrl: `http://localhost:${rpPort}`,
47-
issuerUrl: `http://localhost:${opPort}`,
48+
baseUrl: `${mockOrigin}:${rpPort}`,
49+
issuerUrl: `${mockOrigin}:${opPort}`,
4850
clientMetadata: {
4951
client_id: "mock-client-id",
5052
client_secret: "mock-client-secret",
@@ -144,7 +146,7 @@ export const mockGetInit = (sid?: string): RequestInit => ({
144146

145147
export const mockClaims = {
146148
exp: 5000000000000 / 1000,
147-
iss: `http://localhost:${opPort}`,
149+
iss: `${mockOrigin}:${opPort}`,
148150
sub: "mock-sub",
149151
} as IdTokenClaims;
150152

@@ -157,6 +159,8 @@ export const mockStatus = {
157159
sub: mockClaims.sub,
158160
};
159161

162+
const mockCookieName = defaultCookieSettings.sessionIdName;
163+
160164
export const mockBaseClient = {
161165
...mockBaseOptions,
162166
factory: mock(),
@@ -166,7 +170,7 @@ export const mockBaseClient = {
166170
updateSession: mock(),
167171
fetchSession: mock(),
168172
deleteSession: mock(),
169-
cookieTypeBox: t.Cookie({ "mock-cookie-name": t.String() }),
173+
cookieTypeBox: t.Cookie({ [mockCookieName]: t.Optional(t.String()) }),
170174
createAuthHook: mock(),
171175
createEndpoints: mock(),
172176
logger: mockLogger,
@@ -176,7 +180,7 @@ export const mockBaseClient = {
176180
sessions: mock(),
177181
} as DeepPartial<OidcClient> as OidcClient;
178182
mockBaseClient.clients["https://op.example.com"] = mockBaseClient.client;
179-
mockBaseClient.clients[`http://localhost:${opPort}`] = mockBaseClient.client;
183+
mockBaseClient.clients[`${mockOrigin}:${opPort}`] = mockBaseClient.client;
180184

181185
export const mockCookie = {
182186
[defaultCookieSettings.sessionIdName]: {

__mock__/issuerMetadata.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { mockOrigin } from "./const.ts";
2+
13
export const mockIssuerMetadata = (opPort: number) => ({
2-
issuer: `http://localhost:${opPort}`,
3-
authorization_endpoint: `http://localhost:${opPort}/oauth2/authorize`,
4-
token_endpoint: `http://localhost:${opPort}/oauth2/token`,
5-
userinfo_endpoint: `http://localhost:${opPort}/oauth2/userinfo`,
6-
registration_endpoint: `http://localhost:${opPort}/oauth2/clients`,
7-
jwks_uri: `http://localhost:${opPort}/oauth2/keys`,
4+
issuer: `${mockOrigin}:${opPort}`,
5+
authorization_endpoint: `${mockOrigin}:${opPort}/oauth2/authorize`,
6+
token_endpoint: `${mockOrigin}:${opPort}/oauth2/token`,
7+
userinfo_endpoint: `${mockOrigin}:${opPort}/oauth2/userinfo`,
8+
registration_endpoint: `${mockOrigin}:${opPort}/oauth2/clients`,
9+
jwks_uri: `${mockOrigin}:${opPort}/oauth2/keys`,
810
response_types_supported: ["code"],
911
response_modes_supported: ["query"],
1012
grant_types_supported: ["authorization_code", "refresh_token"],
@@ -60,23 +62,23 @@ export const mockIssuerMetadata = (opPort: number) => ({
6062
"c_hash",
6163
],
6264
code_challenge_methods_supported: ["S256"],
63-
introspection_endpoint: `http://localhost:${opPort}/oauth2/introspect`,
65+
introspection_endpoint: `${mockOrigin}:${opPort}/oauth2/introspect`,
6466
introspection_endpoint_auth_methods_supported: [
6567
"client_secret_basic",
6668
"client_secret_post",
6769
"client_secret_jwt",
6870
"private_key_jwt",
6971
"none",
7072
],
71-
revocation_endpoint: `http://localhost:${opPort}/oauth2/revoke`,
73+
revocation_endpoint: `${mockOrigin}:${opPort}/oauth2/revoke`,
7274
revocation_endpoint_auth_methods_supported: [
7375
"client_secret_basic",
7476
"client_secret_post",
7577
"client_secret_jwt",
7678
"private_key_jwt",
7779
"none",
7880
],
79-
end_session_endpoint: `http://localhost:${opPort}/oauth2/logout`,
81+
end_session_endpoint: `${mockOrigin}:${opPort}/oauth2/logout`,
8082
request_parameter_supported: true,
8183
request_object_signing_alg_values_supported: [
8284
"HS256",
@@ -89,7 +91,7 @@ export const mockIssuerMetadata = (opPort: number) => ({
8991
"ES384",
9092
"ES512",
9193
],
92-
device_authorization_endpoint: `http://localhost:${opPort}/oauth2/device/authorize`,
94+
device_authorization_endpoint: `${mockOrigin}:${opPort}/oauth2/device/authorize`,
9395
dpop_signing_alg_values_supported: [
9496
"RS256",
9597
"RS384",

__mock__/mockProvider.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type TokenSetParameters,
1010
generators,
1111
} from "openid-client";
12+
import { mockOrigin } from "./const.ts";
1213
import { mockIssuerMetadata } from "./issuerMetadata.ts";
1314

1415
export const mockProvider = async (port: number) => {
@@ -75,7 +76,7 @@ export const mockProvider = async (port: number) => {
7576
}
7677

7778
const claims: IdTokenClaims = {
78-
iss: `http://localhost:${port}`,
79+
iss: `${mockOrigin}:${port}`,
7980
sub: "elysia-openid-client",
8081
aud: client_id,
8182
exp: Math.floor(Date.now() / 1000) + 3600,

__test__/general.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterAll, describe, expect, test } from "bun:test";
22
import OidcClient from "@/index";
3-
import { mockPostInit } from "@/mock/const";
3+
import { mockOrigin, mockPostInit } from "@/mock/const";
44
import { getRandomPort } from "@/mock/getRandomPort";
55
import { mockProvider } from "@/mock/mockProvider";
66
import Elysia from "elysia";
@@ -10,8 +10,8 @@ describe("Integration/general", async () => {
1010
const opPort = getRandomPort();
1111
const rpPort = getRandomPort();
1212

13-
const baseUrl = `http://localhost:${rpPort}`;
14-
const issuerUrl = `http://localhost:${opPort}`;
13+
const baseUrl = `${mockOrigin}:${rpPort}`;
14+
const issuerUrl = `${mockOrigin}:${opPort}`;
1515

1616
const op = await mockProvider(opPort);
1717

src/core/baseOidcClient.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterAll, describe, expect, test } from "bun:test";
22
import { defaultCookieSettings, defaultSettings } from "@/const";
3-
import { mockBaseOptions, opPort } from "@/mock/const";
3+
import { mockBaseOptions, mockOrigin, opPort } from "@/mock/const";
44
import { mockProvider } from "@/mock/mockProvider";
55
import { BaseOidcClient } from "./BaseOidcClient.ts";
66

@@ -16,7 +16,7 @@ describe("Unit/core/BaseOidcClient", () => {
1616
const mockOptions = structuredClone(mockBaseOptions);
1717
const client = await BaseOidcClient.factory({
1818
...mockOptions,
19-
issuerUrl: `http://localhost:${opPort}`,
19+
issuerUrl: `${mockOrigin}:${opPort}`,
2020
settings: defaultSettings,
2121
cookieSettings: defaultCookieSettings,
2222
});

src/endpoints/callbackEndpoint.test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
mockActiveSession,
55
mockBaseClient,
66
mockLoginSession,
7+
mockOrigin,
78
mockResetRecursively,
89
mockSessionId,
910
rpPort,
@@ -29,19 +30,19 @@ describe("Unit/endpoints/callbackEndpoint", () => {
2930

3031
test("Succeeded", async () => {
3132
const app = new Elysia().use(endpoint.call(mockBaseClient));
32-
const response = await app.handle(new Request(`http://localhost${path}`));
33+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
3334

3435
expect(response.status).toBe(302);
3536
expect(response.headers.get("location")).toBe(
36-
`http://localhost:${rpPort}${defaultSettings.callbackCompletedPath}`,
37+
`${mockOrigin}:${rpPort}${defaultSettings.callbackCompletedPath}`,
3738
);
3839
});
3940

4041
test("Session does not exist", async () => {
4142
mockBaseClient.fetchSession = mock().mockReturnValue(null);
4243

4344
const app = new Elysia().use(endpoint.call(mockBaseClient));
44-
const response = await app.handle(new Request(`http://localhost${path}`));
45+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
4546

4647
expect(response.status).toBe(401);
4748
expect(logger?.warn).toHaveBeenCalledTimes(1);
@@ -67,7 +68,7 @@ describe("Unit/endpoints/callbackEndpoint", () => {
6768
mockBaseClient.fetchSession = mock().mockReturnValue(session);
6869

6970
const app = new Elysia().use(endpoint.call(mockBaseClient));
70-
const response = await app.handle(new Request(`http://localhost${path}`));
71+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
7172

7273
expect(response.status).toBe(401);
7374
expect(logger?.warn).toHaveBeenCalledTimes(1);
@@ -77,7 +78,7 @@ describe("Unit/endpoints/callbackEndpoint", () => {
7778
mockBaseClient.updateSession = mock().mockReturnValue(null);
7879

7980
const app = new Elysia().use(endpoint.call(mockBaseClient));
80-
const response = await app.handle(new Request(`http://localhost${path}`));
81+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
8182

8283
expect(response.status).toBe(401);
8384
expect(logger?.warn).toHaveBeenCalledTimes(1);
@@ -89,7 +90,7 @@ describe("Unit/endpoints/callbackEndpoint", () => {
8990
});
9091

9192
const app = new Elysia().use(endpoint.call(mockBaseClient));
92-
const response = await app.handle(new Request(`http://localhost${path}`));
93+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
9394

9495
expect(response.status).toBe(500);
9596
expect(logger?.warn).toHaveBeenCalledTimes(1);

src/endpoints/claimsEndpoint.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
mockActiveSessionWithRealIdToken,
55
mockBaseClient,
66
mockIdTokenClaims,
7+
mockOrigin,
78
mockPostInit,
89
mockResetRecursively,
910
} from "@/mock/const";
@@ -25,7 +26,7 @@ describe("Unit/endpoints/claimsEndpoint", () => {
2526
.resolve(() => ({ session: mockActiveSessionWithRealIdToken }))
2627
.use(endpoint.call(mockBaseClient));
2728
const response = await app
28-
.handle(new Request(`http://localhost${path}`, mockPostInit()))
29+
.handle(new Request(`${mockOrigin}${path}`, mockPostInit()))
2930
.then((res) => res);
3031

3132
expect(response.status).toBe(200);
@@ -39,7 +40,7 @@ describe("Unit/endpoints/claimsEndpoint", () => {
3940
.resolve(() => ({ session: null }))
4041
.use(endpoint.call(mockBaseClient));
4142
const response = await app
42-
.handle(new Request(`http://localhost${path}`, mockPostInit()))
43+
.handle(new Request(`${mockOrigin}${path}`, mockPostInit()))
4344
.then((res) => res.status);
4445

4546
expect(response).toBe(401);

src/endpoints/introspectEndpoint.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
mockActiveSession,
55
mockBaseClient,
66
mockGetInit,
7+
mockOrigin,
78
mockResetRecursively,
89
} from "@/mock/const";
910
import Elysia from "elysia";
@@ -25,7 +26,7 @@ describe("Unit/endpoints/introspectEndpoint", () => {
2526
.resolve(() => ({ session: mockActiveSession }))
2627
.use(endpoint.call(mockBaseClient));
2728
const response = await app.handle(
28-
new Request(`http://localhost${path}`, mockGetInit()),
29+
new Request(`${mockOrigin}${path}`, mockGetInit()),
2930
);
3031

3132
expect(response.status).toBe(200);
@@ -39,7 +40,7 @@ describe("Unit/endpoints/introspectEndpoint", () => {
3940
.resolve(() => ({ session: null }))
4041
.use(endpoint.call(mockBaseClient));
4142
const response = await app.handle(
42-
new Request(`http://localhost${path}`, mockGetInit()),
43+
new Request(`${mockOrigin}${path}`, mockGetInit()),
4344
);
4445

4546
expect(response.status).toBe(401);
@@ -58,7 +59,7 @@ describe("Unit/endpoints/introspectEndpoint", () => {
5859
.resolve(() => ({ session: mockActiveSession }))
5960
.use(endpoint.call(mockBaseClient));
6061
const response = await app
61-
.handle(new Request(`http://localhost${path}`))
62+
.handle(new Request(`${mockOrigin}${path}`))
6263
.then((res) => res.status);
6364

6465
expect(response).toBe(500);

src/endpoints/loginEndpoint.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, mock, test } from "bun:test";
22
import { defaultSettings } from "@/const";
33
import {
44
mockBaseClient,
5+
mockOrigin,
56
mockResetRecursively,
67
mockSessionId,
78
opPort,
@@ -19,18 +20,18 @@ describe("Unit/endpoints/loginEndpoint", () => {
1920
mockResetRecursively(mockBaseClient);
2021
mockBaseClient.createSession = mock().mockReturnValue([
2122
mockSessionId,
22-
`http://localhost:${opPort}/authorization`,
23+
`${mockOrigin}:${opPort}/authorization`,
2324
]);
2425
});
2526

2627
test("Succeeded", async () => {
2728
const app = new Elysia().use(endpoint.call(mockBaseClient));
2829

29-
const response = await app.handle(new Request(`http://localhost${path}`));
30+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
3031

3132
expect(response.status).toBe(303);
3233
expect(response.headers.get("location")).toBe(
33-
`http://localhost:${opPort}/authorization`,
34+
`${mockOrigin}:${opPort}/authorization`,
3435
);
3536

3637
const cookie = setCookie.parse(
@@ -55,7 +56,7 @@ describe("Unit/endpoints/loginEndpoint", () => {
5556
});
5657

5758
const app = new Elysia().use(endpoint.call(mockBaseClient));
58-
const response = await app.handle(new Request(`http://localhost${path}`));
59+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
5960

6061
expect(response.status).toBe(401);
6162
expect(logger?.warn).toHaveBeenCalledTimes(1);
@@ -67,7 +68,7 @@ describe("Unit/endpoints/loginEndpoint", () => {
6768
});
6869

6970
const app = new Elysia().use(endpoint.call(mockBaseClient));
70-
const response = await app.handle(new Request(`http://localhost${path}`));
71+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
7172

7273
expect(response.status).toBe(500);
7374
expect(logger?.warn).toHaveBeenCalledTimes(1);

src/endpoints/logoutEndpoint.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defaultSettings } from "@/const";
33
import {
44
mockActiveSession,
55
mockBaseClient,
6+
mockOrigin,
67
mockResetRecursively,
78
} from "@/mock/const";
89
import Elysia from "elysia";
@@ -22,7 +23,7 @@ describe("Unit/endpoints/logoutEndpoint", () => {
2223

2324
test("Succeeded", async () => {
2425
const app = new Elysia().use(endpoint.call(mockBaseClient));
25-
const response = await app.handle(new Request(`http://localhost${path}`));
26+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
2627

2728
expect(response.status).toBe(303);
2829
expect(response.headers.get("location")).toBe(redirectPath);
@@ -32,7 +33,7 @@ describe("Unit/endpoints/logoutEndpoint", () => {
3233
mockBaseClient.fetchSession = mock().mockReturnValue(null);
3334

3435
const app = new Elysia().use(endpoint.call(mockBaseClient));
35-
const response = await app.handle(new Request(`http://localhost${path}`));
36+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
3637

3738
expect(response.status).toBe(401);
3839
expect(logger?.warn).toHaveBeenCalledTimes(1);
@@ -45,7 +46,7 @@ describe("Unit/endpoints/logoutEndpoint", () => {
4546
});
4647

4748
const app = new Elysia().use(endpoint.call(mockBaseClient));
48-
const response = await app.handle(new Request(`http://localhost${path}`));
49+
const response = await app.handle(new Request(`${mockOrigin}${path}`));
4950

5051
expect(response.status).toBe(500);
5152
expect(logger?.warn).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)