Skip to content

Commit

Permalink
fix(project-utils): await handlers in dynamodb to elastic simulation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Nov 2, 2021
1 parent 39acd5d commit 473d1ae
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 98 deletions.
Expand Up @@ -4,7 +4,6 @@ const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const elasticsearchClientContextPlugin = require("@webiny/api-elasticsearch").default;
const { createHandler } = require("@webiny/handler-aws");
const dynamoToElastic = require("@webiny/api-dynamodb-to-elasticsearch/handler").default;
const { Client } = require("@elastic/elasticsearch");
const { simulateStream } = require("@webiny/project-utils/testing/dynamodb");
const NodeEnvironment = require("jest-environment-node");
const elasticsearchDataGzipCompression =
Expand All @@ -13,6 +12,7 @@ const { ContextPlugin } = require("@webiny/handler/plugins/ContextPlugin");
const {
elasticIndexManager
} = require("@webiny/project-utils/testing/helpers/elasticIndexManager");
const { createElasticsearchClient } = require("@webiny/api-elasticsearch/client");
/**
* For this to work it must load plugins that have already been built
*/
Expand All @@ -28,8 +28,9 @@ class FileManagerTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();

const elasticsearchClient = new Client({
node: `http://localhost:${ELASTICSEARCH_PORT}`
const elasticsearchClient = createElasticsearchClient({
node: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const documentClient = new DocumentClient({
convertEmptyValues: true,
Expand All @@ -39,10 +40,7 @@ class FileManagerTestEnvironment extends NodeEnvironment {
accessKeyId: "test",
secretAccessKey: "test"
});
const elasticsearchClientContext = elasticsearchClientContextPlugin({
endpoint: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const elasticsearchClientContext = elasticsearchClientContextPlugin(elasticsearchClient);

/**
* Intercept DocumentClient operations and trigger dynamoToElastic function (almost like a DynamoDB Stream trigger)
Expand All @@ -66,7 +64,7 @@ class FileManagerTestEnvironment extends NodeEnvironment {
})
});
return [
elasticsearchDataGzipCompression(),
//elasticsearchDataGzipCompression(),
...pluginsValue,
...dbPluginsValue,
elasticsearchClientContext
Expand Down
11 changes: 5 additions & 6 deletions packages/api-file-manager-ddb-es/__tests__/useGqlHandler.ts
Expand Up @@ -6,7 +6,6 @@ import { DynamoDbDriver } from "@webiny/db-dynamodb";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
import dbPlugins from "@webiny/handler-db";
import dynamoDbPlugins from "@webiny/db-dynamodb/plugins";
import { Client } from "@elastic/elasticsearch";
import { simulateStream } from "@webiny/project-utils/testing/dynamodb";
import elasticsearchClientContextPlugin from "@webiny/api-elasticsearch";
import { createHandler } from "@webiny/handler-aws";
Expand Down Expand Up @@ -38,6 +37,7 @@ import { until } from "@webiny/project-utils/testing/helpers/until";
import { FilePhysicalStoragePlugin } from "@webiny/api-file-manager/plugins/definitions/FilePhysicalStoragePlugin";
import { createTenancyAndSecurity } from "./tenancySecurity";
import { SecurityIdentity } from "@webiny/api-security/types";
import { createElasticsearchClient } from "@webiny/api-elasticsearch/client";

type UseGqlHandlerParams = {
permissions?: SecurityPermission[];
Expand All @@ -48,8 +48,9 @@ export default (params?: UseGqlHandlerParams) => {
const { permissions, identity } = params || {};

const ELASTICSEARCH_PORT = process.env.ELASTICSEARCH_PORT || "9200";
const elasticsearchClient = new Client({
node: `http://localhost:${ELASTICSEARCH_PORT}`
const elasticsearchClient = createElasticsearchClient({
node: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const documentClient = new DocumentClient({
convertEmptyValues: true,
Expand All @@ -59,9 +60,7 @@ export default (params?: UseGqlHandlerParams) => {
accessKeyId: "test",
secretAccessKey: "test"
});
const elasticsearchClientContext = elasticsearchClientContextPlugin({
endpoint: `http://localhost:${ELASTICSEARCH_PORT}`
});
const elasticsearchClientContext = elasticsearchClientContextPlugin(elasticsearchClient);
const clearElasticsearch = async () => {
return elasticsearchClient.indices.delete({
index: "_all"
Expand Down
Expand Up @@ -4,7 +4,6 @@ const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const elasticsearchClientContextPlugin = require("@webiny/api-elasticsearch").default;
const { createHandler } = require("@webiny/handler-aws");
const dynamoToElastic = require("@webiny/api-dynamodb-to-elasticsearch/handler").default;
const { Client } = require("@elastic/elasticsearch");
const { simulateStream } = require("@webiny/project-utils/testing/dynamodb");
const NodeEnvironment = require("jest-environment-node");
const elasticsearchDataGzipCompression =
Expand All @@ -13,6 +12,7 @@ const { ContextPlugin } = require("@webiny/handler/plugins/ContextPlugin");
const {
elasticIndexManager
} = require("@webiny/project-utils/testing/helpers/elasticIndexManager");
const { createElasticsearchClient } = require("@webiny/api-elasticsearch/client");
/**
* For this to work it must load plugins that have already been built
*/
Expand Down Expand Up @@ -97,8 +97,9 @@ class CmsTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();

const elasticsearchClient = new Client({
node: `http://localhost:${ELASTICSEARCH_PORT}`
const elasticsearchClient = createElasticsearchClient({
node: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const documentClient = new DocumentClient({
convertEmptyValues: true,
Expand All @@ -108,10 +109,7 @@ class CmsTestEnvironment extends NodeEnvironment {
accessKeyId: "test",
secretAccessKey: "test"
});
const elasticsearchClientContext = elasticsearchClientContextPlugin({
endpoint: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const elasticsearchClientContext = elasticsearchClientContextPlugin(elasticsearchClient);

/**
* This is a global function that will be called inside the tests to get all relevant plugins, methods and objects.
Expand Down
Expand Up @@ -349,21 +349,13 @@ export default class CmsContentEntryDynamoElastic implements CmsContentEntryStor
/**
* Delete records of given entry revision.
*/
batch
.delete({
...configurations.db(),
query: {
PK: primaryKey,
SK: this.getSortKeyRevision(entryToDelete.id)
}
})
.delete({
...configurations.esDb(),
query: {
PK: primaryKey,
SK: this.getSortKeyRevision(entryToDelete.id)
}
});
batch.delete({
...configurations.db(),
query: {
PK: primaryKey,
SK: this.getSortKeyRevision(entryToDelete.id)
}
});
/**
* If revision we are deleting is the published one as well, we need to delete those records as well.
*/
Expand Down
32 changes: 24 additions & 8 deletions packages/api-headless-cms/__tests__/validators/date.test.ts
Expand Up @@ -34,7 +34,9 @@ describe("date validators", () => {
const result = await gtePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
}
Expand All @@ -56,7 +58,9 @@ describe("date validators", () => {
const result = await gtePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
Expand All @@ -78,7 +82,9 @@ describe("date validators", () => {
const result = await ltePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
}
Expand All @@ -100,7 +106,9 @@ describe("date validators", () => {
const result = await ltePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
Expand All @@ -124,7 +132,9 @@ describe("date validators", () => {
const result = await gtePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
}
Expand All @@ -146,7 +156,9 @@ describe("date validators", () => {
const result = await gtePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
Expand All @@ -170,7 +182,9 @@ describe("date validators", () => {
const result = await ltePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
}
Expand All @@ -194,7 +208,9 @@ describe("date validators", () => {
const result = await ltePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
Expand Down
24 changes: 18 additions & 6 deletions packages/api-headless-cms/__tests__/validators/time.test.ts
Expand Up @@ -35,7 +35,9 @@ describe("time validators", () => {
const result = await gtePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
});
Expand All @@ -58,7 +60,9 @@ describe("time validators", () => {
const result = await gtePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
Expand All @@ -83,7 +87,9 @@ describe("time validators", () => {
const result = await ltePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(true);
}
Expand All @@ -106,7 +112,9 @@ describe("time validators", () => {
const result = await ltePlugin.validator.validate({
value,
validator,
context
context,
field: {} as any,
contentModel: {} as any
});
expect(result).toEqual(false);
}
Expand All @@ -122,12 +130,16 @@ describe("time validators", () => {
const lteValid = await ltePlugin.validator.validate({
value,
validator: lteValidator,
context
context,
field: {} as any,
contentModel: {} as any
});
const gteValid = await gtePlugin.validator.validate({
value,
validator: gteValidator,
context
context,
field: {} as any,
contentModel: {} as any
});
return lteValid && gteValid;
};
Expand Down
Expand Up @@ -4,7 +4,7 @@ const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const elasticsearchClientContextPlugin = require("@webiny/api-elasticsearch").default;
const { createHandler } = require("@webiny/handler-aws");
const dynamoToElastic = require("@webiny/api-dynamodb-to-elasticsearch/handler").default;
const { Client } = require("@elastic/elasticsearch");
const { createElasticsearchClient } = require("@webiny/api-elasticsearch/client");
const { simulateStream } = require("@webiny/project-utils/testing/dynamodb");
const NodeEnvironment = require("jest-environment-node");
const elasticsearchDataGzipCompression =
Expand Down Expand Up @@ -48,8 +48,9 @@ class PageBuilderTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();

const elasticsearchClient = new Client({
node: `http://localhost:${ELASTICSEARCH_PORT}`
const elasticsearchClient = createElasticsearchClient({
node: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const documentClient = new DocumentClient({
convertEmptyValues: true,
Expand All @@ -59,10 +60,7 @@ class PageBuilderTestEnvironment extends NodeEnvironment {
accessKeyId: "test",
secretAccessKey: "test"
});
const elasticsearchClientContext = elasticsearchClientContextPlugin({
endpoint: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const elasticsearchClientContext = elasticsearchClientContextPlugin(elasticsearchClient);

/**
* Intercept DocumentClient operations and trigger dynamoToElastic function (almost like a DynamoDB Stream trigger)
Expand Down

0 comments on commit 473d1ae

Please sign in to comment.