0
Fork 0
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:
Gao Sun 2023-03-07 14:54:57 +08:00
parent dd40863ca6
commit af4276e08e
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
16 changed files with 522 additions and 982 deletions

View file

@ -70,14 +70,14 @@
"@silverhand/eslint-config": "2.0.1",
"@silverhand/ts-config": "2.0.3",
"@types/inquirer": "^9.0.0",
"@types/jest": "^29.1.2",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"@types/semver": "^7.3.12",
"@types/sinon": "^10.0.13",
"@types/tar": "^6.1.2",
"@types/yargs": "^17.0.13",
"eslint": "^8.34.0",
"jest": "^29.3.1",
"jest": "^29.5.0",
"lint-staged": "^13.0.0",
"prettier": "^2.8.2",
"sinon": "^15.0.0",

View file

@ -0,0 +1,5 @@
import baseConfig from '@silverhand/jest-config';
/** @type {import('jest').Config} */
const config = { ...baseConfig, roots: ['./build'] };
export default config;

View file

@ -13,10 +13,14 @@
"scripts": {
"precommit": "lint-staged",
"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:report": "pnpm lint --format json --output-file report.json",
"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": {
"@logto/cli": "workspace:*",
@ -38,11 +42,14 @@
},
"devDependencies": {
"@silverhand/eslint-config": "2.0.1",
"@silverhand/jest-config": "^2.0.1",
"@silverhand/ts-config": "2.0.3",
"@types/http-proxy": "^1.17.9",
"@types/jest": "^29.4.0",
"@types/mime-types": "^2.1.1",
"@types/node": "^18.11.18",
"eslint": "^8.21.0",
"jest": "^29.5.0",
"lint-staged": "^13.0.0",
"nodemon": "^2.0.19",
"prettier": "^2.8.1",

View file

@ -1,8 +1,8 @@
import assert from 'node:assert';
import type { IncomingHttpHeaders } from 'node:http';
import path from 'node:path/posix';
import { tryThat } from '@logto/shared';
import { appendPath } from '@silverhand/essentials';
import type { NextFunction, RequestContext } from '@withtyped/server';
import { RequestError } from '@withtyped/server';
import fetchRetry from 'fetch-retry';
@ -50,9 +50,7 @@ export default function withAuth<InputContext extends RequestContext>({
const fetch = fetchRetry(global.fetch);
const getJwkSet = (async () => {
const fetched = await fetch(
new Request(
new URL(path.join(endpoint.pathname, 'oidc/.well-known/openid-configuration'), endpoint)
),
new Request(appendPath(endpoint, 'oidc/.well-known/openid-configuration')),
{ retries: 5, retryDelay: (attempt) => 2 ** attempt * 1000 }
);
const { jwks_uri: jwksUri, issuer } = z

View 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()
);
});
});

View 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 },
});

View file

@ -1,7 +1,7 @@
{
"extends": "@silverhand/ts-config/tsconfig.base",
"compilerOptions": {
"types": ["node"],
"types": ["node", "jest"],
"declaration": false,
"outDir": "build",
"baseUrl": ".",

View file

@ -0,0 +1,7 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"isolatedModules": false,
"allowJs": true,
}
}

View file

@ -80,7 +80,7 @@
"@types/debug": "^4.1.7",
"@types/etag": "^1.8.1",
"@types/http-errors": "^1.8.2",
"@types/jest": "^29.1.2",
"@types/jest": "^29.4.0",
"@types/js-yaml": "^4.0.5",
"@types/koa": "^2.13.3",
"@types/koa-compose": "^3.2.5",
@ -96,7 +96,7 @@
"copyfiles": "^2.4.1",
"eslint": "^8.34.0",
"http-errors": "^1.6.3",
"jest": "^29.1.2",
"jest": "^29.5.0",
"jest-matcher-specific-error": "^1.0.0",
"lint-staged": "^13.0.0",
"node-mocks-http": "^1.12.1",

View file

@ -31,13 +31,13 @@
"@silverhand/essentials": "2.4.0",
"@silverhand/ts-config": "2.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/node": "^18.11.18",
"dotenv": "^16.0.0",
"eslint": "^8.34.0",
"got": "^12.5.3",
"jest": "^29.1.2",
"jest": "^29.5.0",
"jest-puppeteer": "^7.0.0",
"node-fetch": "^3.3.0",
"openapi-schema-validator": "^12.0.0",

View file

@ -44,13 +44,13 @@
"@silverhand/essentials": "2.4.0",
"@silverhand/ts-config": "2.0.3",
"@types/inquirer": "^9.0.0",
"@types/jest": "^29.1.2",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"@types/pluralize": "^0.0.29",
"camelcase": "^7.0.0",
"chalk": "^5.0.0",
"eslint": "^8.34.0",
"jest": "^29.1.2",
"jest": "^29.5.0",
"lint-staged": "^13.0.0",
"pluralize": "^8.0.0",
"prettier": "^2.8.2",

View file

@ -35,10 +35,10 @@
"@logto/connector-kit": "workspace:*",
"@silverhand/eslint-config": "2.0.1",
"@silverhand/ts-config": "2.0.3",
"@types/jest": "^29.1.2",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"eslint": "^8.34.0",
"jest": "^29.1.2",
"jest": "^29.5.0",
"lint-staged": "^13.0.0",
"prettier": "^2.8.2",
"typescript": "^4.9.4"

View file

@ -53,11 +53,11 @@
"@silverhand/essentials": "2.4.0",
"@silverhand/ts-config": "2.0.3",
"@types/color": "^3.0.3",
"@types/jest": "^29.0.3",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"@types/react": "^18.0.20",
"eslint": "^8.34.0",
"jest": "^29.0.3",
"jest": "^29.5.0",
"lint-staged": "^13.0.0",
"postcss": "^8.4.6",
"prettier": "^2.8.2",

View file

@ -41,10 +41,10 @@
"@jest/types": "^29.0.3",
"@silverhand/eslint-config": "2.0.1",
"@silverhand/ts-config": "2.0.3",
"@types/jest": "^29.0.3",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"eslint": "^8.34.0",
"jest": "^29.0.3",
"jest": "^29.5.0",
"lint-staged": "^13.0.0",
"prettier": "^2.8.2",
"tslib": "^2.4.1",

View file

@ -37,7 +37,7 @@
"@silverhand/ts-config-react": "2.0.3",
"@testing-library/react": "^14.0.0",
"@types/color": "^3.0.3",
"@types/jest": "^29.1.2",
"@types/jest": "^29.4.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react-modal": "^3.13.1",
@ -49,7 +49,7 @@
"eslint": "^8.34.0",
"i18next": "^21.8.16",
"i18next-browser-languagedetector": "^6.1.4",
"jest": "^29.1.2",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.0.0",
"jest-transformer-svg": "^2.0.0",
"js-base64": "^3.7.2",

File diff suppressed because it is too large Load diff