Skip to content

Commit

Permalink
test: use env-var and base url (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleF83 authored Jan 20, 2025
1 parent 150cbd9 commit 8d0a586
Show file tree
Hide file tree
Showing 16 changed files with 1,062 additions and 1,645 deletions.
10 changes: 1 addition & 9 deletions e2e/.env
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
OIDC_TOKEN_URL=https://localhost:8443/connect/token
OIDC_TOKEN_URL_WITH_BASE_PATH=https://localhost:8443/some-base-path/connect/token
OIDC_AUTHORIZE_URL=https://localhost:8443/connect/authorize
OIDC_INTROSPECTION_URL=https://localhost:8443/connect/introspect
OIDC_USERINFO_URL=https://localhost:8443/connect/userinfo
OIDC_GRANTS_URL=https://localhost:8443/grants
OIDC_MANAGE_USERS_URL=https://localhost:8443/api/v1/user
OIDC_DISCOVERY_ENDPOINT=https://localhost:8443/.well-known/openid-configuration
OIDC_DISCOVERY_ENDPOINT_WITH_BASE_PATH=https://localhost:8443/some-base-path/.well-known/openid-configuration
OIDC_BASE_URL=https://localhost:8443
5 changes: 3 additions & 2 deletions e2e/helpers/authorization-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { expect } from '@jest/globals';
import { Page } from 'playwright-chromium';

import { User } from '../types';
import { oidcAuthorizeUrl } from './endpoints';

const authorizationEndpoint = async (
page: Page,
parameters: URLSearchParams,
user: User,
redirect_uri: string,
): Promise<URL> => {
const url = `${process.env.OIDC_AUTHORIZE_URL}?${parameters.toString()}`;
const url = `${oidcAuthorizeUrl.href}?${parameters.toString()}`;
const response = await page.goto(url);
expect(response.ok()).toBeTruthy();
expect(response.ok()).toBe(true);

await page.waitForSelector('[id=Input_Username]');
await page.type('[id=Input_Username]', user.Username);
Expand Down
18 changes: 18 additions & 0 deletions e2e/helpers/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { from, logger } from 'env-var';
import * as dotenv from 'dotenv';
import { URL } from 'node:url';
dotenv.config();
const env = from(process.env, undefined, logger);

export const oidcBaseUrl = env.get('OIDC_BASE_URL').required().asUrlObject();
export const basePath = 'some-base-path';

export const oidcTokenUrl = new URL('/connect/token', oidcBaseUrl);
export const oidcTokenUrlWithBasePath = new URL(`/${basePath}/connect/token`, oidcBaseUrl);
export const oidcAuthorizeUrl = new URL('connect/authorize', oidcBaseUrl);
export const oidcIntrospectionUrl = new URL('/connect/introspect', oidcBaseUrl);
export const oidcUserInfoUrl = new URL('/connect/userinfo', oidcBaseUrl);
export const oidcGrantsUrl = new URL('/grants', oidcBaseUrl);
export const oidcUserManagementUrl = new URL('/api/v1/user', oidcBaseUrl);
export const oidcDiscoveryEndpoint = new URL('/.well-known/openid-configuration', oidcBaseUrl);
export const oidcDiscoveryEndpointWithBasePath = new URL(`/${basePath}/.well-known/openid-configuration`, oidcBaseUrl);
5 changes: 3 additions & 2 deletions e2e/helpers/grants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { expect } from '@jest/globals';
import { Page } from 'playwright-chromium';

import { User } from '../types';
import { oidcGrantsUrl } from './endpoints';

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

await page.waitForSelector('[id=Input_Username]');
await page.type('[id=Input_Username]', user.Username);
Expand Down
5 changes: 3 additions & 2 deletions e2e/helpers/introspect-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs/promises';
import path from 'path';

import * as yaml from 'yaml';
import { oidcIntrospectionUrl } from './endpoints';

const introspectEndpoint = async (
token: string,
Expand All @@ -24,13 +25,13 @@ const introspectEndpoint = async (
token,
});

const response = await fetch(process.env.OIDC_INTROSPECTION_URL, {
const response = await fetch(oidcIntrospectionUrl, {
method: 'POST',
body: requestBody,
headers,
});

expect(response).toBeDefined();
expect(response.ok).toBe(true);
const result = (await response.json()) as unknown;
expect(result).toMatchSnapshot(snapshotPropertyMatchers);
};
Expand Down
6 changes: 4 additions & 2 deletions e2e/helpers/token-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { expect } from '@jest/globals';
import { decode as decodeJWT } from 'jws';
import { oidcTokenUrl } from './endpoints';

const tokenEndpoint = async (
parameters: URLSearchParams,
snapshotPropertyMatchers: Record<string, unknown> = {},
): Promise<string> => {
const tokenEndpointResponse = await fetch(process.env.OIDC_TOKEN_URL, {
const response = await fetch(oidcTokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: parameters.toString(),
});
const result = (await tokenEndpointResponse.json()) as { access_token: string };
expect(response.ok).toBe(true);
const result = (await response.json()) as { access_token: string };
expect(result.access_token).toBeDefined();
const token = result.access_token;
const decodedToken = decodeJWT(token);
Expand Down
4 changes: 3 additions & 1 deletion e2e/helpers/user-info-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { expect } from '@jest/globals';
import { oidcUserInfoUrl } from './endpoints';

const userInfoEndpoint = async (
token: string,
snapshotPropertyMatchers: Record<string, unknown> = {},
): Promise<void> => {
const response = await fetch(process.env.OIDC_USERINFO_URL, {
const response = await fetch(oidcUserInfoUrl, {
headers: { authorization: `Bearer ${token}` },
});
expect(response.ok).toBe(true);
const result = (await response.json()) as unknown;
expect(result).toMatchSnapshot(snapshotPropertyMatchers);
};
Expand Down
1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"chance": "^1.1.12",
"dotenv": "^16.4.7",
"env-var": "^7.5.0",
"jws": "^4.0.0",
"playwright-chromium": "^1.49.1",
"wait-on": "^8.0.1",
Expand Down
14 changes: 8 additions & 6 deletions e2e/tests/base-path.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { describe, test, beforeAll, expect } from '@jest/globals';
import * as dotenv from 'dotenv';

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

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

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

test('Discovery Endpoint', async () => {
const response = await fetch(process.env.OIDC_DISCOVERY_ENDPOINT_WITH_BASE_PATH);
const response = await fetch(oidcDiscoveryEndpointWithBasePath);
expect(response.ok).toBe(true);
const result = (await response.json()) as unknown;
expect(result).toHaveProperty('token_endpoint', process.env.OIDC_TOKEN_URL_WITH_BASE_PATH);
expect(result).toHaveProperty('token_endpoint', oidcTokenUrlWithBasePath.href);
});

test('Token Endpoint', async () => {
Expand All @@ -29,13 +29,15 @@ describe('Base path', () => {
scope: client.AllowedScopes.join(' '),
});

const response = await fetch(process.env.OIDC_TOKEN_URL_WITH_BASE_PATH, {
const response = await fetch(oidcTokenUrlWithBasePath, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: parameters.toString(),
});
expect(response).toBeDefined();
expect(response.ok).toBe(true);
const result = (await response.json()) as { access_token: string };
expect(result.access_token).toBeDefined();
});
});
10 changes: 4 additions & 6 deletions e2e/tests/custom-endpoints/user-management.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, test, beforeAll, expect } from '@jest/globals';
import Chance from 'chance';
import * as dotenv from 'dotenv';

import clients from '../../config/clients.json';
import { introspectEndpoint, tokenEndpoint, userInfoEndpoint } from '../../helpers';
import { Client, User } from '../../types';
import { oidcUserManagementUrl } from 'e2e/helpers/endpoints';

describe('User management', () => {
const chance = new Chance();
Expand All @@ -19,15 +19,13 @@ describe('User management', () => {
let token: string;

beforeAll(() => {
dotenv.config();

client = clients.find(c => c.ClientId === 'password-flow-client-id');
});

test('Get user from configuration', async () => {
const configUserId = 'user_with_all_claim_types';
const configUsername = 'user_with_all_claim_types';
const response = await fetch(`${process.env.OIDC_MANAGE_USERS_URL}/${configUserId}`);
const response = await fetch(`${oidcUserManagementUrl.href}/${configUserId}`);
expect(response.status).toBe(200);
const receivedUser = (await response.json()) as User;
expect(receivedUser).toHaveProperty('username', configUsername);
Expand Down Expand Up @@ -61,7 +59,7 @@ describe('User management', () => {
},
],
};
const response = await fetch(process.env.OIDC_MANAGE_USERS_URL, {
const response = await fetch(oidcUserManagementUrl, {
method: 'POST',
body: JSON.stringify(user),
headers: { 'Content-Type': 'application/json' },
Expand All @@ -72,7 +70,7 @@ describe('User management', () => {
});

test('Get user', async () => {
const response = await fetch(`${process.env.OIDC_MANAGE_USERS_URL}/${subjectId}`);
const response = await fetch(`${oidcUserManagementUrl.href}/${subjectId}`);
expect(response.status).toBe(200);
const receivedUser = (await response.json()) as User;
expect(receivedUser).toHaveProperty('username', username);
Expand Down
3 changes: 0 additions & 3 deletions e2e/tests/flows/authorization-code-pkce.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as crypto from 'crypto';
import * as fs from 'fs';
import path from 'path';

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

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

beforeAll(async () => {
dotenv.config();

browser = await chromium.launch();
client = clients.find(c => c.ClientId === 'authorization-code-with-pkce-client-id');
expect(client).toBeDefined();
Expand Down
3 changes: 0 additions & 3 deletions e2e/tests/flows/authorization-code.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { describe, test, beforeAll, afterAll, beforeEach, afterEach, expect } fr
import * as fs from 'fs';
import path from 'path';

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

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

beforeAll(async () => {
dotenv.config();

browser = await chromium.launch();
client = clients.find(c => c.ClientId === 'authorization-code-client-id');
expect(client).toBeDefined();
Expand Down
2 changes: 0 additions & 2 deletions e2e/tests/flows/client-credentials-flow.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, test, beforeAll, expect } from '@jest/globals';
import * as dotenv from 'dotenv';

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

beforeAll(() => {
dotenv.config();
client = clients.find(c => c.ClientId === 'client-credentials-flow-client-id');
expect(client).toBeDefined();
});
Expand Down
3 changes: 0 additions & 3 deletions e2e/tests/flows/implicit-flow.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { describe, test, beforeAll, afterAll, beforeEach, afterEach, expect } fr
import * as fs from 'fs';
import path from 'path';

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

beforeAll(async () => {
dotenv.config();

browser = await chromium.launch();
client = clients.find(c => c.ClientId === 'implicit-flow-client-id');
expect(client).toBeDefined();
Expand Down
2 changes: 0 additions & 2 deletions e2e/tests/flows/password-flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { describe, test, beforeAll, expect } from '@jest/globals';
import * as fs from 'fs';
import path from 'path';

import * as dotenv from 'dotenv';
import * as yaml from 'yaml';

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

beforeAll(() => {
dotenv.config();
client = clients.find(c => c.ClientId === 'password-flow-client-id');
expect(client).toBeDefined();
});
Expand Down
Loading

0 comments on commit 8d0a586

Please sign in to comment.