@strapi/strapi 4.10.0-beta.1 → 4.10.1-experimental.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/strapi.js +2 -468
- package/coverage/clover.xml +1613 -0
- package/coverage/coverage-final.json +48 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/commands/__tests__/data-transfer/shared/index.html +116 -0
- package/coverage/lcov-report/commands/__tests__/data-transfer/shared/transfer.test.utils.js.html +133 -0
- package/coverage/lcov-report/commands/admin-create.js.html +424 -0
- package/coverage/lcov-report/commands/admin-reset.js.html +241 -0
- package/coverage/lcov-report/commands/generate-template.js.html +373 -0
- package/coverage/lcov-report/commands/index.html +146 -0
- package/coverage/lcov-report/commands/transfer/export.js.html +619 -0
- package/coverage/lcov-report/commands/transfer/import.js.html +562 -0
- package/coverage/lcov-report/commands/transfer/index.html +146 -0
- package/coverage/lcov-report/commands/transfer/transfer.js.html +532 -0
- package/coverage/lcov-report/commands/utils/helpers.js.html +430 -0
- package/coverage/lcov-report/commands/utils/index.html +116 -0
- package/coverage/lcov-report/core/registries/custom-fields.js.html +301 -0
- package/coverage/lcov-report/core/registries/index.html +116 -0
- package/coverage/lcov-report/core-api/controller/collection-type.js.html +418 -0
- package/coverage/lcov-report/core-api/controller/index.html +161 -0
- package/coverage/lcov-report/core-api/controller/index.js.html +220 -0
- package/coverage/lcov-report/core-api/controller/single-type.js.html +274 -0
- package/coverage/lcov-report/core-api/controller/transform.js.html +376 -0
- package/coverage/lcov-report/core-api/service/collection-type.js.html +325 -0
- package/coverage/lcov-report/core-api/service/index.html +161 -0
- package/coverage/lcov-report/core-api/service/index.js.html +220 -0
- package/coverage/lcov-report/core-api/service/pagination.js.html +460 -0
- package/coverage/lcov-report/core-api/service/single-type.js.html +301 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +386 -0
- package/coverage/lcov-report/load/filepath-to-prop-path.js.html +151 -0
- package/coverage/lcov-report/load/index.html +116 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/services/content-api/index.html +116 -0
- package/coverage/lcov-report/services/content-api/index.js.html +307 -0
- package/coverage/lcov-report/services/content-api/permissions/engine.js.html +100 -0
- package/coverage/lcov-report/services/content-api/permissions/index.html +131 -0
- package/coverage/lcov-report/services/content-api/permissions/index.js.html +529 -0
- package/coverage/lcov-report/services/content-api/permissions/providers/action.js.html +142 -0
- package/coverage/lcov-report/services/content-api/permissions/providers/condition.js.html +142 -0
- package/coverage/lcov-report/services/content-api/permissions/providers/index.html +146 -0
- package/coverage/lcov-report/services/content-api/permissions/providers/index.js.html +112 -0
- package/coverage/lcov-report/services/core-store.js.html +520 -0
- package/coverage/lcov-report/services/entity-service/attributes/index.html +131 -0
- package/coverage/lcov-report/services/entity-service/attributes/index.js.html +178 -0
- package/coverage/lcov-report/services/entity-service/attributes/transforms.js.html +145 -0
- package/coverage/lcov-report/services/entity-service/components.js.html +1246 -0
- package/coverage/lcov-report/services/entity-service/index.html +146 -0
- package/coverage/lcov-report/services/entity-service/index.js.html +1120 -0
- package/coverage/lcov-report/services/entity-service/params.js.html +112 -0
- package/coverage/lcov-report/services/entity-validator/__tests__/relations/utils/index.html +116 -0
- package/coverage/lcov-report/services/entity-validator/__tests__/relations/utils/relations.testdata.js.html +544 -0
- package/coverage/lcov-report/services/entity-validator/index.html +131 -0
- package/coverage/lcov-report/services/entity-validator/index.js.html +1231 -0
- package/coverage/lcov-report/services/entity-validator/validators.js.html +733 -0
- package/coverage/lcov-report/services/event-hub.js.html +319 -0
- package/coverage/lcov-report/services/fs.js.html +259 -0
- package/coverage/lcov-report/services/index.html +161 -0
- package/coverage/lcov-report/services/metrics/admin-user-hash.js.html +148 -0
- package/coverage/lcov-report/services/metrics/index.html +206 -0
- package/coverage/lcov-report/services/metrics/index.js.html +265 -0
- package/coverage/lcov-report/services/metrics/is-truthy.js.html +112 -0
- package/coverage/lcov-report/services/metrics/middleware.js.html +184 -0
- package/coverage/lcov-report/services/metrics/rate-limiter.js.html +166 -0
- package/coverage/lcov-report/services/metrics/sender.js.html +394 -0
- package/coverage/lcov-report/services/metrics/stringify-deep.js.html +151 -0
- package/coverage/lcov-report/services/utils/index.html +116 -0
- package/coverage/lcov-report/services/utils/upload-files.js.html +322 -0
- package/coverage/lcov-report/services/worker-queue.js.html +262 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/utils/convert-custom-field-type.js.html +151 -0
- package/coverage/lcov-report/utils/index.html +146 -0
- package/coverage/lcov-report/utils/machine-id.js.html +127 -0
- package/coverage/lcov-report/utils/url-from-segments.js.html +121 -0
- package/ee/LICENSE.txt +21 -0
- package/ee/index.js +3 -1
- package/ee/license.js +8 -7
- package/lib/Strapi.js +3 -0
- package/lib/commands/__tests__/commands.test.js +20 -0
- package/lib/commands/__tests__/commands.test.utils.js +16 -0
- package/lib/commands/actions/admin/create-user/__tests__/admin.create-user.test.js +450 -0
- package/lib/commands/{admin-create.js → actions/admin/create-user/action.js} +2 -2
- package/lib/commands/actions/admin/create-user/command.js +19 -0
- package/lib/commands/actions/admin/reset-user-password/__tests__/admin.reset-user-password.test.js +145 -0
- package/lib/commands/{admin-reset.js → actions/admin/reset-user-password/action.js} +2 -2
- package/lib/commands/actions/admin/reset-user-password/command.js +17 -0
- package/lib/commands/{build.js → actions/build/action.js} +2 -2
- package/lib/commands/actions/build/command.js +15 -0
- package/lib/commands/{configurationDump.js → actions/configuration/dump/action.js} +1 -1
- package/lib/commands/actions/configuration/dump/command.js +17 -0
- package/lib/commands/{configurationRestore.js → actions/configuration/restore/action.js} +1 -1
- package/lib/commands/actions/configuration/restore/command.js +17 -0
- package/lib/commands/{console.js → actions/console/action.js} +2 -2
- package/lib/commands/actions/console/command.js +14 -0
- package/lib/commands/{content-types/list.js → actions/content-types/list/action.js} +2 -2
- package/lib/commands/actions/content-types/list/command.js +14 -0
- package/lib/commands/{controllers/list.js → actions/controllers/list/action.js} +2 -2
- package/lib/commands/actions/controllers/list/command.js +14 -0
- package/lib/commands/{develop.js → actions/develop/action.js} +3 -3
- package/lib/commands/actions/develop/command.js +19 -0
- package/lib/commands/actions/export/__tests__/export.test.js +175 -0
- package/lib/commands/{transfer/export.js → actions/export/action.js} +3 -2
- package/lib/commands/actions/export/command.js +45 -0
- package/lib/commands/actions/generate/command.js +18 -0
- package/lib/commands/{hooks/list.js → actions/hooks/list/action.js} +2 -2
- package/lib/commands/actions/hooks/list/command.js +14 -0
- package/lib/commands/actions/import/__tests__/import.test.js +143 -0
- package/lib/commands/{transfer/import.js → actions/import/action.js} +2 -2
- package/lib/commands/actions/import/command.js +97 -0
- package/lib/commands/{install.js → actions/install/action.js} +1 -1
- package/lib/commands/actions/install/command.js +14 -0
- package/lib/commands/{middlewares/list.js → actions/middlewares/list/action.js} +2 -2
- package/lib/commands/actions/middlewares/list/command.js +14 -0
- package/lib/commands/{new.js → actions/new/action.js} +1 -1
- package/lib/commands/actions/new/command.js +35 -0
- package/lib/commands/{policies/list.js → actions/policies/list/action.js} +2 -2
- package/lib/commands/actions/policies/list/command.js +14 -0
- package/lib/commands/{report.js → actions/report/action.js} +1 -1
- package/lib/commands/actions/report/command.js +17 -0
- package/lib/commands/{routes/list.js → actions/routes/list/action.js} +2 -2
- package/lib/commands/actions/routes/list/command.js +14 -0
- package/lib/commands/{services/list.js → actions/services/list/action.js} +2 -2
- package/lib/commands/actions/services/list/command.js +14 -0
- package/lib/commands/{start.js → actions/start/action.js} +1 -1
- package/lib/commands/actions/start/command.js +14 -0
- package/lib/commands/{opt-out-telemetry.js → actions/telemetry/disable/action.js} +1 -1
- package/lib/commands/actions/telemetry/disable/command.js +14 -0
- package/lib/commands/{opt-in-telemetry.js → actions/telemetry/enable/action.js} +1 -1
- package/lib/commands/actions/telemetry/enable/command.js +14 -0
- package/lib/commands/actions/templates/generate/__tests__/templates.generate.js +118 -0
- package/lib/commands/actions/templates/generate/command.js +14 -0
- package/lib/commands/actions/transfer/__tests__/transfer.test.js +178 -0
- package/lib/commands/{transfer/transfer.js → actions/transfer/action.js} +2 -2
- package/lib/commands/actions/transfer/command.js +115 -0
- package/lib/commands/{ts/generate-types.js → actions/ts/generate-types/action.js} +2 -2
- package/lib/commands/actions/ts/generate-types/command.js +21 -0
- package/lib/commands/{uninstall.js → actions/uninstall/action.js} +1 -1
- package/lib/commands/actions/uninstall/command.js +15 -0
- package/lib/commands/actions/version/command.js +19 -0
- package/lib/commands/{watchAdmin.js → actions/watch-admin/action.js} +4 -4
- package/lib/commands/actions/watch-admin/command.js +15 -0
- package/lib/commands/index.js +66 -0
- package/lib/commands/{transfer/utils.js → utils/data-transfer.js} +2 -2
- package/lib/commands/utils/helpers.js +54 -3
- package/lib/core/registries/__tests__/custom-fields.test.js +152 -0
- package/lib/core/registries/custom-fields.js +19 -2
- package/lib/core-api/__tests__/controller.test.js +39 -0
- package/lib/core-api/controller/__tests__/transform.test.js +226 -0
- package/lib/core-api/service/__tests__/index.test.js +127 -0
- package/lib/core-api/service/__tests__/pagination.test.js +275 -0
- package/lib/load/__tests__/filepath-to-prop-path.test.js +30 -0
- package/lib/middlewares/__tests__/errors.test.js +21 -0
- package/lib/services/__tests__/content-api-permissions.test.js +291 -0
- package/lib/services/__tests__/core-store.test.js +148 -0
- package/lib/services/__tests__/event-hub.test.js +126 -0
- package/lib/services/__tests__/fs.test.js +78 -0
- package/lib/services/__tests__/worker-queue.test.js +47 -0
- package/lib/services/entity-service/__tests__/entity-service-events.test.js +117 -0
- package/lib/services/entity-service/__tests__/entity-service.test.js +587 -0
- package/lib/services/entity-validator/__tests__/biginteger-validators.test.js +220 -0
- package/lib/services/entity-validator/__tests__/date-validators.test.js +183 -0
- package/lib/services/entity-validator/__tests__/datetime-validators.test.js +183 -0
- package/lib/services/entity-validator/__tests__/email-validators.test.js +56 -0
- package/lib/services/entity-validator/__tests__/enumeration-validators.test.js +43 -0
- package/lib/services/entity-validator/__tests__/float-validators.test.js +278 -0
- package/lib/services/entity-validator/__tests__/index.test.js +609 -0
- package/lib/services/entity-validator/__tests__/integer-validators.test.js +278 -0
- package/lib/services/entity-validator/__tests__/relations/attribute-level.test.js +123 -0
- package/lib/services/entity-validator/__tests__/relations/component-level.test.js +275 -0
- package/lib/services/entity-validator/__tests__/relations/dynamic-zone-level.test.js +159 -0
- package/lib/services/entity-validator/__tests__/relations/media-level.test.js +74 -0
- package/lib/services/entity-validator/__tests__/relations/utils/relations.testdata.js +153 -0
- package/lib/services/entity-validator/__tests__/string-validators.test.js +374 -0
- package/lib/services/entity-validator/__tests__/time-validators.test.js +183 -0
- package/lib/services/entity-validator/__tests__/timestamp-validators.test.js +204 -0
- package/lib/services/entity-validator/__tests__/uid-validators.test.js +229 -0
- package/lib/services/metrics/__tests__/admin-user-hash.test.js +41 -0
- package/lib/services/metrics/__tests__/index.test.js +157 -0
- package/lib/services/metrics/__tests__/is-truthy.js +33 -0
- package/lib/services/metrics/__tests__/middleware.test.js +60 -0
- package/lib/services/metrics/__tests__/rate-limiter.test.js +50 -0
- package/lib/services/metrics/__tests__/stringify-deep.test.js +27 -0
- package/lib/services/metrics/sender.js +1 -2
- package/lib/services/webhook-runner.js +4 -4
- package/lib/types/core/commands/index.d.ts +6 -0
- package/lib/utils/__tests__/convert-custom-field-type.test.js +69 -0
- package/lib/utils/__tests__/url-from-segments.test.js +40 -0
- package/lib/utils/fetch.js +23 -0
- package/package.json +17 -16
- /package/lib/commands/{generate-template.js → actions/templates/generate/action.js} +0 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { createCoreStore } = require('../core-store');
|
|
4
|
+
|
|
5
|
+
const data = {
|
|
6
|
+
key: 'testKey',
|
|
7
|
+
value: 'testValue',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const queryRes = {
|
|
11
|
+
id: 1,
|
|
12
|
+
key: 'testKey',
|
|
13
|
+
value: 'testValue',
|
|
14
|
+
type: 'string',
|
|
15
|
+
environment: null,
|
|
16
|
+
tag: null,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const where = {
|
|
20
|
+
key: 'plugin_testName_testKey',
|
|
21
|
+
environment: 'test',
|
|
22
|
+
tag: null,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
describe('Core Store', () => {
|
|
26
|
+
test('Set key in empty store', async () => {
|
|
27
|
+
const fakeEmptyDBQuery = {
|
|
28
|
+
findOne: jest.fn(() => Promise.resolve()),
|
|
29
|
+
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
30
|
+
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
31
|
+
delete: jest.fn(() => Promise.resolve()),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const fakeEmptyDB = {
|
|
35
|
+
query: jest.fn(() => fakeEmptyDBQuery),
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const emptyTestStore = createCoreStore({
|
|
39
|
+
db: fakeEmptyDB,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const store = emptyTestStore({
|
|
43
|
+
environment: 'test',
|
|
44
|
+
type: 'plugin',
|
|
45
|
+
name: 'testName',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
await store.set(data);
|
|
49
|
+
|
|
50
|
+
expect(fakeEmptyDB.query).toHaveBeenCalledTimes(2);
|
|
51
|
+
expect(fakeEmptyDBQuery.findOne).toHaveBeenCalledWith({ where });
|
|
52
|
+
expect(fakeEmptyDBQuery.create).toHaveBeenCalledWith({
|
|
53
|
+
data: { ...where, value: JSON.stringify('testValue'), type: 'string' },
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('Set key in not empty store', async () => {
|
|
58
|
+
const fakeNotEmptyDBQuery = {
|
|
59
|
+
findOne: jest.fn(() => Promise.resolve(queryRes)),
|
|
60
|
+
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
61
|
+
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
62
|
+
delete: jest.fn(() => Promise.resolve()),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const fakeNotEmptyDB = {
|
|
66
|
+
query: jest.fn(() => fakeNotEmptyDBQuery),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const notEmptyTestStore = createCoreStore({
|
|
70
|
+
db: fakeNotEmptyDB,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const store = notEmptyTestStore({
|
|
74
|
+
environment: 'test',
|
|
75
|
+
type: 'plugin',
|
|
76
|
+
name: 'testName',
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
await store.set(data);
|
|
80
|
+
|
|
81
|
+
expect(fakeNotEmptyDB.query).toHaveBeenCalledTimes(2);
|
|
82
|
+
expect(fakeNotEmptyDBQuery.findOne).toHaveBeenCalledWith({ where });
|
|
83
|
+
expect(fakeNotEmptyDBQuery.update).toHaveBeenCalledWith({
|
|
84
|
+
where: { id: queryRes.id },
|
|
85
|
+
data: {
|
|
86
|
+
type: 'string',
|
|
87
|
+
value: JSON.stringify('testValue'),
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('Delete key from empty store', async () => {
|
|
93
|
+
const fakeEmptyDBQuery = {
|
|
94
|
+
findOne: jest.fn(() => Promise.resolve()),
|
|
95
|
+
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
96
|
+
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
97
|
+
delete: jest.fn(() => Promise.resolve()),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const fakeEmptyDB = {
|
|
101
|
+
query: jest.fn(() => fakeEmptyDBQuery),
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const emptyTestStore = createCoreStore({
|
|
105
|
+
db: fakeEmptyDB,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const store = emptyTestStore({
|
|
109
|
+
environment: 'test',
|
|
110
|
+
type: 'plugin',
|
|
111
|
+
name: 'testName',
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
await store.delete(data);
|
|
115
|
+
|
|
116
|
+
expect(fakeEmptyDB.query).toHaveBeenCalledTimes(1);
|
|
117
|
+
expect(fakeEmptyDBQuery.delete).toHaveBeenCalledWith({ where });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('Delete key from not empty store', async () => {
|
|
121
|
+
const fakeNotEmptyDBQuery = {
|
|
122
|
+
findOne: jest.fn(() => Promise.resolve(queryRes)),
|
|
123
|
+
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
124
|
+
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
125
|
+
delete: jest.fn(() => Promise.resolve()),
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const fakeNotEmptyDB = {
|
|
129
|
+
query: jest.fn(() => fakeNotEmptyDBQuery),
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const notEmptyTestStore = createCoreStore({
|
|
133
|
+
db: fakeNotEmptyDB,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const store = notEmptyTestStore({
|
|
137
|
+
environment: 'test',
|
|
138
|
+
type: 'plugin',
|
|
139
|
+
name: 'testName',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const rest = await store.delete(data);
|
|
143
|
+
|
|
144
|
+
expect(fakeNotEmptyDB.query).toHaveBeenCalledTimes(1);
|
|
145
|
+
expect(fakeNotEmptyDBQuery.delete).toHaveBeenCalledWith({ where });
|
|
146
|
+
expect(rest).toEqual(undefined);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const createEventHub = require('../event-hub');
|
|
4
|
+
|
|
5
|
+
describe('Event Hub', () => {
|
|
6
|
+
it('only triggers the callback once with once()', async () => {
|
|
7
|
+
const { once, emit } = createEventHub();
|
|
8
|
+
|
|
9
|
+
const fn = jest.fn();
|
|
10
|
+
|
|
11
|
+
const args = [1, 2, 3];
|
|
12
|
+
once('my-event', fn);
|
|
13
|
+
|
|
14
|
+
await emit('my-event', ...args);
|
|
15
|
+
await emit('my-event');
|
|
16
|
+
await emit('my-event');
|
|
17
|
+
|
|
18
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
19
|
+
expect(fn).toHaveBeenCalledWith(...args);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('subscribes and unsubscribes to all events', async () => {
|
|
23
|
+
const { subscribe, unsubscribe, emit } = createEventHub();
|
|
24
|
+
|
|
25
|
+
const fn = jest.fn();
|
|
26
|
+
subscribe(fn);
|
|
27
|
+
|
|
28
|
+
const args1 = [1, 2, 3];
|
|
29
|
+
const args2 = [4, 5, 6];
|
|
30
|
+
|
|
31
|
+
await emit('my-event', ...args1);
|
|
32
|
+
await emit('my-event', ...args2);
|
|
33
|
+
await emit('my-other-event');
|
|
34
|
+
|
|
35
|
+
expect(fn).toHaveBeenCalled();
|
|
36
|
+
expect(fn).toHaveBeenNthCalledWith(1, 'my-event', ...args1);
|
|
37
|
+
expect(fn).toHaveBeenNthCalledWith(2, 'my-event', ...args2);
|
|
38
|
+
expect(fn).toHaveBeenNthCalledWith(3, 'my-other-event');
|
|
39
|
+
|
|
40
|
+
// Unsubscribes with unsubscribe()
|
|
41
|
+
unsubscribe(fn);
|
|
42
|
+
await emit('my-event');
|
|
43
|
+
expect(fn).toHaveBeenCalledTimes(3);
|
|
44
|
+
|
|
45
|
+
// Unsubscribes with the returned function
|
|
46
|
+
const unsubscribe2 = subscribe(fn);
|
|
47
|
+
await emit('my-event');
|
|
48
|
+
expect(fn).toHaveBeenCalledTimes(4);
|
|
49
|
+
unsubscribe2();
|
|
50
|
+
await emit('my-event');
|
|
51
|
+
expect(fn).toHaveBeenCalledTimes(4);
|
|
52
|
+
|
|
53
|
+
// Avoid removing the wrong subscriber when unsubscribe is given a non-existing subscriber
|
|
54
|
+
const unsubscribe3 = subscribe(fn);
|
|
55
|
+
const unrelatedFunction = jest.fn();
|
|
56
|
+
unsubscribe(unrelatedFunction);
|
|
57
|
+
await emit('my-event');
|
|
58
|
+
expect(fn).toHaveBeenCalledTimes(5);
|
|
59
|
+
unsubscribe3();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('adds and removes simple listeners', async () => {
|
|
63
|
+
const { on, off, emit } = createEventHub();
|
|
64
|
+
|
|
65
|
+
const fn = jest.fn();
|
|
66
|
+
const args = [1, 2, 3];
|
|
67
|
+
|
|
68
|
+
// Listens to event with on()
|
|
69
|
+
on('my-event', fn);
|
|
70
|
+
await emit('my-event', ...args);
|
|
71
|
+
expect(fn).toHaveBeenCalledWith(...args);
|
|
72
|
+
|
|
73
|
+
// Removes listener with off()
|
|
74
|
+
off('my-event', fn);
|
|
75
|
+
await emit('my-event');
|
|
76
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
77
|
+
|
|
78
|
+
// Removes listener with the returned function
|
|
79
|
+
const off2 = on('my-event', fn);
|
|
80
|
+
await emit('my-event', ...args);
|
|
81
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
82
|
+
off2();
|
|
83
|
+
await emit('my-event');
|
|
84
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('removes all subscribers on destroy()', async () => {
|
|
88
|
+
const { subscribe, on, emit, destroy } = createEventHub();
|
|
89
|
+
|
|
90
|
+
const fn = jest.fn();
|
|
91
|
+
const fn2 = jest.fn();
|
|
92
|
+
subscribe(fn);
|
|
93
|
+
on('my-event', fn2);
|
|
94
|
+
|
|
95
|
+
await emit('my-event');
|
|
96
|
+
expect(fn).toHaveBeenCalled();
|
|
97
|
+
expect(fn2).toHaveBeenCalled();
|
|
98
|
+
|
|
99
|
+
destroy();
|
|
100
|
+
|
|
101
|
+
// Subscribers are removed
|
|
102
|
+
await emit('my-event');
|
|
103
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
104
|
+
expect(fn2).toHaveBeenCalledTimes(1);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('removes all subscribers on removeAllListeners()', async () => {
|
|
108
|
+
const { subscribe, on, emit, removeAllListeners } = createEventHub();
|
|
109
|
+
|
|
110
|
+
const fn = jest.fn();
|
|
111
|
+
const fn2 = jest.fn();
|
|
112
|
+
subscribe(fn);
|
|
113
|
+
on('my-event', fn2);
|
|
114
|
+
|
|
115
|
+
await emit('my-event');
|
|
116
|
+
expect(fn).toHaveBeenCalled();
|
|
117
|
+
expect(fn2).toHaveBeenCalled();
|
|
118
|
+
|
|
119
|
+
removeAllListeners();
|
|
120
|
+
|
|
121
|
+
// Subscribers are removed
|
|
122
|
+
await emit('my-event');
|
|
123
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
124
|
+
expect(fn2).toHaveBeenCalledTimes(1);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
jest.mock('fs-extra', () => ({
|
|
4
|
+
ensureFile: jest.fn(() => Promise.resolve()),
|
|
5
|
+
writeFile: jest.fn(() => Promise.resolve()),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const fse = require('fs-extra');
|
|
10
|
+
const fs = require('../fs');
|
|
11
|
+
|
|
12
|
+
describe('Strapi fs utils', () => {
|
|
13
|
+
const strapi = {
|
|
14
|
+
dirs: { dist: { root: '/tmp' }, app: { root: '/tmp' } },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
test('Provides new functions', () => {
|
|
18
|
+
const strapiFS = fs(strapi);
|
|
19
|
+
|
|
20
|
+
expect(strapiFS.writeAppFile).toBeInstanceOf(Function);
|
|
21
|
+
expect(strapiFS.writePluginFile).toBeInstanceOf(Function);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('Write App File', () => {
|
|
25
|
+
test('Makes sure the path exists and writes', async () => {
|
|
26
|
+
const strapiFS = fs(strapi);
|
|
27
|
+
|
|
28
|
+
const content = '';
|
|
29
|
+
|
|
30
|
+
await strapiFS.writeAppFile('test', content);
|
|
31
|
+
|
|
32
|
+
expect(fse.ensureFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'));
|
|
33
|
+
expect(fse.writeFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'), content);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('Normalize the path to avoid relative access to folders in parent directories', async () => {
|
|
37
|
+
const strapiFS = fs(strapi);
|
|
38
|
+
|
|
39
|
+
const content = '';
|
|
40
|
+
|
|
41
|
+
await strapiFS.writeAppFile('../../test', content);
|
|
42
|
+
|
|
43
|
+
expect(fse.ensureFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'));
|
|
44
|
+
expect(fse.writeFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'), content);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('Works with array path', async () => {
|
|
48
|
+
const strapiFS = fs(strapi);
|
|
49
|
+
|
|
50
|
+
const content = '';
|
|
51
|
+
|
|
52
|
+
await strapiFS.writeAppFile(['test', 'sub', 'path'], content);
|
|
53
|
+
|
|
54
|
+
expect(fse.ensureFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test', 'sub', 'path'));
|
|
55
|
+
expect(fse.writeFile).toHaveBeenCalledWith(
|
|
56
|
+
path.join('/', 'tmp', 'test', 'sub', 'path'),
|
|
57
|
+
content
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('Write Plugin File', () => {
|
|
63
|
+
test('Scopes the writes in the extensions folder', async () => {
|
|
64
|
+
const strapiFS = fs(strapi);
|
|
65
|
+
|
|
66
|
+
const content = '';
|
|
67
|
+
|
|
68
|
+
strapiFS.writeAppFile = jest.fn(() => Promise.resolve());
|
|
69
|
+
|
|
70
|
+
await strapiFS.writePluginFile('users-permissions', ['test', 'sub', 'path'], content);
|
|
71
|
+
|
|
72
|
+
expect(strapiFS.writeAppFile).toHaveBeenCalledWith(
|
|
73
|
+
'extensions/users-permissions/test/sub/path',
|
|
74
|
+
content
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const WorkerQueue = require('../worker-queue');
|
|
4
|
+
|
|
5
|
+
describe('WorkerQueue', () => {
|
|
6
|
+
test('Executes worker', async () => {
|
|
7
|
+
const fn = jest.fn();
|
|
8
|
+
const input = 1;
|
|
9
|
+
|
|
10
|
+
const q = new WorkerQueue({
|
|
11
|
+
logger: console.log.bind(console),
|
|
12
|
+
concurrency: 1,
|
|
13
|
+
});
|
|
14
|
+
q.subscribe(fn);
|
|
15
|
+
|
|
16
|
+
q.enqueue(input);
|
|
17
|
+
|
|
18
|
+
await new Promise((resolve) => {
|
|
19
|
+
setTimeout(resolve);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(fn).toHaveBeenCalledWith(input);
|
|
23
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('Executes worker', async () => {
|
|
27
|
+
const fn = jest.fn();
|
|
28
|
+
const input = 1;
|
|
29
|
+
|
|
30
|
+
const q = new WorkerQueue({
|
|
31
|
+
logger: console.log.bind(console),
|
|
32
|
+
concurrency: 1,
|
|
33
|
+
});
|
|
34
|
+
q.subscribe(fn);
|
|
35
|
+
|
|
36
|
+
q.enqueue(input);
|
|
37
|
+
q.enqueue(input);
|
|
38
|
+
q.enqueue(input);
|
|
39
|
+
|
|
40
|
+
await new Promise((resolve) => {
|
|
41
|
+
setTimeout(resolve);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(fn).toHaveBeenCalledWith(input);
|
|
45
|
+
expect(fn).toHaveBeenCalledTimes(3);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const createEntityService = require('..');
|
|
4
|
+
const entityValidator = require('../../entity-validator');
|
|
5
|
+
|
|
6
|
+
describe('Entity service triggers webhooks', () => {
|
|
7
|
+
let instance;
|
|
8
|
+
const eventHub = { emit: jest.fn() };
|
|
9
|
+
let entity = { attr: 'value' };
|
|
10
|
+
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
const model = {
|
|
13
|
+
uid: 'api::test.test',
|
|
14
|
+
kind: 'singleType',
|
|
15
|
+
modelName: 'test-model',
|
|
16
|
+
privateAttributes: [],
|
|
17
|
+
attributes: {
|
|
18
|
+
attr: { type: 'string' },
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
instance = createEntityService({
|
|
22
|
+
strapi: {
|
|
23
|
+
getModel: () => model,
|
|
24
|
+
},
|
|
25
|
+
db: {
|
|
26
|
+
transaction: (cb) => cb(),
|
|
27
|
+
query: () => ({
|
|
28
|
+
count: () => 0,
|
|
29
|
+
create: ({ data }) => data,
|
|
30
|
+
update: ({ data }) => data,
|
|
31
|
+
findOne: () => entity,
|
|
32
|
+
findMany: () => [entity, entity],
|
|
33
|
+
delete: () => ({}),
|
|
34
|
+
deleteMany: () => ({}),
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
eventHub,
|
|
38
|
+
entityValidator,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
global.strapi = {
|
|
42
|
+
getModel: () => model,
|
|
43
|
+
config: {
|
|
44
|
+
get: () => [],
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('Emit event: Create', async () => {
|
|
50
|
+
// Create entity
|
|
51
|
+
await instance.create('test-model', { data: entity });
|
|
52
|
+
|
|
53
|
+
// Expect entry.create event to be emitted
|
|
54
|
+
expect(eventHub.emit).toHaveBeenCalledWith('entry.create', {
|
|
55
|
+
entry: entity,
|
|
56
|
+
model: 'test-model',
|
|
57
|
+
uid: 'api::test.test',
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
eventHub.emit.mockClear();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('Emit event: Update', async () => {
|
|
64
|
+
// Update entity
|
|
65
|
+
await instance.update('test-model', 'entity-id', { data: entity });
|
|
66
|
+
|
|
67
|
+
// Expect entry.update event to be emitted
|
|
68
|
+
expect(eventHub.emit).toHaveBeenCalledWith('entry.update', {
|
|
69
|
+
entry: entity,
|
|
70
|
+
model: 'test-model',
|
|
71
|
+
uid: 'api::test.test',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
eventHub.emit.mockClear();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('Emit event: Delete', async () => {
|
|
78
|
+
// Delete entity
|
|
79
|
+
await instance.delete('test-model', 'entity-id', {});
|
|
80
|
+
|
|
81
|
+
// Expect entry.create event to be emitted
|
|
82
|
+
expect(eventHub.emit).toHaveBeenCalledWith('entry.delete', {
|
|
83
|
+
entry: entity,
|
|
84
|
+
model: 'test-model',
|
|
85
|
+
uid: 'api::test.test',
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
eventHub.emit.mockClear();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('Emit event: Delete Many', async () => {
|
|
92
|
+
// Delete entity
|
|
93
|
+
await instance.deleteMany('test-model', {});
|
|
94
|
+
|
|
95
|
+
// Expect entry.create event to be emitted
|
|
96
|
+
expect(eventHub.emit).toHaveBeenCalledWith('entry.delete', {
|
|
97
|
+
entry: entity,
|
|
98
|
+
model: 'test-model',
|
|
99
|
+
uid: 'api::test.test',
|
|
100
|
+
});
|
|
101
|
+
// One event per each entity deleted
|
|
102
|
+
expect(eventHub.emit).toHaveBeenCalledTimes(2);
|
|
103
|
+
|
|
104
|
+
eventHub.emit.mockClear();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('Do not emit event when no deleted entity', async () => {
|
|
108
|
+
entity = null;
|
|
109
|
+
// Delete non existent entity
|
|
110
|
+
await instance.delete('test-model', 'entity-id', {});
|
|
111
|
+
|
|
112
|
+
// Expect entry.create event to be emitted
|
|
113
|
+
expect(eventHub.emit).toHaveBeenCalledTimes(0);
|
|
114
|
+
|
|
115
|
+
eventHub.emit.mockClear();
|
|
116
|
+
});
|
|
117
|
+
});
|