const router = express.Router()
router.get('/', listUsers)
+router.get('/me', oAuth.authenticate, getUserInformation)
router.post('/',
oAuth.authenticate,
})
}
+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)
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) {
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
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) {
'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) {
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) {
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)
})
})
getAllVideosListBy: getAllVideosListBy,
getClient: getClient,
getFriendsList: getFriendsList,
+ getUserInformation: getUserInformation,
getUsersList: getUsersList,
getVideo: getVideo,
getVideosList: getVideosList,
.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'