// # Dynamic Routing Tests // As it stands, these tests depend on the database, and as such are integration tests. // These tests are here to cover the headers sent with requests and high-level redirects that can't be // tested with the unit tests const should = require('should'), supertest = require('supertest'), sinon = require('sinon'), moment = require('moment'), path = require('path'), testUtils = require('../utils'), cheerio = require('cheerio'), config = require('../../server/config'), api = require('../../server/api'), settingsCache = require('../../server/services/settings/cache'), ghost = testUtils.startGhost; let request; describe('Dynamic Routing', function () { let ghostServer; function doEnd(done) { return function (err, res) { if (err) { return done(err); } 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); done(); }; } before(function () { // Default is always casper. We use the old compatible 1.4 casper theme for these tests. Available in the test content folder. var originalSettingsCacheGetFn = settingsCache.get; sinon.stub(settingsCache, 'get').callsFake(function (key, options) { if (key === 'active_theme') { return 'casper-1.4'; } return originalSettingsCacheGetFn(key, options); }); return ghost() .then(function (_ghostServer) { ghostServer = _ghostServer; request = supertest.agent(config.get('url')); }); }); after(function () { sinon.restore(); }); describe('Collection Index', function () { it('should respond with html', function (done) { request.get('/') .expect('Content-Type', /html/) .expect('Cache-Control', testUtils.cacheRules.public) .expect(200) .end(function (err, res) { if (err) { return done(err); } var $ = cheerio.load(res.text); 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); $('title').text().should.equal('Ghost'); $('.content .post').length.should.equal(5); $('.poweredby').text().should.equal('Proudly published with Ghost'); $('body.home-template').length.should.equal(1); $('article.post').length.should.equal(5); $('article.tag-getting-started').length.should.equal(5); done(); }); }); it('should not have a third page', function (done) { request.get('/page/3/') .expect('Cache-Control', testUtils.cacheRules.private) .expect(404) .expect(/Page not found/) .end(doEnd(done)); }); describe('RSS', function () { before(testUtils.teardown); before(function (done) { testUtils.initData().then(function () { return testUtils.fixtures.overrideOwnerUser(); }).then(function () { done(); }); }); after(testUtils.teardown); it('should 301 redirect with CC=1year without slash', function (done) { request.get('/rss') .expect('Location', '/rss/') .expect('Cache-Control', testUtils.cacheRules.year) .expect(301) .end(doEnd(done)); }); it('should respond with 200 & CC=public', function (done) { request.get('/rss/') .expect('Content-Type', 'text/xml; charset=utf-8') .expect('Cache-Control', testUtils.cacheRules.public) .expect(200) .end(function (err, res) { if (err) { return done(err); } 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); // The remainder of the XML is tested in the unit/xml_spec.js res.text.should.match(/^<\?xml version="1.0" encoding="UTF-8"\?> { return testUtils.integrationTesting.urlService.waitTillFinished({dbIsReady: true}); }); }); it('serve welcome post with old permalink structure', function (done) { request.get('/welcome/') .expect(404) .end(function (err) { if (err) { return done(err); } done(); }); }); it('serve welcome post with new permalink structure', function (done) { const year = moment().year(); request.get(`/blog/${year}/welcome/`) .expect(200) .end(function (err) { if (err) { return done(err); } done(); }); }); it('serve welcome post with new permalink structure and old date', function (done) { request.get('/blog/2016/welcome/') .expect(301) .end(function (err) { if (err) { return done(err); } done(); }); }); it('serve serve rss', function (done) { request.get('/blog/rss/') .expect(200) .end(function (err, res) { if (err) { return done(err); } const content = res.text; const todayMoment = moment(); const year = todayMoment.format('YYYY'); const postLink = `/blog/${year}/welcome/`; content.indexOf(postLink).should.be.above(0); done(); }); }); it('serve collection index', function (done) { request.get('/blog/') .expect(200) .end(function (err, res) { if (err) { return done(err); } done(); }); }); it('serve tag', function (done) { request.get('/category/getting-started/') .expect(200) .end(function (err, res) { if (err) { return done(err); } done(); }); }); }); });