]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users/users.ts
Begin to add avatar to actors
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
1 import { isAbsolute, join } from 'path'
2 import * as request from 'supertest'
3 import { makePostUploadRequest, makePutBodyRequest } from '../'
4
5 import { UserRole } from '../../../../shared/index'
6
7 function createUser (
8 url: string,
9 accessToken: string,
10 username: string,
11 password: string,
12 videoQuota = 1000000,
13 role: UserRole = UserRole.USER,
14 specialStatus = 204
15 ) {
16 const path = '/api/v1/users'
17 const body = {
18 username,
19 password,
20 role,
21 email: username + '@example.com',
22 videoQuota
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
33 function 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
48 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
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)
55 .expect(specialStatus)
56 .expect('Content-Type', /json/)
57 }
58
59 function getUserInformation (url: string, accessToken: string, userId: number) {
60 const path = '/api/v1/users/' + userId
61
62 return request(url)
63 .get(path)
64 .set('Accept', 'application/json')
65 .set('Authorization', 'Bearer ' + accessToken)
66 .expect(200)
67 .expect('Content-Type', /json/)
68 }
69
70 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
71 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
72
73 return request(url)
74 .get(path)
75 .set('Accept', 'application/json')
76 .set('Authorization', 'Bearer ' + accessToken)
77 .expect(specialStatus)
78 .expect('Content-Type', /json/)
79 }
80
81 function getUsersList (url: string, accessToken: string) {
82 const path = '/api/v1/users'
83
84 return request(url)
85 .get(path)
86 .set('Accept', 'application/json')
87 .set('Authorization', 'Bearer ' + accessToken)
88 .expect(200)
89 .expect('Content-Type', /json/)
90 }
91
92 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
93 const path = '/api/v1/users'
94
95 return request(url)
96 .get(path)
97 .query({ start })
98 .query({ count })
99 .query({ sort })
100 .set('Accept', 'application/json')
101 .set('Authorization', 'Bearer ' + accessToken)
102 .expect(200)
103 .expect('Content-Type', /json/)
104 }
105
106 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
107 const path = '/api/v1/users'
108
109 return request(url)
110 .delete(path + '/' + userId)
111 .set('Accept', 'application/json')
112 .set('Authorization', 'Bearer ' + accessToken)
113 .expect(expectedStatus)
114 }
115
116 function updateMyUser (options: {
117 url: string
118 accessToken: string,
119 newPassword?: string,
120 displayNSFW?: boolean,
121 email?: string,
122 autoPlayVideo?: boolean
123 }) {
124 const path = '/api/v1/users/me'
125
126 const toSend = {}
127 if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
128 if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
129 if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
130 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
131
132 return makePutBodyRequest({
133 url: options.url,
134 path,
135 token: options.accessToken,
136 fields: toSend,
137 statusCodeExpected: 204
138 })
139 }
140
141 function updateMyAvatar (options: {
142 url: string,
143 accessToken: string,
144 fixture: string
145 }) {
146 const path = '/api/v1/users/me/avatar/pick'
147 let filePath = ''
148 if (isAbsolute(options.fixture)) {
149 filePath = options.fixture
150 } else {
151 filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
152 }
153
154 return makePostUploadRequest({
155 url: options.url,
156 path,
157 token: options.accessToken,
158 fields: {},
159 attaches: { avatarfile: filePath },
160 statusCodeExpected: 200
161 })
162 }
163
164 function updateUser (options: {
165 url: string
166 userId: number,
167 accessToken: string,
168 email?: string,
169 videoQuota?: number,
170 role?: UserRole
171 }) {
172 const path = '/api/v1/users/' + options.userId
173
174 const toSend = {}
175 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
176 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
177 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
178
179 return makePutBodyRequest({
180 url: options.url,
181 path,
182 token: options.accessToken,
183 fields: toSend,
184 statusCodeExpected: 204
185 })
186 }
187
188 // ---------------------------------------------------------------------------
189
190 export {
191 createUser,
192 registerUser,
193 getMyUserInformation,
194 getMyUserVideoRating,
195 getUsersList,
196 getUsersListPaginationAndSort,
197 removeUser,
198 updateUser,
199 updateMyUser,
200 getUserInformation,
201 updateMyAvatar
202 }