]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/utils.js
Change api output for videos
[github/Chocobozzz/PeerTube.git] / server / tests / api / utils.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const child_process = require('child_process')
4const exec = child_process.exec
5const fork = child_process.fork
6const pathUtils = require('path')
7const request = require('supertest')
9f10b292 8
f0f5567b 9const testUtils = {
9f10b292
C
10 flushTests: flushTests,
11 getFriendsList: getFriendsList,
2df82d42 12 getVideo: getVideo,
9f10b292
C
13 getVideosList: getVideosList,
14 makeFriends: makeFriends,
15 quitFriends: quitFriends,
16 removeVideo: removeVideo,
17 flushAndRunMultipleServers: flushAndRunMultipleServers,
18 runServer: runServer,
19 searchVideo: searchVideo,
20 uploadVideo: uploadVideo
21}
22
23// ---------------------- Export functions --------------------
24
25function flushTests (callback) {
3d446a26 26 exec(pathUtils.join(__dirname, '../../../bin/clean_test.sh'), callback)
9f10b292
C
27}
28
29function getFriendsList (url, end) {
f0f5567b 30 const path = '/api/v1/pods/'
9f10b292
C
31
32 request(url)
33 .get(path)
34 .set('Accept', 'application/json')
35 .expect(200)
36 .expect('Content-Type', /json/)
37 .end(end)
38}
39
2df82d42
C
40function getVideo (url, id, end) {
41 const path = '/api/v1/videos/' + id
42
43 request(url)
44 .get(path)
45 .set('Accept', 'application/json')
46 .expect(200)
47 .expect('Content-Type', /json/)
48 .end(end)
49}
50
9f10b292 51function getVideosList (url, end) {
f0f5567b 52 const path = '/api/v1/videos'
9f10b292
C
53
54 request(url)
55 .get(path)
56 .set('Accept', 'application/json')
57 .expect(200)
58 .expect('Content-Type', /json/)
59 .end(end)
60}
61
62function makeFriends (url, expected_status, callback) {
63 if (!callback) {
64 callback = expected_status
65 expected_status = 204
ee66c593
C
66 }
67
f0f5567b 68 const path = '/api/v1/pods/makefriends'
ee66c593 69
9f10b292
C
70 // The first pod make friend with the third
71 request(url)
72 .get(path)
73 .set('Accept', 'application/json')
74 .expect(expected_status)
75 .end(function (err, res) {
76 if (err) throw err
45239549 77
9f10b292
C
78 // Wait for the request between pods
79 setTimeout(callback, 1000)
876d1bcf 80 })
9f10b292 81}
876d1bcf 82
9f10b292 83function quitFriends (url, callback) {
f0f5567b 84 const path = '/api/v1/pods/quitfriends'
876d1bcf 85
9f10b292
C
86 // The first pod make friend with the third
87 request(url)
88 .get(path)
89 .set('Accept', 'application/json')
90 .expect(204)
91 .end(function (err, res) {
92 if (err) throw err
876d1bcf 93
9f10b292
C
94 // Wait for the request between pods
95 setTimeout(callback, 1000)
876d1bcf 96 })
9f10b292
C
97}
98
99function removeVideo (url, id, end) {
f0f5567b 100 const path = '/api/v1/videos'
9f10b292
C
101
102 request(url)
103 .delete(path + '/' + id)
104 .set('Accept', 'application/json')
105 .expect(204)
106 .end(end)
107}
108
109function flushAndRunMultipleServers (total_servers, serversRun) {
f0f5567b
C
110 let apps = []
111 let urls = []
112 let i = 0
9f10b292
C
113
114 function anotherServerDone (number, app, url) {
115 apps[number - 1] = app
116 urls[number - 1] = url
117 i++
118 if (i === total_servers) {
119 serversRun(apps, urls)
120 }
876d1bcf
C
121 }
122
9f10b292 123 flushTests(function () {
f0f5567b
C
124 for (let j = 1; j <= total_servers; j++) {
125 // For the virtual buffer
126 setTimeout(function () {
127 runServer(j, function (app, url) {
128 anotherServerDone(j, app, url)
129 })
130 }, 1000 * j)
9f10b292
C
131 }
132 })
133}
134
135function runServer (number, callback) {
f0f5567b
C
136 const port = 9000 + number
137 const server_run_string = {
9f10b292
C
138 'Connected to mongodb': false,
139 'Server listening on port': false
876d1bcf
C
140 }
141
9f10b292 142 // Share the environment
f0f5567b 143 const env = Object.create(process.env)
9f10b292
C
144 env.NODE_ENV = 'test'
145 env.NODE_APP_INSTANCE = number
f0f5567b 146 const options = {
9f10b292
C
147 silent: true,
148 env: env,
149 detached: true
876d1bcf 150 }
c45f7f84 151
f0f5567b 152 const app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
9f10b292 153 app.stdout.on('data', function onStdout (data) {
f0f5567b 154 let dont_continue = false
9f10b292 155 // Check if all required sentences are here
f0f5567b 156 for (const key of Object.keys(server_run_string)) {
9f10b292
C
157 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
158 if (server_run_string[key] === false) dont_continue = true
159 }
c45f7f84 160
9f10b292
C
161 // If no, there is maybe one thing not already initialized (mongodb...)
162 if (dont_continue === true) return
163
164 app.stdout.removeListener('data', onStdout)
165 callback(app, 'http://localhost:' + port)
166 })
167}
168
169function searchVideo (url, search, end) {
f0f5567b 170 const path = '/api/v1/videos'
9f10b292
C
171
172 request(url)
173 .get(path + '/search/' + search)
174 .set('Accept', 'application/json')
175 .expect(200)
176 .expect('Content-Type', /json/)
177 .end(end)
178}
179
180function uploadVideo (url, name, description, fixture, end) {
f0f5567b 181 const path = '/api/v1/videos'
9f10b292
C
182
183 request(url)
184 .post(path)
185 .set('Accept', 'application/json')
186 .field('name', name)
187 .field('description', description)
188 .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
2c4a0b5d 189 .expect(204)
9f10b292
C
190 .end(end)
191}
192
193// ---------------------------------------------------------------------------
194
195module.exports = testUtils