]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - test/api/utils.js
8d059b01cd0fd51bef2327bc715ae83025dcb9f4
[github/Chocobozzz/PeerTube.git] / test / 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 request = require('supertest')
8
9 function flushTests (callback) {
10 exec(__dirname + '/../../scripts/clean_test.sh', function () {
11 callback()
12 })
13 }
14
15 function getFriendsList (url, end) {
16 var path = '/api/v1/pods/'
17
18 request(url)
19 .get(path)
20 .set('Accept', 'application/json')
21 .expect(200)
22 .expect('Content-Type', /json/)
23 .end(end)
24 }
25
26 function getVideosList (url, end) {
27 var path = '/api/v1/videos'
28
29 request(url)
30 .get(path)
31 .set('Accept', 'application/json')
32 .expect(200)
33 .expect('Content-Type', /json/)
34 .end(end)
35 }
36
37 function makeFriend (url, callback) {
38 var path = '/api/v1/pods/makefriends'
39
40 // The first pod make friend with the third
41 request(url)
42 .get(path)
43 .set('Accept', 'application/json')
44 .expect(204)
45 .end(function (err, res) {
46 if (err) throw err
47
48 // Wait for the request between pods
49 setTimeout(function () {
50 callback()
51 }, 1000)
52 })
53 }
54
55 function uploadVideo (url, name, description, fixture, end) {
56 var path = '/api/v1/videos'
57
58 request(url)
59 .post(path)
60 .set('Accept', 'application/json')
61 .field('name', name)
62 .field('description', description)
63 .attach('input_video', __dirname + '/fixtures/' + fixture)
64 .expect(201)
65 .end(end)
66 }
67
68 function removeVideo (url, id, end) {
69 var path = '/api/v1/videos'
70
71 request(url)
72 .delete(path + '/' + id)
73 .set('Accept', 'application/json')
74 .expect(204)
75 .end(end)
76 }
77
78 function runMultipleServers (total_servers, serversRun) {
79 var apps = []
80 var urls = []
81 var i = 0
82
83 function anotherServerDone (number, app, url) {
84 apps[number - 1] = app
85 urls[number - 1] = url
86 i++
87 if (i === total_servers) {
88 serversRun(apps, urls)
89 }
90 }
91
92 flushTests(function () {
93 for (var j = 1; j <= total_servers; j++) {
94 (function (k) { // TODO: ES6 with let
95 // For the virtual buffer
96 setTimeout(function () {
97 runServer(k, function (app, url) {
98 anotherServerDone(k, app, url)
99 })
100 }, 1000 * k)
101 })(j)
102 }
103 })
104 }
105
106 function runServer (number, callback) {
107 var port = 9000 + number
108 var server_run_string = {
109 'Connected to mongodb': false,
110 'Server listening on port': false
111 }
112
113 // Share the environment
114 var env = Object.create(process.env)
115 env.NODE_ENV = 'test'
116 env.NODE_APP_INSTANCE = number
117 var options = {
118 silent: true,
119 env: env,
120 detached: true
121 }
122
123 var app = fork(__dirname + '/../../server.js', [], options)
124 app.stdout.on('data', function onStdout (data) {
125 var dont_continue = false
126 // Check if all required sentences are here
127 for (var key of Object.keys(server_run_string)) {
128 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
129 if (server_run_string[key] === false) dont_continue = true
130 }
131
132 // If no, there is maybe one thing not already initialized (mongodb...)
133 if (dont_continue === true) return
134
135 app.stdout.removeListener('data', onStdout)
136 callback(app, 'http://localhost:' + port)
137 })
138 }
139
140 function searchVideo (url, search, end) {
141 var path = '/api/v1/videos'
142
143 request(url)
144 .get(path + '/search/' + search)
145 .set('Accept', 'application/json')
146 .expect(200)
147 .expect('Content-Type', /json/)
148 .end(end)
149 }
150
151 module.exports = {
152 flushTests: flushTests,
153 getFriendsList: getFriendsList,
154 getVideosList: getVideosList,
155 makeFriend: makeFriend,
156 removeVideo: removeVideo,
157 runMultipleServers: runMultipleServers,
158 runServer: runServer,
159 searchVideo: searchVideo,
160 uploadVideo: uploadVideo
161 }
162 })()