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