mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-02-04 13:21:45 +00:00
refactor: add formatter to Playwright tests
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
import playwrightConfig from "../playwright.config";
|
||||
import playwrightConfig from '../playwright.config';
|
||||
|
||||
export async function cleanupBackend() {
|
||||
const url = new URL("/api/test/reset", playwrightConfig.use!.baseURL);
|
||||
const url = new URL('/api/test/reset', playwrightConfig.use!.baseURL);
|
||||
|
||||
if (process.env.SKIP_LDAP_TESTS === "true") {
|
||||
url.searchParams.append("skip-ldap", "true");
|
||||
}
|
||||
if (process.env.SKIP_LDAP_TESTS === 'true') {
|
||||
url.searchParams.append('skip-ldap', 'true');
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
});
|
||||
const response = await fetch(url, {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Failed to reset backend: ${response.status} ${response.statusText}`
|
||||
);
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to reset backend: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,56 @@
|
||||
import * as jose from "jose";
|
||||
import playwrightConfig from "../playwright.config";
|
||||
import * as jose from 'jose';
|
||||
import playwrightConfig from '../playwright.config';
|
||||
|
||||
const PRIVATE_KEY_STRING = `{"alg":"RS256","d":"mvMDWSdPPvcum0c0iEHE2gbqtV2NKMmLwrl9E6K7g8lTV95SePLnW_bwyMPV7EGp7PQk3l17I5XRhFjze7GqTnFIOgKzMianPs7jv2ELtBMGK0xOPATgu1iGb70xZ6vcvuEfRyY3dJ0zr4jpUdVuXwKmx9rK4IdZn2dFCKfvSuspqIpz11RhF1ALrqDLkxGVv7ZwNh0_VhJZU9hcjG5l6xc7rQEKpPRkZp0IdjkGS8Z0FskoVaiRIWAbZuiVFB9WCW8k1czC4HQTPLpII01bUQx2ludbm0UlXRgVU9ptUUbU7GAImQq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qdO9qZqHSEPZ3NcFL_SZMFgggxhM1uzRiPzsVN0lnD_6prZU","qi":"2Grt6uXHm61ji3xSdkBWNtUnj19vS1-7rFJp5SoYztVQVThf_W52BAiXKBdYZDRVoItC_VS2NvAOjeJjhYO_xQ_q3hK7MdtuXfEPpLnyXKkmWo3lrJ26wbeF6l05LexCkI7ShsOuSt-dsyaTJTszuKDIA6YOfWvfo3aVZmlWRaI","use":"sig"}`;
|
||||
|
||||
type User = {
|
||||
id: string;
|
||||
email: string;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
id: string;
|
||||
email: string;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
};
|
||||
|
||||
const privateKey = JSON.parse(PRIVATE_KEY_STRING);
|
||||
const privateKeyImported = await jose.importJWK(privateKey, "RS256");
|
||||
const privateKeyImported = await jose.importJWK(privateKey, 'RS256');
|
||||
|
||||
export async function generateIdToken(
|
||||
user: User,
|
||||
clientId: string,
|
||||
expired = false
|
||||
) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const expiration = expired ? now + 1 : now + 1000000000; // Either expired or valid for a long time
|
||||
export async function generateIdToken(user: User, clientId: string, expired = false) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const expiration = expired ? now + 1 : now + 1000000000; // Either expired or valid for a long time
|
||||
|
||||
const payload = {
|
||||
aud: clientId,
|
||||
email: user.email,
|
||||
email_verified: true,
|
||||
exp: expiration,
|
||||
family_name: user.lastname,
|
||||
given_name: user.firstname,
|
||||
iat: now,
|
||||
iss: playwrightConfig.use!.baseURL,
|
||||
name: `${user.firstname} ${user.lastname}`,
|
||||
nonce: "oW1A1O78GQ15D73OsHEx7WQKj7ZqvHLZu_37mdXIqAQ",
|
||||
sub: user.id,
|
||||
type: "id-token",
|
||||
};
|
||||
const payload = {
|
||||
aud: clientId,
|
||||
email: user.email,
|
||||
email_verified: true,
|
||||
exp: expiration,
|
||||
family_name: user.lastname,
|
||||
given_name: user.firstname,
|
||||
iat: now,
|
||||
iss: playwrightConfig.use!.baseURL,
|
||||
name: `${user.firstname} ${user.lastname}`,
|
||||
nonce: 'oW1A1O78GQ15D73OsHEx7WQKj7ZqvHLZu_37mdXIqAQ',
|
||||
sub: user.id,
|
||||
type: 'id-token'
|
||||
};
|
||||
|
||||
return await new jose.SignJWT(payload)
|
||||
.setProtectedHeader({ alg: "RS256", kid: privateKey.kid, typ: "JWT" })
|
||||
.sign(privateKeyImported);
|
||||
return await new jose.SignJWT(payload)
|
||||
.setProtectedHeader({ alg: 'RS256', kid: privateKey.kid, typ: 'JWT' })
|
||||
.sign(privateKeyImported);
|
||||
}
|
||||
|
||||
export async function generateOauthAccessToken(
|
||||
user: User,
|
||||
clientId: string,
|
||||
expired = false
|
||||
) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const expiration = expired ? now - 1000 : now + 1000000000; // Either expired or valid for a long time
|
||||
export async function generateOauthAccessToken(user: User, clientId: string, expired = false) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const expiration = expired ? now - 1000 : now + 1000000000; // Either expired or valid for a long time
|
||||
|
||||
const payload = {
|
||||
aud: [clientId],
|
||||
exp: expiration,
|
||||
iat: now,
|
||||
iss: playwrightConfig.use!.baseURL,
|
||||
sub: user.id,
|
||||
type: "oauth-access-token",
|
||||
};
|
||||
const payload = {
|
||||
aud: [clientId],
|
||||
exp: expiration,
|
||||
iat: now,
|
||||
iss: playwrightConfig.use!.baseURL,
|
||||
sub: user.id,
|
||||
type: 'oauth-access-token'
|
||||
};
|
||||
|
||||
return await new jose.SignJWT(payload)
|
||||
.setProtectedHeader({ alg: "RS256", kid: privateKey.kid, typ: "JWT" })
|
||||
.sign(privateKeyImported);
|
||||
return await new jose.SignJWT(payload)
|
||||
.setProtectedHeader({ alg: 'RS256', kid: privateKey.kid, typ: 'JWT' })
|
||||
.sign(privateKeyImported);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import type { Page } from '@playwright/test';
|
||||
|
||||
export async function getUserCode(page: Page, clientId: string, clientSecret: string): Promise<string> {
|
||||
export async function getUserCode(
|
||||
page: Page,
|
||||
clientId: string,
|
||||
clientSecret: string
|
||||
): Promise<string> {
|
||||
return page.request
|
||||
.post('/api/oidc/device/authorize', {
|
||||
headers: {
|
||||
@@ -16,25 +20,31 @@ export async function getUserCode(page: Page, clientId: string, clientSecret: st
|
||||
.then((r) => r.user_code);
|
||||
}
|
||||
|
||||
export async function exchangeCode(page: Page, params: Record<string,string>): Promise<{access_token?: string, token_type?: string, expires_in?: number, error?: string}> {
|
||||
export async function exchangeCode(
|
||||
page: Page,
|
||||
params: Record<string, string>
|
||||
): Promise<{ access_token?: string; token_type?: string; expires_in?: number; error?: string }> {
|
||||
return page.request
|
||||
.post('/api/oidc/token', {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
form: params,
|
||||
form: params
|
||||
})
|
||||
.then((r) => r.json());
|
||||
}
|
||||
|
||||
export async function getClientAssertion(page: Page, data: {issuer: string, audience: string, subject: string}): Promise<string> {
|
||||
export async function getClientAssertion(
|
||||
page: Page,
|
||||
data: { issuer: string; audience: string; subject: string }
|
||||
): Promise<string> {
|
||||
return page.request
|
||||
.post('/api/externalidp/sign', {
|
||||
data: {
|
||||
iss: data.issuer,
|
||||
aud: data.audience,
|
||||
sub: data.subject,
|
||||
},
|
||||
sub: data.subject
|
||||
}
|
||||
})
|
||||
.then((r) => r.text());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user