0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/integration-tests/src/utils.ts
2023-05-29 11:27:23 +08:00

66 lines
2.2 KiB
TypeScript

import crypto from 'node:crypto';
import path from 'node:path';
import { assert } from '@silverhand/essentials';
export const generateName = () => crypto.randomUUID();
export const generateUserId = () => crypto.randomUUID();
export const generateUsername = () => `usr_${crypto.randomUUID().replaceAll('-', '_')}`;
export const generatePassword = () => `pwd_${crypto.randomUUID()}`;
export const generateResourceName = () => `res_${crypto.randomUUID()}`;
export const generateResourceIndicator = () => `https://${crypto.randomUUID()}.logto.io`;
export const generateEmail = () => `${crypto.randomUUID().toLowerCase()}@logto.io`;
export const generateScopeName = () => `sc:${crypto.randomUUID()}`;
export const generateRoleName = () => `role_${crypto.randomUUID()}`;
export const generateDomain = () => `${crypto.randomUUID().toLowerCase().slice(0, 5)}.example.com`;
export const generatePhone = (isE164?: boolean) => {
const plus = isE164 ? '+' : '';
const countryAndAreaCode = '1310'; // California, US
const validCentralOfficeCodes = [
'205',
'208',
'215',
'216',
'220',
'228',
'229',
'230',
'231',
'232',
];
const centralOfficeCode =
validCentralOfficeCodes[Math.floor(Math.random() * validCentralOfficeCodes.length)] ?? '205';
const phoneNumber = Math.floor(Math.random() * 10_000)
.toString()
.padStart(4, '0');
return plus + countryAndAreaCode + centralOfficeCode + phoneNumber;
};
export const formatPhoneNumberToInternational = (phoneNumber: string) =>
phoneNumber.slice(0, 2) +
' ' +
phoneNumber.slice(2, 5) +
' ' +
phoneNumber.slice(5, 8) +
' ' +
phoneNumber.slice(8);
export const waitFor = async (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
export const getAccessTokenPayload = (accessToken: string): Record<string, unknown> => {
const payloadPart = accessToken.split('.')[1];
assert(typeof payloadPart === 'string', new Error('Invalid access token'));
const payload = Buffer.from(payloadPart, 'base64').toString();
// eslint-disable-next-line no-restricted-syntax
return JSON.parse(payload) as Record<string, unknown>;
};
export const appendPathname = (pathname: string, baseUrl: URL) =>
new URL(path.join(baseUrl.pathname, pathname), baseUrl);