-
Notifications
You must be signed in to change notification settings - Fork 582
/
environment.js
129 lines (120 loc) · 5.05 KB
/
environment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const dbPlugins = require("@webiny/handler-db").default;
const { DynamoDbDriver } = require("@webiny/db-dynamodb");
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 { simulateStream } = require("@webiny/project-utils/testing/dynamodb");
const NodeEnvironment = require("jest-environment-node");
const elasticsearchDataGzipCompression =
require("@webiny/api-elasticsearch/plugins/GzipCompression").default;
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
*/
const plugins = require("../../dist/index").default;
if (typeof plugins !== "function") {
throw new Error(`Loaded plugins file must export a function that returns an array of plugins.`);
}
const ELASTICSEARCH_PORT = process.env.ELASTICSEARCH_PORT || "9200";
const getStorageOperationsPlugins = ({
elasticsearchClient,
documentClient,
elasticsearchClientContext
}) => {
/**
* Intercept DocumentClient operations and trigger dynamoToElastic function (almost like a DynamoDB Stream trigger)
*/
const simulationContext = new ContextPlugin(async context => {
context.plugins.register([elasticsearchDataGzipCompression()]);
await elasticsearchClientContext.apply(context);
});
simulateStream(documentClient, createHandler(simulationContext, dynamoToElastic()));
return () => {
return [
elasticsearchDataGzipCompression(),
plugins(),
dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient
})
}),
elasticsearchClientContext,
{
type: "context",
async apply() {
await elasticsearchClient.indices.putTemplate({
name: "headless-cms-entries-index",
body: {
index_patterns: ["*headless-cms*"],
settings: {
analysis: {
analyzer: {
lowercase_analyzer: {
type: "custom",
filter: ["lowercase", "trim"],
tokenizer: "keyword"
}
}
}
},
mappings: {
properties: {
property: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
},
analyzer: "lowercase_analyzer"
},
rawValues: {
type: "object",
enabled: false
}
}
}
}
});
}
}
];
};
};
class CmsTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
const elasticsearchClient = createElasticsearchClient({
node: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const documentClient = new DocumentClient({
convertEmptyValues: true,
endpoint: process.env.MOCK_DYNAMODB_ENDPOINT || "http://localhost:8001",
sslEnabled: false,
region: "local",
accessKeyId: "test",
secretAccessKey: "test"
});
const elasticsearchClientContext = elasticsearchClientContextPlugin(elasticsearchClient);
/**
* This is a global function that will be called inside the tests to get all relevant plugins, methods and objects.
*/
this.global.__getStorageOperationsPlugins = () => {
return getStorageOperationsPlugins({
elasticsearchClient,
elasticsearchClientContext,
documentClient
});
};
elasticIndexManager(this.global, elasticsearchClient);
}
}
module.exports = CmsTestEnvironment;