mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
test(cloud): init api tests
This commit is contained in:
parent
dd40863ca6
commit
af4276e08e
16 changed files with 522 additions and 982 deletions
|
@ -70,14 +70,14 @@
|
||||||
"@silverhand/eslint-config": "2.0.1",
|
"@silverhand/eslint-config": "2.0.1",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/inquirer": "^9.0.0",
|
"@types/inquirer": "^9.0.0",
|
||||||
"@types/jest": "^29.1.2",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"@types/semver": "^7.3.12",
|
"@types/semver": "^7.3.12",
|
||||||
"@types/sinon": "^10.0.13",
|
"@types/sinon": "^10.0.13",
|
||||||
"@types/tar": "^6.1.2",
|
"@types/tar": "^6.1.2",
|
||||||
"@types/yargs": "^17.0.13",
|
"@types/yargs": "^17.0.13",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"jest": "^29.3.1",
|
"jest": "^29.5.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"prettier": "^2.8.2",
|
"prettier": "^2.8.2",
|
||||||
"sinon": "^15.0.0",
|
"sinon": "^15.0.0",
|
||||||
|
|
5
packages/cloud/jest.config.js
Normal file
5
packages/cloud/jest.config.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import baseConfig from '@silverhand/jest-config';
|
||||||
|
|
||||||
|
/** @type {import('jest').Config} */
|
||||||
|
const config = { ...baseConfig, roots: ['./build'] };
|
||||||
|
export default config;
|
|
@ -13,10 +13,14 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"precommit": "lint-staged",
|
"precommit": "lint-staged",
|
||||||
"build": "rm -rf build/ && tsc -p tsconfig.build.json",
|
"build": "rm -rf build/ && tsc -p tsconfig.build.json",
|
||||||
|
"build:test": "rm -rf build/ && tsc -p tsconfig.test.json --sourcemap",
|
||||||
"lint": "eslint --ext .ts src",
|
"lint": "eslint --ext .ts src",
|
||||||
"lint:report": "pnpm lint --format json --output-file report.json",
|
"lint:report": "pnpm lint --format json --output-file report.json",
|
||||||
"dev": "rm -rf build/ && nodemon",
|
"dev": "rm -rf build/ && nodemon",
|
||||||
"start": "NODE_ENV=production node ."
|
"start": "NODE_ENV=production node .",
|
||||||
|
"test:only": "NODE_OPTIONS=\"--experimental-vm-modules --max_old_space_size=4096\" jest --logHeapUsage",
|
||||||
|
"test": "pnpm build:test && pnpm test:only",
|
||||||
|
"test:ci": "pnpm test:only --coverage --silent"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@logto/cli": "workspace:*",
|
"@logto/cli": "workspace:*",
|
||||||
|
@ -38,11 +42,14 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@silverhand/eslint-config": "2.0.1",
|
"@silverhand/eslint-config": "2.0.1",
|
||||||
|
"@silverhand/jest-config": "^2.0.1",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/http-proxy": "^1.17.9",
|
"@types/http-proxy": "^1.17.9",
|
||||||
|
"@types/jest": "^29.4.0",
|
||||||
"@types/mime-types": "^2.1.1",
|
"@types/mime-types": "^2.1.1",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"eslint": "^8.21.0",
|
"eslint": "^8.21.0",
|
||||||
|
"jest": "^29.5.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"nodemon": "^2.0.19",
|
"nodemon": "^2.0.19",
|
||||||
"prettier": "^2.8.1",
|
"prettier": "^2.8.1",
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import assert from 'node:assert';
|
import assert from 'node:assert';
|
||||||
import type { IncomingHttpHeaders } from 'node:http';
|
import type { IncomingHttpHeaders } from 'node:http';
|
||||||
import path from 'node:path/posix';
|
|
||||||
|
|
||||||
import { tryThat } from '@logto/shared';
|
import { tryThat } from '@logto/shared';
|
||||||
|
import { appendPath } from '@silverhand/essentials';
|
||||||
import type { NextFunction, RequestContext } from '@withtyped/server';
|
import type { NextFunction, RequestContext } from '@withtyped/server';
|
||||||
import { RequestError } from '@withtyped/server';
|
import { RequestError } from '@withtyped/server';
|
||||||
import fetchRetry from 'fetch-retry';
|
import fetchRetry from 'fetch-retry';
|
||||||
|
@ -50,9 +50,7 @@ export default function withAuth<InputContext extends RequestContext>({
|
||||||
const fetch = fetchRetry(global.fetch);
|
const fetch = fetchRetry(global.fetch);
|
||||||
const getJwkSet = (async () => {
|
const getJwkSet = (async () => {
|
||||||
const fetched = await fetch(
|
const fetched = await fetch(
|
||||||
new Request(
|
new Request(appendPath(endpoint, 'oidc/.well-known/openid-configuration')),
|
||||||
new URL(path.join(endpoint.pathname, 'oidc/.well-known/openid-configuration'), endpoint)
|
|
||||||
),
|
|
||||||
{ retries: 5, retryDelay: (attempt) => 2 ** attempt * 1000 }
|
{ retries: 5, retryDelay: (attempt) => 2 ** attempt * 1000 }
|
||||||
);
|
);
|
||||||
const { jwks_uri: jwksUri, issuer } = z
|
const { jwks_uri: jwksUri, issuer } = z
|
||||||
|
|
33
packages/cloud/src/routes-anonymous/index.test.ts
Normal file
33
packages/cloud/src/routes-anonymous/index.test.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import { isKeyInObject } from '@logto/shared';
|
||||||
|
|
||||||
|
import { buildRequestContext, createHttpContext } from '#src/test-utils/context.js';
|
||||||
|
|
||||||
|
import router from './index.js';
|
||||||
|
|
||||||
|
describe('GET /api/status', () => {
|
||||||
|
it('should set status to 204', async () => {
|
||||||
|
await router.routes()(
|
||||||
|
buildRequestContext('/api/status'),
|
||||||
|
async ({ status, json, stream }) => {
|
||||||
|
expect(status).toBe(204);
|
||||||
|
expect(json).toBe(undefined);
|
||||||
|
expect(stream).toBe(undefined);
|
||||||
|
},
|
||||||
|
createHttpContext()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GET /api/teapot', () => {
|
||||||
|
it('should refuse to brew coffee', async () => {
|
||||||
|
await router.routes()(
|
||||||
|
buildRequestContext('/api/teapot'),
|
||||||
|
async ({ status, json, stream }) => {
|
||||||
|
expect(status).toBe(418);
|
||||||
|
expect(isKeyInObject(json, 'message')).toBeTruthy();
|
||||||
|
expect(stream).toBe(undefined);
|
||||||
|
},
|
||||||
|
createHttpContext()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
29
packages/cloud/src/test-utils/context.ts
Normal file
29
packages/cloud/src/test-utils/context.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
||||||
|
import { Socket } from 'node:net';
|
||||||
|
import { TLSSocket } from 'node:tls';
|
||||||
|
|
||||||
|
import type { HttpContext, RequestContext } from '@withtyped/server';
|
||||||
|
import { RequestMethod } from '@withtyped/server';
|
||||||
|
|
||||||
|
export const createHttpContext: (isHttps?: boolean) => HttpContext = (isHttps = false) => {
|
||||||
|
const request = new IncomingMessage(isHttps ? new TLSSocket(new Socket()) : new Socket());
|
||||||
|
|
||||||
|
return {
|
||||||
|
request,
|
||||||
|
response: new ServerResponse(request),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type BuildRequestContext = Partial<RequestContext['request']>;
|
||||||
|
|
||||||
|
export const buildRequestContext = (
|
||||||
|
pathname: string,
|
||||||
|
{
|
||||||
|
method = RequestMethod.GET,
|
||||||
|
headers = {},
|
||||||
|
url = new URL(pathname, 'http://localhost'),
|
||||||
|
body,
|
||||||
|
}: BuildRequestContext = {}
|
||||||
|
): RequestContext => ({
|
||||||
|
request: { method, headers, url, body },
|
||||||
|
});
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"extends": "@silverhand/ts-config/tsconfig.base",
|
"extends": "@silverhand/ts-config/tsconfig.base",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"types": ["node"],
|
"types": ["node", "jest"],
|
||||||
"declaration": false,
|
"declaration": false,
|
||||||
"outDir": "build",
|
"outDir": "build",
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
|
|
7
packages/cloud/tsconfig.test.json
Normal file
7
packages/cloud/tsconfig.test.json
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"extends": "./tsconfig",
|
||||||
|
"compilerOptions": {
|
||||||
|
"isolatedModules": false,
|
||||||
|
"allowJs": true,
|
||||||
|
}
|
||||||
|
}
|
|
@ -80,7 +80,7 @@
|
||||||
"@types/debug": "^4.1.7",
|
"@types/debug": "^4.1.7",
|
||||||
"@types/etag": "^1.8.1",
|
"@types/etag": "^1.8.1",
|
||||||
"@types/http-errors": "^1.8.2",
|
"@types/http-errors": "^1.8.2",
|
||||||
"@types/jest": "^29.1.2",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/js-yaml": "^4.0.5",
|
"@types/js-yaml": "^4.0.5",
|
||||||
"@types/koa": "^2.13.3",
|
"@types/koa": "^2.13.3",
|
||||||
"@types/koa-compose": "^3.2.5",
|
"@types/koa-compose": "^3.2.5",
|
||||||
|
@ -96,7 +96,7 @@
|
||||||
"copyfiles": "^2.4.1",
|
"copyfiles": "^2.4.1",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"http-errors": "^1.6.3",
|
"http-errors": "^1.6.3",
|
||||||
"jest": "^29.1.2",
|
"jest": "^29.5.0",
|
||||||
"jest-matcher-specific-error": "^1.0.0",
|
"jest-matcher-specific-error": "^1.0.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"node-mocks-http": "^1.12.1",
|
"node-mocks-http": "^1.12.1",
|
||||||
|
|
|
@ -31,13 +31,13 @@
|
||||||
"@silverhand/essentials": "2.4.0",
|
"@silverhand/essentials": "2.4.0",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/expect-puppeteer": "^5.0.3",
|
"@types/expect-puppeteer": "^5.0.3",
|
||||||
"@types/jest": "^29.1.2",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/jest-environment-puppeteer": "^5.0.3",
|
"@types/jest-environment-puppeteer": "^5.0.3",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"got": "^12.5.3",
|
"got": "^12.5.3",
|
||||||
"jest": "^29.1.2",
|
"jest": "^29.5.0",
|
||||||
"jest-puppeteer": "^7.0.0",
|
"jest-puppeteer": "^7.0.0",
|
||||||
"node-fetch": "^3.3.0",
|
"node-fetch": "^3.3.0",
|
||||||
"openapi-schema-validator": "^12.0.0",
|
"openapi-schema-validator": "^12.0.0",
|
||||||
|
|
|
@ -44,13 +44,13 @@
|
||||||
"@silverhand/essentials": "2.4.0",
|
"@silverhand/essentials": "2.4.0",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/inquirer": "^9.0.0",
|
"@types/inquirer": "^9.0.0",
|
||||||
"@types/jest": "^29.1.2",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"@types/pluralize": "^0.0.29",
|
"@types/pluralize": "^0.0.29",
|
||||||
"camelcase": "^7.0.0",
|
"camelcase": "^7.0.0",
|
||||||
"chalk": "^5.0.0",
|
"chalk": "^5.0.0",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"jest": "^29.1.2",
|
"jest": "^29.5.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"pluralize": "^8.0.0",
|
"pluralize": "^8.0.0",
|
||||||
"prettier": "^2.8.2",
|
"prettier": "^2.8.2",
|
||||||
|
|
|
@ -35,10 +35,10 @@
|
||||||
"@logto/connector-kit": "workspace:*",
|
"@logto/connector-kit": "workspace:*",
|
||||||
"@silverhand/eslint-config": "2.0.1",
|
"@silverhand/eslint-config": "2.0.1",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/jest": "^29.1.2",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"jest": "^29.1.2",
|
"jest": "^29.5.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"prettier": "^2.8.2",
|
"prettier": "^2.8.2",
|
||||||
"typescript": "^4.9.4"
|
"typescript": "^4.9.4"
|
||||||
|
|
|
@ -53,11 +53,11 @@
|
||||||
"@silverhand/essentials": "2.4.0",
|
"@silverhand/essentials": "2.4.0",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/color": "^3.0.3",
|
"@types/color": "^3.0.3",
|
||||||
"@types/jest": "^29.0.3",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"@types/react": "^18.0.20",
|
"@types/react": "^18.0.20",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"jest": "^29.0.3",
|
"jest": "^29.5.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"postcss": "^8.4.6",
|
"postcss": "^8.4.6",
|
||||||
"prettier": "^2.8.2",
|
"prettier": "^2.8.2",
|
||||||
|
|
|
@ -41,10 +41,10 @@
|
||||||
"@jest/types": "^29.0.3",
|
"@jest/types": "^29.0.3",
|
||||||
"@silverhand/eslint-config": "2.0.1",
|
"@silverhand/eslint-config": "2.0.1",
|
||||||
"@silverhand/ts-config": "2.0.3",
|
"@silverhand/ts-config": "2.0.3",
|
||||||
"@types/jest": "^29.0.3",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"jest": "^29.0.3",
|
"jest": "^29.5.0",
|
||||||
"lint-staged": "^13.0.0",
|
"lint-staged": "^13.0.0",
|
||||||
"prettier": "^2.8.2",
|
"prettier": "^2.8.2",
|
||||||
"tslib": "^2.4.1",
|
"tslib": "^2.4.1",
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
"@silverhand/ts-config-react": "2.0.3",
|
"@silverhand/ts-config-react": "2.0.3",
|
||||||
"@testing-library/react": "^14.0.0",
|
"@testing-library/react": "^14.0.0",
|
||||||
"@types/color": "^3.0.3",
|
"@types/color": "^3.0.3",
|
||||||
"@types/jest": "^29.1.2",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/react": "^18.0.0",
|
"@types/react": "^18.0.0",
|
||||||
"@types/react-dom": "^18.0.0",
|
"@types/react-dom": "^18.0.0",
|
||||||
"@types/react-modal": "^3.13.1",
|
"@types/react-modal": "^3.13.1",
|
||||||
|
@ -49,7 +49,7 @@
|
||||||
"eslint": "^8.34.0",
|
"eslint": "^8.34.0",
|
||||||
"i18next": "^21.8.16",
|
"i18next": "^21.8.16",
|
||||||
"i18next-browser-languagedetector": "^6.1.4",
|
"i18next-browser-languagedetector": "^6.1.4",
|
||||||
"jest": "^29.1.2",
|
"jest": "^29.5.0",
|
||||||
"jest-environment-jsdom": "^29.0.0",
|
"jest-environment-jsdom": "^29.0.0",
|
||||||
"jest-transformer-svg": "^2.0.0",
|
"jest-transformer-svg": "^2.0.0",
|
||||||
"js-base64": "^3.7.2",
|
"js-base64": "^3.7.2",
|
||||||
|
|
1381
pnpm-lock.yaml
1381
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue