]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users/users.ts
Move fixtures in tests/
[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 displayName?: string,
136 description?: string
137 }) {
138 const path = '/api/v1/users/me'
139
140 const toSend = {}
141 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
142 if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
143 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
144 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
145 if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
146 if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
147
148 return makePutBodyRequest({
149 url: options.url,
150 path,
151 token: options.accessToken,
152 fields: toSend,
153 statusCodeExpected: 204
154 })
155 }
156
157 function updateMyAvatar (options: {
158 url: string,
159 accessToken: string,
160 fixture: string
161 }) {
162 const path = '/api/v1/users/me/avatar/pick'
163 let filePath = ''
164 if (isAbsolute(options.fixture)) {
165 filePath = options.fixture
166 } else {
167 filePath = join(__dirname, '..', '..', 'fixtures', options.fixture)
168 }
169
170 return makeUploadRequest({
171 url: options.url,
172 path,
173 token: options.accessToken,
174 fields: {},
175 attaches: { avatarfile: filePath },
176 statusCodeExpected: 200
177 })
178 }
179
180 function updateUser (options: {
181 url: string
182 userId: number,
183 accessToken: string,
184 email?: string,
185 videoQuota?: number,
186 role?: UserRole
187 }) {
188 const path = '/api/v1/users/' + options.userId
189
190 const toSend = {}
191 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
192 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
193 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
194
195 return makePutBodyRequest({
196 url: options.url,
197 path,
198 token: options.accessToken,
199 fields: toSend,
200 statusCodeExpected: 204
201 })
202 }
203
204 function askResetPassword (url: string, email: string) {
205 const path = '/api/v1/users/ask-reset-password'
206
207 return makePostBodyRequest({
208 url,
209 path,
210 fields: { email },
211 statusCodeExpected: 204
212 })
213 }
214
215 function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
216 const path = '/api/v1/users/' + userId + '/reset-password'
217
218 return makePostBodyRequest({
219 url,
220 path,
221 fields: { password, verificationString },
222 statusCodeExpected
223 })
224 }
225
226 // ---------------------------------------------------------------------------
227
228 export {
229 createUser,
230 registerUser,
231 getMyUserInformation,
232 getMyUserVideoRating,
233 getMyUserVideoQuotaUsed,
234 getUsersList,
235 getUsersListPaginationAndSort,
236 removeUser,
237 updateUser,
238 updateMyUser,
239 getUserInformation,
240 askResetPassword,
241 resetPassword,
242 updateMyAvatar
243 }