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