Skip to content

Commit

Permalink
Merge branch 'dev' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Jun 18, 2024
2 parents 8ad56da + 1bb5d6a commit 97234a8
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 30 deletions.
4 changes: 3 additions & 1 deletion packages/api-file-manager/src/delivery/setupAssetDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ export const setupAssetDelivery = (params: AssetDeliveryParams) => {
return false;
}

const assetLocale = resolvedAsset.getLocale();

request.headers = {
...request.headers,
"x-tenant": resolvedAsset.getTenant(),
"x-i18n-locale": resolvedAsset.getLocale()
"x-i18n-locale": `default:${assetLocale};content:${assetLocale};`
};

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class ImportPagesProcessPages {
pageIdList.push(page.pid);
done.push(item.key);
} catch (ex) {
console.error(ex);
await store.addErrorLog({
error: ex,
message: `Could not import page: ${item.key}.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function extractAndUploadZipFileContents(zipFileUrl: string): Promi
const response = await fetch(zipFileUrl);
const readStream = response.body;
if (!response.ok || !readStream) {
throw new WebinyError(`Unable to downloading file: "${zipFileUrl}"`, response.statusText);
throw new WebinyError(`Unable to download file: "${zipFileUrl}"`, response.statusText);
}

const uniquePath = uniqueId("IMPORTS/");
Expand Down
4 changes: 2 additions & 2 deletions packages/ioc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"author": "Webiny Ltd",
"license": "MIT",
"dependencies": {
"inversify": "^6.0.1"
"inversify": "^6.0.1",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@webiny/cli": "0.0.0",
"@webiny/project-utils": "0.0.0",
"reflect-metadata": "^0.1.13",
"ttypescript": "^1.5.13",
"typescript": "4.7.4",
"zod": "^3.22.4"
Expand Down
4 changes: 2 additions & 2 deletions packages/tasks/__tests__/crud/definitions.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useHandler } from "~tests/helpers/useHandler";
import { useRawHandler } from "~tests/helpers/useRawHandler";
import { createTaskDefinition } from "~/task";

describe("definitions crud", () => {
const handler = useHandler({
const handler = useRawHandler({
plugins: [
createTaskDefinition({
id: "testDefinitionNumber1",
Expand Down
4 changes: 2 additions & 2 deletions packages/tasks/__tests__/crud/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useHandler } from "~tests/helpers/useHandler";
import { useRawHandler } from "~tests/helpers/useRawHandler";
import { createTaskDefinition } from "~/task";
import { ITask, TaskDataStatus } from "~/types";
import { NotFoundError } from "@webiny/handler-graphql";
import WebinyError from "@webiny/error";
import { createMockIdentity } from "~tests/mocks/identity";

describe("store crud", () => {
const handler = useHandler({
const handler = useRawHandler({
plugins: [
createTaskDefinition({
id: "testDefinition",
Expand Down
4 changes: 2 additions & 2 deletions packages/tasks/__tests__/crud/trigger.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useHandler } from "~tests/helpers/useHandler";
import { useRawHandler } from "~tests/helpers/useRawHandler";
import { createMockTaskDefinitions } from "~tests/mocks/definition";
import { createMockIdentity } from "~tests/mocks/identity";
import { TaskDataStatus } from "~/types";
Expand All @@ -23,7 +23,7 @@ jest.mock("@webiny/aws-sdk/client-eventbridge", () => {
});

describe("trigger crud", () => {
const handler = useHandler({
const handler = useRawHandler({
plugins: [...createMockTaskDefinitions()]
});

Expand Down
4 changes: 2 additions & 2 deletions packages/tasks/__tests__/graphql/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useGraphQLHandler } from "~tests/helpers/useGraphQLHandler";
import { createMockTaskDefinitions } from "~tests/mocks/definition";
import { useHandler } from "~tests/helpers/useHandler";
import { useRawHandler } from "~tests/helpers/useRawHandler";
import { ITaskLogItemType } from "~/types";
import { createMockIdentity } from "~tests/mocks/identity";

describe("graphql - logs", () => {
const contextHandler = useHandler({
const contextHandler = useRawHandler({
plugins: [...createMockTaskDefinitions()]
});
const handler = useGraphQLHandler({
Expand Down
4 changes: 2 additions & 2 deletions packages/tasks/__tests__/graphql/tasks.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useGraphQLHandler } from "~tests/helpers/useGraphQLHandler";
import { createMockTaskDefinitions } from "~tests/mocks/definition";
import { useHandler } from "~tests/helpers/useHandler";
import { useRawHandler } from "~tests/helpers/useRawHandler";
import { TaskDataStatus } from "~/types";
import { createMockIdentity } from "~tests/mocks/identity";

describe("graphql - tasks", () => {
const contextHandler = useHandler({
const contextHandler = useRawHandler({
plugins: [...createMockTaskDefinitions()]
});
const handler = useGraphQLHandler({
Expand Down
13 changes: 11 additions & 2 deletions packages/tasks/__tests__/helpers/tenancySecurity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ export const createTenancyAndSecurity = ({
const securityStorage = getStorageOps<SecurityStorageOperations>("security");

return [
new ContextPlugin<Context>(async context => {
const original = context.wcp.canUseFeature;
context.wcp.canUseFeature = feature => {
if (feature === "multiTenancy") {
return true;
}
return original(feature);
};
}),
createTenancyContext({ storageOperations: tenancyStorage.storageOperations }),
setupGraphQL ? createTenancyGraphQL() : null,
createSecurityContext({ storageOperations: securityStorage.storageOperations }),
setupGraphQL ? createSecurityGraphQL() : null,
new ContextPlugin<Context>(context => {
context.tenancy.setCurrentTenant({
id: "root",
name: "Root",
id: context.request.headers["x-tenant"] || "root",
name: context.request.headers["x-tenant"] || "Root",
webinyVersion: context.WEBINY_VERSION
} as unknown as Tenant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface UseHandlerParams {
plugins?: PluginCollection;
}

export const useHandler = <C extends Context = Context>(params?: UseHandlerParams) => {
export const useRawHandler = <C extends Context = Context>(params?: UseHandlerParams) => {
const { plugins = [] } = params || {};
const cmsStorage = getStorageOps<HeadlessCmsStorageOperations>("cms");
const i18nStorage = getStorageOps<any[]>("i18n");
Expand Down Expand Up @@ -49,8 +49,18 @@ export const useHandler = <C extends Context = Context>(params?: UseHandlerParam
});

return {
handle: async () => {
return await handler({}, {} as LambdaContext);
handle: async (payload?: Record<string, any>) => {
const headers = {
["x-tenant"]: "root",
...(payload?.headers || {})
};
return await handler(
{
...payload,
headers
},
{} as LambdaContext
);
}
};
};
57 changes: 57 additions & 0 deletions packages/tasks/__tests__/helpers/useTaskHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { createHandler } from "~/handler";
import { createWcpContext } from "@webiny/api-wcp";
import { createTenancyAndSecurity } from "~tests/helpers/tenancySecurity";
import { createDummyLocales, createIdentity, createPermissions } from "~tests/helpers/helpers";
import i18nContext from "@webiny/api-i18n/graphql/context";
import { mockLocalesPlugins } from "@webiny/api-i18n/graphql/testing";
import { createHeadlessCmsContext, createHeadlessCmsGraphQL } from "@webiny/api-headless-cms";
import graphQLHandlerPlugins from "@webiny/handler-graphql";
import { createBackgroundTaskContext } from "~/context";
import { createRawEventHandler } from "@webiny/handler-aws";
import { getStorageOps } from "@webiny/project-utils/testing/environment";
import { HeadlessCmsStorageOperations } from "@webiny/api-headless-cms/types";
import { PluginCollection } from "@webiny/plugins/types";
import { LambdaContext } from "@webiny/handler-aws/types";
import { ITaskRawEvent } from "~/handler/types";

export interface UseTaskHandlerParams {
plugins?: PluginCollection;
}

export const useTaskHandler = (params?: UseTaskHandlerParams) => {
const { plugins = [] } = params || {};
const cmsStorage = getStorageOps<HeadlessCmsStorageOperations>("cms");
const i18nStorage = getStorageOps<any[]>("i18n");

const handler = createHandler({
plugins: [
createWcpContext(),
...cmsStorage.plugins,
...createTenancyAndSecurity({
setupGraphQL: false,
permissions: createPermissions(),
identity: createIdentity()
}),
i18nContext(),
i18nStorage.storageOperations,
createDummyLocales(),
mockLocalesPlugins(),
createHeadlessCmsContext({
storageOperations: cmsStorage.storageOperations
}),
createHeadlessCmsGraphQL(),
graphQLHandlerPlugins(),
createBackgroundTaskContext(),
createRawEventHandler(async ({ context }) => {
return context;
}),
...plugins
]
});

return {
handle: async (payload: ITaskRawEvent) => {
return await handler(payload, {} as LambdaContext);
}
};
};
17 changes: 9 additions & 8 deletions packages/tasks/__tests__/live/context.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { PluginCollection } from "@webiny/plugins/types";
import { useHandler } from "~tests/helpers/useHandler";
import { useRawHandler } from "~tests/helpers/useRawHandler";
import { Context } from "~tests/types";

export interface CreateLiveContextParams<C extends Context = Context> {
plugins?: PluginCollection;
handler?: ReturnType<typeof useHandler<C>>;
handler?: ReturnType<typeof useRawHandler<C>>;
}

export const createLiveContext = async <C extends Context = Context>(
params?: CreateLiveContextParams<C>
params?: CreateLiveContextParams<C>,
payload?: Record<string, any>
) => {
if (params?.handler) {
return params.handler.handle();
return params.handler.handle(payload);
}
const handler = useHandler<C>({
const handler = useRawHandler<C>({
plugins: [...(params?.plugins || [])]
});

return handler.handle();
return handler.handle(payload);
};

export const createLiveContextFactory = <C extends Context = Context>(
params?: CreateLiveContextParams<C>
) => {
return () => {
return createLiveContext<C>(params);
return (payload: Record<string, any> = {}) => {
return createLiveContext<C>(params, payload);
};
};
75 changes: 75 additions & 0 deletions packages/tasks/__tests__/runner/taskTenantAndLocale.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { createMockEvent } from "~tests/mocks";
import { createLiveContextFactory } from "~tests/live";
import { createTaskDefinition } from "~/task";
import { useTaskHandler } from "~tests/helpers/useTaskHandler";

const taskDefinition = createTaskDefinition({
id: "taskRunnerTask",
title: "Task Runner Task",
maxIterations: 2,
run: async ({ response, context }) => {
return response.done("Task is done!", {
tenant: context.tenancy.getCurrentTenant().id,
locale: context.cms.getLocale().code,
defaultLocale: context.i18n.getCurrentLocale("default")!.code,
contentLocale: context.i18n.getCurrentLocale("content")!.code
});
}
});

const defaults = {
tenant: "aCustomTenantId",
locale: "de-DE"
};

describe("task tenant and locale", () => {
it("should properly set the tenant and locale", async () => {
const contextFactory = createLiveContextFactory({
plugins: [taskDefinition]
});

const context = await contextFactory({
headers: {
["x-tenant"]: defaults.tenant,
["x-webiny-cms-endpoint"]: "manage",
["x-webiny-cms-locale"]: defaults.locale,
["x-i18n-locale"]: defaults.locale,
["accept-language"]: defaults.locale
}
});

const task = await context.tasks.createTask({
definitionId: taskDefinition.id,
input: {},
name: "My task name"
});

const { handle } = useTaskHandler({
plugins: [taskDefinition]
});

const result = await handle(
createMockEvent({
webinyTaskId: task.id,
webinyTaskDefinitionId: taskDefinition.id,
tenant: defaults.tenant,
locale: defaults.locale
})
);

expect(result).toEqual({
status: "done",
webinyTaskId: task.id,
webinyTaskDefinitionId: taskDefinition.id,
tenant: "aCustomTenantId",
locale: "de-DE",
message: "Task is done!",
output: {
tenant: "aCustomTenantId",
locale: "de-DE",
defaultLocale: "de-DE",
contentLocale: "de-DE"
}
});
});
});
4 changes: 3 additions & 1 deletion packages/tasks/src/handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export const createHandler = (params: HandlerParams): HandlerCallable => {
headers: {
["x-tenant"]: event.tenant,
["x-webiny-cms-endpoint"]: event.endpoint,
["x-webiny-cms-locale"]: event.locale
["x-webiny-cms-locale"]: event.locale,
["x-i18n-locale"]: event.locale,
["accept-language"]: event.locale
}
}
});
Expand Down
3 changes: 1 addition & 2 deletions packages/tasks/src/runner/TaskControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class TaskControl implements ITaskControl {
let task: ITask<ITaskDataInput>;
try {
task = await this.getTask(taskId);
this.context.security.setIdentity(task.createdBy);
} catch (error) {
return this.response.error({
error
Expand Down Expand Up @@ -111,8 +112,6 @@ export class TaskControl implements ITaskControl {
}

try {
this.context.security.setIdentity(task.createdBy);

const result = await manager.run(definition);

await this.runEvents(result, definition, task);
Expand Down

0 comments on commit 97234a8

Please sign in to comment.