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
This commit is contained in:
Jason Williams 2014-05-01 01:50:24 +00:00
parent b192dd9303
commit 4b46d85c26
3 changed files with 103 additions and 3 deletions

View File

@ -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')

View File

@ -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);

View File

@ -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 = /<meta.*?name="csrf-param".*?content="(.*?)".*?>/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();
});
});
});
});