aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-05 17:19:08 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-05 17:19:08 +0200
commit99a64bfed25e45547df3045cf249bc895e6f220b (patch)
tree0e8150155d616ee25eac41196bdb6cf7413b376c /server
parent6606150c49f587bc7eb0ecec4263ce7fbb18bf15 (diff)
downloadPeerTube-99a64bfed25e45547df3045cf249bc895e6f220b.tar.gz
PeerTube-99a64bfed25e45547df3045cf249bc895e6f220b.tar.zst
PeerTube-99a64bfed25e45547df3045cf249bc895e6f220b.zip
Server: allow user to get its informations (/users/me)
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/v1/users.js9
-rw-r--r--server/tests/api/checkParams.js34
-rw-r--r--server/tests/api/users.js13
-rw-r--r--server/tests/api/utils.js13
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')
19const router = express.Router() 19const router = express.Router()
20 20
21router.get('/', listUsers) 21router.get('/', listUsers)
22router.get('/me', oAuth.authenticate, getUserInformation)
22 23
23router.post('/', 24router.post('/',
24 oAuth.authenticate, 25 oAuth.authenticate,
@@ -63,6 +64,14 @@ function createUser (req, res, next) {
63 }) 64 })
64} 65}
65 66
67function getUserInformation (req, res, next) {
68 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
69 if (err) return next(err)
70
71 return res.json(user.toFormatedJSON())
72 })
73}
74
66function listUsers (req, res, next) { 75function listUsers (req, res, next) {
67 User.list(function (err, usersList) { 76 User.list(function (err, usersList) {
68 if (err) return next(err) 77 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 () {
504 504
505 describe('Of the users API', function () { 505 describe('Of the users API', function () {
506 const path = '/api/v1/users/' 506 const path = '/api/v1/users/'
507 let userId = null
508 let userAccessToken = null
507 509
508 describe('When adding a new user', function () { 510 describe('When adding a new user', function () {
509 it('Should fail with a too small username', function (done) { 511 it('Should fail with a too small username', function (done) {
@@ -580,19 +582,19 @@ describe('Test parameters validator', function () {
580 utils.loginAndGetAccessToken(server, function (err, accessToken) { 582 utils.loginAndGetAccessToken(server, function (err, accessToken) {
581 if (err) throw err 583 if (err) throw err
582 584
585 userAccessToken = accessToken
586
583 const data = { 587 const data = {
584 username: 'user2', 588 username: 'user2',
585 password: 'my super password' 589 password: 'my super password'
586 } 590 }
587 591
588 makePostBodyRequest(path, accessToken, data, done, 403) 592 makePostBodyRequest(path, userAccessToken, data, done, 403)
589 }) 593 })
590 }) 594 })
591 }) 595 })
592 596
593 describe('When updating a user', function () { 597 describe('When updating a user', function () {
594 let userId = null
595
596 before(function (done) { 598 before(function (done) {
597 utils.getUsersList(server.url, function (err, res) { 599 utils.getUsersList(server.url, function (err, res) {
598 if (err) throw err 600 if (err) throw err
@@ -607,7 +609,7 @@ describe('Test parameters validator', function () {
607 password: 'bla' 609 password: 'bla'
608 } 610 }
609 611
610 makePutBodyRequest(path + '/' + userId, server.accessToken, data, done) 612 makePutBodyRequest(path + userId, userAccessToken, data, done)
611 }) 613 })
612 614
613 it('Should fail with a too long password', function (done) { 615 it('Should fail with a too long password', function (done) {
@@ -617,7 +619,7 @@ describe('Test parameters validator', function () {
617 'very very very very very very very very very very very very very very very very very very very very long' 619 'very very very very very very very very very very very very very very very very very very very very long'
618 } 620 }
619 621
620 makePutBodyRequest(path + '/' + userId, server.accessToken, data, done) 622 makePutBodyRequest(path + userId, userAccessToken, data, done)
621 }) 623 })
622 624
623 it('Should fail with an non authenticated user', function (done) { 625 it('Should fail with an non authenticated user', function (done) {
@@ -625,7 +627,7 @@ describe('Test parameters validator', function () {
625 password: 'my super password' 627 password: 'my super password'
626 } 628 }
627 629
628 makePutBodyRequest(path + '/' + userId, 'super token', data, done, 401) 630 makePutBodyRequest(path + userId, 'super token', data, done, 401)
629 }) 631 })
630 632
631 it('Should succeed with the correct params', function (done) { 633 it('Should succeed with the correct params', function (done) {
@@ -633,7 +635,25 @@ describe('Test parameters validator', function () {
633 password: 'my super password' 635 password: 'my super password'
634 } 636 }
635 637
636 makePutBodyRequest(path + '/' + userId, server.accessToken, data, done, 204) 638 makePutBodyRequest(path + userId, userAccessToken, data, done, 204)
639 })
640 })
641
642 describe('When getting my information', function () {
643 it('Should fail with a non authenticated user', function (done) {
644 request(server.url)
645 .get(path + 'me')
646 .set('Authorization', 'Bearer faketoken')
647 .set('Accept', 'application/json')
648 .expect(401, done)
649 })
650
651 it('Should success with the correct parameters', function (done) {
652 request(server.url)
653 .get(path + 'me')
654 .set('Authorization', 'Bearer ' + userAccessToken)
655 .set('Accept', 'application/json')
656 .expect(200, done)
637 }) 657 })
638 }) 658 })
639 659
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 () {
179 }) 179 })
180 }) 180 })
181 181
182 it('Should be able to get the user informations', function (done) {
183 utils.getUserInformation(server.url, accessTokenUser, function (err, res) {
184 if (err) throw err
185
186 const user = res.body
187
188 expect(user.username).to.equal('user_1')
189 expect(user.id).to.exist
190
191 done()
192 })
193 })
194
182 it('Should be able to upload a video with this user', function (done) { 195 it('Should be able to upload a video with this user', function (done) {
183 this.timeout(5000) 196 this.timeout(5000)
184 197
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 = {
14 getAllVideosListBy: getAllVideosListBy, 14 getAllVideosListBy: getAllVideosListBy,
15 getClient: getClient, 15 getClient: getClient,
16 getFriendsList: getFriendsList, 16 getFriendsList: getFriendsList,
17 getUserInformation: getUserInformation,
17 getUsersList: getUsersList, 18 getUsersList: getUsersList,
18 getVideo: getVideo, 19 getVideo: getVideo,
19 getVideosList: getVideosList, 20 getVideosList: getVideosList,
@@ -93,6 +94,18 @@ function getClient (url, end) {
93 .end(end) 94 .end(end)
94} 95}
95 96
97function getUserInformation (url, accessToken, end) {
98 const path = '/api/v1/users/me'
99
100 request(url)
101 .get(path)
102 .set('Accept', 'application/json')
103 .set('Authorization', 'Bearer ' + accessToken)
104 .expect(200)
105 .expect('Content-Type', /json/)
106 .end(end)
107}
108
96function getUsersList (url, end) { 109function getUsersList (url, end) {
97 const path = '/api/v1/users' 110 const path = '/api/v1/users'
98 111