-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
186 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
packages/tasks/__tests__/runner/taskTenantAndLocale.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters