Skip to content

Commit 7929a66

Browse files
committed
test: use env-var and base url
1 parent 150cbd9 commit 7929a66

16 files changed

+56
-43
lines changed

e2e/.env

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
OIDC_TOKEN_URL=https://localhost:8443/connect/token
2-
OIDC_TOKEN_URL_WITH_BASE_PATH=https://localhost:8443/some-base-path/connect/token
3-
OIDC_AUTHORIZE_URL=https://localhost:8443/connect/authorize
4-
OIDC_INTROSPECTION_URL=https://localhost:8443/connect/introspect
5-
OIDC_USERINFO_URL=https://localhost:8443/connect/userinfo
6-
OIDC_GRANTS_URL=https://localhost:8443/grants
7-
OIDC_MANAGE_USERS_URL=https://localhost:8443/api/v1/user
8-
OIDC_DISCOVERY_ENDPOINT=https://localhost:8443/.well-known/openid-configuration
9-
OIDC_DISCOVERY_ENDPOINT_WITH_BASE_PATH=https://localhost:8443/some-base-path/.well-known/openid-configuration
1+
OIDC_BASE_URL=https://localhost:8443

e2e/helpers/authorization-endpoint.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { expect } from '@jest/globals';
22
import { Page } from 'playwright-chromium';
33

44
import { User } from '../types';
5+
import { oidcAuthorizeUrl } from './endpoints';
56

67
const authorizationEndpoint = async (
78
page: Page,
89
parameters: URLSearchParams,
910
user: User,
1011
redirect_uri: string,
1112
): Promise<URL> => {
12-
const url = `${process.env.OIDC_AUTHORIZE_URL}?${parameters.toString()}`;
13+
const url = `${oidcAuthorizeUrl.toString()}?${parameters.toString()}`;
1314
const response = await page.goto(url);
14-
expect(response.ok()).toBeTruthy();
15+
expect(response.ok()).toBe(true);
1516

1617
await page.waitForSelector('[id=Input_Username]');
1718
await page.type('[id=Input_Username]', user.Username);

e2e/helpers/endpoints.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { from, logger } from 'env-var';
2+
import * as dotenv from 'dotenv';
3+
import { URL } from 'node:url';
4+
dotenv.config();
5+
const env = from(process.env, undefined, logger);
6+
7+
export const oidcBaseUrl = env.get('OIDC_BASE_URL').required().asUrlObject();
8+
export const basePath = 'some-base-path';
9+
10+
export const oidcTokenUrl = new URL('/connect/token', oidcBaseUrl);
11+
export const oidcTokenUrlWithBasePath = new URL(`${basePath}/connect/token`, oidcBaseUrl);
12+
export const oidcAuthorizeUrl = new URL('connect/authorize', oidcBaseUrl);
13+
export const oidcIntrospectionUrl = new URL('/connect/introspect', oidcBaseUrl);
14+
export const oidcUserInfoUrl = new URL('/connect/userinfo', oidcBaseUrl);
15+
export const oidcGrantsUrl = new URL('/grants', oidcBaseUrl);
16+
export const oidcUserManagementUrl = new URL('/api/v1/user', oidcBaseUrl);
17+
export const oidcDiscoveryEndpoint = new URL('/.well-known/openid-configuration', oidcBaseUrl);
18+
export const oidcDiscoveryEndpointWithBasePath = new URL(`/${basePath}/.well-known/openid-configuration`, oidcBaseUrl);

e2e/helpers/grants.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { expect } from '@jest/globals';
22
import { Page } from 'playwright-chromium';
33

44
import { User } from '../types';
5+
import { oidcGrantsUrl } from './endpoints';
56

67
const grantsEndpoint = async (page: Page, user: User): Promise<void> => {
7-
const response = await page.goto(process.env.OIDC_GRANTS_URL);
8-
expect(response.ok()).toBeTruthy();
8+
const response = await page.goto(oidcGrantsUrl.toString());
9+
expect(response.ok()).toBe(true);
910

1011
await page.waitForSelector('[id=Input_Username]');
1112
await page.type('[id=Input_Username]', user.Username);

e2e/helpers/introspect-endpoint.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs/promises';
44
import path from 'path';
55

66
import * as yaml from 'yaml';
7+
import { oidcIntrospectionUrl } from './endpoints';
78

89
const introspectEndpoint = async (
910
token: string,
@@ -24,13 +25,13 @@ const introspectEndpoint = async (
2425
token,
2526
});
2627

27-
const response = await fetch(process.env.OIDC_INTROSPECTION_URL, {
28+
const response = await fetch(oidcIntrospectionUrl, {
2829
method: 'POST',
2930
body: requestBody,
3031
headers,
3132
});
3233

33-
expect(response).toBeDefined();
34+
expect(response.ok).toBe(true);
3435
const result = (await response.json()) as unknown;
3536
expect(result).toMatchSnapshot(snapshotPropertyMatchers);
3637
};

e2e/helpers/token-endpoint.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { expect } from '@jest/globals';
22
import { decode as decodeJWT } from 'jws';
3+
import { oidcTokenUrl } from './endpoints';
34

45
const tokenEndpoint = async (
56
parameters: URLSearchParams,
67
snapshotPropertyMatchers: Record<string, unknown> = {},
78
): Promise<string> => {
8-
const tokenEndpointResponse = await fetch(process.env.OIDC_TOKEN_URL, {
9+
const response = await fetch(oidcTokenUrl, {
910
method: 'POST',
1011
headers: {
1112
'Content-Type': 'application/x-www-form-urlencoded',
1213
},
1314
body: parameters.toString(),
1415
});
15-
const result = (await tokenEndpointResponse.json()) as { access_token: string };
16+
expect(response.ok).toBe(true);
17+
const result = (await response.json()) as { access_token: string };
1618
expect(result.access_token).toBeDefined();
1719
const token = result.access_token;
1820
const decodedToken = decodeJWT(token);

e2e/helpers/user-info-endpoint.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { expect } from '@jest/globals';
2+
import { oidcUserInfoUrl } from './endpoints';
23

34
const userInfoEndpoint = async (
45
token: string,
56
snapshotPropertyMatchers: Record<string, unknown> = {},
67
): Promise<void> => {
7-
const response = await fetch(process.env.OIDC_USERINFO_URL, {
8+
const response = await fetch(oidcUserInfoUrl, {
89
headers: { authorization: `Bearer ${token}` },
910
});
11+
expect(response.ok).toBe(true);
1012
const result = (await response.json()) as unknown;
1113
expect(result).toMatchSnapshot(snapshotPropertyMatchers);
1214
};

e2e/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"chance": "^1.1.12",
1313
"dotenv": "^16.4.7",
14+
"env-var": "^7.5.0",
1415
"jws": "^4.0.0",
1516
"playwright-chromium": "^1.49.1",
1617
"wait-on": "^8.0.1",

e2e/tests/base-path.spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { describe, test, beforeAll, expect } from '@jest/globals';
2-
import * as dotenv from 'dotenv';
32

43
import clients from '../config/clients.json';
54
import type { Client } from '../types';
5+
import { oidcDiscoveryEndpointWithBasePath, oidcTokenUrlWithBasePath } from 'e2e/helpers/endpoints';
66

77
describe('Base path', () => {
88
let client: Client | undefined;
99

1010
beforeAll(() => {
11-
dotenv.config();
1211
client = clients.find(c => c.ClientId === 'client-credentials-flow-client-id');
1312
expect(client).toBeDefined();
1413
});
1514

1615
test('Discovery Endpoint', async () => {
17-
const response = await fetch(process.env.OIDC_DISCOVERY_ENDPOINT_WITH_BASE_PATH);
16+
const response = await fetch(oidcDiscoveryEndpointWithBasePath);
1817
const result = (await response.json()) as unknown;
19-
expect(result).toHaveProperty('token_endpoint', process.env.OIDC_TOKEN_URL_WITH_BASE_PATH);
18+
expect(result).toHaveProperty('token_endpoint', oidcTokenUrlWithBasePath);
2019
});
2120

2221
test('Token Endpoint', async () => {
@@ -29,13 +28,13 @@ describe('Base path', () => {
2928
scope: client.AllowedScopes.join(' '),
3029
});
3130

32-
const response = await fetch(process.env.OIDC_TOKEN_URL_WITH_BASE_PATH, {
31+
const response = await fetch(oidcTokenUrlWithBasePath, {
3332
method: 'POST',
3433
headers: {
3534
'Content-Type': 'application/x-www-form-urlencoded',
3635
},
3736
body: parameters.toString(),
3837
});
39-
expect(response).toBeDefined();
38+
expect(response.ok).toBeDefined();
4039
});
4140
});

e2e/tests/custom-endpoints/user-management.spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, test, beforeAll, expect } from '@jest/globals';
22
import Chance from 'chance';
3-
import * as dotenv from 'dotenv';
43

54
import clients from '../../config/clients.json';
65
import { introspectEndpoint, tokenEndpoint, userInfoEndpoint } from '../../helpers';
76
import { Client, User } from '../../types';
7+
import { oidcUserManagementUrl } from 'e2e/helpers/endpoints';
8+
import { URL } from 'node:url';
89

910
describe('User management', () => {
1011
const chance = new Chance();
@@ -19,15 +20,13 @@ describe('User management', () => {
1920
let token: string;
2021

2122
beforeAll(() => {
22-
dotenv.config();
23-
2423
client = clients.find(c => c.ClientId === 'password-flow-client-id');
2524
});
2625

2726
test('Get user from configuration', async () => {
2827
const configUserId = 'user_with_all_claim_types';
2928
const configUsername = 'user_with_all_claim_types';
30-
const response = await fetch(`${process.env.OIDC_MANAGE_USERS_URL}/${configUserId}`);
29+
const response = await fetch(new URL(`/${configUserId}`, oidcUserManagementUrl));
3130
expect(response.status).toBe(200);
3231
const receivedUser = (await response.json()) as User;
3332
expect(receivedUser).toHaveProperty('username', configUsername);
@@ -61,7 +60,7 @@ describe('User management', () => {
6160
},
6261
],
6362
};
64-
const response = await fetch(process.env.OIDC_MANAGE_USERS_URL, {
63+
const response = await fetch(oidcUserManagementUrl, {
6564
method: 'POST',
6665
body: JSON.stringify(user),
6766
headers: { 'Content-Type': 'application/json' },
@@ -72,7 +71,7 @@ describe('User management', () => {
7271
});
7372

7473
test('Get user', async () => {
75-
const response = await fetch(`${process.env.OIDC_MANAGE_USERS_URL}/${subjectId}`);
74+
const response = await fetch(new URL(`/${subjectId}`, oidcUserManagementUrl));
7675
expect(response.status).toBe(200);
7776
const receivedUser = (await response.json()) as User;
7877
expect(receivedUser).toHaveProperty('username', username);

e2e/tests/flows/authorization-code-pkce.e2e-spec.ts

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as crypto from 'crypto';
33
import * as fs from 'fs';
44
import path from 'path';
55

6-
import * as dotenv from 'dotenv';
76
import { Browser, BrowserContext, chromium, Page } from 'playwright-chromium';
87
import * as yaml from 'yaml';
98

@@ -40,8 +39,6 @@ describe('Authorization Code Flow (with PKCE)', () => {
4039
let client: Client | undefined;
4140

4241
beforeAll(async () => {
43-
dotenv.config();
44-
4542
browser = await chromium.launch();
4643
client = clients.find(c => c.ClientId === 'authorization-code-with-pkce-client-id');
4744
expect(client).toBeDefined();

e2e/tests/flows/authorization-code.e2e-spec.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, test, beforeAll, afterAll, beforeEach, afterEach, expect } fr
22
import * as fs from 'fs';
33
import path from 'path';
44

5-
import * as dotenv from 'dotenv';
65
import { Browser, BrowserContext, chromium, Page } from 'playwright-chromium';
76
import * as yaml from 'yaml';
87

@@ -33,8 +32,6 @@ describe('Authorization Code Flow', () => {
3332
let client: Client | undefined;
3433

3534
beforeAll(async () => {
36-
dotenv.config();
37-
3835
browser = await chromium.launch();
3936
client = clients.find(c => c.ClientId === 'authorization-code-client-id');
4037
expect(client).toBeDefined();

e2e/tests/flows/client-credentials-flow.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { describe, test, beforeAll, expect } from '@jest/globals';
2-
import * as dotenv from 'dotenv';
32

43
import clients from '../../config/clients.json';
54
import { introspectEndpoint, tokenEndpoint } from '../../helpers';
@@ -10,7 +9,6 @@ describe('Client Credentials Flow', () => {
109
let token: string;
1110

1211
beforeAll(() => {
13-
dotenv.config();
1412
client = clients.find(c => c.ClientId === 'client-credentials-flow-client-id');
1513
expect(client).toBeDefined();
1614
});

e2e/tests/flows/implicit-flow.e2e-spec.ts

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, test, beforeAll, afterAll, beforeEach, afterEach, expect } fr
33
import * as fs from 'fs';
44
import path from 'path';
55

6-
import * as dotenv from 'dotenv';
76
import { decode as decodeJWT } from 'jws';
87
import { Browser, BrowserContext, chromium, Page } from 'playwright-chromium';
98
import * as yaml from 'yaml';
@@ -34,8 +33,6 @@ describe('Implicit Flow', () => {
3433
let client: Client | undefined;
3534

3635
beforeAll(async () => {
37-
dotenv.config();
38-
3936
browser = await chromium.launch();
4037
client = clients.find(c => c.ClientId === 'implicit-flow-client-id');
4138
expect(client).toBeDefined();

e2e/tests/flows/password-flow.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, test, beforeAll, expect } from '@jest/globals';
33
import * as fs from 'fs';
44
import path from 'path';
55

6-
import * as dotenv from 'dotenv';
76
import * as yaml from 'yaml';
87

98
import clients from '../../config/clients.json';
@@ -28,7 +27,6 @@ describe('Password Flow', () => {
2827
let token: string;
2928

3029
beforeAll(() => {
31-
dotenv.config();
3230
client = clients.find(c => c.ClientId === 'password-flow-client-id');
3331
expect(client).toBeDefined();
3432
});

pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)