Address polyfill issue with OIDC test.

This commit is contained in:
Sebastian Jeltsch
2025-03-08 21:32:53 +01:00
parent c14c544db7
commit 2a7db2b6eb
4 changed files with 132 additions and 100 deletions
@@ -0,0 +1,70 @@
import { expect, test } from "vitest";
import { OAuth2Server } from "oauth2-mock-server";
const port: number = 4005;
const address: string = `http://127.0.0.1:${port}`;
type OpenIdConfig = {
issuer: string;
token_endpoint: string;
authorization_endpoint: string;
userinfo_endpoint: string;
};
// NOTE: Having this server test live alongside the client is a bit odd.
test("OIDC", async () => {
const server = new OAuth2Server();
// Generate a new RSA key and add it to the keystore
await server.issuer.keys.generate("RS256");
const authPort = 9088;
const authAddress = "127.0.0.1";
await server.start(authPort, authAddress);
const response = await fetch(
`http://${authAddress}:${authPort}/.well-known/openid-configuration`,
);
const config: OpenIdConfig = await response.json();
expect(config.token_endpoint).toBe(`http://localhost:${authPort}/token`);
server.service.on("beforeUserinfo", (userInfoResponse, _req) => {
userInfoResponse.body = {
sub: "joanadoe",
email: "joana@doe.org",
email_verified: true,
};
userInfoResponse.statusCode = 200;
});
const login = await fetch(`${address}/api/auth/v1/oauth/oidc0/login`, {
redirect: "manual",
});
expect(login.status).toBe(303);
const location = login.headers.get("location")!;
expect(location).toContain(`http://localhost:${authPort}/authorize`);
const stateCookie = login.headers.get("set-cookie")!.split(";")[0];
const authorize = await fetch(location, { redirect: "manual" });
expect(authorize.status).toBe(302);
const callbackUrl = authorize.headers.get("location")!;
const callback = await fetch(callbackUrl, {
redirect: "manual",
credentials: "include",
headers: {
cookie: stateCookie,
},
});
expect(callback.status).toBe(303);
expect(callback.headers.get("location")).toBe("/_/auth/profile");
const authHeader = callback.headers.get("set-cookie")!;
expect(authHeader)
.to.be.a("string")
.and.match(new RegExp(".*auth_token=ey.*"));
await server.stop();
});
@@ -3,7 +3,11 @@ import { expect, test } from "vitest";
import { Client, Event, urlSafeBase64Encode } from "../../src/index";
import { status } from "http-status";
import { v7 as uuidv7, parse as uuidParse } from "uuid";
import { OAuth2Server } from "oauth2-mock-server";
const port: number = 4005;
const address: string = `http://127.0.0.1:${port}`;
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
type SimpleStrict = {
id: string;
@@ -27,10 +31,6 @@ type SimpleSubsetView = {
t_not_null: string;
};
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
const port: number = 4005;
const address: string = `http://127.0.0.1:${port}`;
async function connect(): Promise<Client> {
const client = Client.init(address);
await client.login("admin@localhost", "secret");
@@ -322,91 +322,3 @@ test("realtime subscribe table tests", async () => {
expect(events[1]["Update"]["text_not_null"]).equals(updatedMessage);
expect(events[2]["Delete"]["text_not_null"]).equals(updatedMessage);
});
test("JS runtime", async () => {
const expected = {
int: 5,
real: 4.2,
msg: "foo",
obj: {
nested: true,
},
};
const jsonUrl = `${address}/json`;
const json = await (await fetch(jsonUrl)).json();
expect(json).toMatchObject(expected);
const response = await fetch(`${address}/fetch?url=${encodeURI(jsonUrl)}`);
expect(await response.json()).toMatchObject(expected);
const errResp = await fetch(`${address}/error`);
expect(errResp.status).equals(status.IM_A_TEAPOT);
// Test that the periodic callback was called.
expect((await fetch(`${address}/await`)).status).equals(status.OK);
});
type OpenIdConfig = {
issuer: string;
token_endpoint: string;
authorization_endpoint: string;
userinfo_endpoint: string;
};
// NOTE: Having this test with the client is a bit odd.
test("OIDC", async () => {
const server = new OAuth2Server();
// Generate a new RSA key and add it to the keystore
await server.issuer.keys.generate("RS256");
const authPort = 9088;
const authAddress = "127.0.0.1";
await server.start(authPort, authAddress);
const response = await fetch(
`http://${authAddress}:${authPort}/.well-known/openid-configuration`,
);
const config: OpenIdConfig = await response.json();
expect(config.token_endpoint).toBe(`http://localhost:${authPort}/token`);
server.service.on("beforeUserinfo", (userInfoResponse, _req) => {
userInfoResponse.body = {
sub: "joanadoe",
email: "joana@doe.org",
email_verified: true,
};
userInfoResponse.statusCode = 200;
});
const login = await fetch(`${address}/api/auth/v1/oauth/oidc0/login`, {
redirect: "manual",
});
expect(login.status).toBe(303);
const location = login.headers.get("location")!;
expect(location).toContain(`http://localhost:${authPort}/authorize`);
const stateCookie = login.headers.get("set-cookie")!.split(";")[0];
const authorize = await fetch(location, { redirect: "manual" });
expect(authorize.status).toBe(302);
const callbackUrl = authorize.headers.get("location")!;
const callback = await fetch(callbackUrl, {
redirect: "manual",
credentials: "include",
headers: {
cookie: stateCookie,
},
});
// FIXME: The test passes if I spin up a separate oauth2-mock-server with the
// same code :/
expect(callback.status).toBe(424);
// expect(callback.status).toBe(303);
// expect(callback.headers.get("location")).toBe("/_/auth/profile");
// TODO: Assert bearer token is in 'Authorization' header.
await server.stop();
});
@@ -0,0 +1,29 @@
import { expect, test } from "vitest";
import { status } from "http-status";
const port: number = 4005;
const address: string = `http://127.0.0.1:${port}`;
test("JS runtime", async () => {
const expected = {
int: 5,
real: 4.2,
msg: "foo",
obj: {
nested: true,
},
};
const jsonUrl = `${address}/json`;
const json = await (await fetch(jsonUrl)).json();
expect(json).toMatchObject(expected);
const response = await fetch(`${address}/fetch?url=${encodeURI(jsonUrl)}`);
expect(await response.json()).toMatchObject(expected);
const errResp = await fetch(`${address}/error`);
expect(errResp.status).equals(status.IM_A_TEAPOT);
// Test that the periodic callback was called.
expect((await fetch(`${address}/await`)).status).equals(status.OK);
});
@@ -60,13 +60,34 @@ async function initTrailBase(): Promise<{ subprocess: Subprocess }> {
const { subprocess } = await initTrailBase();
const ctx = await createVitest("test", {
watch: false,
environment: "jsdom",
include: ["tests/integration/*"],
});
await ctx.start();
await ctx.close();
{
const ctx = await createVitest("test", {
watch: false,
environment: "jsdom",
include: ["tests/integration/*"],
exclude: [
"tests/integration/auth_integration.test.ts",
"tests/integration/v8_integration.test.ts",
],
});
await ctx.start();
await ctx.close();
}
{
const ctx = await createVitest("test", {
watch: false,
environment: "node",
include: [
"tests/integration/auth_integration.test.ts",
"tests/integration/v8_integration.test.ts",
],
});
await ctx.start();
await ctx.close();
}
if (subprocess.exitCode === null) {
// Still running