Remove one pod (#76)
[oweals/peertube.git] / server / tests / utils / users.js
1 'use strict'
2
3 const request = require('supertest')
4
5 const usersUtils = {
6   createUser,
7   registerUser,
8   getUserInformation,
9   getUserVideoRating,
10   getUsersList,
11   getUsersListPaginationAndSort,
12   removeUser,
13   updateUser
14 }
15
16 // ---------------------- Export functions --------------------
17
18 function createUser (url, accessToken, username, password, specialStatus, end) {
19   if (!end) {
20     end = specialStatus
21     specialStatus = 204
22   }
23
24   const path = '/api/v1/users'
25   const body = {
26     username,
27     password,
28     email: username + '@example.com'
29   }
30
31   request(url)
32     .post(path)
33     .set('Accept', 'application/json')
34     .set('Authorization', 'Bearer ' + accessToken)
35     .send(body)
36     .expect(specialStatus)
37     .end(end)
38 }
39
40 function registerUser (url, username, password, specialStatus, end) {
41   if (!end) {
42     end = specialStatus
43     specialStatus = 204
44   }
45
46   const path = '/api/v1/users/register'
47   const body = {
48     username,
49     password,
50     email: username + '@example.com'
51   }
52
53   request(url)
54     .post(path)
55     .set('Accept', 'application/json')
56     .send(body)
57     .expect(specialStatus)
58     .end(end)
59 }
60
61 function getUserInformation (url, accessToken, end) {
62   const path = '/api/v1/users/me'
63
64   request(url)
65     .get(path)
66     .set('Accept', 'application/json')
67     .set('Authorization', 'Bearer ' + accessToken)
68     .expect(200)
69     .expect('Content-Type', /json/)
70     .end(end)
71 }
72
73 function getUserVideoRating (url, accessToken, videoId, end) {
74   const path = '/api/v1/users/me/videos/' + videoId + '/rating'
75
76   request(url)
77     .get(path)
78     .set('Accept', 'application/json')
79     .set('Authorization', 'Bearer ' + accessToken)
80     .expect(200)
81     .expect('Content-Type', /json/)
82     .end(end)
83 }
84
85 function getUsersList (url, end) {
86   const path = '/api/v1/users'
87
88   request(url)
89     .get(path)
90     .set('Accept', 'application/json')
91     .expect(200)
92     .expect('Content-Type', /json/)
93     .end(end)
94 }
95
96 function getUsersListPaginationAndSort (url, start, count, sort, end) {
97   const path = '/api/v1/users'
98
99   request(url)
100     .get(path)
101     .query({ start: start })
102     .query({ count: count })
103     .query({ sort: sort })
104     .set('Accept', 'application/json')
105     .expect(200)
106     .expect('Content-Type', /json/)
107     .end(end)
108 }
109
110 function removeUser (url, userId, accessToken, expectedStatus, end) {
111   if (!end) {
112     end = expectedStatus
113     expectedStatus = 204
114   }
115
116   const path = '/api/v1/users'
117
118   request(url)
119     .delete(path + '/' + userId)
120     .set('Accept', 'application/json')
121     .set('Authorization', 'Bearer ' + accessToken)
122     .expect(expectedStatus)
123     .end(end)
124 }
125
126 function updateUser (url, userId, accessToken, newPassword, displayNSFW, end) {
127   const path = '/api/v1/users/' + userId
128
129   const toSend = {}
130   if (newPassword !== undefined && newPassword !== null) toSend.password = newPassword
131   if (displayNSFW !== undefined && displayNSFW !== null) toSend.displayNSFW = displayNSFW
132
133   request(url)
134     .put(path)
135     .set('Accept', 'application/json')
136     .set('Authorization', 'Bearer ' + accessToken)
137     .send(toSend)
138     .expect(204)
139     .end(end)
140 }
141
142 // ---------------------------------------------------------------------------
143
144 module.exports = usersUtils