]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users/users.ts
Improve check users parameters tests
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
1 import * as request from 'supertest'
2 import { makePutBodyRequest } from '../'
3
4 import { UserRole } from '../../../../shared/index'
5
6 function 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 ) {
15 const path = '/api/v1/users'
16 const body = {
17 username,
18 password,
19 role,
20 email: username + '@example.com',
21 videoQuota
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
32 function 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
47 function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
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)
54 .expect(specialStatus)
55 .expect('Content-Type', /json/)
56 }
57
58 function 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
69 function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
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)
76 .expect(specialStatus)
77 .expect('Content-Type', /json/)
78 }
79
80 function getUsersList (url: string, accessToken: string) {
81 const path = '/api/v1/users'
82
83 return request(url)
84 .get(path)
85 .set('Accept', 'application/json')
86 .set('Authorization', 'Bearer ' + accessToken)
87 .expect(200)
88 .expect('Content-Type', /json/)
89 }
90
91 function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) {
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')
100 .set('Authorization', 'Bearer ' + accessToken)
101 .expect(200)
102 .expect('Content-Type', /json/)
103 }
104
105 function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
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
115 function updateMyUser (options: {
116 url: string
117 accessToken: string,
118 newPassword?: string,
119 displayNSFW?: boolean,
120 email?: string,
121 autoPlayVideo?: boolean
122 }) {
123 const path = '/api/v1/users/me'
124
125 const toSend = {}
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 })
138 }
139
140 function 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
149
150 const toSend = {}
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 })
162 }
163
164 // ---------------------------------------------------------------------------
165
166 export {
167 createUser,
168 registerUser,
169 getMyUserInformation,
170 getMyUserVideoRating,
171 getUsersList,
172 getUsersListPaginationAndSort,
173 removeUser,
174 updateUser,
175 updateMyUser,
176 getUserInformation
177 }