]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users.ts
Fix tests and user quota
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users.ts
1 import * as request from 'supertest'
2
3 function createUser (url: string, accessToken: string, username: string, password: string, videoQuota = 1000000, specialStatus = 204) {
4 const path = '/api/v1/users'
5 const body = {
6 username,
7 password,
8 email: username + '@example.com',
9 videoQuota
10 }
11
12 return request(url)
13 .post(path)
14 .set('Accept', 'application/json')
15 .set('Authorization', 'Bearer ' + accessToken)
16 .send(body)
17 .expect(specialStatus)
18 }
19
20 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
21 const path = '/api/v1/users/register'
22 const body = {
23 username,
24 password,
25 email: username + '@example.com'
26 }
27
28 return request(url)
29 .post(path)
30 .set('Accept', 'application/json')
31 .send(body)
32 .expect(specialStatus)
33 }
34
35 function getMyUserInformation (url: string, accessToken: string) {
36 const path = '/api/v1/users/me'
37
38 return request(url)
39 .get(path)
40 .set('Accept', 'application/json')
41 .set('Authorization', 'Bearer ' + accessToken)
42 .expect(200)
43 .expect('Content-Type', /json/)
44 }
45
46 function getUserInformation (url: string, accessToken: string, userId: number) {
47 const path = '/api/v1/users/' + userId
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
57 function getUserVideoRating (url: string, accessToken: string, videoId: number) {
58 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
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
68 function getUsersList (url: string) {
69 const path = '/api/v1/users'
70
71 return request(url)
72 .get(path)
73 .set('Accept', 'application/json')
74 .expect(200)
75 .expect('Content-Type', /json/)
76 }
77
78 function getUsersListPaginationAndSort (url: string, start: number, count: number, sort: string) {
79 const path = '/api/v1/users'
80
81 return request(url)
82 .get(path)
83 .query({ start })
84 .query({ count })
85 .query({ sort })
86 .set('Accept', 'application/json')
87 .expect(200)
88 .expect('Content-Type', /json/)
89 }
90
91 function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) {
92 const path = '/api/v1/users'
93
94 return request(url)
95 .delete(path + '/' + userId)
96 .set('Accept', 'application/json')
97 .set('Authorization', 'Bearer ' + accessToken)
98 .expect(expectedStatus)
99 }
100
101 function updateMyUser (url: string, accessToken: string, newPassword: string, displayNSFW?: boolean, email?: string) {
102 const path = '/api/v1/users/me'
103
104 const toSend = {}
105 if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword
106 if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW
107 if (email !== undefined && email !== null) toSend['email'] = email
108
109 return request(url)
110 .put(path)
111 .set('Accept', 'application/json')
112 .set('Authorization', 'Bearer ' + accessToken)
113 .send(toSend)
114 .expect(204)
115 }
116
117 function updateUser (url: string, userId: number, accessToken: string, email: string, videoQuota: number) {
118 const path = '/api/v1/users/' + userId
119
120 const toSend = {}
121 if (email !== undefined && email !== null) toSend['email'] = email
122 if (videoQuota !== undefined && videoQuota !== null) toSend['videoQuota'] = videoQuota
123
124 return request(url)
125 .put(path)
126 .set('Accept', 'application/json')
127 .set('Authorization', 'Bearer ' + accessToken)
128 .send(toSend)
129 .expect(204)
130 }
131
132 // ---------------------------------------------------------------------------
133
134 export {
135 createUser,
136 registerUser,
137 getMyUserInformation,
138 getUserVideoRating,
139 getUsersList,
140 getUsersListPaginationAndSort,
141 removeUser,
142 updateUser,
143 updateMyUser,
144 getUserInformation
145 }