aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/utils.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
commitb9a3e09ad5a7673f64556d1dba122ed4c4fac980 (patch)
tree66d4928b82af19a2372a2505822233884f3fd471 /server/tests/api/utils.js
parentb2ff5e3e686eb552c5ccd64ce67b0455972ceef0 (diff)
downloadPeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.gz
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.zst
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.zip
Prepare folders structure for angular app
Diffstat (limited to 'server/tests/api/utils.js')
-rw-r--r--server/tests/api/utils.js185
1 files changed, 185 insertions, 0 deletions
diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js
new file mode 100644
index 000000000..47b706294
--- /dev/null
+++ b/server/tests/api/utils.js
@@ -0,0 +1,185 @@
1'use strict'
2
3var child_process = require('child_process')
4var exec = child_process.exec
5var fork = child_process.fork
6var pathUtils = require('path')
7var request = require('supertest')
8
9var testUtils = {
10 flushTests: flushTests,
11 getFriendsList: getFriendsList,
12 getVideosList: getVideosList,
13 makeFriends: makeFriends,
14 quitFriends: quitFriends,
15 removeVideo: removeVideo,
16 flushAndRunMultipleServers: flushAndRunMultipleServers,
17 runServer: runServer,
18 searchVideo: searchVideo,
19 uploadVideo: uploadVideo
20}
21
22// ---------------------- Export functions --------------------
23
24function flushTests (callback) {
25 exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback)
26}
27
28function getFriendsList (url, end) {
29 var path = '/api/v1/pods/'
30
31 request(url)
32 .get(path)
33 .set('Accept', 'application/json')
34 .expect(200)
35 .expect('Content-Type', /json/)
36 .end(end)
37}
38
39function getVideosList (url, end) {
40 var path = '/api/v1/videos'
41
42 request(url)
43 .get(path)
44 .set('Accept', 'application/json')
45 .expect(200)
46 .expect('Content-Type', /json/)
47 .end(end)
48}
49
50function makeFriends (url, expected_status, callback) {
51 if (!callback) {
52 callback = expected_status
53 expected_status = 204
54 }
55
56 var path = '/api/v1/pods/makefriends'
57
58 // The first pod make friend with the third
59 request(url)
60 .get(path)
61 .set('Accept', 'application/json')
62 .expect(expected_status)
63 .end(function (err, res) {
64 if (err) throw err
65
66 // Wait for the request between pods
67 setTimeout(callback, 1000)
68 })
69}
70
71function quitFriends (url, callback) {
72 var path = '/api/v1/pods/quitfriends'
73
74 // The first pod make friend with the third
75 request(url)
76 .get(path)
77 .set('Accept', 'application/json')
78 .expect(204)
79 .end(function (err, res) {
80 if (err) throw err
81
82 // Wait for the request between pods
83 setTimeout(callback, 1000)
84 })
85}
86
87function removeVideo (url, id, end) {
88 var path = '/api/v1/videos'
89
90 request(url)
91 .delete(path + '/' + id)
92 .set('Accept', 'application/json')
93 .expect(204)
94 .end(end)
95}
96
97function flushAndRunMultipleServers (total_servers, serversRun) {
98 var apps = []
99 var urls = []
100 var i = 0
101
102 function anotherServerDone (number, app, url) {
103 apps[number - 1] = app
104 urls[number - 1] = url
105 i++
106 if (i === total_servers) {
107 serversRun(apps, urls)
108 }
109 }
110
111 flushTests(function () {
112 for (var j = 1; j <= total_servers; j++) {
113 (function (k) { // TODO: ES6 with let
114 // For the virtual buffer
115 setTimeout(function () {
116 runServer(k, function (app, url) {
117 anotherServerDone(k, app, url)
118 })
119 }, 1000 * k)
120 })(j)
121 }
122 })
123}
124
125function runServer (number, callback) {
126 var port = 9000 + number
127 var server_run_string = {
128 'Connected to mongodb': false,
129 'Server listening on port': false
130 }
131
132 // Share the environment
133 var env = Object.create(process.env)
134 env.NODE_ENV = 'test'
135 env.NODE_APP_INSTANCE = number
136 var options = {
137 silent: true,
138 env: env,
139 detached: true
140 }
141
142 var app = fork(pathUtils.join(__dirname, '../../server.js'), [], options)
143 app.stdout.on('data', function onStdout (data) {
144 var dont_continue = false
145 // Check if all required sentences are here
146 for (var key of Object.keys(server_run_string)) {
147 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
148 if (server_run_string[key] === false) dont_continue = true
149 }
150
151 // If no, there is maybe one thing not already initialized (mongodb...)
152 if (dont_continue === true) return
153
154 app.stdout.removeListener('data', onStdout)
155 callback(app, 'http://localhost:' + port)
156 })
157}
158
159function searchVideo (url, search, end) {
160 var path = '/api/v1/videos'
161
162 request(url)
163 .get(path + '/search/' + search)
164 .set('Accept', 'application/json')
165 .expect(200)
166 .expect('Content-Type', /json/)
167 .end(end)
168}
169
170function uploadVideo (url, name, description, fixture, end) {
171 var path = '/api/v1/videos'
172
173 request(url)
174 .post(path)
175 .set('Accept', 'application/json')
176 .field('name', name)
177 .field('description', description)
178 .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
179 .expect(201)
180 .end(end)
181}
182
183// ---------------------------------------------------------------------------
184
185module.exports = testUtils