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