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