aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-07 22:09:59 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-07 22:09:59 +0200
commit8d30905858245f12a42fc327d2d57cbfe062d548 (patch)
tree9bd590872fbb753782f2ebbb9516db0414022d70 /server/tests/api
parent58b2ba55a90f05f24661e664b1fb0a3486f037e8 (diff)
downloadPeerTube-8d30905858245f12a42fc327d2d57cbfe062d548.tar.gz
PeerTube-8d30905858245f12a42fc327d2d57cbfe062d548.tar.zst
PeerTube-8d30905858245f12a42fc327d2d57cbfe062d548.zip
Server: split tests utils in multiple files
Diffstat (limited to 'server/tests/api')
-rw-r--r--server/tests/api/checkParams.js20
-rw-r--r--server/tests/api/friendsAdvanced.js23
-rw-r--r--server/tests/api/friendsBasic.js30
-rw-r--r--server/tests/api/multiplePods.js62
-rw-r--r--server/tests/api/singlePod.js90
-rw-r--r--server/tests/api/users.js59
-rw-r--r--server/tests/api/utils.js492
7 files changed, 150 insertions, 626 deletions
diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js
index e489df277..675dc19e6 100644
--- a/server/tests/api/checkParams.js
+++ b/server/tests/api/checkParams.js
@@ -6,7 +6,9 @@ const pathUtils = require('path')
6const request = require('supertest') 6const request = require('supertest')
7const series = require('async/series') 7const series = require('async/series')
8 8
9const utils = require('./utils') 9const loginUtils = require('../utils/login')
10const serversUtils = require('../utils/servers')
11const usersUtils = require('../utils/users')
10 12
11describe('Test parameters validator', function () { 13describe('Test parameters validator', function () {
12 let server = null 14 let server = null
@@ -71,17 +73,17 @@ describe('Test parameters validator', function () {
71 73
72 series([ 74 series([
73 function (next) { 75 function (next) {
74 utils.flushTests(next) 76 serversUtils.flushTests(next)
75 }, 77 },
76 function (next) { 78 function (next) {
77 utils.runServer(1, function (server1) { 79 serversUtils.runServer(1, function (server1) {
78 server = server1 80 server = server1
79 81
80 next() 82 next()
81 }) 83 })
82 }, 84 },
83 function (next) { 85 function (next) {
84 utils.loginAndGetAccessToken(server, function (err, token) { 86 loginUtils.loginAndGetAccessToken(server, function (err, token) {
85 if (err) throw err 87 if (err) throw err
86 server.accessToken = token 88 server.accessToken = token
87 89
@@ -141,13 +143,13 @@ describe('Test parameters validator', function () {
141 let userAccessToken = null 143 let userAccessToken = null
142 144
143 before(function (done) { 145 before(function (done) {
144 utils.createUser(server.url, server.accessToken, 'user1', 'password', function () { 146 usersUtils.createUser(server.url, server.accessToken, 'user1', 'password', function () {
145 server.user = { 147 server.user = {
146 username: 'user1', 148 username: 'user1',
147 password: 'password' 149 password: 'password'
148 } 150 }
149 151
150 utils.loginAndGetAccessToken(server, function (err, accessToken) { 152 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
151 if (err) throw err 153 if (err) throw err
152 154
153 userAccessToken = accessToken 155 userAccessToken = accessToken
@@ -581,7 +583,7 @@ describe('Test parameters validator', function () {
581 password: 'my super password' 583 password: 'my super password'
582 } 584 }
583 585
584 utils.loginAndGetAccessToken(server, function (err, accessToken) { 586 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
585 if (err) throw err 587 if (err) throw err
586 588
587 userAccessToken = accessToken 589 userAccessToken = accessToken
@@ -598,7 +600,7 @@ describe('Test parameters validator', function () {
598 600
599 describe('When updating a user', function () { 601 describe('When updating a user', function () {
600 before(function (done) { 602 before(function (done) {
601 utils.getUsersList(server.url, function (err, res) { 603 usersUtils.getUsersList(server.url, function (err, res) {
602 if (err) throw err 604 if (err) throw err
603 605
604 userId = res.body.data[1].id 606 userId = res.body.data[1].id
@@ -702,7 +704,7 @@ describe('Test parameters validator', function () {
702 704
703 // Keep the logs if the test failed 705 // Keep the logs if the test failed
704 if (this.ok) { 706 if (this.ok) {
705 utils.flushTests(done) 707 serversUtils.flushTests(done)
706 } else { 708 } else {
707 done() 709 done()
708 } 710 }
diff --git a/server/tests/api/friendsAdvanced.js b/server/tests/api/friendsAdvanced.js
index 603fbc16b..0d24481ef 100644
--- a/server/tests/api/friendsAdvanced.js
+++ b/server/tests/api/friendsAdvanced.js
@@ -5,24 +5,27 @@ const each = require('async/each')
5const expect = chai.expect 5const expect = chai.expect
6const series = require('async/series') 6const series = require('async/series')
7 7
8const utils = require('./utils') 8const loginUtils = require('../utils/login')
9const podsUtils = require('../utils/pods')
10const serversUtils = require('../utils/servers')
11const videosUtils = require('../utils/videos')
9 12
10describe('Test advanced friends', function () { 13describe('Test advanced friends', function () {
11 let servers = [] 14 let servers = []
12 15
13 function makeFriends (podNumber, callback) { 16 function makeFriends (podNumber, callback) {
14 const server = servers[podNumber - 1] 17 const server = servers[podNumber - 1]
15 return utils.makeFriends(server.url, server.accessToken, callback) 18 return podsUtils.makeFriends(server.url, server.accessToken, callback)
16 } 19 }
17 20
18 function quitFriends (podNumber, callback) { 21 function quitFriends (podNumber, callback) {
19 const server = servers[podNumber - 1] 22 const server = servers[podNumber - 1]
20 return utils.quitFriends(server.url, server.accessToken, callback) 23 return podsUtils.quitFriends(server.url, server.accessToken, callback)
21 } 24 }
22 25
23 function getFriendsList (podNumber, end) { 26 function getFriendsList (podNumber, end) {
24 const server = servers[podNumber - 1] 27 const server = servers[podNumber - 1]
25 return utils.getFriendsList(server.url, end) 28 return podsUtils.getFriendsList(server.url, end)
26 } 29 }
27 30
28 function uploadVideo (podNumber, callback) { 31 function uploadVideo (podNumber, callback) {
@@ -32,22 +35,22 @@ describe('Test advanced friends', function () {
32 const fixture = 'video_short.webm' 35 const fixture = 'video_short.webm'
33 const server = servers[podNumber - 1] 36 const server = servers[podNumber - 1]
34 37
35 return utils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback) 38 return videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
36 } 39 }
37 40
38 function getVideos (podNumber, callback) { 41 function getVideos (podNumber, callback) {
39 return utils.getVideosList(servers[podNumber - 1].url, callback) 42 return videosUtils.getVideosList(servers[podNumber - 1].url, callback)
40 } 43 }
41 44
42 // --------------------------------------------------------------- 45 // ---------------------------------------------------------------
43 46
44 before(function (done) { 47 before(function (done) {
45 this.timeout(30000) 48 this.timeout(30000)
46 utils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) { 49 serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
47 servers = serversRun 50 servers = serversRun
48 51
49 each(servers, function (server, callbackEach) { 52 each(servers, function (server, callbackEach) {
50 utils.loginAndGetAccessToken(server, function (err, accessToken) { 53 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
51 if (err) return callbackEach(err) 54 if (err) return callbackEach(err)
52 55
53 server.accessToken = accessToken 56 server.accessToken = accessToken
@@ -169,7 +172,7 @@ describe('Test advanced friends', function () {
169 }, 172 },
170 // Rerun server 4 173 // Rerun server 4
171 function (next) { 174 function (next) {
172 utils.runServer(4, function (server) { 175 serversUtils.runServer(4, function (server) {
173 servers[3].app = server.app 176 servers[3].app = server.app
174 next() 177 next()
175 }) 178 })
@@ -273,7 +276,7 @@ describe('Test advanced friends', function () {
273 }) 276 })
274 277
275 if (this.ok) { 278 if (this.ok) {
276 utils.flushTests(done) 279 serversUtils.flushTests(done)
277 } else { 280 } else {
278 done() 281 done()
279 } 282 }
diff --git a/server/tests/api/friendsBasic.js b/server/tests/api/friendsBasic.js
index c74a7f224..2a6883acb 100644
--- a/server/tests/api/friendsBasic.js
+++ b/server/tests/api/friendsBasic.js
@@ -5,14 +5,16 @@ const each = require('async/each')
5const expect = chai.expect 5const expect = chai.expect
6const series = require('async/series') 6const series = require('async/series')
7 7
8const utils = require('./utils') 8const loginUtils = require('../utils/login')
9const podsUtils = require('../utils/pods')
10const serversUtils = require('../utils/servers')
9 11
10describe('Test basic friends', function () { 12describe('Test basic friends', function () {
11 let servers = [] 13 let servers = []
12 14
13 function makeFriends (podNumber, callback) { 15 function makeFriends (podNumber, callback) {
14 const server = servers[podNumber - 1] 16 const server = servers[podNumber - 1]
15 return utils.makeFriends(server.url, server.accessToken, callback) 17 return podsUtils.makeFriends(server.url, server.accessToken, callback)
16 } 18 }
17 19
18 function testMadeFriends (servers, serverToTest, callback) { 20 function testMadeFriends (servers, serverToTest, callback) {
@@ -22,7 +24,7 @@ describe('Test basic friends', function () {
22 friends.push(servers[i].url) 24 friends.push(servers[i].url)
23 } 25 }
24 26
25 utils.getFriendsList(serverToTest.url, function (err, res) { 27 podsUtils.getFriendsList(serverToTest.url, function (err, res) {
26 if (err) throw err 28 if (err) throw err
27 29
28 const result = res.body 30 const result = res.body
@@ -43,11 +45,11 @@ describe('Test basic friends', function () {
43 45
44 before(function (done) { 46 before(function (done) {
45 this.timeout(20000) 47 this.timeout(20000)
46 utils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) { 48 serversUtils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) {
47 servers = serversRun 49 servers = serversRun
48 50
49 each(servers, function (server, callbackEach) { 51 each(servers, function (server, callbackEach) {
50 utils.loginAndGetAccessToken(server, function (err, accessToken) { 52 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
51 if (err) return callbackEach(err) 53 if (err) return callbackEach(err)
52 54
53 server.accessToken = accessToken 55 server.accessToken = accessToken
@@ -59,7 +61,7 @@ describe('Test basic friends', function () {
59 61
60 it('Should not have friends', function (done) { 62 it('Should not have friends', function (done) {
61 each(servers, function (server, callback) { 63 each(servers, function (server, callback) {
62 utils.getFriendsList(server.url, function (err, res) { 64 podsUtils.getFriendsList(server.url, function (err, res) {
63 if (err) throw err 65 if (err) throw err
64 66
65 const result = res.body 67 const result = res.body
@@ -84,7 +86,7 @@ describe('Test basic friends', function () {
84 }, 86 },
85 // The second pod should have the third as a friend 87 // The second pod should have the third as a friend
86 function (next) { 88 function (next) {
87 utils.getFriendsList(servers[1].url, function (err, res) { 89 podsUtils.getFriendsList(servers[1].url, function (err, res) {
88 if (err) throw err 90 if (err) throw err
89 91
90 const result = res.body 92 const result = res.body
@@ -97,7 +99,7 @@ describe('Test basic friends', function () {
97 }, 99 },
98 // Same here, the third pod should have the second pod as a friend 100 // Same here, the third pod should have the second pod as a friend
99 function (next) { 101 function (next) {
100 utils.getFriendsList(servers[2].url, function (err, res) { 102 podsUtils.getFriendsList(servers[2].url, function (err, res) {
101 if (err) throw err 103 if (err) throw err
102 104
103 const result = res.body 105 const result = res.body
@@ -128,7 +130,7 @@ describe('Test basic friends', function () {
128 130
129 it('Should not be allowed to make friend again', function (done) { 131 it('Should not be allowed to make friend again', function (done) {
130 const server = servers[1] 132 const server = servers[1]
131 utils.makeFriends(server.url, server.accessToken, 409, done) 133 podsUtils.makeFriends(server.url, server.accessToken, 409, done)
132 }) 134 })
133 135
134 it('Should quit friends of pod 2', function (done) { 136 it('Should quit friends of pod 2', function (done) {
@@ -136,11 +138,11 @@ describe('Test basic friends', function () {
136 // Pod 1 quit friends 138 // Pod 1 quit friends
137 function (next) { 139 function (next) {
138 const server = servers[1] 140 const server = servers[1]
139 utils.quitFriends(server.url, server.accessToken, next) 141 podsUtils.quitFriends(server.url, server.accessToken, next)
140 }, 142 },
141 // Pod 1 should not have friends anymore 143 // Pod 1 should not have friends anymore
142 function (next) { 144 function (next) {
143 utils.getFriendsList(servers[1].url, function (err, res) { 145 podsUtils.getFriendsList(servers[1].url, function (err, res) {
144 if (err) throw err 146 if (err) throw err
145 147
146 const result = res.body 148 const result = res.body
@@ -153,7 +155,7 @@ describe('Test basic friends', function () {
153 // Other pods shouldn't have pod 1 too 155 // Other pods shouldn't have pod 1 too
154 function (next) { 156 function (next) {
155 each([ servers[0].url, servers[2].url ], function (url, callback) { 157 each([ servers[0].url, servers[2].url ], function (url, callback) {
156 utils.getFriendsList(url, function (err, res) { 158 podsUtils.getFriendsList(url, function (err, res) {
157 if (err) throw err 159 if (err) throw err
158 160
159 const result = res.body 161 const result = res.body
@@ -169,7 +171,7 @@ describe('Test basic friends', function () {
169 171
170 it('Should allow pod 2 to make friend again', function (done) { 172 it('Should allow pod 2 to make friend again', function (done) {
171 const server = servers[1] 173 const server = servers[1]
172 utils.makeFriends(server.url, server.accessToken, function () { 174 podsUtils.makeFriends(server.url, server.accessToken, function () {
173 each(servers, function (server, callback) { 175 each(servers, function (server, callback) {
174 testMadeFriends(servers, server, callback) 176 testMadeFriends(servers, server, callback)
175 }, done) 177 }, done)
@@ -182,7 +184,7 @@ describe('Test basic friends', function () {
182 }) 184 })
183 185
184 if (this.ok) { 186 if (this.ok) {
185 utils.flushTests(done) 187 serversUtils.flushTests(done)
186 } else { 188 } else {
187 done() 189 done()
188 } 190 }
diff --git a/server/tests/api/multiplePods.js b/server/tests/api/multiplePods.js
index ac140f6bb..b86f88c22 100644
--- a/server/tests/api/multiplePods.js
+++ b/server/tests/api/multiplePods.js
@@ -6,7 +6,11 @@ const expect = chai.expect
6const pathUtils = require('path') 6const pathUtils = require('path')
7const series = require('async/series') 7const series = require('async/series')
8 8
9const utils = require('./utils') 9const loginUtils = require('../utils/login')
10const miscsUtils = require('../utils/miscs')
11const podsUtils = require('../utils/pods')
12const serversUtils = require('../utils/servers')
13const videosUtils = require('../utils/videos')
10const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) 14const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
11webtorrent.silent = true 15webtorrent.silent = true
12 16
@@ -20,7 +24,7 @@ describe('Test multiple pods', function () {
20 series([ 24 series([
21 // Run servers 25 // Run servers
22 function (next) { 26 function (next) {
23 utils.flushAndRunMultipleServers(3, function (serversRun) { 27 serversUtils.flushAndRunMultipleServers(3, function (serversRun) {
24 servers = serversRun 28 servers = serversRun
25 next() 29 next()
26 }) 30 })
@@ -28,7 +32,7 @@ describe('Test multiple pods', function () {
28 // Get the access tokens 32 // Get the access tokens
29 function (next) { 33 function (next) {
30 each(servers, function (server, callbackEach) { 34 each(servers, function (server, callbackEach) {
31 utils.loginAndGetAccessToken(server, function (err, accessToken) { 35 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
32 if (err) return callbackEach(err) 36 if (err) return callbackEach(err)
33 37
34 server.accessToken = accessToken 38 server.accessToken = accessToken
@@ -39,7 +43,7 @@ describe('Test multiple pods', function () {
39 // The second pod make friend with the third 43 // The second pod make friend with the third
40 function (next) { 44 function (next) {
41 const server = servers[1] 45 const server = servers[1]
42 utils.makeFriends(server.url, server.accessToken, next) 46 podsUtils.makeFriends(server.url, server.accessToken, next)
43 }, 47 },
44 // Wait for the request between pods 48 // Wait for the request between pods
45 function (next) { 49 function (next) {
@@ -48,7 +52,7 @@ describe('Test multiple pods', function () {
48 // Pod 1 make friends too 52 // Pod 1 make friends too
49 function (next) { 53 function (next) {
50 const server = servers[0] 54 const server = servers[0]
51 utils.makeFriends(server.url, server.accessToken, next) 55 podsUtils.makeFriends(server.url, server.accessToken, next)
52 }, 56 },
53 function (next) { 57 function (next) {
54 webtorrent.create({ host: 'client', port: '1' }, next) 58 webtorrent.create({ host: 'client', port: '1' }, next)
@@ -58,7 +62,7 @@ describe('Test multiple pods', function () {
58 62
59 it('Should not have videos for all pods', function (done) { 63 it('Should not have videos for all pods', function (done) {
60 each(servers, function (server, callback) { 64 each(servers, function (server, callback) {
61 utils.getVideosList(server.url, function (err, res) { 65 videosUtils.getVideosList(server.url, function (err, res) {
62 if (err) throw err 66 if (err) throw err
63 67
64 const videos = res.body.data 68 const videos = res.body.data
@@ -80,7 +84,7 @@ describe('Test multiple pods', function () {
80 const description = 'my super description for pod 1' 84 const description = 'my super description for pod 1'
81 const tags = [ 'tag1p1', 'tag2p1' ] 85 const tags = [ 'tag1p1', 'tag2p1' ]
82 const file = 'video_short1.webm' 86 const file = 'video_short1.webm'
83 utils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next) 87 videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next)
84 }, 88 },
85 function (next) { 89 function (next) {
86 setTimeout(next, 11000) 90 setTimeout(next, 11000)
@@ -92,7 +96,7 @@ describe('Test multiple pods', function () {
92 each(servers, function (server, callback) { 96 each(servers, function (server, callback) {
93 let baseMagnet = null 97 let baseMagnet = null
94 98
95 utils.getVideosList(server.url, function (err, res) { 99 videosUtils.getVideosList(server.url, function (err, res) {
96 if (err) throw err 100 if (err) throw err
97 101
98 const videos = res.body.data 102 const videos = res.body.data
@@ -105,7 +109,7 @@ describe('Test multiple pods', function () {
105 expect(video.magnetUri).to.exist 109 expect(video.magnetUri).to.exist
106 expect(video.duration).to.equal(10) 110 expect(video.duration).to.equal(10)
107 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ]) 111 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
108 expect(utils.dateIsValid(video.createdDate)).to.be.true 112 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
109 expect(video.author).to.equal('root') 113 expect(video.author).to.equal('root')
110 114
111 if (server.url !== 'http://localhost:9001') { 115 if (server.url !== 'http://localhost:9001') {
@@ -121,7 +125,7 @@ describe('Test multiple pods', function () {
121 expect(video.magnetUri).to.equal.magnetUri 125 expect(video.magnetUri).to.equal.magnetUri
122 } 126 }
123 127
124 utils.testImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) { 128 videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
125 if (err) throw err 129 if (err) throw err
126 expect(test).to.equal(true) 130 expect(test).to.equal(true)
127 131
@@ -142,7 +146,7 @@ describe('Test multiple pods', function () {
142 const description = 'my super description for pod 2' 146 const description = 'my super description for pod 2'
143 const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ] 147 const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ]
144 const file = 'video_short2.webm' 148 const file = 'video_short2.webm'
145 utils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next) 149 videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next)
146 }, 150 },
147 function (next) { 151 function (next) {
148 setTimeout(next, 11000) 152 setTimeout(next, 11000)
@@ -154,7 +158,7 @@ describe('Test multiple pods', function () {
154 each(servers, function (server, callback) { 158 each(servers, function (server, callback) {
155 let baseMagnet = null 159 let baseMagnet = null
156 160
157 utils.getVideosList(server.url, function (err, res) { 161 videosUtils.getVideosList(server.url, function (err, res) {
158 if (err) throw err 162 if (err) throw err
159 163
160 const videos = res.body.data 164 const videos = res.body.data
@@ -167,7 +171,7 @@ describe('Test multiple pods', function () {
167 expect(video.magnetUri).to.exist 171 expect(video.magnetUri).to.exist
168 expect(video.duration).to.equal(5) 172 expect(video.duration).to.equal(5)
169 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ]) 173 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
170 expect(utils.dateIsValid(video.createdDate)).to.be.true 174 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
171 expect(video.author).to.equal('root') 175 expect(video.author).to.equal('root')
172 176
173 if (server.url !== 'http://localhost:9002') { 177 if (server.url !== 'http://localhost:9002') {
@@ -183,7 +187,7 @@ describe('Test multiple pods', function () {
183 expect(video.magnetUri).to.equal.magnetUri 187 expect(video.magnetUri).to.equal.magnetUri
184 } 188 }
185 189
186 utils.testImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) { 190 videosUtils.testVideoImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
187 if (err) throw err 191 if (err) throw err
188 expect(test).to.equal(true) 192 expect(test).to.equal(true)
189 193
@@ -204,14 +208,14 @@ describe('Test multiple pods', function () {
204 const description = 'my super description for pod 3' 208 const description = 'my super description for pod 3'
205 const tags = [ 'tag1p3' ] 209 const tags = [ 'tag1p3' ]
206 const file = 'video_short3.webm' 210 const file = 'video_short3.webm'
207 utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) 211 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
208 }, 212 },
209 function (next) { 213 function (next) {
210 const name = 'my super name for pod 3-2' 214 const name = 'my super name for pod 3-2'
211 const description = 'my super description for pod 3-2' 215 const description = 'my super description for pod 3-2'
212 const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ] 216 const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ]
213 const file = 'video_short.webm' 217 const file = 'video_short.webm'
214 utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) 218 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
215 }, 219 },
216 function (next) { 220 function (next) {
217 setTimeout(next, 22000) 221 setTimeout(next, 22000)
@@ -222,7 +226,7 @@ describe('Test multiple pods', function () {
222 let baseMagnet = null 226 let baseMagnet = null
223 // All pods should have this video 227 // All pods should have this video
224 each(servers, function (server, callback) { 228 each(servers, function (server, callback) {
225 utils.getVideosList(server.url, function (err, res) { 229 videosUtils.getVideosList(server.url, function (err, res) {
226 if (err) throw err 230 if (err) throw err
227 231
228 const videos = res.body.data 232 const videos = res.body.data
@@ -247,7 +251,7 @@ describe('Test multiple pods', function () {
247 expect(video1.duration).to.equal(5) 251 expect(video1.duration).to.equal(5)
248 expect(video1.tags).to.deep.equal([ 'tag1p3' ]) 252 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
249 expect(video1.author).to.equal('root') 253 expect(video1.author).to.equal('root')
250 expect(utils.dateIsValid(video1.createdDate)).to.be.true 254 expect(miscsUtils.dateIsValid(video1.createdDate)).to.be.true
251 255
252 expect(video2.name).to.equal('my super name for pod 3-2') 256 expect(video2.name).to.equal('my super name for pod 3-2')
253 expect(video2.description).to.equal('my super description for pod 3-2') 257 expect(video2.description).to.equal('my super description for pod 3-2')
@@ -256,7 +260,7 @@ describe('Test multiple pods', function () {
256 expect(video2.duration).to.equal(5) 260 expect(video2.duration).to.equal(5)
257 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ]) 261 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
258 expect(video2.author).to.equal('root') 262 expect(video2.author).to.equal('root')
259 expect(utils.dateIsValid(video2.createdDate)).to.be.true 263 expect(miscsUtils.dateIsValid(video2.createdDate)).to.be.true
260 264
261 if (server.url !== 'http://localhost:9003') { 265 if (server.url !== 'http://localhost:9003') {
262 expect(video1.isLocal).to.be.false 266 expect(video1.isLocal).to.be.false
@@ -273,11 +277,11 @@ describe('Test multiple pods', function () {
273 expect(video2.magnetUri).to.equal.magnetUri 277 expect(video2.magnetUri).to.equal.magnetUri
274 } 278 }
275 279
276 utils.testImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) { 280 videosUtils.testVideoImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
277 if (err) throw err 281 if (err) throw err
278 expect(test).to.equal(true) 282 expect(test).to.equal(true)
279 283
280 utils.testImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) { 284 videosUtils.testVideoImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
281 if (err) throw err 285 if (err) throw err
282 expect(test).to.equal(true) 286 expect(test).to.equal(true)
283 287
@@ -296,7 +300,7 @@ describe('Test multiple pods', function () {
296 // Yes, this could be long 300 // Yes, this could be long
297 this.timeout(200000) 301 this.timeout(200000)
298 302
299 utils.getVideosList(servers[2].url, function (err, res) { 303 videosUtils.getVideosList(servers[2].url, function (err, res) {
300 if (err) throw err 304 if (err) throw err
301 305
302 const video = res.body.data[0] 306 const video = res.body.data[0]
@@ -317,7 +321,7 @@ describe('Test multiple pods', function () {
317 // Yes, this could be long 321 // Yes, this could be long
318 this.timeout(200000) 322 this.timeout(200000)
319 323
320 utils.getVideosList(servers[0].url, function (err, res) { 324 videosUtils.getVideosList(servers[0].url, function (err, res) {
321 if (err) throw err 325 if (err) throw err
322 326
323 const video = res.body.data[1] 327 const video = res.body.data[1]
@@ -336,7 +340,7 @@ describe('Test multiple pods', function () {
336 // Yes, this could be long 340 // Yes, this could be long
337 this.timeout(200000) 341 this.timeout(200000)
338 342
339 utils.getVideosList(servers[1].url, function (err, res) { 343 videosUtils.getVideosList(servers[1].url, function (err, res) {
340 if (err) throw err 344 if (err) throw err
341 345
342 const video = res.body.data[2] 346 const video = res.body.data[2]
@@ -355,7 +359,7 @@ describe('Test multiple pods', function () {
355 // Yes, this could be long 359 // Yes, this could be long
356 this.timeout(200000) 360 this.timeout(200000)
357 361
358 utils.getVideosList(servers[0].url, function (err, res) { 362 videosUtils.getVideosList(servers[0].url, function (err, res) {
359 if (err) throw err 363 if (err) throw err
360 364
361 const video = res.body.data[3] 365 const video = res.body.data[3]
@@ -375,10 +379,10 @@ describe('Test multiple pods', function () {
375 379
376 series([ 380 series([
377 function (next) { 381 function (next) {
378 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next) 382 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
379 }, 383 },
380 function (next) { 384 function (next) {
381 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next) 385 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
382 }], 386 }],
383 function (err) { 387 function (err) {
384 if (err) throw err 388 if (err) throw err
@@ -389,7 +393,7 @@ describe('Test multiple pods', function () {
389 393
390 it('Should have videos 1 and 3 on each pod', function (done) { 394 it('Should have videos 1 and 3 on each pod', function (done) {
391 each(servers, function (server, callback) { 395 each(servers, function (server, callback) {
392 utils.getVideosList(server.url, function (err, res) { 396 videosUtils.getVideosList(server.url, function (err, res) {
393 if (err) throw err 397 if (err) throw err
394 398
395 const videos = res.body.data 399 const videos = res.body.data
@@ -415,7 +419,7 @@ describe('Test multiple pods', function () {
415 419
416 // Keep the logs if the test failed 420 // Keep the logs if the test failed
417 if (this.ok) { 421 if (this.ok) {
418 utils.flushTests(done) 422 serversUtils.flushTests(done)
419 } else { 423 } else {
420 done() 424 done()
421 } 425 }
diff --git a/server/tests/api/singlePod.js b/server/tests/api/singlePod.js
index 6ed719f87..573eaa3a8 100644
--- a/server/tests/api/singlePod.js
+++ b/server/tests/api/singlePod.js
@@ -8,11 +8,13 @@ const keyBy = require('lodash/keyBy')
8const pathUtils = require('path') 8const pathUtils = require('path')
9const series = require('async/series') 9const series = require('async/series')
10 10
11const loginUtils = require('../utils/login')
12const miscsUtils = require('../utils/miscs')
13const serversUtils = require('../utils/servers')
14const videosUtils = require('../utils/videos')
11const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) 15const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
12webtorrent.silent = true 16webtorrent.silent = true
13 17
14const utils = require('./utils')
15
16describe('Test a single pod', function () { 18describe('Test a single pod', function () {
17 let server = null 19 let server = null
18 let videoId = -1 20 let videoId = -1
@@ -23,16 +25,16 @@ describe('Test a single pod', function () {
23 25
24 series([ 26 series([
25 function (next) { 27 function (next) {
26 utils.flushTests(next) 28 serversUtils.flushTests(next)
27 }, 29 },
28 function (next) { 30 function (next) {
29 utils.runServer(1, function (server1) { 31 serversUtils.runServer(1, function (server1) {
30 server = server1 32 server = server1
31 next() 33 next()
32 }) 34 })
33 }, 35 },
34 function (next) { 36 function (next) {
35 utils.loginAndGetAccessToken(server, function (err, token) { 37 loginUtils.loginAndGetAccessToken(server, function (err, token) {
36 if (err) throw err 38 if (err) throw err
37 server.accessToken = token 39 server.accessToken = token
38 next() 40 next()
@@ -45,7 +47,7 @@ describe('Test a single pod', function () {
45 }) 47 })
46 48
47 it('Should not have videos', function (done) { 49 it('Should not have videos', function (done) {
48 utils.getVideosList(server.url, function (err, res) { 50 videosUtils.getVideosList(server.url, function (err, res) {
49 if (err) throw err 51 if (err) throw err
50 52
51 expect(res.body.total).to.equal(0) 53 expect(res.body.total).to.equal(0)
@@ -62,14 +64,14 @@ describe('Test a single pod', function () {
62 const description = 'my super description' 64 const description = 'my super description'
63 const tags = [ 'tag1', 'tag2', 'tag3' ] 65 const tags = [ 'tag1', 'tag2', 'tag3' ]
64 const file = 'video_short.webm' 66 const file = 'video_short.webm'
65 utils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done) 67 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
66 }) 68 })
67 69
68 it('Should seed the uploaded video', function (done) { 70 it('Should seed the uploaded video', function (done) {
69 // Yes, this could be long 71 // Yes, this could be long
70 this.timeout(60000) 72 this.timeout(60000)
71 73
72 utils.getVideosList(server.url, function (err, res) { 74 videosUtils.getVideosList(server.url, function (err, res) {
73 if (err) throw err 75 if (err) throw err
74 76
75 expect(res.body.total).to.equal(1) 77 expect(res.body.total).to.equal(1)
@@ -84,9 +86,9 @@ describe('Test a single pod', function () {
84 expect(video.author).to.equal('root') 86 expect(video.author).to.equal('root')
85 expect(video.isLocal).to.be.true 87 expect(video.isLocal).to.be.true
86 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) 88 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
87 expect(utils.dateIsValid(video.createdDate)).to.be.true 89 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
88 90
89 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { 91 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
90 if (err) throw err 92 if (err) throw err
91 expect(test).to.equal(true) 93 expect(test).to.equal(true)
92 94
@@ -108,7 +110,7 @@ describe('Test a single pod', function () {
108 // Yes, this could be long 110 // Yes, this could be long
109 this.timeout(60000) 111 this.timeout(60000)
110 112
111 utils.getVideo(server.url, videoId, function (err, res) { 113 videosUtils.getVideo(server.url, videoId, function (err, res) {
112 if (err) throw err 114 if (err) throw err
113 115
114 const video = res.body 116 const video = res.body
@@ -119,9 +121,9 @@ describe('Test a single pod', function () {
119 expect(video.author).to.equal('root') 121 expect(video.author).to.equal('root')
120 expect(video.isLocal).to.be.true 122 expect(video.isLocal).to.be.true
121 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) 123 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
122 expect(utils.dateIsValid(video.createdDate)).to.be.true 124 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
123 125
124 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { 126 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
125 if (err) throw err 127 if (err) throw err
126 expect(test).to.equal(true) 128 expect(test).to.equal(true)
127 129
@@ -137,7 +139,7 @@ describe('Test a single pod', function () {
137 }) 139 })
138 140
139 it('Should search the video by name by default', function (done) { 141 it('Should search the video by name by default', function (done) {
140 utils.searchVideo(server.url, 'my', function (err, res) { 142 videosUtils.searchVideo(server.url, 'my', function (err, res) {
141 if (err) throw err 143 if (err) throw err
142 144
143 expect(res.body.total).to.equal(1) 145 expect(res.body.total).to.equal(1)
@@ -151,9 +153,9 @@ describe('Test a single pod', function () {
151 expect(video.author).to.equal('root') 153 expect(video.author).to.equal('root')
152 expect(video.isLocal).to.be.true 154 expect(video.isLocal).to.be.true
153 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) 155 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
154 expect(utils.dateIsValid(video.createdDate)).to.be.true 156 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
155 157
156 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { 158 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
157 if (err) throw err 159 if (err) throw err
158 expect(test).to.equal(true) 160 expect(test).to.equal(true)
159 161
@@ -163,7 +165,7 @@ describe('Test a single pod', function () {
163 }) 165 })
164 166
165 it('Should search the video by podUrl', function (done) { 167 it('Should search the video by podUrl', function (done) {
166 utils.searchVideo(server.url, '9001', 'podUrl', function (err, res) { 168 videosUtils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
167 if (err) throw err 169 if (err) throw err
168 170
169 expect(res.body.total).to.equal(1) 171 expect(res.body.total).to.equal(1)
@@ -177,9 +179,9 @@ describe('Test a single pod', function () {
177 expect(video.author).to.equal('root') 179 expect(video.author).to.equal('root')
178 expect(video.isLocal).to.be.true 180 expect(video.isLocal).to.be.true
179 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) 181 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
180 expect(utils.dateIsValid(video.createdDate)).to.be.true 182 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
181 183
182 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { 184 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
183 if (err) throw err 185 if (err) throw err
184 expect(test).to.equal(true) 186 expect(test).to.equal(true)
185 187
@@ -189,7 +191,7 @@ describe('Test a single pod', function () {
189 }) 191 })
190 192
191 it('Should search the video by tag', function (done) { 193 it('Should search the video by tag', function (done) {
192 utils.searchVideo(server.url, 'tag1', 'tags', function (err, res) { 194 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
193 if (err) throw err 195 if (err) throw err
194 196
195 expect(res.body.total).to.equal(1) 197 expect(res.body.total).to.equal(1)
@@ -203,9 +205,9 @@ describe('Test a single pod', function () {
203 expect(video.author).to.equal('root') 205 expect(video.author).to.equal('root')
204 expect(video.isLocal).to.be.true 206 expect(video.isLocal).to.be.true
205 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) 207 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
206 expect(utils.dateIsValid(video.createdDate)).to.be.true 208 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
207 209
208 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { 210 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
209 if (err) throw err 211 if (err) throw err
210 expect(test).to.equal(true) 212 expect(test).to.equal(true)
211 213
@@ -215,7 +217,7 @@ describe('Test a single pod', function () {
215 }) 217 })
216 218
217 it('Should not find a search by name by default', function (done) { 219 it('Should not find a search by name by default', function (done) {
218 utils.searchVideo(server.url, 'hello', function (err, res) { 220 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
219 if (err) throw err 221 if (err) throw err
220 222
221 expect(res.body.total).to.equal(0) 223 expect(res.body.total).to.equal(0)
@@ -227,7 +229,7 @@ describe('Test a single pod', function () {
227 }) 229 })
228 230
229 it('Should not find a search by author', function (done) { 231 it('Should not find a search by author', function (done) {
230 utils.searchVideo(server.url, 'hello', 'author', function (err, res) { 232 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
231 if (err) throw err 233 if (err) throw err
232 234
233 expect(res.body.total).to.equal(0) 235 expect(res.body.total).to.equal(0)
@@ -239,7 +241,7 @@ describe('Test a single pod', function () {
239 }) 241 })
240 242
241 it('Should not find a search by tag', function (done) { 243 it('Should not find a search by tag', function (done) {
242 utils.searchVideo(server.url, 'tag', 'tags', function (err, res) { 244 videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
243 if (err) throw err 245 if (err) throw err
244 246
245 expect(res.body.total).to.equal(0) 247 expect(res.body.total).to.equal(0)
@@ -251,7 +253,7 @@ describe('Test a single pod', function () {
251 }) 253 })
252 254
253 it('Should remove the video', function (done) { 255 it('Should remove the video', function (done) {
254 utils.removeVideo(server.url, server.accessToken, videoId, function (err) { 256 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
255 if (err) throw err 257 if (err) throw err
256 258
257 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) { 259 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
@@ -264,7 +266,7 @@ describe('Test a single pod', function () {
264 }) 266 })
265 267
266 it('Should not have videos', function (done) { 268 it('Should not have videos', function (done) {
267 utils.getVideosList(server.url, function (err, res) { 269 videosUtils.getVideosList(server.url, function (err, res) {
268 if (err) throw err 270 if (err) throw err
269 271
270 expect(res.body.total).to.equal(0) 272 expect(res.body.total).to.equal(0)
@@ -286,12 +288,12 @@ describe('Test a single pod', function () {
286 const description = video + ' description' 288 const description = video + ' description'
287 const tags = [ 'tag1', 'tag2', 'tag3' ] 289 const tags = [ 'tag1', 'tag2', 'tag3' ]
288 290
289 utils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach) 291 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
290 }, done) 292 }, done)
291 }) 293 })
292 294
293 it('Should have the correct durations', function (done) { 295 it('Should have the correct durations', function (done) {
294 utils.getVideosList(server.url, function (err, res) { 296 videosUtils.getVideosList(server.url, function (err, res) {
295 if (err) throw err 297 if (err) throw err
296 298
297 expect(res.body.total).to.equal(6) 299 expect(res.body.total).to.equal(6)
@@ -312,7 +314,7 @@ describe('Test a single pod', function () {
312 }) 314 })
313 315
314 it('Should have the correct thumbnails', function (done) { 316 it('Should have the correct thumbnails', function (done) {
315 utils.getVideosList(server.url, function (err, res) { 317 videosUtils.getVideosList(server.url, function (err, res) {
316 if (err) throw err 318 if (err) throw err
317 319
318 const videos = res.body.data 320 const videos = res.body.data
@@ -323,7 +325,7 @@ describe('Test a single pod', function () {
323 if (err) throw err 325 if (err) throw err
324 const videoName = video.name.replace(' name', '') 326 const videoName = video.name.replace(' name', '')
325 327
326 utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) { 328 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
327 if (err) throw err 329 if (err) throw err
328 330
329 expect(test).to.equal(true) 331 expect(test).to.equal(true)
@@ -334,7 +336,7 @@ describe('Test a single pod', function () {
334 }) 336 })
335 337
336 it('Should list only the two first videos', function (done) { 338 it('Should list only the two first videos', function (done) {
337 utils.getVideosListPagination(server.url, 0, 2, function (err, res) { 339 videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
338 if (err) throw err 340 if (err) throw err
339 341
340 const videos = res.body.data 342 const videos = res.body.data
@@ -348,7 +350,7 @@ describe('Test a single pod', function () {
348 }) 350 })
349 351
350 it('Should list only the next three videos', function (done) { 352 it('Should list only the next three videos', function (done) {
351 utils.getVideosListPagination(server.url, 2, 3, function (err, res) { 353 videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
352 if (err) throw err 354 if (err) throw err
353 355
354 const videos = res.body.data 356 const videos = res.body.data
@@ -363,7 +365,7 @@ describe('Test a single pod', function () {
363 }) 365 })
364 366
365 it('Should list the last video', function (done) { 367 it('Should list the last video', function (done) {
366 utils.getVideosListPagination(server.url, 5, 6, function (err, res) { 368 videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
367 if (err) throw err 369 if (err) throw err
368 370
369 const videos = res.body.data 371 const videos = res.body.data
@@ -376,7 +378,7 @@ describe('Test a single pod', function () {
376 }) 378 })
377 379
378 it('Should search the first video', function (done) { 380 it('Should search the first video', function (done) {
379 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) { 381 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
380 if (err) throw err 382 if (err) throw err
381 383
382 const videos = res.body.data 384 const videos = res.body.data
@@ -389,7 +391,7 @@ describe('Test a single pod', function () {
389 }) 391 })
390 392
391 it('Should search the last two videos', function (done) { 393 it('Should search the last two videos', function (done) {
392 utils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) { 394 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
393 if (err) throw err 395 if (err) throw err
394 396
395 const videos = res.body.data 397 const videos = res.body.data
@@ -403,7 +405,7 @@ describe('Test a single pod', function () {
403 }) 405 })
404 406
405 it('Should search all the webm videos', function (done) { 407 it('Should search all the webm videos', function (done) {
406 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) { 408 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
407 if (err) throw err 409 if (err) throw err
408 410
409 const videos = res.body.data 411 const videos = res.body.data
@@ -415,7 +417,7 @@ describe('Test a single pod', function () {
415 }) 417 })
416 418
417 it('Should search all the root author videos', function (done) { 419 it('Should search all the root author videos', function (done) {
418 utils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) { 420 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
419 if (err) throw err 421 if (err) throw err
420 422
421 const videos = res.body.data 423 const videos = res.body.data
@@ -427,7 +429,7 @@ describe('Test a single pod', function () {
427 }) 429 })
428 430
429 it('Should search all the 9001 port videos', function (done) { 431 it('Should search all the 9001 port videos', function (done) {
430 utils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) { 432 videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
431 if (err) throw err 433 if (err) throw err
432 434
433 const videos = res.body.data 435 const videos = res.body.data
@@ -439,7 +441,7 @@ describe('Test a single pod', function () {
439 }) 441 })
440 442
441 it('Should search all the localhost videos', function (done) { 443 it('Should search all the localhost videos', function (done) {
442 utils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) { 444 videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
443 if (err) throw err 445 if (err) throw err
444 446
445 const videos = res.body.data 447 const videos = res.body.data
@@ -452,7 +454,7 @@ describe('Test a single pod', function () {
452 454
453 it('Should search the good magnetUri video', function (done) { 455 it('Should search the good magnetUri video', function (done) {
454 const video = videosListBase[0] 456 const video = videosListBase[0]
455 utils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) { 457 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
456 if (err) throw err 458 if (err) throw err
457 459
458 const videos = res.body.data 460 const videos = res.body.data
@@ -465,7 +467,7 @@ describe('Test a single pod', function () {
465 }) 467 })
466 468
467 it('Should list and sort by name in descending order', function (done) { 469 it('Should list and sort by name in descending order', function (done) {
468 utils.getVideosListSort(server.url, '-name', function (err, res) { 470 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
469 if (err) throw err 471 if (err) throw err
470 472
471 const videos = res.body.data 473 const videos = res.body.data
@@ -483,7 +485,7 @@ describe('Test a single pod', function () {
483 }) 485 })
484 486
485 it('Should search and sort by name in ascending order', function (done) { 487 it('Should search and sort by name in ascending order', function (done) {
486 utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) { 488 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
487 if (err) throw err 489 if (err) throw err
488 490
489 const videos = res.body.data 491 const videos = res.body.data
@@ -505,7 +507,7 @@ describe('Test a single pod', function () {
505 507
506 // Keep the logs if the test failed 508 // Keep the logs if the test failed
507 if (this.ok) { 509 if (this.ok) {
508 utils.flushTests(done) 510 serversUtils.flushTests(done)
509 } else { 511 } else {
510 done() 512 done()
511 } 513 }
diff --git a/server/tests/api/users.js b/server/tests/api/users.js
index e1d4a8cf4..6f9eef181 100644
--- a/server/tests/api/users.js
+++ b/server/tests/api/users.js
@@ -5,11 +5,14 @@ const expect = chai.expect
5const pathUtils = require('path') 5const pathUtils = require('path')
6const series = require('async/series') 6const series = require('async/series')
7 7
8const loginUtils = require('../utils/login')
9const podsUtils = require('../utils/pods')
10const serversUtils = require('../utils/servers')
11const usersUtils = require('../utils/users')
12const videosUtils = require('../utils/videos')
8const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) 13const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9webtorrent.silent = true 14webtorrent.silent = true
10 15
11const utils = require('./utils')
12
13describe('Test users', function () { 16describe('Test users', function () {
14 let server = null 17 let server = null
15 let accessToken = null 18 let accessToken = null
@@ -22,10 +25,10 @@ describe('Test users', function () {
22 25
23 series([ 26 series([
24 function (next) { 27 function (next) {
25 utils.flushTests(next) 28 serversUtils.flushTests(next)
26 }, 29 },
27 function (next) { 30 function (next) {
28 utils.runServer(1, function (server1) { 31 serversUtils.runServer(1, function (server1) {
29 server = server1 32 server = server1
30 next() 33 next()
31 }) 34 })
@@ -41,7 +44,7 @@ describe('Test users', function () {
41 44
42 it('Should not login with an invalid client id', function (done) { 45 it('Should not login with an invalid client id', function (done) {
43 const client = { id: 'client', password: server.client.secret } 46 const client = { id: 'client', password: server.client.secret }
44 utils.login(server.url, client, server.user, 400, function (err, res) { 47 loginUtils.login(server.url, client, server.user, 400, function (err, res) {
45 if (err) throw err 48 if (err) throw err
46 49
47 expect(res.body.error).to.equal('invalid_client') 50 expect(res.body.error).to.equal('invalid_client')
@@ -51,7 +54,7 @@ describe('Test users', function () {
51 54
52 it('Should not login with an invalid client password', function (done) { 55 it('Should not login with an invalid client password', function (done) {
53 const client = { id: server.client.id, password: 'coucou' } 56 const client = { id: server.client.id, password: 'coucou' }
54 utils.login(server.url, client, server.user, 400, function (err, res) { 57 loginUtils.login(server.url, client, server.user, 400, function (err, res) {
55 if (err) throw err 58 if (err) throw err
56 59
57 expect(res.body.error).to.equal('invalid_client') 60 expect(res.body.error).to.equal('invalid_client')
@@ -61,7 +64,7 @@ describe('Test users', function () {
61 64
62 it('Should not login with an invalid username', function (done) { 65 it('Should not login with an invalid username', function (done) {
63 const user = { username: 'captain crochet', password: server.user.password } 66 const user = { username: 'captain crochet', password: server.user.password }
64 utils.login(server.url, server.client, user, 400, function (err, res) { 67 loginUtils.login(server.url, server.client, user, 400, function (err, res) {
65 if (err) throw err 68 if (err) throw err
66 69
67 expect(res.body.error).to.equal('invalid_grant') 70 expect(res.body.error).to.equal('invalid_grant')
@@ -71,7 +74,7 @@ describe('Test users', function () {
71 74
72 it('Should not login with an invalid password', function (done) { 75 it('Should not login with an invalid password', function (done) {
73 const user = { username: server.user.username, password: 'mewthree' } 76 const user = { username: server.user.username, password: 'mewthree' }
74 utils.login(server.url, server.client, user, 400, function (err, res) { 77 loginUtils.login(server.url, server.client, user, 400, function (err, res) {
75 if (err) throw err 78 if (err) throw err
76 79
77 expect(res.body.error).to.equal('invalid_grant') 80 expect(res.body.error).to.equal('invalid_grant')
@@ -86,21 +89,21 @@ describe('Test users', function () {
86 const description = 'my super description' 89 const description = 'my super description'
87 const tags = [ 'tag1', 'tag2' ] 90 const tags = [ 'tag1', 'tag2' ]
88 const video = 'video_short.webm' 91 const video = 'video_short.webm'
89 utils.uploadVideo(server.url, accessToken, name, description, tags, video, 401, done) 92 videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 401, done)
90 }) 93 })
91 94
92 it('Should not be able to make friends', function (done) { 95 it('Should not be able to make friends', function (done) {
93 accessToken = 'mysupertoken' 96 accessToken = 'mysupertoken'
94 utils.makeFriends(server.url, accessToken, 401, done) 97 podsUtils.makeFriends(server.url, accessToken, 401, done)
95 }) 98 })
96 99
97 it('Should not be able to quit friends', function (done) { 100 it('Should not be able to quit friends', function (done) {
98 accessToken = 'mysupertoken' 101 accessToken = 'mysupertoken'
99 utils.quitFriends(server.url, accessToken, 401, done) 102 podsUtils.quitFriends(server.url, accessToken, 401, done)
100 }) 103 })
101 104
102 it('Should be able to login', function (done) { 105 it('Should be able to login', function (done) {
103 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) {
104 if (err) throw err 107 if (err) throw err
105 108
106 accessToken = res.body.access_token 109 accessToken = res.body.access_token
@@ -113,10 +116,10 @@ describe('Test users', function () {
113 const description = 'my super description' 116 const description = 'my super description'
114 const tags = [ 'tag1', 'tag2' ] 117 const tags = [ 'tag1', 'tag2' ]
115 const video = 'video_short.webm' 118 const video = 'video_short.webm'
116 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) {
117 if (err) throw err 120 if (err) throw err
118 121
119 utils.getVideosList(server.url, function (err, res) { 122 videosUtils.getVideosList(server.url, function (err, res) {
120 if (err) throw err 123 if (err) throw err
121 124
122 const video = res.body.data[0] 125 const video = res.body.data[0]
@@ -133,17 +136,17 @@ describe('Test users', function () {
133 const description = 'my super description 2' 136 const description = 'my super description 2'
134 const tags = [ 'tag1' ] 137 const tags = [ 'tag1' ]
135 const video = 'video_short.webm' 138 const video = 'video_short.webm'
136 utils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done) 139 videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done)
137 }) 140 })
138 141
139 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) {
140 utils.removeVideo(server.url, 'bad_token', videoId, 401, done) 143 videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done)
141 }) 144 })
142 145
143 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')
144 147
145 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) {
146 utils.removeVideo(server.url, accessToken, videoId, done) 149 videosUtils.removeVideo(server.url, accessToken, videoId, done)
147 }) 150 })
148 151
149 it('Should logout (revoke token)') 152 it('Should logout (revoke token)')
@@ -161,7 +164,7 @@ describe('Test users', function () {
161 it('Should be able to upload a video again') 164 it('Should be able to upload a video again')
162 165
163 it('Should be able to create a new user', function (done) { 166 it('Should be able to create a new user', function (done) {
164 utils.createUser(server.url, accessToken, 'user_1', 'super password', done) 167 usersUtils.createUser(server.url, accessToken, 'user_1', 'super password', done)
165 }) 168 })
166 169
167 it('Should be able to login with this user', function (done) { 170 it('Should be able to login with this user', function (done) {
@@ -170,7 +173,7 @@ describe('Test users', function () {
170 password: 'super password' 173 password: 'super password'
171 } 174 }
172 175
173 utils.loginAndGetAccessToken(server, function (err, token) { 176 loginUtils.loginAndGetAccessToken(server, function (err, token) {
174 if (err) throw err 177 if (err) throw err
175 178
176 accessTokenUser = token 179 accessTokenUser = token
@@ -180,7 +183,7 @@ describe('Test users', function () {
180 }) 183 })
181 184
182 it('Should be able to get the user informations', function (done) { 185 it('Should be able to get the user informations', function (done) {
183 utils.getUserInformation(server.url, accessTokenUser, function (err, res) { 186 usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) {
184 if (err) throw err 187 if (err) throw err
185 188
186 const user = res.body 189 const user = res.body
@@ -199,11 +202,11 @@ describe('Test users', function () {
199 const description = 'my super description' 202 const description = 'my super description'
200 const tags = [ 'tag1', 'tag2', 'tag3' ] 203 const tags = [ 'tag1', 'tag2', 'tag3' ]
201 const file = 'video_short.webm' 204 const file = 'video_short.webm'
202 utils.uploadVideo(server.url, accessTokenUser, name, description, tags, file, done) 205 videosUtils.uploadVideo(server.url, accessTokenUser, name, description, tags, file, done)
203 }) 206 })
204 207
205 it('Should list all the users', function (done) { 208 it('Should list all the users', function (done) {
206 utils.getUsersList(server.url, function (err, res) { 209 usersUtils.getUsersList(server.url, function (err, res) {
207 if (err) throw err 210 if (err) throw err
208 211
209 const users = res.body.data 212 const users = res.body.data
@@ -223,25 +226,25 @@ describe('Test users', function () {
223 }) 226 })
224 227
225 it('Should update the user password', function (done) { 228 it('Should update the user password', function (done) {
226 utils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) { 229 usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) {
227 if (err) throw err 230 if (err) throw err
228 231
229 server.user.password = 'new password' 232 server.user.password = 'new password'
230 utils.login(server.url, server.client, server.user, 200, done) 233 loginUtils.login(server.url, server.client, server.user, 200, done)
231 }) 234 })
232 }) 235 })
233 236
234 it('Should be able to remove this user', function (done) { 237 it('Should be able to remove this user', function (done) {
235 utils.removeUser(server.url, accessToken, 'user_1', done) 238 usersUtils.removeUser(server.url, accessToken, 'user_1', done)
236 }) 239 })
237 240
238 it('Should not be able to login with this user', function (done) { 241 it('Should not be able to login with this user', function (done) {
239 // server.user is already set to user 1 242 // server.user is already set to user 1
240 utils.login(server.url, server.client, server.user, 400, done) 243 loginUtils.login(server.url, server.client, server.user, 400, done)
241 }) 244 })
242 245
243 it('Should not have videos of this user', function (done) { 246 it('Should not have videos of this user', function (done) {
244 utils.getVideosList(server.url, function (err, res) { 247 videosUtils.getVideosList(server.url, function (err, res) {
245 if (err) throw err 248 if (err) throw err
246 249
247 expect(res.body.total).to.equal(1) 250 expect(res.body.total).to.equal(1)
@@ -257,7 +260,7 @@ describe('Test users', function () {
257 260
258 // Keep the logs if the test failed 261 // Keep the logs if the test failed
259 if (this.ok) { 262 if (this.ok) {
260 utils.flushTests(done) 263 serversUtils.flushTests(done)
261 } else { 264 } else {
262 done() 265 done()
263 } 266 }
diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js
deleted file mode 100644
index 8871f1f84..000000000
--- a/server/tests/api/utils.js
+++ /dev/null
@@ -1,492 +0,0 @@
1'use strict'
2
3const childProcess = require('child_process')
4const exec = childProcess.exec
5const fork = childProcess.fork
6const fs = require('fs')
7const pathUtils = require('path')
8const request = require('supertest')
9
10const testUtils = {
11 createUser: createUser,
12 dateIsValid: dateIsValid,
13 flushTests: flushTests,
14 getAllVideosListBy: getAllVideosListBy,
15 getClient: getClient,
16 getFriendsList: getFriendsList,
17 getUserInformation: getUserInformation,
18 getUsersList: getUsersList,
19 getVideo: getVideo,
20 getVideosList: getVideosList,
21 getVideosListPagination: getVideosListPagination,
22 getVideosListSort: getVideosListSort,
23 login: login,
24 loginAndGetAccessToken: loginAndGetAccessToken,
25 makeFriends: makeFriends,
26 quitFriends: quitFriends,
27 removeUser: removeUser,
28 removeVideo: removeVideo,
29 flushAndRunMultipleServers: flushAndRunMultipleServers,
30 runServer: runServer,
31 searchVideo: searchVideo,
32 searchVideoWithPagination: searchVideoWithPagination,
33 searchVideoWithSort: searchVideoWithSort,
34 testImage: testImage,
35 uploadVideo: uploadVideo,
36 updateUser: updateUser
37}
38
39// ---------------------- Export functions --------------------
40
41function createUser (url, accessToken, username, password, specialStatus, end) {
42 if (!end) {
43 end = specialStatus
44 specialStatus = 204
45 }
46
47 const path = '/api/v1/users'
48
49 request(url)
50 .post(path)
51 .set('Accept', 'application/json')
52 .set('Authorization', 'Bearer ' + accessToken)
53 .send({ username: username, password: password })
54 .expect(specialStatus)
55 .end(end)
56}
57
58function dateIsValid (dateString) {
59 const dateToCheck = new Date(dateString)
60 const now = new Date()
61
62 // Check if the interval is more than 2 minutes
63 if (now - dateToCheck > 120000) return false
64
65 return true
66}
67
68function flushTests (callback) {
69 exec('npm run clean:server:test', callback)
70}
71
72function getAllVideosListBy (url, end) {
73 const path = '/api/v1/videos'
74
75 request(url)
76 .get(path)
77 .query({ sort: 'createdDate' })
78 .query({ start: 0 })
79 .query({ count: 10000 })
80 .set('Accept', 'application/json')
81 .expect(200)
82 .expect('Content-Type', /json/)
83 .end(end)
84}
85
86function getClient (url, end) {
87 const path = '/api/v1/users/client'
88
89 request(url)
90 .get(path)
91 .set('Accept', 'application/json')
92 .expect(200)
93 .expect('Content-Type', /json/)
94 .end(end)
95}
96
97function getUserInformation (url, accessToken, end) {
98 const path = '/api/v1/users/me'
99
100 request(url)
101 .get(path)
102 .set('Accept', 'application/json')
103 .set('Authorization', 'Bearer ' + accessToken)
104 .expect(200)
105 .expect('Content-Type', /json/)
106 .end(end)
107}
108
109function getUsersList (url, end) {
110 const path = '/api/v1/users'
111
112 request(url)
113 .get(path)
114 .set('Accept', 'application/json')
115 .expect(200)
116 .expect('Content-Type', /json/)
117 .end(end)
118}
119
120function getFriendsList (url, end) {
121 const path = '/api/v1/pods/'
122
123 request(url)
124 .get(path)
125 .set('Accept', 'application/json')
126 .expect(200)
127 .expect('Content-Type', /json/)
128 .end(end)
129}
130
131function getVideo (url, id, end) {
132 const path = '/api/v1/videos/' + id
133
134 request(url)
135 .get(path)
136 .set('Accept', 'application/json')
137 .expect(200)
138 .expect('Content-Type', /json/)
139 .end(end)
140}
141
142function getVideosList (url, end) {
143 const path = '/api/v1/videos'
144
145 request(url)
146 .get(path)
147 .query({ sort: 'name' })
148 .set('Accept', 'application/json')
149 .expect(200)
150 .expect('Content-Type', /json/)
151 .end(end)
152}
153
154function getVideosListPagination (url, start, count, end) {
155 const path = '/api/v1/videos'
156
157 request(url)
158 .get(path)
159 .query({ start: start })
160 .query({ count: count })
161 .set('Accept', 'application/json')
162 .expect(200)
163 .expect('Content-Type', /json/)
164 .end(end)
165}
166
167function getVideosListSort (url, sort, end) {
168 const path = '/api/v1/videos'
169
170 request(url)
171 .get(path)
172 .query({ sort: sort })
173 .set('Accept', 'application/json')
174 .expect(200)
175 .expect('Content-Type', /json/)
176 .end(end)
177}
178
179function login (url, client, user, expectedStatus, end) {
180 if (!end) {
181 end = expectedStatus
182 expectedStatus = 200
183 }
184
185 const path = '/api/v1/users/token'
186
187 const body = {
188 client_id: client.id,
189 client_secret: client.secret,
190 username: user.username,
191 password: user.password,
192 response_type: 'code',
193 grant_type: 'password',
194 scope: 'upload'
195 }
196
197 request(url)
198 .post(path)
199 .type('form')
200 .send(body)
201 .expect(expectedStatus)
202 .end(end)
203}
204
205function loginAndGetAccessToken (server, callback) {
206 login(server.url, server.client, server.user, 200, function (err, res) {
207 if (err) return callback(err)
208
209 return callback(null, res.body.access_token)
210 })
211}
212
213function makeFriends (url, accessToken, expectedStatus, callback) {
214 if (!callback) {
215 callback = expectedStatus
216 expectedStatus = 204
217 }
218
219 const path = '/api/v1/pods/makefriends'
220
221 // The first pod make friend with the third
222 request(url)
223 .get(path)
224 .set('Accept', 'application/json')
225 .set('Authorization', 'Bearer ' + accessToken)
226 .expect(expectedStatus)
227 .end(function (err, res) {
228 if (err) throw err
229
230 // Wait for the request between pods
231 setTimeout(callback, 1000)
232 })
233}
234
235function quitFriends (url, accessToken, expectedStatus, callback) {
236 if (!callback) {
237 callback = expectedStatus
238 expectedStatus = 204
239 }
240
241 const path = '/api/v1/pods/quitfriends'
242
243 // The first pod make friend with the third
244 request(url)
245 .get(path)
246 .set('Accept', 'application/json')
247 .set('Authorization', 'Bearer ' + accessToken)
248 .expect(expectedStatus)
249 .end(function (err, res) {
250 if (err) throw err
251
252 // Wait for the request between pods
253 setTimeout(callback, 1000)
254 })
255}
256
257function removeUser (url, token, username, expectedStatus, end) {
258 if (!end) {
259 end = expectedStatus
260 expectedStatus = 204
261 }
262
263 const path = '/api/v1/users'
264
265 request(url)
266 .delete(path + '/' + username)
267 .set('Accept', 'application/json')
268 .set('Authorization', 'Bearer ' + token)
269 .expect(expectedStatus)
270 .end(end)
271}
272
273function removeVideo (url, token, id, expectedStatus, end) {
274 if (!end) {
275 end = expectedStatus
276 expectedStatus = 204
277 }
278
279 const path = '/api/v1/videos'
280
281 request(url)
282 .delete(path + '/' + id)
283 .set('Accept', 'application/json')
284 .set('Authorization', 'Bearer ' + token)
285 .expect(expectedStatus)
286 .end(end)
287}
288
289function flushAndRunMultipleServers (totalServers, serversRun) {
290 let apps = []
291 let urls = []
292 let i = 0
293
294 function anotherServerDone (number, app, url) {
295 apps[number - 1] = app
296 urls[number - 1] = url
297 i++
298 if (i === totalServers) {
299 serversRun(apps, urls)
300 }
301 }
302
303 flushTests(function () {
304 for (let j = 1; j <= totalServers; j++) {
305 // For the virtual buffer
306 setTimeout(function () {
307 runServer(j, function (app, url) {
308 anotherServerDone(j, app, url)
309 })
310 }, 1000 * j)
311 }
312 })
313}
314
315function runServer (number, callback) {
316 const server = {
317 app: null,
318 url: `http://localhost:${9000 + number}`,
319 client: {
320 id: null,
321 secret: null
322 },
323 user: {
324 username: null,
325 password: null
326 }
327 }
328
329 // These actions are async so we need to be sure that they have both been done
330 const serverRunString = {
331 'Connected to mongodb': false,
332 'Server listening on port': false
333 }
334
335 const regexps = {
336 client_id: 'Client id: ([a-f0-9]+)',
337 client_secret: 'Client secret: (.+)',
338 user_username: 'Username: (.+)',
339 user_password: 'User password: (.+)'
340 }
341
342 // Share the environment
343 const env = Object.create(process.env)
344 env.NODE_ENV = 'test'
345 env.NODE_APP_INSTANCE = number
346 const options = {
347 silent: true,
348 env: env,
349 detached: true
350 }
351
352 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
353 server.app.stdout.on('data', function onStdout (data) {
354 let dontContinue = false
355
356 // Capture things if we want to
357 for (const key of Object.keys(regexps)) {
358 const regexp = regexps[key]
359 const matches = data.toString().match(regexp)
360 if (matches !== null) {
361 if (key === 'client_id') server.client.id = matches[1]
362 else if (key === 'client_secret') server.client.secret = matches[1]
363 else if (key === 'user_username') server.user.username = matches[1]
364 else if (key === 'user_password') server.user.password = matches[1]
365 }
366 }
367
368 // Check if all required sentences are here
369 for (const key of Object.keys(serverRunString)) {
370 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
371 if (serverRunString[key] === false) dontContinue = true
372 }
373
374 // If no, there is maybe one thing not already initialized (mongodb...)
375 if (dontContinue === true) return
376
377 server.app.stdout.removeListener('data', onStdout)
378 callback(server)
379 })
380}
381
382function searchVideo (url, search, field, end) {
383 if (!end) {
384 end = field
385 field = null
386 }
387
388 const path = '/api/v1/videos'
389 const req = request(url)
390 .get(path + '/search/' + search)
391 .set('Accept', 'application/json')
392
393 if (field) req.query({ field: field })
394 req.expect(200)
395 .expect('Content-Type', /json/)
396 .end(end)
397}
398
399function searchVideoWithPagination (url, search, field, start, count, end) {
400 const path = '/api/v1/videos'
401
402 request(url)
403 .get(path + '/search/' + search)
404 .query({ start: start })
405 .query({ count: count })
406 .query({ field: field })
407 .set('Accept', 'application/json')
408 .expect(200)
409 .expect('Content-Type', /json/)
410 .end(end)
411}
412
413function searchVideoWithSort (url, search, sort, end) {
414 const path = '/api/v1/videos'
415
416 request(url)
417 .get(path + '/search/' + search)
418 .query({ sort: sort })
419 .set('Accept', 'application/json')
420 .expect(200)
421 .expect('Content-Type', /json/)
422 .end(end)
423}
424
425function testImage (url, videoName, imagePath, callback) {
426 // Don't test images if the node env is not set
427 // Because we need a special ffmpeg version for this test
428 if (process.env.NODE_TEST_IMAGE) {
429 request(url)
430 .get(imagePath)
431 .expect(200)
432 .end(function (err, res) {
433 if (err) return callback(err)
434
435 fs.readFile(pathUtils.join(__dirname, 'fixtures', videoName + '.jpg'), function (err, data) {
436 if (err) return callback(err)
437
438 callback(null, data.equals(res.body))
439 })
440 })
441 } else {
442 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
443 callback(null, true)
444 }
445}
446
447function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) {
448 if (!end) {
449 end = specialStatus
450 specialStatus = 204
451 }
452
453 const path = '/api/v1/videos'
454
455 const req = request(url)
456 .post(path)
457 .set('Accept', 'application/json')
458 .set('Authorization', 'Bearer ' + accessToken)
459 .field('name', name)
460 .field('description', description)
461
462 for (let i = 0; i < tags.length; i++) {
463 req.field('tags[' + i + ']', tags[i])
464 }
465
466 let filepath = ''
467 if (pathUtils.isAbsolute(fixture)) {
468 filepath = fixture
469 } else {
470 filepath = pathUtils.join(__dirname, 'fixtures', fixture)
471 }
472
473 req.attach('videofile', filepath)
474 .expect(specialStatus)
475 .end(end)
476}
477
478function updateUser (url, userId, accessToken, newPassword, end) {
479 const path = '/api/v1/users/' + userId
480
481 request(url)
482 .put(path)
483 .set('Accept', 'application/json')
484 .set('Authorization', 'Bearer ' + accessToken)
485 .send({ password: newPassword })
486 .expect(204)
487 .end(end)
488}
489
490// ---------------------------------------------------------------------------
491
492module.exports = testUtils