/
default_routes.test.js
510 lines (446 loc) · 21.6 KB
/
default_routes.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// # Default Frontend Routing Test
// These tests check the default out-of-the-box behavior of Ghost is working as expected.
// Test Structure
// As it stands, these tests depend on the database, and as such are integration tests.
// Mocking out the models to not touch the DB would turn these into unit tests, and should probably be done in future,
// But then again testing real code, rather than mock code, might be more useful...
const should = require('should');
const sinon = require('sinon');
const supertest = require('supertest');
const moment = require('moment');
const cheerio = require('cheerio');
const _ = require('lodash');
const testUtils = require('../utils');
const configUtils = require('../utils/configUtils');
const settingsCache = require('../../core/shared/settings-cache');
const origCache = _.cloneDeep(settingsCache);
function assertCorrectFrontendHeaders(res) {
should.not.exist(res.headers['x-cache-invalidate']);
should.not.exist(res.headers['X-CSRF-Token']);
should.not.exist(res.headers['set-cookie']);
should.exist(res.headers.date);
}
describe('Default Frontend routing', function () {
let request;
afterEach(function () {
sinon.restore();
});
before(async function () {
await testUtils.startGhost();
request = supertest.agent(configUtils.config.get('url'));
});
describe('Error', function () {
it('should 404 for unknown post', async function () {
await request.get('/spectacular/')
.expect('Cache-Control', testUtils.cacheRules.noCache)
.expect(404)
.expect(/Page not found/)
.expect(assertCorrectFrontendHeaders);
});
it('should 404 for unknown file', async function () {
await request.get('/content/images/some/file/that/doesnt-exist.jpg')
.expect('Cache-Control', testUtils.cacheRules.noCache)
.expect(404)
.expect(/Image not found/)
.expect(assertCorrectFrontendHeaders);
});
});
describe('Main Routes', function () {
it('/ should respond with valid HTML', async function () {
await request.get('/')
.expect('Content-Type', /html/)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect(200)
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
const $ = cheerio.load(res.text);
// NOTE: "Ghost" is the title from the settings.
$('title').text().should.equal('Ghost');
$('body.home-template').length.should.equal(1);
$('article.post').length.should.equal(7);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('/author/ghost/ should respond with valid HTML', async function () {
await request.get('/author/ghost/')
.expect('Content-Type', /html/)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect(200)
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
const $ = cheerio.load(res.text);
// NOTE: "Ghost" is the title from the settings.
$('title').text().should.equal('Ghost - Ghost');
$('body.author-template').length.should.equal(1);
$('article.post').length.should.equal(7);
$('article.tag-getting-started').length.should.equal(7);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('/tag/getting-started/ should respond with valid HTML', async function () {
await request.get('/tag/getting-started/')
.expect('Content-Type', /html/)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect(200)
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
const $ = cheerio.load(res.text);
// NOTE: "Ghost" is the title from the settings.
$('title').text().should.equal('Getting Started - Ghost');
$('body.tag-template').length.should.equal(1);
$('article.post').length.should.equal(7);
$('article.tag-getting-started').length.should.equal(7);
res.text.should.not.containEql('__GHOST_URL__');
});
});
});
describe('Single post', function () {
it('/welcome/ should respond with valid HTML', async function () {
await request.get('/welcome/')
.expect('Content-Type', /html/)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect(200)
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
// Test that head and body have rendered something...
res.text.should.containEql('<title>Start here for a quick overview of everything you need to know</title>');
res.text.should.match(/<h1[^>]*?>Start here for a quick overview of everything you need to know<\/h1>/);
// We should write a single test for this, or encapsulate it as an assertion
// E.g. res.text.should.not.containInvalidUrls()
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should not work with date permalinks', async function () {
// get today's date
const date = moment().format('YYYY/MM/DD');
await request.get('/' + date + '/welcome/')
.expect('Cache-Control', testUtils.cacheRules.noCache)
.expect(404)
.expect(/Page not found/)
.expect(assertCorrectFrontendHeaders);
});
});
describe('Post edit', function () {
it('should redirect to editor', async function () {
await request.get('/welcome/edit/')
.expect('Location', /ghost\/#\/editor\/\w+/)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect(302)
.expect(assertCorrectFrontendHeaders);
});
it('should 404 for non-edit parameter', async function () {
await request.get('/welcome/notedit/')
.expect('Cache-Control', testUtils.cacheRules.noCache)
.expect(404)
.expect(/Page not found/)
.expect(assertCorrectFrontendHeaders);
});
describe('Admin Redirects Disabled', function () {
before(async function () {
configUtils.set('admin:redirects', false);
await testUtils.startGhost({forceStart: true});
request = supertest.agent(configUtils.config.get('url'));
});
after(async function () {
await configUtils.restore();
await testUtils.startGhost({forceStart: true});
request = supertest.agent(configUtils.config.get('url'));
});
it('/edit/ should NOT redirect to the editor', async function () {
await request.get('/welcome/edit/')
.expect('Cache-Control', testUtils.cacheRules.noCache)
.expect(404)
.expect(assertCorrectFrontendHeaders);
});
});
});
describe('AMP post', function () {
describe('AMP Enabled', function () {
beforeEach(function () {
sinon.stub(settingsCache, 'get').callsFake(function (key, options) {
if (key === 'amp' && !options) {
return true;
}
return origCache.get(key, options);
});
});
it('should respond with html for valid url', async function () {
await request.get('/welcome/amp/')
.expect('Content-Type', /html/)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect(200)
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
const $ = cheerio.load(res.text);
$('.post-title').text().should.equal('Start here for a quick overview of everything you need to know');
$('.content .post').length.should.equal(1);
$('.powered').text().should.equal(' Published with Ghost');
$('body.amp-template').length.should.equal(1);
$('article.post').length.should.equal(1);
$('style[amp-custom]').length.should.equal(1);
// This asserts we should have some content (and not [object Promise] !)
$('.post-content p').length.should.be.greaterThan(0);
res.text.should.containEql(':root {--ghost-accent-color: #FF1A75;}');
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should not work with date permalinks', async function () {
// get today's date
const date = moment().format('YYYY/MM/DD');
await request.get('/' + date + '/welcome/amp/')
.expect('Cache-Control', testUtils.cacheRules.noCache)
.expect(404)
.expect(/Page not found/)
.expect(assertCorrectFrontendHeaders);
});
});
describe('AMP Disabled', function () {
beforeEach(function () {
sinon.stub(settingsCache, 'get').callsFake(function (key, options) {
if (key === 'amp' && !options) {
return false;
}
return origCache.get(key, options);
});
});
it('/amp/ should redirect to regular post, including any query params', async function () {
await request.get('/welcome/amp/?q=a')
.expect('Location', '/welcome/?q=a')
.expect(301)
.expect(assertCorrectFrontendHeaders);
});
});
});
describe('RSS', function () {
it('should 301 redirect with CC=1year without slash', function () {
request.get('/rss')
.expect('Location', '/rss/')
.expect('Cache-Control', testUtils.cacheRules.year)
.expect(301)
.expect(assertCorrectFrontendHeaders);
});
it('should get 301 redirect with CC=1year to /rss/ from /feed/', function () {
request.get('/feed/')
.expect('Location', '/rss/')
.expect('Cache-Control', testUtils.cacheRules.year)
.expect(301)
.expect(assertCorrectFrontendHeaders);
});
it('/rss/ should serve an RSS feed', async function () {
await request.get('/rss/')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/<!\[CDATA\[Start here for a quick overview of everything you need to know\]\]>/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('/author/ghost/rss/ should serve an RSS feed', async function () {
await request.get('/author/ghost/rss/')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/<!\[CDATA\[Start here for a quick overview of everything you need to know\]\]>/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('/tag/getting-started/rss/ should serve an RSS feed', async function () {
await request.get('/tag/getting-started/rss/')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.public)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/<!\[CDATA\[Start here for a quick overview of everything you need to know\]\]>/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
});
describe('Static assets', function () {
it('should retrieve theme assets', async function () {
await request.get('/assets/built/screen.css')
.expect('Cache-Control', testUtils.cacheRules.year)
.expect(200)
.expect(assertCorrectFrontendHeaders);
});
it('should retrieve default robots.txt', async function () {
const res = await request.get('/robots.txt')
.expect('Cache-Control', testUtils.cacheRules.hour)
.expect('ETag', /[0-9a-f]{32}/i)
.expect(200)
.expect(assertCorrectFrontendHeaders);
res.text.should.equal(
'User-agent: *\n' +
'Sitemap: http://127.0.0.1:2369/sitemap.xml\nDisallow: /ghost/\n' +
'Disallow: /email/\n' +
'Disallow: /members/api/comments/counts/\n' +
'Disallow: /r/\n' +
'Disallow: /webmentions/receive/\n'
);
});
it('should retrieve default favicon.ico', async function () {
await request.get('/favicon.ico')
.expect('Cache-Control', testUtils.cacheRules.day)
.expect('ETag', /[0-9a-f]{32}/i)
.expect(200)
.expect(assertCorrectFrontendHeaders);
});
});
describe('Site Map', function () {
before(async function () {
await testUtils.clearData();
await testUtils.initData();
await testUtils.initFixtures('posts');
});
it('should serve sitemap.xml', async function () {
await request.get('/sitemap.xml')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.hour)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/sitemapindex/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should serve sitemap-posts.xml', async function () {
await request.get('/sitemap-posts.xml')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.hour)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/urlset/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should serve sitemap-pages.xml', async function () {
await request.get('/sitemap-pages.xml')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.hour)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/urlset/);
// CASE: the index page should always be present in pages sitemap
res.text.should.containEql('<loc>http://127.0.0.1:2369/</loc>');
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should serve sitemap-tags.xml', async function () {
await request.get('/sitemap-tags.xml')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.hour)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/urlset/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should serve sitemap-users.xml', async function () {
await request.get('/sitemap-users.xml')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.hour)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/urlset/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
it('should serve sitemap.xsl', async function () {
await request.get('/sitemap.xsl')
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.day)
.expect('Content-Type', 'text/xsl')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/urlset/);
res.text.should.not.containEql('__GHOST_URL__');
});
});
});
describe('Private Blogging', function () {
beforeEach(function () {
sinon.stub(settingsCache, 'get').callsFake(function (key, options) {
if (key === 'is_private') {
return true;
}
return origCache.get(key, options);
});
});
it('/ should redirect to /private/', async function () {
await request.get('/')
.expect('Location', '/private/?r=%2F')
.expect(302)
.expect(assertCorrectFrontendHeaders);
});
it('/welcome/ should redirect to /private/', async function () {
await request.get('/welcome/')
.expect('Location', '/private/?r=%2Fwelcome%2F')
.expect(302)
.expect(assertCorrectFrontendHeaders);
});
it('/private/?r=%2Fwelcome%2F should not redirect', async function () {
await request.get('/private/?r=%2Fwelcome%2F')
.expect(200)
.expect(assertCorrectFrontendHeaders);
});
it('should redirect, NOT 404 for private route with extra path', async function () {
await request.get('/private/welcome/')
.expect('Location', '/private/?r=%2Fprivate%2Fwelcome%2F')
.expect(302)
.expect(assertCorrectFrontendHeaders);
});
it('should still serve private RSS feed', async function () {
await request.get(`/${settingsCache.get('public_hash')}/rss/`)
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/<!\[CDATA\[Start here for a quick overview of everything you need to know\]\]>/);
});
});
it('should still serve private tag RSS feed', async function () {
await request.get(`/tag/getting-started/${settingsCache.get('public_hash')}/rss/`)
.expect(200)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match(/<!\[CDATA\[Start here for a quick overview of everything you need to know\]\]>/);
});
});
it('should redirect, NOT 404 for private tag RSS feed with extra path', async function () {
await request.get(`/tag/getting-started/${settingsCache.get('public_hash')}/rss/hack/`)
.expect('Location', `/private/?r=%2Ftag%2Fgetting-started%2F${settingsCache.get('public_hash')}%2Frss%2Fhack%2F`)
.expect(302)
.expect(assertCorrectFrontendHeaders);
});
// NOTE: this case is covered by extra error handling, and cannot be unit tested
it('should redirect, NOT 404 for unknown private RSS feed', async function () {
// NOTE: the redirect will be to /hack/rss because we strip the hash from the URL before trying to serve RSS
// This isn't ideal, but it's better to expose this internal logic than it is a 404 page
await request.get(`/hack/${settingsCache.get('public_hash')}/rss/`)
.expect('Location', '/private/?r=%2Fhack%2Frss%2F')
.expect(302)
.expect(assertCorrectFrontendHeaders);
});
// NOTE: this test extends the unit test, checking that there is no other robots.txt middleware overriding private blogging
it('should serve private robots.txt', async function () {
await request.get('/robots.txt')
.expect('Cache-Control', 'public, max-age=3600')
.expect(200)
.expect(assertCorrectFrontendHeaders)
.expect((res) => {
res.text.should.match('User-agent: *\nDisallow: /');
});
});
});
});