]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users/users.ts
Merge branch 'develop' of framagit.org:chocobozzz/PeerTube into develop
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
1 import { isAbsolute, join } from 'path'
2 import * as request from 'supertest'
3 import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
4
5 import { UserRole } from '../../../../shared/index'
6 import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type'
7
8 function createUser (
9 url: string,
10 accessToken: string,
11 username: string,
12 password: string,
13 videoQuota = 1000000,
14 role: UserRole = UserRole.USER,
15 specialStatus = 200
16 ) {
17 const path = '/api/v1/users'
18 const body = {
19 username,
20 password,
21 role,
22 email: username + '@example.com',
23 videoQuota
24 }
25
26 return request(url)
27 .post(path)
28 .set('Accept', 'application/json')
29 .set('Authorization', 'Bearer ' + accessToken)
30 .send(body)
31 .expect(specialStatus)
32 }
33
34 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
35 const path = '/api/v1/users/register'
36 const body = {
37 username,
38 password,
39 email: username + '@example.com'
40 }
41
42 return request(url)
43 .post(path)
44 .set('Accept', 'application/json')
45 .send(body)
46 .expect(specialStatus)
47 }
48
49 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
50 const path = '/api/v1/users/me'
51
52 return request(url)
53 .get(path)
54 .set('Accept', 'application/json')
55 .set('Authorization', 'Bearer ' + accessToken)
56 .expect(specialStatus)
57 .expect('Content-Type', /json/)
58 }
59
60 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
61 const path = '/api/v1/users/me/video-quota-used'
62
63 return request(url)
64 .get(path)
65 .set('Accept', 'application/json')
66 .set('Authorization', 'Bearer ' + accessToken)
67 .expect(specialStatus)
68 .expect('Content-Type', /json/)
69 }
70
71 function getUserInformation (url: string, accessToken: string, userId: number) {
72 const path = '/api/v1/users/' + userId
73
74 return request(url)
75 .get(path)
76 .set('Accept', 'application/json')
77 .set('Authorization', 'Bearer ' + accessToken)
78 .expect(200)
79 .expect('Content-Type', /json/)
80 }
81
82 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
83 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
84
85 return request(url)
86 .get(path)
87 .set('Accept', 'application/json')
88 .set('Authorization', 'Bearer ' + accessToken)
89 .expect(specialStatus)
90 .expect('Content-Type', /json/)
91 }
92
93 function getUsersList (url: string, accessToken: string) {
94 const path = '/api/v1/users'
95
96 return request(url)
97 .get(path)
98 .set('Accept', 'application/json')
99 .set('Authorization', 'Bearer ' + accessToken)
100 .expect(200)
101 .expect('Content-Type', /json/)
102 }
103
104 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
105 const path = '/api/v1/users'
106
107 return request(url)
108 .get(path)
109 .query({ start })
110 .query({ count })
111 .query({ sort })
112 .set('Accept', 'application/json')
113 .set('Authorization', 'Bearer ' + accessToken)
114 .expect(200)
115 .expect('Content-Type', /json/)
116 }
117
118 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
119 const path = '/api/v1/users'
120
121 return request(url)
122 .delete(path + '/' + userId)
123 .set('Accept', 'application/json')
124 .set('Authorization', 'Bearer ' + accessToken)
125 .expect(expectedStatus)
126 }
127
128 function updateMyUser (options: {
129 url: string
130 accessToken: string,
131 newPassword?: string,
132 nsfwPolicy?: NSFWPolicyType,
133 email?: string,
134 autoPlayVideo?: boolean
135 description?: string
136 }) {
137 const path = '/api/v1/users/me'
138
139 const toSend = {}
140 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
141 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
142 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
143 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
144 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
145
146 return makePutBodyRequest({
147 url: options.url,
148 path,
149 token: options.accessToken,
150 fields: toSend,
151 statusCodeExpected: 204
152 })
153 }
154
155 function updateMyAvatar (options: {
156 url: string,
157 accessToken: string,
158 fixture: string
159 }) {
160 const path = '/api/v1/users/me/avatar/pick'
161 let filePath = ''
162 if (isAbsolute(options.fixture)) {
163 filePath = options.fixture
164 } else {
165 filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
166 }
167
168 return makeUploadRequest({
169 url: options.url,
170 path,
171 token: options.accessToken,
172 fields: {},
173 attaches: { avatarfile: filePath },
174 statusCodeExpected: 200
175 })
176 }
177
178 function updateUser (options: {
179 url: string
180 userId: number,
181 accessToken: string,
182 email?: string,
183 videoQuota?: number,
184 role?: UserRole
185 }) {
186 const path = '/api/v1/users/' + options.userId
187
188 const toSend = {}
189 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
190 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
191 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
192
193 return makePutBodyRequest({
194 url: options.url,
195 path,
196 token: options.accessToken,
197 fields: toSend,
198 statusCodeExpected: 204
199 })
200 }
201
202 function askResetPassword (url: string, email: string) {
203 const path = '/api/v1/users/ask-reset-password'
204
205 return makePostBodyRequest({
206 url,
207 path,
208 fields: { email },
209 statusCodeExpected: 204
210 })
211 }
212
213 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
214 const path = '/api/v1/users/' + userId + '/reset-password'
215
216 return makePostBodyRequest({
217 url,
218 path,
219 fields: { password, verificationString },
220 statusCodeExpected
221 })
222 }
223
224 // ---------------------------------------------------------------------------
225
226 export {
227 createUser,
228 registerUser,
229 getMyUserInformation,
230 getMyUserVideoRating,
231 getMyUserVideoQuotaUsed,
232 getUsersList,
233 getUsersListPaginationAndSort,
234 removeUser,
235 updateUser,
236 updateMyUser,
237 getUserInformation,
238 askResetPassword,
239 resetPassword,
240 updateMyAvatar
241 }