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