forked from duddud11/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-loading.test.ts
More file actions
142 lines (121 loc) · 5.49 KB
/
content-loading.test.ts
File metadata and controls
142 lines (121 loc) · 5.49 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
import { parse, validate } from 'graphql';
import getConfig from 'next/config';
import { getAllContent } from './content-loading';
import { getGraphQLSchema, getRESTSchema } from './fusionfeed';
const { publicRuntimeConfig } = getConfig();
describe('getAllContent', () => {
it('returns some content', async () => {
const allContent = await getAllContent();
expect(Array.from(allContent.keys()).length).toBeGreaterThan(1);
});
it('finds some graphql', async () => {
const allContent = await getAllContent();
let count = 0;
for (const content of Array.from(allContent.values())) {
count += content.graphql.length;
}
expect(count).toBeGreaterThan(0);
});
it('finds some links', async () => {
const allContent = await getAllContent();
let count = 0;
for (const content of Array.from(allContent.values())) {
count += content.links.size;
}
expect(count).toBeGreaterThan(0);
});
});
describe('graphql', () => {
it('validates', async () => {
const auth = process.env.FUSION_FEED_AUTHORIZATION || '';
expect(auth, 'To perform validation, FUSION_FEED_AUTHORIZATION must be defined.').toBeTruthy();
const v1Schema = await getGraphQLSchema('v1', auth);
const v2Schema = await getGraphQLSchema('v2', auth);
const allContent = await getAllContent();
for (const content of Array.from(allContent.values())) {
for (const graphql of content.graphql) {
const doc = parse(graphql.document);
const schema = graphql.version === 'v1' ? v1Schema : v2Schema;
expect(validate(schema, doc)).toHaveLength(0);
}
}
});
});
describe('rest', () => {
it('validates', async () => {
const auth = process.env.FUSION_FEED_AUTHORIZATION || '';
expect(auth, 'To perform validation, FUSION_FEED_AUTHORIZATION must be defined.').toBeTruthy();
const v1 = await getRESTSchema('v1', auth);
const v2 = await getRESTSchema('v2', auth);
const allContent = await getAllContent();
for (const content of Array.from(allContent.values())) {
for (const rest of content.rest) {
const reqURL = new URL(publicRuntimeConfig.fusionFeedUrl + rest.url);
const parts = reqURL.pathname.split('/');
const version = parts[1];
const path = decodeURI('/' + parts.slice(2).join('/'));
expect(version, `Invalid version in REST request "${rest.url}"`).toMatch(/v[12]/);
let schema = v2;
if (version === 'v1') {
schema = v1;
}
let found = false;
for (const [def, pathSchema] of Object.entries(version === 'v1' ? schema.paths : schema.paths)) {
if (path === def) {
found = true;
let schema = pathSchema.get;
if (rest.method === 'POST') {
schema = pathSchema.post;
}
// check to see that request contains all required parameters
for (const param of schema.parameters) {
if (param.in === 'query' && param.required && !reqURL.searchParams.has(param.name)) {
throw new Error(
`Missing required parameter ${param.name} in REST request "${rest.url}"`,
);
}
}
// check to see that request contains no unknown parameters
for (const param of Array.from(reqURL.searchParams)) {
if (!schema.parameters.find((p) => p.name === param[0])) {
throw new Error(`Unknown parameter ${param[0]} in REST request "${rest.url}"`);
}
}
// check that the body is valid JSON
if (rest.body) {
try {
let json = JSON.parse(rest.body);
} catch (e) {
throw new Error(`Invalid JSON body in REST request "${rest.url}": ${e}`);
}
}
break;
}
}
expect(found, `Invalid path in REST request "${rest.url}"`).toBeTruthy();
}
}
});
});
describe('links', () => {
it('validate', async () => {
const allContent = await getAllContent();
for (const content of Array.from(allContent.values())) {
for (const link of Array.from(content.links)) {
if (link.startsWith('mailto:')) {
// mailto
continue;
} else if (link.indexOf('://') > 0 || link.indexOf('//') === 0) {
// external url
const resp = await fetch(link);
expect(resp.status).toBe(200);
} else {
// content url
// all links should have been made absolute
expect(link[0]).toBe('/');
expect(allContent.get(link), `Invalid link in ${content.path}: ${link}`).toBeDefined();
}
}
}
});
});