Server: add user list sort/pagination
[oweals/peertube.git] / server / tests / utils / users.js
1 'use strict'
2
3 const request = require('supertest')
4
5 const usersUtils = {
6   createUser: createUser,
7   getUserInformation: getUserInformation,
8   getUsersList: getUsersList,
9   getUsersListPaginationAndSort: getUsersListPaginationAndSort,
10   removeUser: removeUser,
11   updateUser: updateUser
12 }
13
14 // ---------------------- Export functions --------------------
15
16 function createUser (url, accessToken, username, password, specialStatus, end) {
17   if (!end) {
18     end = specialStatus
19     specialStatus = 204
20   }
21
22   const path = '/api/v1/users'
23
24   request(url)
25     .post(path)
26     .set('Accept', 'application/json')
27     .set('Authorization', 'Bearer ' + accessToken)
28     .send({ username: username, password: password })
29     .expect(specialStatus)
30     .end(end)
31 }
32
33 function getUserInformation (url, accessToken, end) {
34   const path = '/api/v1/users/me'
35
36   request(url)
37     .get(path)
38     .set('Accept', 'application/json')
39     .set('Authorization', 'Bearer ' + accessToken)
40     .expect(200)
41     .expect('Content-Type', /json/)
42     .end(end)
43 }
44
45 function getUsersList (url, end) {
46   const path = '/api/v1/users'
47
48   request(url)
49     .get(path)
50     .set('Accept', 'application/json')
51     .expect(200)
52     .expect('Content-Type', /json/)
53     .end(end)
54 }
55
56 function getUsersListPaginationAndSort (url, start, count, sort, end) {
57   const path = '/api/v1/users'
58
59   request(url)
60     .get(path)
61     .query({ start: start })
62     .query({ count: count })
63     .query({ sort: sort })
64     .set('Accept', 'application/json')
65     .expect(200)
66     .expect('Content-Type', /json/)
67     .end(end)
68 }
69
70 function removeUser (url, userId, accessToken, expectedStatus, end) {
71   if (!end) {
72     end = expectedStatus
73     expectedStatus = 204
74   }
75
76   const path = '/api/v1/users'
77
78   request(url)
79     .delete(path + '/' + userId)
80     .set('Accept', 'application/json')
81     .set('Authorization', 'Bearer ' + accessToken)
82     .expect(expectedStatus)
83     .end(end)
84 }
85
86 function updateUser (url, userId, accessToken, newPassword, end) {
87   const path = '/api/v1/users/' + userId
88
89   request(url)
90     .put(path)
91     .set('Accept', 'application/json')
92     .set('Authorization', 'Bearer ' + accessToken)
93     .send({ password: newPassword })
94     .expect(204)
95     .end(end)
96 }
97
98 // ---------------------------------------------------------------------------
99
100 module.exports = usersUtils