aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils
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/utils
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/utils')
-rw-r--r--server/tests/utils/clients.js24
-rw-r--r--server/tests/utils/login.js48
-rw-r--r--server/tests/utils/miscs.js21
-rw-r--r--server/tests/utils/pods.js70
-rw-r--r--server/tests/utils/servers.js115
-rw-r--r--server/tests/utils/users.js85
-rw-r--r--server/tests/utils/videos.js199
7 files changed, 562 insertions, 0 deletions
diff --git a/server/tests/utils/clients.js b/server/tests/utils/clients.js
new file mode 100644
index 000000000..e3ded493e
--- /dev/null
+++ b/server/tests/utils/clients.js
@@ -0,0 +1,24 @@
1'use strict'
2
3const request = require('supertest')
4
5const clientsUtils = {
6 getClient: getClient
7}
8
9// ---------------------- Export functions --------------------
10
11function getClient (url, end) {
12 const path = '/api/v1/users/client'
13
14 request(url)
15 .get(path)
16 .set('Accept', 'application/json')
17 .expect(200)
18 .expect('Content-Type', /json/)
19 .end(end)
20}
21
22// ---------------------------------------------------------------------------
23
24module.exports = clientsUtils
diff --git a/server/tests/utils/login.js b/server/tests/utils/login.js
new file mode 100644
index 000000000..1a5d75bc4
--- /dev/null
+++ b/server/tests/utils/login.js
@@ -0,0 +1,48 @@
1'use strict'
2
3const request = require('supertest')
4
5const loginUtils = {
6 login: login,
7 loginAndGetAccessToken: loginAndGetAccessToken
8}
9
10// ---------------------- Export functions --------------------
11
12function login (url, client, user, expectedStatus, end) {
13 if (!end) {
14 end = expectedStatus
15 expectedStatus = 200
16 }
17
18 const path = '/api/v1/users/token'
19
20 const body = {
21 client_id: client.id,
22 client_secret: client.secret,
23 username: user.username,
24 password: user.password,
25 response_type: 'code',
26 grant_type: 'password',
27 scope: 'upload'
28 }
29
30 request(url)
31 .post(path)
32 .type('form')
33 .send(body)
34 .expect(expectedStatus)
35 .end(end)
36}
37
38function loginAndGetAccessToken (server, callback) {
39 login(server.url, server.client, server.user, 200, function (err, res) {
40 if (err) return callback(err)
41
42 return callback(null, res.body.access_token)
43 })
44}
45
46// ---------------------------------------------------------------------------
47
48module.exports = loginUtils
diff --git a/server/tests/utils/miscs.js b/server/tests/utils/miscs.js
new file mode 100644
index 000000000..5414cd561
--- /dev/null
+++ b/server/tests/utils/miscs.js
@@ -0,0 +1,21 @@
1'use strict'
2
3const miscsUtils = {
4 dateIsValid: dateIsValid
5}
6
7// ---------------------- Export functions --------------------
8
9function dateIsValid (dateString) {
10 const dateToCheck = new Date(dateString)
11 const now = new Date()
12
13 // Check if the interval is more than 2 minutes
14 if (now - dateToCheck > 120000) return false
15
16 return true
17}
18
19// ---------------------------------------------------------------------------
20
21module.exports = miscsUtils
diff --git a/server/tests/utils/pods.js b/server/tests/utils/pods.js
new file mode 100644
index 000000000..366492110
--- /dev/null
+++ b/server/tests/utils/pods.js
@@ -0,0 +1,70 @@
1'use strict'
2
3const request = require('supertest')
4
5const podsUtils = {
6 getFriendsList: getFriendsList,
7 makeFriends: makeFriends,
8 quitFriends: quitFriends
9}
10
11// ---------------------- Export functions --------------------
12
13function getFriendsList (url, end) {
14 const path = '/api/v1/pods/'
15
16 request(url)
17 .get(path)
18 .set('Accept', 'application/json')
19 .expect(200)
20 .expect('Content-Type', /json/)
21 .end(end)
22}
23
24function makeFriends (url, accessToken, expectedStatus, end) {
25 if (!end) {
26 end = expectedStatus
27 expectedStatus = 204
28 }
29
30 const path = '/api/v1/pods/makefriends'
31
32 // The first pod make friend with the third
33 request(url)
34 .get(path)
35 .set('Accept', 'application/json')
36 .set('Authorization', 'Bearer ' + accessToken)
37 .expect(expectedStatus)
38 .end(function (err, res) {
39 if (err) throw err
40
41 // Wait for the request between pods
42 setTimeout(end, 1000)
43 })
44}
45
46function quitFriends (url, accessToken, expectedStatus, end) {
47 if (!end) {
48 end = expectedStatus
49 expectedStatus = 204
50 }
51
52 const path = '/api/v1/pods/quitfriends'
53
54 // The first pod make friend with the third
55 request(url)
56 .get(path)
57 .set('Accept', 'application/json')
58 .set('Authorization', 'Bearer ' + accessToken)
59 .expect(expectedStatus)
60 .end(function (err, res) {
61 if (err) throw err
62
63 // Wait for the request between pods
64 setTimeout(end, 1000)
65 })
66}
67
68// ---------------------------------------------------------------------------
69
70module.exports = podsUtils
diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js
new file mode 100644
index 000000000..ee7cd8c0a
--- /dev/null
+++ b/server/tests/utils/servers.js
@@ -0,0 +1,115 @@
1'use strict'
2
3const childProcess = require('child_process')
4const exec = childProcess.exec
5const fork = childProcess.fork
6const pathUtils = require('path')
7
8const serversUtils = {
9 flushAndRunMultipleServers: flushAndRunMultipleServers,
10 flushTests: flushTests,
11 runServer: runServer
12}
13
14// ---------------------- Export functions --------------------
15
16function flushAndRunMultipleServers (totalServers, serversRun) {
17 let apps = []
18 let urls = []
19 let i = 0
20
21 function anotherServerDone (number, app, url) {
22 apps[number - 1] = app
23 urls[number - 1] = url
24 i++
25 if (i === totalServers) {
26 serversRun(apps, urls)
27 }
28 }
29
30 flushTests(function () {
31 for (let j = 1; j <= totalServers; j++) {
32 // For the virtual buffer
33 setTimeout(function () {
34 runServer(j, function (app, url) {
35 anotherServerDone(j, app, url)
36 })
37 }, 1000 * j)
38 }
39 })
40}
41
42function flushTests (callback) {
43 exec('npm run clean:server:test', callback)
44}
45
46function runServer (number, callback) {
47 const server = {
48 app: null,
49 url: `http://localhost:${9000 + number}`,
50 client: {
51 id: null,
52 secret: null
53 },
54 user: {
55 username: null,
56 password: null
57 }
58 }
59
60 // These actions are async so we need to be sure that they have both been done
61 const serverRunString = {
62 'Connected to mongodb': false,
63 'Server listening on port': false
64 }
65
66 const regexps = {
67 client_id: 'Client id: ([a-f0-9]+)',
68 client_secret: 'Client secret: (.+)',
69 user_username: 'Username: (.+)',
70 user_password: 'User password: (.+)'
71 }
72
73 // Share the environment
74 const env = Object.create(process.env)
75 env.NODE_ENV = 'test'
76 env.NODE_APP_INSTANCE = number
77 const options = {
78 silent: true,
79 env: env,
80 detached: true
81 }
82
83 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
84 server.app.stdout.on('data', function onStdout (data) {
85 let dontContinue = false
86
87 // Capture things if we want to
88 for (const key of Object.keys(regexps)) {
89 const regexp = regexps[key]
90 const matches = data.toString().match(regexp)
91 if (matches !== null) {
92 if (key === 'client_id') server.client.id = matches[1]
93 else if (key === 'client_secret') server.client.secret = matches[1]
94 else if (key === 'user_username') server.user.username = matches[1]
95 else if (key === 'user_password') server.user.password = matches[1]
96 }
97 }
98
99 // Check if all required sentences are here
100 for (const key of Object.keys(serverRunString)) {
101 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
102 if (serverRunString[key] === false) dontContinue = true
103 }
104
105 // If no, there is maybe one thing not already initialized (mongodb...)
106 if (dontContinue === true) return
107
108 server.app.stdout.removeListener('data', onStdout)
109 callback(server)
110 })
111}
112
113// ---------------------------------------------------------------------------
114
115module.exports = serversUtils
diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js
new file mode 100644
index 000000000..ed7a9d672
--- /dev/null
+++ b/server/tests/utils/users.js
@@ -0,0 +1,85 @@
1'use strict'
2
3const request = require('supertest')
4
5const usersUtils = {
6 createUser: createUser,
7 getUserInformation: getUserInformation,
8 getUsersList: getUsersList,
9 removeUser: removeUser,
10 updateUser: updateUser
11}
12
13// ---------------------- Export functions --------------------
14
15function createUser (url, accessToken, username, password, specialStatus, end) {
16 if (!end) {
17 end = specialStatus
18 specialStatus = 204
19 }
20
21 const path = '/api/v1/users'
22
23 request(url)
24 .post(path)
25 .set('Accept', 'application/json')
26 .set('Authorization', 'Bearer ' + accessToken)
27 .send({ username: username, password: password })
28 .expect(specialStatus)
29 .end(end)
30}
31
32function getUserInformation (url, accessToken, end) {
33 const path = '/api/v1/users/me'
34
35 request(url)
36 .get(path)
37 .set('Accept', 'application/json')
38 .set('Authorization', 'Bearer ' + accessToken)
39 .expect(200)
40 .expect('Content-Type', /json/)
41 .end(end)
42}
43
44function getUsersList (url, end) {
45 const path = '/api/v1/users'
46
47 request(url)
48 .get(path)
49 .set('Accept', 'application/json')
50 .expect(200)
51 .expect('Content-Type', /json/)
52 .end(end)
53}
54
55function removeUser (url, token, username, expectedStatus, end) {
56 if (!end) {
57 end = expectedStatus
58 expectedStatus = 204
59 }
60
61 const path = '/api/v1/users'
62
63 request(url)
64 .delete(path + '/' + username)
65 .set('Accept', 'application/json')
66 .set('Authorization', 'Bearer ' + token)
67 .expect(expectedStatus)
68 .end(end)
69}
70
71function updateUser (url, userId, accessToken, newPassword, end) {
72 const path = '/api/v1/users/' + userId
73
74 request(url)
75 .put(path)
76 .set('Accept', 'application/json')
77 .set('Authorization', 'Bearer ' + accessToken)
78 .send({ password: newPassword })
79 .expect(204)
80 .end(end)
81}
82
83// ---------------------------------------------------------------------------
84
85module.exports = usersUtils
diff --git a/server/tests/utils/videos.js b/server/tests/utils/videos.js
new file mode 100644
index 000000000..90ee9621e
--- /dev/null
+++ b/server/tests/utils/videos.js
@@ -0,0 +1,199 @@
1'use strict'
2
3const fs = require('fs')
4const pathUtils = require('path')
5const request = require('supertest')
6
7const videosUtils = {
8 getAllVideosListBy: getAllVideosListBy,
9 getVideo: getVideo,
10 getVideosList: getVideosList,
11 getVideosListPagination: getVideosListPagination,
12 getVideosListSort: getVideosListSort,
13 removeVideo: removeVideo,
14 searchVideo: searchVideo,
15 searchVideoWithPagination: searchVideoWithPagination,
16 searchVideoWithSort: searchVideoWithSort,
17 testVideoImage: testVideoImage,
18 uploadVideo: uploadVideo
19}
20
21// ---------------------- Export functions --------------------
22
23function getAllVideosListBy (url, end) {
24 const path = '/api/v1/videos'
25
26 request(url)
27 .get(path)
28 .query({ sort: 'createdDate' })
29 .query({ start: 0 })
30 .query({ count: 10000 })
31 .set('Accept', 'application/json')
32 .expect(200)
33 .expect('Content-Type', /json/)
34 .end(end)
35}
36
37function getVideo (url, id, end) {
38 const path = '/api/v1/videos/' + id
39
40 request(url)
41 .get(path)
42 .set('Accept', 'application/json')
43 .expect(200)
44 .expect('Content-Type', /json/)
45 .end(end)
46}
47
48function getVideosList (url, end) {
49 const path = '/api/v1/videos'
50
51 request(url)
52 .get(path)
53 .query({ sort: 'name' })
54 .set('Accept', 'application/json')
55 .expect(200)
56 .expect('Content-Type', /json/)
57 .end(end)
58}
59
60function getVideosListPagination (url, start, count, end) {
61 const path = '/api/v1/videos'
62
63 request(url)
64 .get(path)
65 .query({ start: start })
66 .query({ count: count })
67 .set('Accept', 'application/json')
68 .expect(200)
69 .expect('Content-Type', /json/)
70 .end(end)
71}
72
73function getVideosListSort (url, sort, end) {
74 const path = '/api/v1/videos'
75
76 request(url)
77 .get(path)
78 .query({ sort: sort })
79 .set('Accept', 'application/json')
80 .expect(200)
81 .expect('Content-Type', /json/)
82 .end(end)
83}
84
85function removeVideo (url, token, id, expectedStatus, end) {
86 if (!end) {
87 end = expectedStatus
88 expectedStatus = 204
89 }
90
91 const path = '/api/v1/videos'
92
93 request(url)
94 .delete(path + '/' + id)
95 .set('Accept', 'application/json')
96 .set('Authorization', 'Bearer ' + token)
97 .expect(expectedStatus)
98 .end(end)
99}
100
101function searchVideo (url, search, field, end) {
102 if (!end) {
103 end = field
104 field = null
105 }
106
107 const path = '/api/v1/videos'
108 const req = request(url)
109 .get(path + '/search/' + search)
110 .set('Accept', 'application/json')
111
112 if (field) req.query({ field: field })
113 req.expect(200)
114 .expect('Content-Type', /json/)
115 .end(end)
116}
117
118function searchVideoWithPagination (url, search, field, start, count, end) {
119 const path = '/api/v1/videos'
120
121 request(url)
122 .get(path + '/search/' + search)
123 .query({ start: start })
124 .query({ count: count })
125 .query({ field: field })
126 .set('Accept', 'application/json')
127 .expect(200)
128 .expect('Content-Type', /json/)
129 .end(end)
130}
131
132function searchVideoWithSort (url, search, sort, end) {
133 const path = '/api/v1/videos'
134
135 request(url)
136 .get(path + '/search/' + search)
137 .query({ sort: sort })
138 .set('Accept', 'application/json')
139 .expect(200)
140 .expect('Content-Type', /json/)
141 .end(end)
142}
143
144function testVideoImage (url, videoName, imagePath, callback) {
145 // Don't test images if the node env is not set
146 // Because we need a special ffmpeg version for this test
147 if (process.env.NODE_TEST_IMAGE) {
148 request(url)
149 .get(imagePath)
150 .expect(200)
151 .end(function (err, res) {
152 if (err) return callback(err)
153
154 fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
155 if (err) return callback(err)
156
157 callback(null, data.equals(res.body))
158 })
159 })
160 } else {
161 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
162 callback(null, true)
163 }
164}
165
166function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) {
167 if (!end) {
168 end = specialStatus
169 specialStatus = 204
170 }
171
172 const path = '/api/v1/videos'
173
174 const req = request(url)
175 .post(path)
176 .set('Accept', 'application/json')
177 .set('Authorization', 'Bearer ' + accessToken)
178 .field('name', name)
179 .field('description', description)
180
181 for (let i = 0; i < tags.length; i++) {
182 req.field('tags[' + i + ']', tags[i])
183 }
184
185 let filepath = ''
186 if (pathUtils.isAbsolute(fixture)) {
187 filepath = fixture
188 } else {
189 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', fixture)
190 }
191
192 req.attach('videofile', filepath)
193 .expect(specialStatus)
194 .end(end)
195}
196
197// ---------------------------------------------------------------------------
198
199module.exports = videosUtils