]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/users.ts
6e4b5f2f563e902f514bc721d0f36029f260734e
[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, specialStatus = 204) {
4 const path = '/api/v1/users'
5 const body = {
6 username,
7 password,
8 email: username + '@example.com'
9 }
10
11 return request(url)
12 .post(path)
13 .set('Accept', 'application/json')
14 .set('Authorization', 'Bearer ' + accessToken)
15 .send(body)
16 .expect(specialStatus)
17 }
18
19 function registerUser (url: string, username: string, password: string, specialStatus = 204) {
20 const path = '/api/v1/users/register'
21 const body = {
22 username,
23 password,
24 email: username + '@example.com'
25 }
26
27 return request(url)
28 .post(path)
29 .set('Accept', 'application/json')
30 .send(body)
31 .expect(specialStatus)
32 }
33
34 function getUserInformation (url: string, accessToken: string) {
35 const path = '/api/v1/users/me'
36
37 return request(url)
38 .get(path)
39 .set('Accept', 'application/json')
40 .set('Authorization', 'Bearer ' + accessToken)
41 .expect(200)
42 .expect('Content-Type', /json/)
43 }
44
45 function getUserVideoRating (url: string, accessToken: string, videoId: number) {
46 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
47
48 return request(url)
49 .get(path)
50 .set('Accept', 'application/json')
51 .set('Authorization', 'Bearer ' + accessToken)
52 .expect(200)
53 .expect('Content-Type', /json/)
54 }
55
56 function getUsersList (url: string) {
57 const path = '/api/v1/users'
58
59 return request(url)
60 .get(path)
61 .set('Accept', 'application/json')
62 .expect(200)
63 .expect('Content-Type', /json/)
64 }
65
66 function getUsersListPaginationAndSort (url: string, start: number, count: number, sort: string) {
67 const path = '/api/v1/users'
68
69 return request(url)
70 .get(path)
71 .query({ start })
72 .query({ count })
73 .query({ sort })
74 .set('Accept', 'application/json')
75 .expect(200)
76 .expect('Content-Type', /json/)
77 }
78
79 function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) {
80 const path = '/api/v1/users'
81
82 return request(url)
83 .delete(path + '/' + userId)
84 .set('Accept', 'application/json')
85 .set('Authorization', 'Bearer ' + accessToken)
86 .expect(expectedStatus)
87 }
88
89 function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) {
90 const path = '/api/v1/users/' + userId
91
92 const toSend = {}
93 if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword
94 if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW
95
96 return request(url)
97 .put(path)
98 .set('Accept', 'application/json')
99 .set('Authorization', 'Bearer ' + accessToken)
100 .send(toSend)
101 .expect(204)
102 }
103
104 // ---------------------------------------------------------------------------
105
106 export {
107 createUser,
108 registerUser,
109 getUserInformation,
110 getUserVideoRating,
111 getUsersList,
112 getUsersListPaginationAndSort,
113 removeUser,
114 updateUser
115 }