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