-
-
Notifications
You must be signed in to change notification settings - Fork 10k
/
api.test.js
263 lines (212 loc) · 10.4 KB
/
api.test.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const should = require('should');
const sinon = require('sinon');
const path = require('path');
const fs = require('fs-extra');
const logging = require('@tryghost/logging');
const CustomRedirectsAPI = require('../../../../../core/server/services/custom-redirects/CustomRedirectsAPI');
describe('UNIT: redirects CustomRedirectsAPI class', function () {
let customRedirectsAPI;
let redirectManager;
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
before(function () {
customRedirectsAPI = new CustomRedirectsAPI({
basePath,
redirectManager,
getBackupFilePath: () => path.join(basePath, 'backup.json'),
validate: () => {}
});
});
beforeEach(function () {
redirectManager = {
removeAllRedirects: sinon.stub(),
addRedirect: sinon.stub()
};
sinon.stub(fs, 'pathExists');
sinon.stub(fs, 'writeFile');
sinon.stub(fs, 'readFile');
sinon.stub(fs, 'unlink');
sinon.stub(fs, 'move');
sinon.stub(fs, 'copy');
sinon.spy(logging, 'error');
});
afterEach(function () {
sinon.restore();
});
describe('init', function () {
it('initializes without errors when redirects file is not present', async function () {
customRedirectsAPI = new CustomRedirectsAPI({
basePath,
redirectManager,
getBackupFilePath: {},
validate: () => {}
});
await customRedirectsAPI.init();
logging.error.called.should.be.false();
});
});
describe('get', function () {
it('returns empty array if file does not exist', async function () {
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
const file = await customRedirectsAPI.get();
should.deepEqual(file, []);
});
it('returns a redirects YAML file if it exists', async function () {
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(true);
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
fs.readFile.withArgs(`${basePath}redirects.yaml`, 'utf-8').resolves('yaml content');
fs.readFile.withArgs(`${basePath}redirects.json`, 'utf-8').resolves(null);
const file = await customRedirectsAPI.get();
should.deepEqual(file, 'yaml content');
});
it('returns a redirects JSON file if YAML does not exists', async function () {
const redirectJSONFixture = [{
from: '^/post/[0-9]+/([a-z0-9\\-]+)',
to: '/$1'
}];
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(true);
fs.readFile.withArgs(`${basePath}redirects.yaml`, 'utf-8').resolves(null);
fs.readFile.withArgs(`${basePath}redirects.json`, 'utf-8').resolves(JSON.stringify(redirectJSONFixture));
const file = await customRedirectsAPI.get();
should.deepEqual(file, redirectJSONFixture);
});
});
describe('setFromFilePath', function () {
it('throws a syntax error when setting invalid JSON redirects file', async function () {
const invalidJSON = '{invalid json';
const invalidFilePath = path.join(__dirname, '/invalid/redirects/path.json');
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves(invalidJSON);
let expectedErrorMessage;
try {
JSON.parse(invalidJSON);
} catch (err) {
expectedErrorMessage = err.message;
}
if (!expectedErrorMessage) {
// This should never happen because the JSON is invalid
should.fail('expectedErrorMessage is not set');
}
try {
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.json');
should.fail('setFromFilePath did not throw');
} catch (err) {
should.exist(err);
err.message.should.eql(`Could not parse JSON: ${expectedErrorMessage}.`);
}
});
it('throws a syntax error when setting invalid (plain string) YAML redirects file', async function () {
const invalidFilePath = path.join(__dirname, '/invalid/redirects/yaml.json');
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves('x');
try {
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.yaml');
should.fail('setFromFilePath did not throw');
} catch (err) {
should.exist(err);
err.message.should.eql('YAML input is invalid. Check the contents of your YAML file.');
}
});
it('throws a syntax error when setting invalid (empty) YAML redirects file', async function () {
const invalidFilePath = path.join(__dirname, '/invalid/redirects/yaml.json');
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves('');
try {
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.yaml');
should.fail('setFromFilePath did not throw');
} catch (err) {
should.exist(err);
err.message.should.eql('YAML input is invalid. Check the contents of your YAML file.');
}
});
it('throws bad request error when the YAML file is invalid', async function () {
const invalidFilePath = path.join(__dirname, '/invalid/redirects/yaml.json');
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves(`
routes:
\
invalid yaml:
/
`);
try {
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.yaml');
should.fail('setFromFilePath did not throw');
} catch (err) {
should.exist(err);
err.errorType.should.eql('BadRequestError');
err.message.should.match(/Could not parse YAML: can not read an implicit mapping pair/);
}
});
it('creates a backup file from existing redirects.json file', async function () {
const incomingFilePath = path.join(__dirname, '/valid/path/redirects_incoming.json');
const existingRedirectsFilePath = `${basePath}redirects.json`;
const backupFilePath = path.join(basePath, 'backup.json');
const redirectsJSONConfig = JSON.stringify([{
from: 'e',
to: 'b'
}]);
// redirects.json file already exits
fs.pathExists.withArgs(existingRedirectsFilePath).resolves(true);
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
// incoming redirects file
fs.readFile.withArgs(incomingFilePath, 'utf-8').resolves(redirectsJSONConfig);
// backup file already exists
fs.pathExists.withArgs(backupFilePath).resolves(true);
fs.unlink.withArgs(backupFilePath).resolves(true);
fs.move.withArgs(incomingFilePath, backupFilePath).resolves(true);
customRedirectsAPI = new CustomRedirectsAPI({
basePath,
redirectManager,
getBackupFilePath: () => backupFilePath,
validate: () => {}
});
await customRedirectsAPI.setFromFilePath(incomingFilePath, '.json');
// backed up file with the same name already exists so remove it
fs.unlink.called.should.be.true();
fs.unlink.calledWith(backupFilePath).should.be.true();
// backed up current routes file
fs.move.called.should.be.true();
fs.move.calledWith(existingRedirectsFilePath, backupFilePath).should.be.true();
// written new routes file
fs.writeFile.calledWith(existingRedirectsFilePath, redirectsJSONConfig, 'utf-8').should.be.true();
// redirects have been re-registered
redirectManager.removeAllRedirects.calledOnce.should.be.true();
// one redirect in total
redirectManager.addRedirect.calledOnce.should.be.true();
});
it('creates a backup file from existing redirects.yaml file', async function () {
const incomingFilePath = path.join(__dirname, '/valid/path/redirects_incoming.yaml');
const backupFilePath = path.join(basePath, 'backup.yaml');
const redirectsYamlConfig = `
301:
/my-old-blog-post/: /revamped-url/
302:
/another-old-blog-post/: /hello-there/
`;
// redirects.json file already exits
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(true);
// incoming redirects file
fs.readFile.withArgs(incomingFilePath, 'utf-8').resolves(redirectsYamlConfig);
// backup file DOES not exists yet
fs.pathExists.withArgs(backupFilePath).resolves(false);
// should not be called
fs.unlink.withArgs(backupFilePath).resolves(false);
fs.move.withArgs(`${basePath}redirects.yaml`, backupFilePath).resolves(true);
customRedirectsAPI = new CustomRedirectsAPI({
basePath,
redirectManager,
getBackupFilePath: () => backupFilePath,
validate: () => {}
});
await customRedirectsAPI.setFromFilePath(incomingFilePath, '.yaml');
// no existing backup file name match, did not remove any files
fs.unlink.called.should.not.be.true();
// backed up current routes file
fs.move.called.should.be.true();
// overwritten with incoming routes.yaml file
fs.copy.calledWith(incomingFilePath, `${basePath}redirects.yaml`).should.be.true();
// redirects have been re-registered
redirectManager.removeAllRedirects.calledOnce.should.be.true();
// two redirects in total
redirectManager.addRedirect.calledTwice.should.be.true();
});
});
});