Merge pull request #25 from zitadel/setup-unit-integration-tests

test: setup unit tests
This commit is contained in:
Max Peintner
2023-06-08 09:12:51 +02:00
committed by GitHub
33 changed files with 3922 additions and 1244 deletions

View File

@@ -0,0 +1,8 @@
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'node'
}
export default jestConfig

View File

@@ -10,20 +10,27 @@
"dist/**"
],
"scripts": {
"generate": "buf generate https://github.com/zitadel/zitadel.git --path ./proto/zitadel",
"prebuild": "pnpm run generate",
"build": "tsup src/index.ts --format esm,cjs --dts",
"test": "jest",
"test:watch": "jest --watch",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"generate": "buf generate https://github.com/zitadel/zitadel.git --path ./proto/zitadel"
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"devDependencies": {
"@bufbuild/buf": "^1.14.0",
"@types/jest": "^29.5.1",
"@zitadel/tsconfig": "workspace:*",
"eslint": "^7.32.0",
"eslint-config-zitadel": "workspace:*",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"ts-proto": "^1.139.0",
"tsup": "^5.10.1",
"typescript": "^4.5.3"
"typescript": "^4.9.3"
},
"publishConfig": {
"access": "public"
@@ -33,4 +40,4 @@
"nice-grpc-web": "^3.2.3",
"protobufjs": "^7.2.3"
}
}
}

View File

@@ -0,0 +1,57 @@
import { CallOptions, ClientMiddlewareCall, Metadata, MethodDescriptor } from "nice-grpc-web";
import { authMiddleware } from "./middleware";
describe('authMiddleware', () => {
const scenarios = [
{
name: 'should add authorization if metadata is undefined',
initialMetadata: undefined,
expectedMetadata: new Metadata().set("authorization", "Bearer mock-token"),
token: "mock-token"
},
{
name: 'should add authorization if metadata exists but no authorization',
initialMetadata: new Metadata().set("other-key", "other-value"),
expectedMetadata: new Metadata().set("other-key", "other-value").set("authorization", "Bearer mock-token"),
token: "mock-token"
},
{
name: 'should not modify authorization if it already exists',
initialMetadata: new Metadata().set("authorization", "Bearer initial-token"),
expectedMetadata: new Metadata().set("authorization", "Bearer initial-token"),
token: "mock-token"
},
];
scenarios.forEach(({ name, initialMetadata, expectedMetadata, token }) => {
it(name, async () => {
const mockNext = jest.fn().mockImplementation(async function*() { });
const mockRequest = {};
const mockMethodDescriptor: MethodDescriptor = {
options: {idempotencyLevel: undefined},
path: '',
requestStream: false,
responseStream: false,
};
const mockCall: ClientMiddlewareCall<unknown, unknown> = {
method: mockMethodDescriptor,
requestStream: false,
responseStream: false,
request: mockRequest,
next: mockNext,
};
const options: CallOptions = {
metadata: initialMetadata
};
await authMiddleware(token)(mockCall, options).next();
expect(mockNext).toHaveBeenCalledTimes(1);
const actualMetadata = mockNext.mock.calls[0][1].metadata;
expect(actualMetadata?.get('authorization')).toEqual(expectedMetadata.get('authorization'));
});
});
});

View File

@@ -0,0 +1,8 @@
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'node'
}
export default jestConfig

View File

@@ -11,16 +11,22 @@
],
"scripts": {
"build": "tsup src/index.tsx --format esm,cjs --dts --external react",
"test": "jest",
"test:watch": "jest --watch",
"dev": "tsup src/index.tsx --format esm,cjs --watch --dts --external react",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"devDependencies": {
"@types/jest": "^29.5.1",
"@zitadel/tsconfig": "workspace:*",
"eslint": "^7.32.0",
"eslint-config-zitadel": "workspace:*",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tsup": "^5.10.1",
"typescript": "^4.5.3"
"typescript": "^4.9.3"
},
"peerDependencies": {
"next": "^13"
@@ -28,4 +34,4 @@
"publishConfig": {
"access": "public"
}
}
}

View File

@@ -0,0 +1,5 @@
describe('slug', () => {
it('this is not a real test', () => { })
})
export { }

View File

@@ -0,0 +1,9 @@
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
setupFilesAfterEnv: [ '@testing-library/jest-dom/extend-expect' ]
}
export default jestConfig

View File

@@ -13,23 +13,33 @@
},
"scripts": {
"build": "tsup",
"test": "jest",
"test:watch": "jest --watch",
"dev": "tsup --watch",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"copy-files": "cp -R ./src/public/ ./dist/"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.1",
"@types/react": "^17.0.13",
"@types/react-dom": "^17.0.8",
"@types/testing-library__jest-dom": "^5.14.6",
"@zitadel/tsconfig": "workspace:*",
"autoprefixer": "10.4.13",
"eslint": "^7.32.0",
"eslint-config-zitadel": "workspace:*",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"postcss": "8.4.21",
"sass": "^1.62.0",
"tailwindcss": "3.2.4",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tsup": "^5.10.1",
"typescript": "^4.5.3",
"typescript": "^4.9.3",
"zitadel-tailwind-config": "workspace:*"
},
"publishConfig": {
@@ -38,4 +48,4 @@
"peerDependencies": {
"react": "18.2.0"
}
}
}

View File

@@ -0,0 +1,15 @@
import { render, screen } from '@testing-library/react';
import { SignInWithGoogle } from './SignInWithGoogle';
describe('<SignInWithGoogle />', () => {
it('renders without crashing', () => {
const { container } = render(<SignInWithGoogle />);
expect(container.firstChild).toBeDefined();
});
it('displays the correct text', () => {
render(<SignInWithGoogle />);
const signInText = screen.getByText(/Sign in with Google/i);
expect(signInText).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,15 @@
import { render, screen } from '@testing-library/react';
import { SignInWithGitlab } from './SignInWithGitlab';
describe('<SignInWithGitlab />', () => {
it('renders without crashing', () => {
const { container } = render(<SignInWithGitlab />);
expect(container.firstChild).toBeDefined();
});
it('displays the correct text', () => {
render(<SignInWithGitlab />);
const signInText = screen.getByText(/Sign in with Gitlab/i);
expect(signInText).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,8 @@
import type { JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'node'
}
export default jestConfig

View File

@@ -11,21 +11,27 @@
"dist/**"
],
"scripts": {
"generate": "buf generate https://github.com/zitadel/zitadel.git --path ./proto/zitadel",
"prebuild": "pnpm run generate",
"build": "tsup --dts",
"test": "jest",
"test:watch": "jest --watch",
"dev": "tsup --dts --watch",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"prebuild": "pnpm run generate",
"generate": "buf generate https://github.com/zitadel/zitadel.git --path ./proto/zitadel"
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"devDependencies": {
"@bufbuild/buf": "^1.14.0",
"@types/jest": "^29.5.1",
"@zitadel/tsconfig": "workspace:*",
"eslint": "^7.32.0",
"eslint-config-zitadel": "workspace:*",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"ts-proto": "^1.139.0",
"tsup": "^5.10.1",
"typescript": "^4.5.3"
"typescript": "^4.9.3"
},
"publishConfig": {
"access": "public"
@@ -37,4 +43,4 @@
"nice-grpc-common": "^2.0.2",
"protobufjs": "^7.2.3"
}
}
}

View File

@@ -32,6 +32,7 @@ export {
export { type LegalAndSupportSettings } from "./proto/server/zitadel/settings/v2alpha/legal_settings";
export { type PasswordComplexitySettings } from "./proto/server/zitadel/settings/v2alpha/password_settings";
export { type ResourceOwnerType } from "./proto/server/zitadel/settings/v2alpha/settings";
import {
getServers,

View File

@@ -0,0 +1,57 @@
import { CallOptions, ClientMiddlewareCall, Metadata, MethodDescriptor } from "nice-grpc";
import { authMiddleware } from "./middleware";
describe('authMiddleware', () => {
const scenarios = [
{
name: 'should add authorization if metadata is undefined',
initialMetadata: undefined,
expectedMetadata: new Metadata().set("authorization", "Bearer mock-token"),
token: "mock-token"
},
{
name: 'should add authorization if metadata exists but no authorization',
initialMetadata: new Metadata().set("other-key", "other-value"),
expectedMetadata: new Metadata().set("other-key", "other-value").set("authorization", "Bearer mock-token"),
token: "mock-token"
},
{
name: 'should not modify authorization if it already exists',
initialMetadata: new Metadata().set("authorization", "Bearer initial-token"),
expectedMetadata: new Metadata().set("authorization", "Bearer initial-token"),
token: "mock-token"
},
];
scenarios.forEach(({ name, initialMetadata, expectedMetadata, token }) => {
it(name, async () => {
const mockNext = jest.fn().mockImplementation(async function*() { });
const mockRequest = {};
const mockMethodDescriptor: MethodDescriptor = {
options: {idempotencyLevel: undefined},
path: '',
requestStream: false,
responseStream: false,
};
const mockCall: ClientMiddlewareCall<unknown, unknown> = {
method: mockMethodDescriptor,
requestStream: false,
responseStream: false,
request: mockRequest,
next: mockNext,
};
const options: CallOptions = {
metadata: initialMetadata
};
await authMiddleware(token)(mockCall, options).next();
expect(mockNext).toHaveBeenCalledTimes(1);
const actualMetadata = mockNext.mock.calls[0][1].metadata;
expect(actualMetadata?.get('authorization')).toEqual(expectedMetadata.get('authorization'));
});
});
});

View File

@@ -1,7 +1,7 @@
import { CallOptions, ClientMiddlewareCall, Metadata } from "nice-grpc";
import { CallOptions, ClientMiddleware, ClientMiddlewareCall, Metadata } from "nice-grpc";
export const authMiddleware = (token: string) =>
async function* <Request, Response>(
export function authMiddleware (token: string): ClientMiddleware {
return async function* <Request, Response>(
call: ClientMiddlewareCall<Request, Response>,
options: CallOptions
) {
@@ -12,6 +12,7 @@ export const authMiddleware = (token: string) =>
return yield* call.next(call.request, options);
};
}
export const orgMetadata = (orgId: string) =>
new Metadata({ "x-zitadel-orgid": orgId });