directus 9.20.4 → 9.21.2

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.
Files changed (143) hide show
  1. package/dist/auth/drivers/openid.js +3 -1
  2. package/dist/cli/commands/schema/apply.js +0 -2
  3. package/dist/cli/commands/schema/snapshot.js +0 -2
  4. package/dist/cli/utils/create-db-connection.d.ts +1 -1
  5. package/dist/controllers/extensions.js +4 -13
  6. package/dist/database/helpers/date/dialects/sqlite.d.ts +1 -1
  7. package/dist/database/helpers/date/dialects/sqlite.js +4 -0
  8. package/dist/database/helpers/date/types.d.ts +1 -1
  9. package/dist/database/helpers/date/types.js +4 -0
  10. package/dist/database/helpers/fn/dialects/mssql.d.ts +8 -8
  11. package/dist/database/helpers/fn/dialects/mssql.js +22 -16
  12. package/dist/database/helpers/fn/dialects/mysql.d.ts +8 -8
  13. package/dist/database/helpers/fn/dialects/mysql.js +22 -16
  14. package/dist/database/helpers/fn/dialects/postgres.d.ts +8 -8
  15. package/dist/database/helpers/fn/dialects/postgres.js +22 -16
  16. package/dist/database/helpers/fn/types.d.ts +1 -1
  17. package/dist/database/helpers/index.d.ts +1 -1
  18. package/dist/database/helpers/schema/dialects/cockroachdb.d.ts +1 -0
  19. package/dist/database/helpers/schema/dialects/cockroachdb.js +11 -0
  20. package/dist/database/helpers/schema/types.d.ts +3 -2
  21. package/dist/database/helpers/schema/types.js +5 -0
  22. package/dist/database/migrations/run.js +29 -3
  23. package/dist/database/run-ast.d.ts +1 -1
  24. package/dist/database/run-ast.js +1 -1
  25. package/dist/env.d.ts +4 -0
  26. package/dist/env.js +9 -4
  27. package/dist/env.test.d.ts +1 -8
  28. package/dist/exceptions/database/contains-null-values.d.ts +1 -1
  29. package/dist/exceptions/database/dialects/types.d.ts +6 -6
  30. package/dist/exceptions/database/invalid-foreign-key.d.ts +1 -1
  31. package/dist/exceptions/database/not-null-violation.d.ts +1 -1
  32. package/dist/exceptions/database/record-not-unique.d.ts +1 -1
  33. package/dist/exceptions/database/value-out-of-range.d.ts +1 -1
  34. package/dist/exceptions/database/value-too-long.d.ts +1 -1
  35. package/dist/exceptions/hit-rate-limit.d.ts +1 -1
  36. package/dist/exceptions/method-not-allowed.d.ts +1 -1
  37. package/dist/exceptions/service-unavailable.d.ts +1 -1
  38. package/dist/extensions.d.ts +7 -7
  39. package/dist/extensions.js +92 -89
  40. package/dist/logger.d.ts +1 -0
  41. package/dist/messenger.d.ts +1 -1
  42. package/dist/middleware/authenticate.d.ts +1 -0
  43. package/dist/middleware/schema.js +1 -1
  44. package/dist/middleware/validate-batch.d.ts +2 -0
  45. package/dist/operations/condition/index.d.ts +1 -1
  46. package/dist/operations/condition/index.js +1 -1
  47. package/dist/operations/condition/index.test.d.ts +1 -0
  48. package/dist/operations/exec/index.d.ts +1 -1
  49. package/dist/operations/item-create/index.d.ts +1 -1
  50. package/dist/operations/item-delete/index.d.ts +1 -1
  51. package/dist/operations/item-read/index.d.ts +1 -1
  52. package/dist/operations/item-update/index.d.ts +1 -1
  53. package/dist/operations/log/index.d.ts +1 -1
  54. package/dist/operations/mail/index.d.ts +1 -1
  55. package/dist/operations/notification/index.d.ts +1 -1
  56. package/dist/operations/request/index.d.ts +1 -1
  57. package/dist/operations/sleep/index.d.ts +1 -1
  58. package/dist/operations/transform/index.d.ts +1 -1
  59. package/dist/operations/trigger/index.d.ts +1 -1
  60. package/dist/operations/trigger/index.js +5 -2
  61. package/dist/rate-limiter.d.ts +1 -1
  62. package/dist/services/authorization.js +7 -3
  63. package/dist/services/collections.d.ts +1 -1
  64. package/dist/services/collections.js +112 -13
  65. package/dist/services/fields.d.ts +5 -4
  66. package/dist/services/fields.js +118 -50
  67. package/dist/services/fields.test.d.ts +1 -0
  68. package/dist/services/graphql/index.js +4 -1
  69. package/dist/services/graphql/utils/process-error.d.ts +4 -0
  70. package/dist/services/graphql/utils/process-error.js +26 -0
  71. package/dist/services/graphql/utils/process-error.test.d.ts +1 -0
  72. package/dist/services/items.d.ts +1 -1
  73. package/dist/services/items.js +39 -13
  74. package/dist/services/mail/index.d.ts +2 -2
  75. package/dist/services/mail/index.js +2 -1
  76. package/dist/services/mail/templates/base.liquid +4 -4
  77. package/dist/services/notifications.js +9 -4
  78. package/dist/services/notifications.test.d.ts +1 -0
  79. package/dist/services/payload.d.ts +2 -2
  80. package/dist/services/payload.js +14 -12
  81. package/dist/services/relations.d.ts +4 -4
  82. package/dist/services/relations.js +66 -8
  83. package/dist/services/users.js +8 -2
  84. package/dist/services/users.test.d.ts +1 -0
  85. package/dist/types/assets.d.ts +7 -7
  86. package/dist/types/ast.d.ts +7 -7
  87. package/dist/types/auth.d.ts +4 -4
  88. package/dist/types/collection.d.ts +2 -2
  89. package/dist/types/events.d.ts +1 -1
  90. package/dist/types/files.d.ts +2 -2
  91. package/dist/types/items.d.ts +5 -5
  92. package/dist/types/migration.d.ts +1 -1
  93. package/dist/types/revision.d.ts +1 -1
  94. package/dist/types/services.d.ts +1 -1
  95. package/dist/types/snapshot.d.ts +4 -4
  96. package/dist/types/webhooks.d.ts +2 -2
  97. package/dist/utils/apply-snapshot.js +32 -13
  98. package/dist/utils/get-ast-from-query.d.ts +1 -1
  99. package/dist/utils/get-column-path.d.ts +2 -2
  100. package/dist/utils/get-module-default.d.ts +1 -1
  101. package/dist/utils/get-relation-info.d.ts +1 -1
  102. package/dist/utils/get-schema.d.ts +6 -2
  103. package/dist/utils/get-schema.js +1 -1
  104. package/dist/utils/get-snapshot.js +1 -1
  105. package/dist/utils/job-queue.d.ts +1 -1
  106. package/dist/utils/merge-permissions.d.ts +1 -0
  107. package/dist/utils/reduce-schema.js +3 -1
  108. package/package.json +69 -80
  109. package/dist/__mocks__/cache.d.ts +0 -5
  110. package/dist/__mocks__/cache.js +0 -7
  111. package/dist/__utils__/items-utils.d.ts +0 -2
  112. package/dist/__utils__/items-utils.js +0 -36
  113. package/dist/__utils__/schemas.d.ts +0 -13
  114. package/dist/__utils__/schemas.js +0 -304
  115. package/dist/__utils__/snapshots.d.ts +0 -5
  116. package/dist/__utils__/snapshots.js +0 -897
  117. package/dist/cli/index.test.js +0 -63
  118. package/dist/controllers/files.test.js +0 -49
  119. package/dist/database/migrations/run.test.js +0 -92
  120. package/dist/env.test.js +0 -40
  121. package/dist/middleware/authenticate.test.js +0 -214
  122. package/dist/middleware/extract-token.test.js +0 -60
  123. package/dist/middleware/validate-batch.test.js +0 -82
  124. package/dist/operations/exec/index.test.js +0 -95
  125. package/dist/services/files.test.js +0 -89
  126. package/dist/services/items.test.js +0 -765
  127. package/dist/services/payload.test.js +0 -196
  128. package/dist/services/specifications.test.js +0 -96
  129. package/dist/utils/apply-snapshot.test.js +0 -305
  130. package/dist/utils/async-handler.test.js +0 -18
  131. package/dist/utils/calculate-field-depth.test.js +0 -76
  132. package/dist/utils/filter-items.test.js +0 -60
  133. package/dist/utils/get-auth-providers.test.js +0 -72
  134. package/dist/utils/get-cache-key.test.js +0 -74
  135. package/dist/utils/get-column-path.test.js +0 -136
  136. package/dist/utils/get-config-from-env.test.js +0 -19
  137. package/dist/utils/get-relation-info.test.js +0 -88
  138. package/dist/utils/get-relation-type.test.js +0 -69
  139. package/dist/utils/get-string-byte-size.test.js +0 -8
  140. package/dist/utils/is-directus-jwt.test.js +0 -26
  141. package/dist/utils/jwt.test.js +0 -36
  142. package/dist/utils/merge-permissions.test.js +0 -80
  143. package/dist/utils/validate-keys.test.js +0 -97
@@ -1,63 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const path_1 = __importDefault(require("path"));
7
- const index_1 = require("./index");
8
- jest.mock('../../src/env', () => ({
9
- ...jest.requireActual('../../src/env').default,
10
- EXTENSIONS_PATH: '',
11
- SERVE_APP: false,
12
- DB_CLIENT: 'pg',
13
- DB_HOST: 'localhost',
14
- DB_PORT: 5432,
15
- DB_DATABASE: 'directus',
16
- DB_USER: 'postgres',
17
- DB_PASSWORD: 'psql1234',
18
- }));
19
- jest.mock('@directus/shared/utils/node/get-extensions', () => ({
20
- getPackageExtensions: jest.fn(() => Promise.resolve([])),
21
- getLocalExtensions: jest.fn(() => Promise.resolve([customCliExtension])),
22
- }));
23
- const customHookPath = path_1.default.resolve('/hooks/custom-cli', 'index.js');
24
- jest.doMock(customHookPath, () => customCliHook, { virtual: true });
25
- const customCliExtension = {
26
- path: `/hooks/custom-cli`,
27
- name: 'custom-cli',
28
- type: 'hook',
29
- entrypoint: 'index.js',
30
- local: true,
31
- };
32
- const beforeHook = jest.fn();
33
- const afterAction = jest.fn();
34
- const afterHook = jest.fn(({ program }) => {
35
- program.command('custom').action(afterAction);
36
- });
37
- const customCliHook = ({ init }) => {
38
- init('cli.before', beforeHook);
39
- init('cli.after', afterHook);
40
- };
41
- const writeOut = jest.fn();
42
- const writeErr = jest.fn();
43
- const setup = async () => {
44
- const program = await (0, index_1.createCli)();
45
- program.exitOverride();
46
- program.configureOutput({ writeOut, writeErr });
47
- return program;
48
- };
49
- beforeEach(jest.clearAllMocks);
50
- describe('cli hooks', () => {
51
- test('should call hooks before and after creating the cli', async () => {
52
- const program = await setup();
53
- expect(beforeHook).toHaveBeenCalledTimes(1);
54
- expect(beforeHook).toHaveBeenCalledWith({ event: 'cli.before', program });
55
- expect(afterHook).toHaveBeenCalledTimes(1);
56
- expect(afterHook).toHaveBeenCalledWith({ event: 'cli.after', program });
57
- });
58
- test('should be able to add a custom cli command', async () => {
59
- const program = await setup();
60
- program.parseAsync(['custom'], { from: 'user' });
61
- expect(afterAction).toHaveBeenCalledTimes(1);
62
- });
63
- });
@@ -1,49 +0,0 @@
1
- "use strict";
2
- // @ts-nocheck
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- jest.mock('../../src/cache');
8
- jest.mock('../../src/database');
9
- jest.mock('../../src/utils/validate-env');
10
- const files_1 = require("./files");
11
- const invalid_payload_1 = require("../exceptions/invalid-payload");
12
- const stream_1 = require("stream");
13
- const form_data_1 = __importDefault(require("form-data"));
14
- describe('multipartHandler', () => {
15
- it(`Errors out if request doesn't contain any files to upload`, () => {
16
- const fakeForm = new form_data_1.default();
17
- fakeForm.append('field', 'test');
18
- const req = {
19
- headers: fakeForm.getHeaders(),
20
- is: jest.fn().mockReturnValue(true),
21
- body: fakeForm.getBuffer(),
22
- params: {},
23
- pipe: (input) => stream.pipe(input),
24
- };
25
- const stream = new stream_1.PassThrough();
26
- stream.push(fakeForm.getBuffer());
27
- (0, files_1.multipartHandler)(req, {}, (err) => {
28
- expect(err.message).toBe('No files where included in the body');
29
- expect(err).toBeInstanceOf(invalid_payload_1.InvalidPayloadException);
30
- });
31
- });
32
- it(`Errors out if uploaded file doesn't include the filename`, () => {
33
- const fakeForm = new form_data_1.default();
34
- fakeForm.append('file', Buffer.from('<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg width="100%" height="100%" viewBox="0 0 243 266" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;"><g id="Calligraphy"><path d="M67.097,135.868c0,3.151 0.598,14.121 -11.586,14.121c-17.076,0 -15.95,-12.947 -15.95,-12.947c0,-2.521 4.597,-5.638 4.597,-7.318c0,-0.63 0.041,-3.519 -2.27,-3.519c-5.671,0 -5.671,10.083 -5.671,10.083c0,0 0.419,15.336 19.116,15.336c20.205,0 30.04,-23.712 30.04,-30.88c0,-18.197 -51.112,-27.701 -51.112,-57.949c0,1.575 -2.205,-13.864 14.18,-13.864c28.358,0 44.426,42.536 44.426,71.524c0,28.988 -16.699,55.455 -16.699,55.455c0,0 33.4,-25.837 33.4,-76.25c0,-70.264 -46.003,-69.634 -46.003,-69.634c-4.792,0 -7.602,-0.241 -28.398,20.555c-20.797,20.797 -17.646,29.83 -17.646,29.83c0,31.93 49.576,32.35 49.576,55.457Z" style="fill-rule:nonzero;"/><path d="M241.886,174.861c-1.602,-9.142 -15.448,-9.916 -22.675,-9.682c-0.7,-0.003 -1.172,0.02 -1.327,0.03c-8.804,0.01 -19.314,4.179 -33.072,13.115c-3.554,2.308 -7.19,4.847 -10.902,7.562c-6.979,-31.39 -13.852,-63.521 -28.033,-63.521c20.415,-20.119 22.19,-16.272 22.19,-39.054c0,-11.244 14.498,-21.35 14.498,-21.35l-0.296,-2.024c-19.193,5.304 -37.307,-8.577 -42.2,-12.755c5.375,-9.663 9.584,-12.565 9.584,-12.565c1.891,-20.377 15.965,-27.31 15.965,-27.31c1.681,-4.201 6.092,-7.142 6.092,-7.142c-70.162,22.267 -54.247,189.298 -54.247,189.298c-0.475,-55.91 5.238,-92.242 11.977,-115.55c9.094,8.248 24.425,11.765 24.425,11.765c-7.396,3.55 -5.324,12.13 -5.324,19.527c0,7.397 -3.848,10.651 -3.848,10.651l-21.893,22.782c17.043,0.294 23.638,31.657 30.689,63.056c-2.548,2.042 -5.125,4.12 -7.728,6.219c-16.396,13.223 -33.351,26.897 -50.266,37.354c-19.086,11.797 -35.151,17.533 -49.116,17.533c-25.25,0 -44.118,-24.368 -44.118,-46.154c0,-9.838 3.227,-17.831 5.935,-22.805c2.935,-5.39 5.911,-8.503 5.967,-8.561c0.001,0 0.001,0 0.001,-0.001l-0.013,-0.012c1.803,-1.885 4.841,-5.181 10.423,-5.181c20.715,0 27.475,40.776 55.603,40.776c24.857,0 31.834,-20.497 37.286,-31.399c0,0 -8.94,11.12 -21.587,11.12c-27.038,0 -35.323,-40.557 -55.166,-40.557c-13.41,0 -22.743,15.506 -31.029,27.281c0,0 0.018,-0.001 0.048,-0.003c-1.02,1.415 -2.214,3.233 -3.41,5.425c-2.847,5.21 -6.239,13.587 -6.239,23.917c0,22.816 19.801,48.334 46.299,48.334c14.381,0 30.822,-5.84 50.262,-17.858c17.033,-10.529 34.04,-24.246 50.489,-37.511c2.309,-1.862 4.607,-3.715 6.891,-5.549c6.952,30.814 14.606,60.912 33.278,60.912c14.794,0 26.923,-25.445 26.923,-25.445c-7.987,7.101 -13.313,5.621 -13.313,5.621c-13.139,0.379 -19.937,-27.594 -26.48,-56.931c16.455,-12.099 31.46,-20.829 43.488,-20.829l0.072,-0.003c0.082,-0.005 5.246,-0.305 9.957,1.471c-2.95,1.636 -4.947,4.782 -4.947,8.394c0,5.299 4.296,9.594 9.594,9.594c5.298,0 9.594,-4.295 9.594,-9.594c0,-0.826 -0.104,-1.627 -0.301,-2.391Z" style="fill-rule:nonzero;"/></g></svg>'));
35
- const req = {
36
- headers: fakeForm.getHeaders(),
37
- is: jest.fn().mockReturnValue(true),
38
- body: fakeForm.getBuffer(),
39
- params: {},
40
- pipe: (input) => stream.pipe(input),
41
- };
42
- const stream = new stream_1.PassThrough();
43
- stream.push(fakeForm.getBuffer());
44
- (0, files_1.multipartHandler)(req, {}, (err) => {
45
- expect(err.message).toBe('File is missing filename');
46
- expect(err).toBeInstanceOf(invalid_payload_1.InvalidPayloadException);
47
- });
48
- });
49
- });
@@ -1,92 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const knex_1 = __importDefault(require("knex"));
7
- const knex_mock_client_1 = require("knex-mock-client");
8
- const run_1 = __importDefault(require("./run"));
9
- describe('run', () => {
10
- let db;
11
- let tracker;
12
- beforeAll(() => {
13
- db = (0, knex_1.default)({ client: knex_mock_client_1.MockClient });
14
- tracker = (0, knex_mock_client_1.getTracker)();
15
- });
16
- afterEach(() => {
17
- tracker.reset();
18
- });
19
- describe('when passed the argument up', () => {
20
- it('returns "Nothing To Upgrade" if no directus_migrations', async () => {
21
- tracker.on.select('directus_migrations').response(['Empty']);
22
- await (0, run_1.default)(db, 'up').catch((e) => {
23
- expect(e).toBeInstanceOf(Error);
24
- expect(e.message).toBe('Nothing to upgrade');
25
- });
26
- });
27
- it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
28
- tracker.on.select('directus_migrations').response([]);
29
- await (0, run_1.default)(db, 'up').catch((e) => {
30
- expect(e).toBeInstanceOf(Error);
31
- expect(e.message).toBe('Method implemented in the dialect driver');
32
- });
33
- });
34
- it('returns undefined if the migration is successful', async () => {
35
- tracker.on.select('directus_migrations').response([
36
- {
37
- version: '20201028A',
38
- name: 'Remove Collection Foreign Keys',
39
- timestamp: '2021-11-27 11:36:56.471595-05',
40
- },
41
- ]);
42
- tracker.on.delete('directus_relations').response([]);
43
- tracker.on.insert('directus_migrations').response(['Remove System Relations', '20201029A']);
44
- expect(await (0, run_1.default)(db, 'up')).toBe(undefined);
45
- });
46
- });
47
- describe('when passed the argument down', () => {
48
- it('returns "Nothing To downgrade" if no valid directus_migrations', async () => {
49
- tracker.on.select('directus_migrations').response(['Empty']);
50
- await (0, run_1.default)(db, 'down').catch((e) => {
51
- expect(e).toBeInstanceOf(Error);
52
- expect(e.message).toBe(`Couldn't find migration`);
53
- });
54
- });
55
- it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
56
- tracker.on.select('directus_migrations').response([]);
57
- await (0, run_1.default)(db, 'down').catch((e) => {
58
- expect(e).toBeInstanceOf(Error);
59
- expect(e.message).toBe('Nothing to downgrade');
60
- });
61
- });
62
- it(`returns "Couldn't find migration" if an invalid migration object is supplied`, async () => {
63
- tracker.on.select('directus_migrations').response([
64
- {
65
- version: '202018129A',
66
- name: 'Fake Migration',
67
- timestamp: '2020-00-32 11:36:56.471595-05',
68
- },
69
- ]);
70
- await (0, run_1.default)(db, 'down').catch((e) => {
71
- expect(e).toBeInstanceOf(Error);
72
- expect(e.message).toBe(`Couldn't find migration`);
73
- });
74
- });
75
- });
76
- describe('when passed the argument latest', () => {
77
- it('returns "Nothing To downgrade" if no valid directus_migrations', async () => {
78
- tracker.on.select('directus_migrations').response(['Empty']);
79
- await (0, run_1.default)(db, 'latest').catch((e) => {
80
- expect(e).toBeInstanceOf(Error);
81
- expect(e.message).toBe(`Method implemented in the dialect driver`);
82
- });
83
- });
84
- it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
85
- tracker.on.select('directus_migrations').response([]);
86
- await (0, run_1.default)(db, 'latest').catch((e) => {
87
- expect(e).toBeInstanceOf(Error);
88
- expect(e.message).toBe('Method implemented in the dialect driver');
89
- });
90
- });
91
- });
92
- });
package/dist/env.test.js DELETED
@@ -1,40 +0,0 @@
1
- "use strict";
2
- const testEnv = {
3
- NUMBER: '1234',
4
- NUMBER_CAST_AS_STRING: 'string:1234',
5
- REGEX: 'regex:\\.example\\.com$',
6
- CSV: 'one,two,three,four',
7
- CSV_CAST_AS_STRING: 'string:one,two,three,four',
8
- MULTIPLE: 'array:string:https://example.com,regex:\\.example2\\.com$',
9
- };
10
- describe('env processed values', () => {
11
- const originalEnv = process.env;
12
- let env;
13
- beforeEach(() => {
14
- jest.resetModules();
15
- process.env = { ...testEnv };
16
- env = jest.requireActual('../src/env').default;
17
- });
18
- afterEach(() => {
19
- process.env = originalEnv;
20
- jest.resetAllMocks();
21
- });
22
- test('Number value should be a number', () => {
23
- expect(env.NUMBER).toStrictEqual(1234);
24
- });
25
- test('Number value casted as string should be a string', () => {
26
- expect(env.NUMBER_CAST_AS_STRING).toStrictEqual('1234');
27
- });
28
- test('Value casted as regex', () => {
29
- expect(env.REGEX).toBeInstanceOf(RegExp);
30
- });
31
- test('CSV value should be an array', () => {
32
- expect(env.CSV).toStrictEqual(['one', 'two', 'three', 'four']);
33
- });
34
- test('CSV value casted as string should be a string', () => {
35
- expect(env.CSV_CAST_AS_STRING).toStrictEqual('one,two,three,four');
36
- });
37
- test('Multiple type cast', () => {
38
- expect(env.MULTIPLE).toStrictEqual(['https://example.com', /\.example2\.com$/]);
39
- });
40
- });
@@ -1,214 +0,0 @@
1
- "use strict";
2
- // @ts-nocheck
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
- const database_1 = __importDefault(require("../database"));
9
- const emitter_1 = __importDefault(require("../emitter"));
10
- const env_1 = __importDefault(require("../env"));
11
- const exceptions_1 = require("../exceptions");
12
- const authenticate_1 = require("./authenticate");
13
- require("../../src/types/express.d.ts");
14
- jest.mock('../../src/database');
15
- jest.mock('../../src/env', () => ({
16
- SECRET: 'test',
17
- }));
18
- afterEach(() => {
19
- jest.resetAllMocks();
20
- });
21
- test('Short-circuits when authenticate filter is used', async () => {
22
- const req = {
23
- ip: '127.0.0.1',
24
- get: jest.fn(),
25
- };
26
- const res = {};
27
- const next = jest.fn();
28
- const customAccountability = { admin: true };
29
- jest.spyOn(emitter_1.default, 'emitFilter').mockResolvedValue(customAccountability);
30
- await (0, authenticate_1.handler)(req, res, next);
31
- expect(req.accountability).toEqual(customAccountability);
32
- expect(next).toHaveBeenCalledTimes(1);
33
- });
34
- test('Uses default public accountability when no token is given', async () => {
35
- const req = {
36
- ip: '127.0.0.1',
37
- get: jest.fn((string) => {
38
- switch (string) {
39
- case 'user-agent':
40
- return 'fake-user-agent';
41
- case 'origin':
42
- return 'fake-origin';
43
- default:
44
- return null;
45
- }
46
- }),
47
- };
48
- const res = {};
49
- const next = jest.fn();
50
- jest.spyOn(emitter_1.default, 'emitFilter').mockImplementation((_, payload) => payload);
51
- await (0, authenticate_1.handler)(req, res, next);
52
- expect(req.accountability).toEqual({
53
- user: null,
54
- role: null,
55
- admin: false,
56
- app: false,
57
- ip: '127.0.0.1',
58
- userAgent: 'fake-user-agent',
59
- origin: 'fake-origin',
60
- });
61
- expect(next).toHaveBeenCalledTimes(1);
62
- });
63
- test('Sets accountability to payload contents if valid token is passed', async () => {
64
- const userID = '3fac3c02-607f-4438-8d6e-6b8b25109b52';
65
- const roleID = '38269fc6-6eb6-475a-93cb-479d97f73039';
66
- const share = 'ca0ad005-f4ad-4bfe-b428-419ee8784790';
67
- const shareScope = {
68
- collection: 'articles',
69
- item: 15,
70
- };
71
- const appAccess = true;
72
- const adminAccess = false;
73
- const token = jsonwebtoken_1.default.sign({
74
- id: userID,
75
- role: roleID,
76
- app_access: appAccess,
77
- admin_access: adminAccess,
78
- share,
79
- share_scope: shareScope,
80
- }, env_1.default.SECRET, { issuer: 'directus' });
81
- const req = {
82
- ip: '127.0.0.1',
83
- get: jest.fn((string) => {
84
- switch (string) {
85
- case 'user-agent':
86
- return 'fake-user-agent';
87
- case 'origin':
88
- return 'fake-origin';
89
- default:
90
- return null;
91
- }
92
- }),
93
- token,
94
- };
95
- const res = {};
96
- const next = jest.fn();
97
- await (0, authenticate_1.handler)(req, res, next);
98
- expect(req.accountability).toEqual({
99
- user: userID,
100
- role: roleID,
101
- app: appAccess,
102
- admin: adminAccess,
103
- share,
104
- share_scope: shareScope,
105
- ip: '127.0.0.1',
106
- userAgent: 'fake-user-agent',
107
- origin: 'fake-origin',
108
- });
109
- expect(next).toHaveBeenCalledTimes(1);
110
- // Test with 1/0 instead or true/false
111
- next.mockClear();
112
- req.token = jsonwebtoken_1.default.sign({
113
- id: userID,
114
- role: roleID,
115
- app_access: 1,
116
- admin_access: 0,
117
- share,
118
- share_scope: shareScope,
119
- }, env_1.default.SECRET, { issuer: 'directus' });
120
- await (0, authenticate_1.handler)(req, res, next);
121
- expect(req.accountability).toEqual({
122
- user: userID,
123
- role: roleID,
124
- app: appAccess,
125
- admin: adminAccess,
126
- share,
127
- share_scope: shareScope,
128
- ip: '127.0.0.1',
129
- userAgent: 'fake-user-agent',
130
- origin: 'fake-origin',
131
- });
132
- expect(next).toHaveBeenCalledTimes(1);
133
- });
134
- test('Throws InvalidCredentialsException when static token is used, but user does not exist', async () => {
135
- jest.mocked(database_1.default).mockReturnValue({
136
- select: jest.fn().mockReturnThis(),
137
- from: jest.fn().mockReturnThis(),
138
- leftJoin: jest.fn().mockReturnThis(),
139
- where: jest.fn().mockReturnThis(),
140
- first: jest.fn().mockResolvedValue(undefined),
141
- });
142
- const req = {
143
- ip: '127.0.0.1',
144
- get: jest.fn((string) => {
145
- switch (string) {
146
- case 'user-agent':
147
- return 'fake-user-agent';
148
- case 'origin':
149
- return 'fake-origin';
150
- default:
151
- return null;
152
- }
153
- }),
154
- token: 'static-token',
155
- };
156
- const res = {};
157
- const next = jest.fn();
158
- expect((0, authenticate_1.handler)(req, res, next)).rejects.toEqual(new exceptions_1.InvalidCredentialsException());
159
- expect(next).toHaveBeenCalledTimes(0);
160
- });
161
- test('Sets accountability to user information when static token is used', async () => {
162
- const req = {
163
- ip: '127.0.0.1',
164
- get: jest.fn((string) => {
165
- switch (string) {
166
- case 'user-agent':
167
- return 'fake-user-agent';
168
- case 'origin':
169
- return 'fake-origin';
170
- default:
171
- return null;
172
- }
173
- }),
174
- token: 'static-token',
175
- };
176
- const res = {};
177
- const next = jest.fn();
178
- const testUser = { id: 'test-id', role: 'test-role', admin_access: true, app_access: false };
179
- const expectedAccountability = {
180
- user: testUser.id,
181
- role: testUser.role,
182
- app: testUser.app_access,
183
- admin: testUser.admin_access,
184
- ip: '127.0.0.1',
185
- userAgent: 'fake-user-agent',
186
- origin: 'fake-origin',
187
- };
188
- jest.mocked(database_1.default).mockReturnValue({
189
- select: jest.fn().mockReturnThis(),
190
- from: jest.fn().mockReturnThis(),
191
- leftJoin: jest.fn().mockReturnThis(),
192
- where: jest.fn().mockReturnThis(),
193
- first: jest.fn().mockResolvedValue(testUser),
194
- });
195
- await (0, authenticate_1.handler)(req, res, next);
196
- expect(req.accountability).toEqual(expectedAccountability);
197
- expect(next).toHaveBeenCalledTimes(1);
198
- // Test for 0 / 1 instead of false / true
199
- next.mockClear();
200
- testUser.admin_access = 1;
201
- testUser.app_access = 0;
202
- await (0, authenticate_1.handler)(req, res, next);
203
- expect(req.accountability).toEqual(expectedAccountability);
204
- expect(next).toHaveBeenCalledTimes(1);
205
- // Test for "1" / "0" instead of true / false
206
- next.mockClear();
207
- testUser.admin_access = '0';
208
- testUser.app_access = '1';
209
- expectedAccountability.admin = false;
210
- expectedAccountability.app = true;
211
- await (0, authenticate_1.handler)(req, res, next);
212
- expect(req.accountability).toEqual(expectedAccountability);
213
- expect(next).toHaveBeenCalledTimes(1);
214
- });
@@ -1,60 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const extract_token_1 = __importDefault(require("../../src/middleware/extract-token"));
7
- require("../../src/types/express.d.ts");
8
- let mockRequest;
9
- let mockResponse;
10
- const nextFunction = jest.fn();
11
- beforeEach(() => {
12
- mockRequest = {};
13
- mockResponse = {};
14
- jest.clearAllMocks();
15
- });
16
- test('Token from query', () => {
17
- mockRequest = {
18
- query: {
19
- access_token: 'test',
20
- },
21
- };
22
- (0, extract_token_1.default)(mockRequest, mockResponse, nextFunction);
23
- expect(mockRequest.token).toBe('test');
24
- expect(nextFunction).toBeCalledTimes(1);
25
- });
26
- test('Token from Authorization header (capitalized)', () => {
27
- mockRequest = {
28
- headers: {
29
- authorization: 'Bearer test',
30
- },
31
- };
32
- (0, extract_token_1.default)(mockRequest, mockResponse, nextFunction);
33
- expect(mockRequest.token).toBe('test');
34
- expect(nextFunction).toBeCalledTimes(1);
35
- });
36
- test('Token from Authorization header (lowercase)', () => {
37
- mockRequest = {
38
- headers: {
39
- authorization: 'bearer test',
40
- },
41
- };
42
- (0, extract_token_1.default)(mockRequest, mockResponse, nextFunction);
43
- expect(mockRequest.token).toBe('test');
44
- expect(nextFunction).toBeCalledTimes(1);
45
- });
46
- test('Ignore the token if authorization header is too many parts', () => {
47
- mockRequest = {
48
- headers: {
49
- authorization: 'bearer test what another one',
50
- },
51
- };
52
- (0, extract_token_1.default)(mockRequest, mockResponse, nextFunction);
53
- expect(mockRequest.token).toBeNull();
54
- expect(nextFunction).toBeCalledTimes(1);
55
- });
56
- test('Null if no token passed', () => {
57
- (0, extract_token_1.default)(mockRequest, mockResponse, nextFunction);
58
- expect(mockRequest.token).toBeNull();
59
- expect(nextFunction).toBeCalledTimes(1);
60
- });
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const validate_batch_1 = require("./validate-batch");
4
- require("../../src/types/express.d.ts");
5
- const exceptions_1 = require("../exceptions");
6
- const exceptions_2 = require("@directus/shared/exceptions");
7
- let mockRequest;
8
- let mockResponse;
9
- const nextFunction = jest.fn();
10
- beforeEach(() => {
11
- mockRequest = {};
12
- mockResponse = {};
13
- jest.clearAllMocks();
14
- });
15
- test('Sets body to empty, calls next on GET requests', async () => {
16
- mockRequest.method = 'GET';
17
- await (0, validate_batch_1.validateBatch)('read')(mockRequest, mockResponse, nextFunction);
18
- expect(mockRequest.body).toEqual({});
19
- expect(nextFunction).toHaveBeenCalledTimes(1);
20
- });
21
- test(`Short circuits on singletons that aren't queried through SEARCH`, async () => {
22
- mockRequest.method = 'PATCH';
23
- mockRequest.singleton = true;
24
- mockRequest.body = { title: 'test' };
25
- await (0, validate_batch_1.validateBatch)('update')(mockRequest, mockResponse, nextFunction);
26
- expect(nextFunction).toHaveBeenCalledTimes(1);
27
- });
28
- test('Throws InvalidPayloadException on missing body', async () => {
29
- mockRequest.method = 'SEARCH';
30
- await (0, validate_batch_1.validateBatch)('read')(mockRequest, mockResponse, nextFunction);
31
- expect(nextFunction).toHaveBeenCalledTimes(1);
32
- expect(jest.mocked(nextFunction).mock.calls[0][0]).toBeInstanceOf(exceptions_1.InvalidPayloadException);
33
- });
34
- test(`Short circuits on Array body in update/delete use`, async () => {
35
- mockRequest.method = 'PATCH';
36
- mockRequest.body = [1, 2, 3];
37
- await (0, validate_batch_1.validateBatch)('update')(mockRequest, mockResponse, nextFunction);
38
- expect(mockRequest.sanitizedQuery).toBe(undefined);
39
- expect(nextFunction).toHaveBeenCalled();
40
- });
41
- test(`Sets sanitizedQuery based on body.query in read operations`, async () => {
42
- mockRequest.method = 'SEARCH';
43
- mockRequest.body = {
44
- query: {
45
- sort: 'id',
46
- },
47
- };
48
- await (0, validate_batch_1.validateBatch)('read')(mockRequest, mockResponse, nextFunction);
49
- expect(mockRequest.sanitizedQuery).toEqual({
50
- sort: ['id'],
51
- });
52
- });
53
- test(`Doesn't allow both query and keys in a batch delete`, async () => {
54
- mockRequest.method = 'DELETE';
55
- mockRequest.body = {
56
- keys: [1, 2, 3],
57
- query: { filter: {} },
58
- };
59
- await (0, validate_batch_1.validateBatch)('delete')(mockRequest, mockResponse, nextFunction);
60
- expect(nextFunction).toHaveBeenCalledTimes(1);
61
- expect(jest.mocked(nextFunction).mock.calls[0][0]).toBeInstanceOf(exceptions_2.FailedValidationException);
62
- });
63
- test(`Requires 'data' on batch update`, async () => {
64
- mockRequest.method = 'PATCH';
65
- mockRequest.body = {
66
- keys: [1, 2, 3],
67
- query: { filter: {} },
68
- };
69
- await (0, validate_batch_1.validateBatch)('update')(mockRequest, mockResponse, nextFunction);
70
- expect(nextFunction).toHaveBeenCalledTimes(1);
71
- expect(jest.mocked(nextFunction).mock.calls[0][0]).toBeInstanceOf(exceptions_2.FailedValidationException);
72
- });
73
- test(`Calls next when all is well`, async () => {
74
- mockRequest.method = 'PATCH';
75
- mockRequest.body = {
76
- query: { filter: {} },
77
- data: {},
78
- };
79
- await (0, validate_batch_1.validateBatch)('update')(mockRequest, mockResponse, nextFunction);
80
- expect(nextFunction).toHaveBeenCalledTimes(1);
81
- expect(jest.mocked(nextFunction).mock.calls[0][0]).toBeUndefined();
82
- });