feat: add zitadel-proto pkg

This commit is contained in:
Yordis Prieto
2024-05-24 10:40:12 -04:00
parent 6c275bb0b6
commit 3e93077f39
187 changed files with 135343 additions and 4493 deletions

View File

@@ -26,7 +26,7 @@ export interface ZitadelApp {
export async function initializeApp(
config: ZitadelCoreProps,
name?: string
name?: string,
): Promise<ZitadelApp> {
const app = { config, name };
return app;
@@ -40,6 +40,6 @@ export function getApp(name?: string): ZitadelApp | undefined {
return name
? apps.find((a) => a.name === name)
: apps.length === 1
? apps[0]
: undefined;
? apps[0]
: undefined;
}

View File

@@ -1,59 +1,76 @@
import { describe, expect, test, vitest } from "vitest";
import { CallOptions, ClientMiddlewareCall, Metadata, MethodDescriptor } from "nice-grpc-web";
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"
},
];
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 }) => {
test(name, async () => {
scenarios.forEach(({ name, initialMetadata, expectedMetadata, token }) => {
test(name, async () => {
const mockNext = vitest.fn().mockImplementation(async function* () {});
const mockRequest = {};
const mockNext = vitest.fn().mockImplementation(async function*() { });
const mockRequest = {};
const mockMethodDescriptor: MethodDescriptor = {
options: { idempotencyLevel: undefined },
path: "",
requestStream: false,
responseStream: false,
};
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,
};
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();
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'));
});
expect(mockNext).toHaveBeenCalledTimes(1);
const actualMetadata = mockNext.mock.calls[0][1].metadata;
expect(actualMetadata?.get("authorization")).toEqual(
expectedMetadata.get("authorization"),
);
});
});
});

View File

@@ -3,7 +3,7 @@ import { CallOptions, ClientMiddlewareCall, Metadata } from "nice-grpc-web";
export const authMiddleware = (token: string) =>
async function* <Request, Response>(
call: ClientMiddlewareCall<Request, Response>,
options: CallOptions
options: CallOptions,
) {
if (!options.metadata?.has("authorization")) {
options.metadata ??= new Metadata();