Server: allow user to get its informations (/users/me)

pull/10/head
Chocobozzz 2016-08-05 17:19:08 +02:00
parent 6606150c49
commit 99a64bfed2
4 changed files with 62 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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