From f5385e38bfe07b88616e0ea435f75b57bdc9aff1 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 16 Nov 2017 15:38:26 -0700 Subject: [PATCH] Add `/me*` route which redirects to `/user/[userslug]*` (#6063) * Add `/me*` route which redirects to the current user's information - `/me` -> `/user/[usertslug]` - `/me/bookmarks` -> `/user/[userslug]/bookmarks` - `/me/settings` -> `/user/[userslug]/settings` etc * Add tests for `/me/*` --- src/middleware/user.js | 16 ++++++++++++++++ src/routes/accounts.js | 3 ++- test/authentication.js | 3 ++- test/controllers.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/middleware/user.js b/src/middleware/user.js index b80b91fea3..3c59bd8923 100644 --- a/src/middleware/user.js +++ b/src/middleware/user.js @@ -140,6 +140,22 @@ module.exports = function (middleware) { ], next); }; + middleware.redirectMeToUserslug = function (req, res, next) { + var uid = req.uid; + async.waterfall([ + function (next) { + user.getUserField(uid, 'userslug', next); + }, + function (userslug) { + if (!userslug) { + return res.status(401).send('not-authorized'); + } + var path = req.path.replace(/^(\/api)?\/me/, '/user/' + userslug); + controllers.helpers.redirect(res, path); + }, + ], next); + }; + middleware.isAdmin = function (req, res, next) { async.waterfall([ function (next) { diff --git a/src/routes/accounts.js b/src/routes/accounts.js index 336a38e442..5c040b6af1 100644 --- a/src/routes/accounts.js +++ b/src/routes/accounts.js @@ -7,7 +7,8 @@ module.exports = function (app, middleware, controllers) { var middlewares = [middleware.checkGlobalPrivacySettings]; var accountMiddlewares = [middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions]; - setupPageRoute(app, '/uid/:uid/:section1?/:section2?', middleware, [], middleware.redirectUidToUserslug); + setupPageRoute(app, '/me/*', middleware, [], middleware.redirectMeToUserslug); + setupPageRoute(app, '/uid/:uid*', middleware, [], middleware.redirectUidToUserslug); setupPageRoute(app, '/user/:userslug', middleware, middlewares, controllers.accounts.profile.get); setupPageRoute(app, '/user/:userslug/following', middleware, middlewares, controllers.accounts.follow.getFollowing); diff --git a/test/authentication.js b/test/authentication.js index 6dd781710c..bacbcb679a 100644 --- a/test/authentication.js +++ b/test/authentication.js @@ -186,8 +186,9 @@ describe('authentication', function () { url: nconf.get('url') + '/api/me', json: true, jar: jar, - }, function (err, response, body) { + }, function (err, res, body) { assert.ifError(err); + assert.equal(res.statusCode, 401); assert.equal(body, 'not-authorized'); done(); }); diff --git a/test/controllers.js b/test/controllers.js index 15d2be3765..b5f0c40b3f 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -937,6 +937,35 @@ describe('Controllers', function () { }); }); + describe('/me/*', function () { + it('api should redirect to /user/[userslug]/bookmarks', function (done) { + request(nconf.get('url') + '/api/me/bookmarks', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert.equal(res.headers['x-redirect'], '/user/foo/bookmarks'); + assert.equal(body, '/user/foo/bookmarks'); + done(); + }); + }); + it('api should redirect to /user/[userslug]/edit/username', function (done) { + request(nconf.get('url') + '/api/me/edit/username', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert.equal(res.headers['x-redirect'], '/user/foo/edit/username'); + assert.equal(body, '/user/foo/edit/username'); + done(); + }); + }); + it('should 401 if user is not logged in', function (done) { + request(nconf.get('url') + '/me/bookmarks', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 401); + assert.equal(body, 'not-authorized'); + done(); + }); + }); + }); + it('should 401 if user is not logged in', function (done) { request(nconf.get('url') + '/api/admin', { json: true }, function (err, res) { assert.ifError(err);