directus

Форк
0
/
sanitize-gql-schema.test.ts 
134 строки · 4.5 Кб
1
import type { CollectionsOverview, SchemaOverview } from '@directus/types';
2
import { describe, expect, test } from 'vitest';
3
import { sanitizeGraphqlSchema } from './sanitize-gql-schema.js';
4

5
const mockCollection = (name: string, fields: string[] = []) =>
6
	({
7
		collection: name,
8
		primary: 'id',
9
		singleton: false,
10
		sortField: null,
11
		note: null,
12
		accountability: null,
13
		fields: Object.fromEntries([['id', null], ...fields.map((f) => [f, null])]),
14
	}) as CollectionsOverview[string];
15

16
describe('Sanitize graphql schema', () => {
17
	test('Filters out invalid names', () => {
18
		const testSchema: SchemaOverview = {
19
			collections: {
20
				normal_collection: mockCollection('normal_collection'),
21
				'123table': mockCollection('123table'),
22
				__underscore: mockCollection('__underscore'),
23
				'a-dash': mockCollection('a-dash'),
24
				Schrödinger: mockCollection('Schrödinger'),
25
				'': mockCollection(''),
26
			},
27
			relations: [],
28
		};
29

30
		const result = sanitizeGraphqlSchema(testSchema);
31

32
		expect(result.collections['normal_collection']).toEqual(testSchema.collections['normal_collection']);
33
		expect(result.collections['123table']).toBeUndefined();
34
		expect(result.collections['__underscore']).toBeUndefined();
35
		expect(result.collections['a-dash']).toBeUndefined();
36
		expect(result.collections['Schrödinger']).toBeUndefined();
37
		expect(result.collections['']).toBeUndefined();
38
	});
39

40
	test('Filters out reserved names', () => {
41
		const testSchema: SchemaOverview = {
42
			collections: {
43
				Subscription: mockCollection('Subscription'),
44
				subscription: mockCollection('subscription'),
45
				Mutation: mockCollection('Mutation'),
46
				mutation: mockCollection('mutation'),
47
				String: mockCollection('String'),
48
				string: mockCollection('string'),
49
			},
50
			relations: [],
51
		};
52

53
		const result = sanitizeGraphqlSchema(testSchema);
54

55
		expect(result.collections['Subscription']).toBeUndefined();
56
		expect(result.collections['subscription']).toEqual(testSchema.collections['subscription']);
57
		expect(result.collections['Mutation']).toBeUndefined();
58
		expect(result.collections['mutation']).toEqual(testSchema.collections['mutation']);
59
		expect(result.collections['String']).toBeUndefined();
60
		expect(result.collections['string']).toEqual(testSchema.collections['string']);
61
	});
62

63
	test('Keeps valid names', () => {
64
		const testSchema: SchemaOverview = {
65
			collections: {
66
				normal: mockCollection('normal'),
67
				Normal: mockCollection('Normal'),
68
				NORMAL: mockCollection('NORMAL'),
69
				t3st_numb3rs: mockCollection('t3st_numb3rs'),
70
				_underscore: mockCollection('_underscore'),
71
			},
72
			relations: [],
73
		};
74

75
		const result = sanitizeGraphqlSchema(testSchema);
76

77
		expect(result.collections['normal']).toEqual(testSchema.collections['normal']);
78
		expect(result.collections['Normal']).toEqual(testSchema.collections['Normal']);
79
		expect(result.collections['NORMAL']).toEqual(testSchema.collections['NORMAL']);
80
		expect(result.collections['t3st_numb3rs']).toEqual(testSchema.collections['t3st_numb3rs']);
81
		expect(result.collections['_underscore']).toEqual(testSchema.collections['_underscore']);
82
	});
83

84
	test('Filters out invalid relations', () => {
85
		const testSchema: SchemaOverview = {
86
			collections: {
87
				normal_collection: mockCollection('normal_collection'),
88
				junction_table: mockCollection('junction_table'),
89
				__invalid_collection: mockCollection('__invalid_collection'),
90
			},
91
			relations: [
92
				{
93
					collection: 'normal_collection',
94
					field: 'test',
95
					related_collection: '__invalid_collection',
96
					meta: null,
97
					schema: null,
98
				},
99
				{
100
					collection: '__invalid_collection',
101
					field: 'test',
102
					related_collection: 'normal_collection',
103
					meta: null,
104
					schema: null,
105
				},
106
				{
107
					collection: 'normal_collection',
108
					field: 'test',
109
					related_collection: null,
110
					meta: {
111
						id: 1,
112
						many_collection: 'junction_table',
113
						many_field: 'item',
114
						one_collection: null,
115
						one_field: null,
116
						one_collection_field: 'collection',
117
						one_allowed_collections: ['__invalid_collection'],
118
						one_deselect_action: 'delete',
119
						junction_field: 'invalid_id',
120
						sort_field: null,
121
					},
122
					schema: null,
123
				},
124
			],
125
		};
126

127
		const result = sanitizeGraphqlSchema(testSchema);
128

129
		expect(result.collections['normal_collection']).toEqual(testSchema.collections['normal_collection']);
130
		expect(result.collections['junction_table']).toEqual(testSchema.collections['junction_table']);
131
		expect(result.collections['__invalid_collection']).toBeUndefined();
132
		expect(result.relations.length).toBe(0);
133
	});
134
});
135

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.