From 4b46d85c262a51abf87eea81f5f2a39526be403e Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Thu, 1 May 2014 01:50:24 +0000 Subject: [PATCH] Redirect from admin editor to frontend post view closes #2628 - added /view/ route to the editor. if /view/ is appended to the url of a post being edited a redirect to the frontend will occur - updated controller to check for /view/ and built the correct url for the post - added test for the new route --- core/server/controllers/admin.js | 17 ++++- core/server/routes/admin.js | 3 +- core/test/functional/routes/admin_test.js | 86 +++++++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/core/server/controllers/admin.js b/core/server/controllers/admin.js index dfe2a9ed10..d7babb3f59 100644 --- a/core/server/controllers/admin.js +++ b/core/server/controllers/admin.js @@ -69,10 +69,23 @@ adminControllers = { }); }, // Route: editor - // Path: /ghost/editor(/:id)?/ + // Path: /ghost/editor(/:id)?(/:action)?/ // Method: GET 'editor': function (req, res) { - if (req.params.id !== undefined) { + if (req.params.id && req.params.action) { + if (req.params.action !== 'view') { + return errors.error404(req, res); + } + + api.posts.read({ id: req.params.id }).then(function (result) { + return config.urlForPost(api.settings, result.posts[0]).then(function (url) { + return res.redirect(url); + }); + }, function () { + return errors.error404(req, res); + }); + + } else if (req.params.id !== undefined) { res.render('editor', { bodyClass: 'editor', adminNav: setSelected(adminNavbar, 'content') diff --git a/core/server/routes/admin.js b/core/server/routes/admin.js index bb49e19a89..ead6cc3065 100644 --- a/core/server/routes/admin.js +++ b/core/server/routes/admin.js @@ -40,7 +40,8 @@ module.exports = function (server) { server.post('/ghost/reset/:token', admin.doReset); server.post('/ghost/changepw/', admin.doChangePassword); - server.get('/ghost/editor(/:id)/', admin.editor); + server.get('/ghost/editor/:id/:action', admin.editor); + server.get('/ghost/editor/:id/', admin.editor); server.get('/ghost/editor/', admin.editor); server.get('/ghost/content/', admin.content); server.get('/ghost/settings*', admin.settings); diff --git a/core/test/functional/routes/admin_test.js b/core/test/functional/routes/admin_test.js index 5c437ba7e6..82efeef4f9 100644 --- a/core/test/functional/routes/admin_test.js +++ b/core/test/functional/routes/admin_test.js @@ -13,6 +13,7 @@ var request = require('supertest'), testUtils = require('../../utils'), ghost = require('../../../../core'), httpServer, + agent = request.agent, ONE_HOUR_S = 60 * 60, ONE_YEAR_S = 365 * 24 * ONE_HOUR_S, @@ -283,3 +284,88 @@ describe('Admin Routing', function () { }); }); }); + +describe('Authenticated Admin Routing', function () { + var user = testUtils.DataGenerator.forModel.users[0], + csrfToken = ''; + + before(function (done) { + var app = express(); + + ghost({app: app}).then(function (_httpServer) { + httpServer = _httpServer; + request = agent(app); + + testUtils.clearData() + .then(function () { + return testUtils.initData(); + }) + .then(function () { + return testUtils.insertDefaultFixtures(); + }) + .then(function () { + + request.get('/ghost/signin/') + .expect(200) + .end(function (err, res) { + if (err) { + return done(err); + } + + var pattern_meta = //i; + pattern_meta.should.exist; + csrfToken = res.text.match(pattern_meta)[1]; + + process.nextTick(function() { + request.post('/ghost/signin/') + .set('X-CSRF-Token', csrfToken) + .send({email: user.email, password: user.password}) + .expect(200) + .end(function (err, res) { + if (err) { + return done(err); + } + + request.saveCookies(res); + request.get('/ghost/') + .expect(200) + .end(function (err, res) { + if (err) { + return done(err); + } + + csrfToken = res.text.match(pattern_meta)[1]; + done(); + }); + }); + + }); + + }); + }, done); + }).otherwise(function (e) { + console.log('Ghost Error: ', e); + console.log(e.stack); + }); + }); + + after(function () { + httpServer.close(); + }); + + describe('Ghost Admin magic /view/ route', function () { + + it('should redirect to the single post page on the frontend', function (done) { + request.get('/ghost/editor/1/view/') + .expect(302) + .expect('Location', '/welcome-to-ghost/') + .end(function (err, res) { + if (err) { + return done(err); + } + + done(); + }); + }); + }); +});