aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'tests/api/utils.js')
-rw-r--r--tests/api/utils.js340
1 files changed, 169 insertions, 171 deletions
diff --git a/tests/api/utils.js b/tests/api/utils.js
index b71e943ed..47b706294 100644
--- a/tests/api/utils.js
+++ b/tests/api/utils.js
@@ -1,187 +1,185 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var child_process = require('child_process')
4 var child_process = require('child_process') 4var exec = child_process.exec
5 var exec = child_process.exec 5var fork = child_process.fork
6 var fork = child_process.fork 6var pathUtils = require('path')
7 var pathUtils = require('path') 7var request = require('supertest')
8 var request = require('supertest') 8
9 9var testUtils = {
10 var testUtils = { 10 flushTests: flushTests,
11 flushTests: flushTests, 11 getFriendsList: getFriendsList,
12 getFriendsList: getFriendsList, 12 getVideosList: getVideosList,
13 getVideosList: getVideosList, 13 makeFriends: makeFriends,
14 makeFriends: makeFriends, 14 quitFriends: quitFriends,
15 quitFriends: quitFriends, 15 removeVideo: removeVideo,
16 removeVideo: removeVideo, 16 flushAndRunMultipleServers: flushAndRunMultipleServers,
17 flushAndRunMultipleServers: flushAndRunMultipleServers, 17 runServer: runServer,
18 runServer: runServer, 18 searchVideo: searchVideo,
19 searchVideo: searchVideo, 19 uploadVideo: uploadVideo
20 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
21 } 54 }
22 55
23 // ---------------------- Export functions -------------------- 56 var path = '/api/v1/pods/makefriends'
24 57
25 function flushTests (callback) { 58 // The first pod make friend with the third
26 exec(pathUtils.join(__dirname, '../../scripts/clean_test.sh'), callback) 59 request(url)
27 } 60 .get(path)
28 61 .set('Accept', 'application/json')
29 function getFriendsList (url, end) { 62 .expect(expected_status)
30 var path = '/api/v1/pods/' 63 .end(function (err, res) {
31 64 if (err) throw err
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 65
75 // The first pod make friend with the third 66 // Wait for the request between pods
76 request(url) 67 setTimeout(callback, 1000)
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 }) 68 })
124 } 69}
125 70
126 function runServer (number, callback) { 71function quitFriends (url, callback) {
127 var port = 9000 + number 72 var path = '/api/v1/pods/quitfriends'
128 var server_run_string = {
129 'Connected to mongodb': false,
130 'Server listening on port': false
131 }
132 73
133 // Share the environment 74 // The first pod make friend with the third
134 var env = Object.create(process.env) 75 request(url)
135 env.NODE_ENV = 'test' 76 .get(path)
136 env.NODE_APP_INSTANCE = number 77 .set('Accept', 'application/json')
137 var options = { 78 .expect(204)
138 silent: true, 79 .end(function (err, res) {
139 env: env, 80 if (err) throw err
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 81
152 // If no, there is maybe one thing not already initialized (mongodb...) 82 // Wait for the request between pods
153 if (dont_continue === true) return 83 setTimeout(callback, 1000)
154
155 app.stdout.removeListener('data', onStdout)
156 callback(app, 'http://localhost:' + port)
157 }) 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 }
158 } 109 }
159 110
160 function searchVideo (url, search, end) { 111 flushTests(function () {
161 var path = '/api/v1/videos' 112 for (var j = 1; j <= total_servers; j++) {
162 113 (function (k) { // TODO: ES6 with let
163 request(url) 114 // For the virtual buffer
164 .get(path + '/search/' + search) 115 setTimeout(function () {
165 .set('Accept', 'application/json') 116 runServer(k, function (app, url) {
166 .expect(200) 117 anotherServerDone(k, app, url)
167 .expect('Content-Type', /json/) 118 })
168 .end(end) 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
169 } 130 }
170 131
171 function uploadVideo (url, name, description, fixture, end) { 132 // Share the environment
172 var path = '/api/v1/videos' 133 var env = Object.create(process.env)
173 134 env.NODE_ENV = 'test'
174 request(url) 135 env.NODE_APP_INSTANCE = number
175 .post(path) 136 var options = {
176 .set('Accept', 'application/json') 137 silent: true,
177 .field('name', name) 138 env: env,
178 .field('description', description) 139 detached: true
179 .attach('input_video', pathUtils.join(__dirname, 'fixtures', fixture))
180 .expect(201)
181 .end(end)
182 } 140 }
183 141
184 // --------------------------------------------------------------------------- 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 }
185 150
186 module.exports = testUtils 151 // If no, there is maybe one thing not already initialized (mongodb...)
187})() 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