-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathediting-config-middleware.test.ts
159 lines (130 loc) · 5.17 KB
/
editing-config-middleware.test.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* eslint-disable no-unused-expressions */
import { NextApiRequest, NextApiResponse } from 'next';
import { spy } from 'sinon';
import { expect } from 'chai';
import { EditingConfigMiddleware } from './editing-config-middleware';
import { QUERY_PARAM_EDITING_SECRET } from './constants';
import { EditMode } from '@sitecore-jss/sitecore-jss/layout';
type Query = {
[key: string]: string;
};
const allowedOrigin = 'https://allowed.com';
const mockRequest = (method: string, query?: Query, headers?: { [key: string]: string }) => {
return {
method,
query: query ?? {},
headers: {
origin: allowedOrigin,
...headers,
},
} as NextApiRequest;
};
const mockResponse = () => {
const res = {} as NextApiResponse;
res.status = spy(() => {
return res;
});
res.json = spy(() => {
return res;
});
res.setHeader = spy(() => {
return res;
});
res.getHeader = spy(() => {
return undefined;
});
return res;
};
const componentsArray = ['TestComponentOne', 'TestComponentTwo'];
const componentsMap = new Map<string, unknown>();
componentsMap.set('TestComponentOne', {});
componentsMap.set('TestComponentTwo', {});
const metadata = { packages: { testPackageOne: '0.1.1' } };
const expectedResultWithChromes = {
components: ['TestComponentOne', 'TestComponentTwo'],
packages: { testPackageOne: '0.1.1' },
editMode: 'chromes',
};
const expectedResultWithMetadata = {
components: ['TestComponentOne', 'TestComponentTwo'],
packages: { testPackageOne: '0.1.1' },
editMode: 'metadata',
};
const expectedResultForbidden = { message: 'Missing or invalid editing secret' };
describe('EditingConfigMiddleware', () => {
const secret = 'jss-editing-secret-mock';
beforeEach(() => {
process.env.JSS_EDITING_SECRET = secret;
process.env.JSS_ALLOWED_ORIGINS = allowedOrigin;
});
after(() => {
delete process.env.JSS_EDITING_SECRET;
delete process.env.JSS_ALLOWED_ORIGINS;
});
it('should respond with 401 for missing secret', async () => {
const key = 'wrongkey';
const query = { key } as Query;
const req = mockRequest('GET', query);
const res = mockResponse();
const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const handler = middleware.getHandler();
await handler(req, res);
expect(res.status).to.have.been.calledWith(401);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResultForbidden);
});
it('should stop request and return 401 when CORS match is not met', async () => {
const req = mockRequest('GET', {}, { origin: 'https://notallowed.com' });
const res = mockResponse();
const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const handler = middleware.getHandler();
await handler(req, res);
expect(res.status).to.have.been.calledOnce;
expect(res.status).to.have.been.calledWith(401);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith({ message: 'Invalid origin' });
});
it('should respond with 401 for invalid secret', async () => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = 'wrongsekret';
const req = mockRequest('GET', query);
const res = mockResponse();
const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const handler = middleware.getHandler();
await handler(req, res);
expect(res.status).to.have.been.calledWith(401);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResultForbidden);
});
const testEditingConfig = async (
components: string[] | Map<string, unknown>,
expectedResult,
pagesEditMode?: EditMode
) => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = secret;
const req = mockRequest('GET', query);
const res = mockResponse();
const middleware = new EditingConfigMiddleware({ components, metadata, pagesEditMode });
const handler = middleware.getHandler();
await handler(req, res);
expect(res.status).to.have.been.calledOnce;
expect(res.status).to.have.been.calledWith(200);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResult);
};
it('should respond with 200 and return config data with components array as argument and editMode as chromes', async () => {
await testEditingConfig(componentsArray, expectedResultWithChromes, EditMode.Chromes);
});
it('should respond with 200 and return config data with components map as argument and editMode as chromes', async () => {
await testEditingConfig(componentsMap, expectedResultWithChromes, EditMode.Chromes);
});
it('should respond with 200 and return config data with components array as argument and editMode as metadata', async () => {
await testEditingConfig(componentsArray, expectedResultWithMetadata);
});
it('should respond with 200 and return config data with components map as argument and editMode as metadata', async () => {
await testEditingConfig(componentsMap, expectedResultWithMetadata);
});
});