]>
Commit | Line | Data |
---|---|---|
72329aaa C |
1 | /* eslint-disable no-unused-expressions */ |
2 | ||
0c1cbbfe C |
3 | 'use strict' |
4 | ||
0c1cbbfe C |
5 | const chai = require('chai') |
6 | const expect = chai.expect | |
1a42c9e2 | 7 | const series = require('async/series') |
0c1cbbfe | 8 | |
8d309058 C |
9 | const loginUtils = require('../utils/login') |
10 | const podsUtils = require('../utils/pods') | |
11 | const serversUtils = require('../utils/servers') | |
12 | const usersUtils = require('../utils/users') | |
13 | const videosUtils = require('../utils/videos') | |
0c1cbbfe | 14 | |
0c1cbbfe C |
15 | describe('Test users', function () { |
16 | let server = null | |
bc503c2a | 17 | let accessToken = null |
9bd26629 C |
18 | let accessTokenUser = null |
19 | let videoId = null | |
20 | let userId = null | |
0c1cbbfe C |
21 | |
22 | before(function (done) { | |
23 | this.timeout(20000) | |
24 | ||
1a42c9e2 | 25 | series([ |
0c1cbbfe | 26 | function (next) { |
8d309058 | 27 | serversUtils.flushTests(next) |
0c1cbbfe C |
28 | }, |
29 | function (next) { | |
8d309058 | 30 | serversUtils.runServer(1, function (server1) { |
0c1cbbfe C |
31 | server = server1 |
32 | next() | |
33 | }) | |
34 | } | |
35 | ], done) | |
36 | }) | |
37 | ||
b0ec596c C |
38 | it('Should create a new client') |
39 | ||
40 | it('Should return the first client') | |
41 | ||
42 | it('Should remove the last client') | |
43 | ||
0c1cbbfe C |
44 | it('Should not login with an invalid client id', function (done) { |
45 | const client = { id: 'client', password: server.client.secret } | |
8d309058 | 46 | loginUtils.login(server.url, client, server.user, 400, function (err, res) { |
0c1cbbfe C |
47 | if (err) throw err |
48 | ||
49 | expect(res.body.error).to.equal('invalid_client') | |
50 | done() | |
51 | }) | |
52 | }) | |
53 | ||
54 | it('Should not login with an invalid client password', function (done) { | |
55 | const client = { id: server.client.id, password: 'coucou' } | |
8d309058 | 56 | loginUtils.login(server.url, client, server.user, 400, function (err, res) { |
0c1cbbfe C |
57 | if (err) throw err |
58 | ||
59 | expect(res.body.error).to.equal('invalid_client') | |
60 | done() | |
61 | }) | |
62 | }) | |
63 | ||
64 | it('Should not login with an invalid username', function (done) { | |
65 | const user = { username: 'captain crochet', password: server.user.password } | |
8d309058 | 66 | loginUtils.login(server.url, server.client, user, 400, function (err, res) { |
0c1cbbfe C |
67 | if (err) throw err |
68 | ||
69 | expect(res.body.error).to.equal('invalid_grant') | |
70 | done() | |
71 | }) | |
72 | }) | |
73 | ||
74 | it('Should not login with an invalid password', function (done) { | |
75 | const user = { username: server.user.username, password: 'mewthree' } | |
8d309058 | 76 | loginUtils.login(server.url, server.client, user, 400, function (err, res) { |
0c1cbbfe C |
77 | if (err) throw err |
78 | ||
79 | expect(res.body.error).to.equal('invalid_grant') | |
80 | done() | |
81 | }) | |
82 | }) | |
83 | ||
84 | it('Should not be able to upload a video', function (done) { | |
bc503c2a | 85 | accessToken = 'mysupertoken' |
be587647 C |
86 | |
87 | const name = 'my super name' | |
88 | const description = 'my super description' | |
89 | const tags = [ 'tag1', 'tag2' ] | |
90 | const video = 'video_short.webm' | |
8d309058 | 91 | videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 401, done) |
0c1cbbfe C |
92 | }) |
93 | ||
b3b92647 C |
94 | it('Should not be able to make friends', function (done) { |
95 | accessToken = 'mysupertoken' | |
8d309058 | 96 | podsUtils.makeFriends(server.url, accessToken, 401, done) |
b3b92647 C |
97 | }) |
98 | ||
99 | it('Should not be able to quit friends', function (done) { | |
100 | accessToken = 'mysupertoken' | |
8d309058 | 101 | podsUtils.quitFriends(server.url, accessToken, 401, done) |
b3b92647 C |
102 | }) |
103 | ||
0c1cbbfe | 104 | it('Should be able to login', function (done) { |
8d309058 | 105 | loginUtils.login(server.url, server.client, server.user, 200, function (err, res) { |
0c1cbbfe C |
106 | if (err) throw err |
107 | ||
bc503c2a | 108 | accessToken = res.body.access_token |
0c1cbbfe C |
109 | done() |
110 | }) | |
111 | }) | |
112 | ||
113 | it('Should upload the video with the correct token', function (done) { | |
be587647 C |
114 | const name = 'my super name' |
115 | const description = 'my super description' | |
116 | const tags = [ 'tag1', 'tag2' ] | |
117 | const video = 'video_short.webm' | |
8d309058 | 118 | videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, function (err, res) { |
0c1cbbfe C |
119 | if (err) throw err |
120 | ||
8d309058 | 121 | videosUtils.getVideosList(server.url, function (err, res) { |
0c1cbbfe C |
122 | if (err) throw err |
123 | ||
68ce3ae0 | 124 | const video = res.body.data[0] |
6d8ada5f C |
125 | expect(video.author).to.equal('root') |
126 | ||
bc503c2a | 127 | videoId = video.id |
0c1cbbfe C |
128 | done() |
129 | }) | |
130 | }) | |
131 | }) | |
132 | ||
133 | it('Should upload the video again with the correct token', function (done) { | |
be587647 C |
134 | const name = 'my super name 2' |
135 | const description = 'my super description 2' | |
136 | const tags = [ 'tag1' ] | |
137 | const video = 'video_short.webm' | |
8d309058 | 138 | videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done) |
0c1cbbfe C |
139 | }) |
140 | ||
141 | it('Should not be able to remove the video with an incorrect token', function (done) { | |
8d309058 | 142 | videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done) |
0c1cbbfe C |
143 | }) |
144 | ||
145 | it('Should not be able to remove the video with the token of another account') | |
146 | ||
147 | it('Should be able to remove the video with the correct token', function (done) { | |
8d309058 | 148 | videosUtils.removeVideo(server.url, accessToken, videoId, done) |
0c1cbbfe C |
149 | }) |
150 | ||
2f372a86 | 151 | it('Should logout (revoke token)') |
0c1cbbfe C |
152 | |
153 | it('Should not be able to upload a video') | |
154 | ||
155 | it('Should not be able to remove a video') | |
156 | ||
157 | it('Should be able to login again') | |
158 | ||
2f372a86 C |
159 | it('Should have an expired access token') |
160 | ||
161 | it('Should refresh the token') | |
162 | ||
163 | it('Should be able to upload a video again') | |
164 | ||
9bd26629 | 165 | it('Should be able to create a new user', function (done) { |
8d309058 | 166 | usersUtils.createUser(server.url, accessToken, 'user_1', 'super password', done) |
9bd26629 C |
167 | }) |
168 | ||
169 | it('Should be able to login with this user', function (done) { | |
170 | server.user = { | |
171 | username: 'user_1', | |
172 | password: 'super password' | |
173 | } | |
174 | ||
8d309058 | 175 | loginUtils.loginAndGetAccessToken(server, function (err, token) { |
9bd26629 C |
176 | if (err) throw err |
177 | ||
178 | accessTokenUser = token | |
179 | ||
180 | done() | |
181 | }) | |
182 | }) | |
183 | ||
99a64bfe | 184 | it('Should be able to get the user informations', function (done) { |
8d309058 | 185 | usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) { |
99a64bfe C |
186 | if (err) throw err |
187 | ||
188 | const user = res.body | |
189 | ||
190 | expect(user.username).to.equal('user_1') | |
ad4a8a1c | 191 | expect(user.email).to.equal('user_1@example.com') |
99a64bfe C |
192 | expect(user.id).to.exist |
193 | ||
194 | done() | |
195 | }) | |
196 | }) | |
197 | ||
9bd26629 C |
198 | it('Should be able to upload a video with this user', function (done) { |
199 | this.timeout(5000) | |
200 | ||
201 | const name = 'my super name' | |
202 | const description = 'my super description' | |
203 | const tags = [ 'tag1', 'tag2', 'tag3' ] | |
204 | const file = 'video_short.webm' | |
8d309058 | 205 | videosUtils.uploadVideo(server.url, accessTokenUser, name, description, tags, file, done) |
9bd26629 C |
206 | }) |
207 | ||
208 | it('Should list all the users', function (done) { | |
8d309058 | 209 | usersUtils.getUsersList(server.url, function (err, res) { |
9bd26629 C |
210 | if (err) throw err |
211 | ||
5c39adb7 C |
212 | const result = res.body |
213 | const total = result.total | |
214 | const users = result.data | |
9bd26629 | 215 | |
5c39adb7 | 216 | expect(total).to.equal(2) |
9bd26629 C |
217 | expect(users).to.be.an('array') |
218 | expect(users.length).to.equal(2) | |
219 | ||
5c39adb7 C |
220 | const user = users[0] |
221 | expect(user.username).to.equal('user_1') | |
ad4a8a1c | 222 | expect(user.email).to.equal('user_1@example.com') |
5c39adb7 C |
223 | |
224 | const rootUser = users[1] | |
9bd26629 | 225 | expect(rootUser.username).to.equal('root') |
ad4a8a1c | 226 | expect(rootUser.email).to.equal('admin1@example.com') |
5c39adb7 C |
227 | userId = user.id |
228 | ||
229 | done() | |
230 | }) | |
231 | }) | |
232 | ||
233 | it('Should list only the first user by username asc', function (done) { | |
234 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, 'username', function (err, res) { | |
235 | if (err) throw err | |
236 | ||
237 | const result = res.body | |
238 | const total = result.total | |
239 | const users = result.data | |
240 | ||
241 | expect(total).to.equal(2) | |
242 | expect(users.length).to.equal(1) | |
9bd26629 | 243 | |
5c39adb7 C |
244 | const user = users[0] |
245 | expect(user.username).to.equal('root') | |
ad4a8a1c | 246 | expect(user.email).to.equal('admin1@example.com') |
5c39adb7 C |
247 | |
248 | done() | |
249 | }) | |
250 | }) | |
251 | ||
252 | it('Should list only the first user by username desc', function (done) { | |
253 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-username', function (err, res) { | |
254 | if (err) throw err | |
255 | ||
256 | const result = res.body | |
257 | const total = result.total | |
258 | const users = result.data | |
259 | ||
260 | expect(total).to.equal(2) | |
261 | expect(users.length).to.equal(1) | |
262 | ||
263 | const user = users[0] | |
9bd26629 | 264 | expect(user.username).to.equal('user_1') |
ad4a8a1c | 265 | expect(user.email).to.equal('user_1@example.com') |
5c39adb7 C |
266 | |
267 | done() | |
268 | }) | |
269 | }) | |
270 | ||
feb4bdfd C |
271 | it('Should list only the second user by createdAt desc', function (done) { |
272 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt', function (err, res) { | |
5c39adb7 C |
273 | if (err) throw err |
274 | ||
275 | const result = res.body | |
276 | const total = result.total | |
277 | const users = result.data | |
278 | ||
279 | expect(total).to.equal(2) | |
280 | expect(users.length).to.equal(1) | |
281 | ||
282 | const user = users[0] | |
283 | expect(user.username).to.equal('user_1') | |
ad4a8a1c | 284 | expect(user.email).to.equal('user_1@example.com') |
5c39adb7 C |
285 | |
286 | done() | |
287 | }) | |
288 | }) | |
289 | ||
feb4bdfd C |
290 | it('Should list all the users by createdAt asc', function (done) { |
291 | usersUtils.getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt', function (err, res) { | |
5c39adb7 C |
292 | if (err) throw err |
293 | ||
294 | const result = res.body | |
295 | const total = result.total | |
296 | const users = result.data | |
297 | ||
298 | expect(total).to.equal(2) | |
299 | expect(users.length).to.equal(2) | |
300 | ||
301 | expect(users[0].username).to.equal('root') | |
ad4a8a1c | 302 | expect(users[0].email).to.equal('admin1@example.com') |
5c39adb7 | 303 | expect(users[1].username).to.equal('user_1') |
ad4a8a1c | 304 | expect(users[1].email).to.equal('user_1@example.com') |
9bd26629 C |
305 | |
306 | done() | |
307 | }) | |
308 | }) | |
309 | ||
310 | it('Should update the user password', function (done) { | |
8d309058 | 311 | usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) { |
9bd26629 C |
312 | if (err) throw err |
313 | ||
314 | server.user.password = 'new password' | |
8d309058 | 315 | loginUtils.login(server.url, server.client, server.user, 200, done) |
9bd26629 C |
316 | }) |
317 | }) | |
318 | ||
319 | it('Should be able to remove this user', function (done) { | |
68a3b9f2 | 320 | usersUtils.removeUser(server.url, userId, accessToken, done) |
9bd26629 C |
321 | }) |
322 | ||
323 | it('Should not be able to login with this user', function (done) { | |
324 | // server.user is already set to user 1 | |
8d309058 | 325 | loginUtils.login(server.url, server.client, server.user, 400, done) |
9bd26629 C |
326 | }) |
327 | ||
328 | it('Should not have videos of this user', function (done) { | |
8d309058 | 329 | videosUtils.getVideosList(server.url, function (err, res) { |
9bd26629 C |
330 | if (err) throw err |
331 | ||
332 | expect(res.body.total).to.equal(1) | |
333 | const video = res.body.data[0] | |
334 | expect(video.author).to.equal('root') | |
335 | ||
336 | done() | |
337 | }) | |
338 | }) | |
339 | ||
0c1cbbfe C |
340 | after(function (done) { |
341 | process.kill(-server.app.pid) | |
342 | ||
343 | // Keep the logs if the test failed | |
344 | if (this.ok) { | |
8d309058 | 345 | serversUtils.flushTests(done) |
0c1cbbfe C |
346 | } else { |
347 | done() | |
348 | } | |
349 | }) | |
350 | }) |