From 99a64bfed25e45547df3045cf249bc895e6f220b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Aug 2016 17:19:08 +0200 Subject: Server: allow user to get its informations (/users/me) --- server/controllers/api/v1/users.js | 9 +++++++++ server/tests/api/checkParams.js | 34 +++++++++++++++++++++++++++------- server/tests/api/users.js | 13 +++++++++++++ server/tests/api/utils.js | 13 +++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index fdbcc3ff5..d831a0de6 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -19,6 +19,7 @@ const Video = mongoose.model('Video') const router = express.Router() router.get('/', listUsers) +router.get('/me', oAuth.authenticate, getUserInformation) router.post('/', oAuth.authenticate, @@ -63,6 +64,14 @@ function createUser (req, res, next) { }) } +function getUserInformation (req, res, next) { + User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { + if (err) return next(err) + + return res.json(user.toFormatedJSON()) + }) +} + function listUsers (req, res, next) { User.list(function (err, usersList) { if (err) return next(err) diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js index bd7227e9c..8b49f5f36 100644 --- a/server/tests/api/checkParams.js +++ b/server/tests/api/checkParams.js @@ -504,6 +504,8 @@ describe('Test parameters validator', function () { describe('Of the users API', function () { const path = '/api/v1/users/' + let userId = null + let userAccessToken = null describe('When adding a new user', function () { it('Should fail with a too small username', function (done) { @@ -580,19 +582,19 @@ describe('Test parameters validator', function () { utils.loginAndGetAccessToken(server, function (err, accessToken) { if (err) throw err + userAccessToken = accessToken + const data = { username: 'user2', password: 'my super password' } - makePostBodyRequest(path, accessToken, data, done, 403) + makePostBodyRequest(path, userAccessToken, data, done, 403) }) }) }) describe('When updating a user', function () { - let userId = null - before(function (done) { utils.getUsersList(server.url, function (err, res) { if (err) throw err @@ -607,7 +609,7 @@ describe('Test parameters validator', function () { password: 'bla' } - makePutBodyRequest(path + '/' + userId, server.accessToken, data, done) + makePutBodyRequest(path + userId, userAccessToken, data, done) }) it('Should fail with a too long password', function (done) { @@ -617,7 +619,7 @@ describe('Test parameters validator', function () { 'very very very very very very very very very very very very very very very very very very very very long' } - makePutBodyRequest(path + '/' + userId, server.accessToken, data, done) + makePutBodyRequest(path + userId, userAccessToken, data, done) }) it('Should fail with an non authenticated user', function (done) { @@ -625,7 +627,7 @@ describe('Test parameters validator', function () { password: 'my super password' } - makePutBodyRequest(path + '/' + userId, 'super token', data, done, 401) + makePutBodyRequest(path + userId, 'super token', data, done, 401) }) it('Should succeed with the correct params', function (done) { @@ -633,7 +635,25 @@ describe('Test parameters validator', function () { password: 'my super password' } - makePutBodyRequest(path + '/' + userId, server.accessToken, data, done, 204) + makePutBodyRequest(path + userId, userAccessToken, data, done, 204) + }) + }) + + describe('When getting my information', function () { + it('Should fail with a non authenticated user', function (done) { + request(server.url) + .get(path + 'me') + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should success with the correct parameters', function (done) { + request(server.url) + .get(path + 'me') + .set('Authorization', 'Bearer ' + userAccessToken) + .set('Accept', 'application/json') + .expect(200, done) }) }) diff --git a/server/tests/api/users.js b/server/tests/api/users.js index c711d6b64..e1d4a8cf4 100644 --- a/server/tests/api/users.js +++ b/server/tests/api/users.js @@ -179,6 +179,19 @@ describe('Test users', function () { }) }) + it('Should be able to get the user informations', function (done) { + utils.getUserInformation(server.url, accessTokenUser, function (err, res) { + if (err) throw err + + const user = res.body + + expect(user.username).to.equal('user_1') + expect(user.id).to.exist + + done() + }) + }) + it('Should be able to upload a video with this user', function (done) { this.timeout(5000) diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js index f34b81e4a..0dc309328 100644 --- a/server/tests/api/utils.js +++ b/server/tests/api/utils.js @@ -14,6 +14,7 @@ const testUtils = { getAllVideosListBy: getAllVideosListBy, getClient: getClient, getFriendsList: getFriendsList, + getUserInformation: getUserInformation, getUsersList: getUsersList, getVideo: getVideo, getVideosList: getVideosList, @@ -93,6 +94,18 @@ function getClient (url, end) { .end(end) } +function getUserInformation (url, accessToken, end) { + const path = '/api/v1/users/me' + + request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + function getUsersList (url, end) { const path = '/api/v1/users' -- cgit v1.2.3