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