1 | 'use strict';
|
2 |
|
3 | var querystring = require('querystring');
|
4 | var fs = require('fs');
|
5 | var path = require('path');
|
6 | var request = require('request');
|
7 | var crypto = require('crypto');
|
8 | var os = require('os');
|
9 |
|
10 | function TestingBot(options) {
|
11 | this.options = options || {};
|
12 | this.options.api_key = this.options.api_key || process.env.TESTINGBOT_KEY || process.env.TB_KEY || null;
|
13 | this.options.api_secret = this.options.api_secret || process.env.TESTINGBOT_SECRET || process.env.TB_SECRET || null;
|
14 |
|
15 | if (!this.options.api_key || !this.options.api_secret) {
|
16 | try {
|
17 | var tbFile = path.join(os.homedir(), '.testingbot');
|
18 | if (fs.statSync(tbFile).isFile() === true) {
|
19 | var data = fs.readFileSync(tbFile);
|
20 | if (data !== null) {
|
21 | var arr = data.toString().replace('\n', '').split(':');
|
22 | this.options.api_key = arr[0];
|
23 | this.options.api_secret = arr[1];
|
24 | }
|
25 | }
|
26 | } catch (e) {
|
27 |
|
28 | }
|
29 | }
|
30 | }
|
31 |
|
32 | TestingBot.prototype.getDevices = function(callback) {
|
33 | var data = {};
|
34 | this.request({
|
35 | method: 'GET',
|
36 | url: '/devices',
|
37 | data: data
|
38 | }, callback);
|
39 | };
|
40 |
|
41 | TestingBot.prototype.getAvailableDevices = function(callback) {
|
42 | var data = {};
|
43 | this.request({
|
44 | method: 'GET',
|
45 | url: '/devices/available',
|
46 | data: data
|
47 | }, callback);
|
48 | };
|
49 |
|
50 | TestingBot.prototype.getDevice = function(deviceId, callback) {
|
51 | var data = {};
|
52 | if (deviceId) {
|
53 | data.deviceId = deviceId;
|
54 | }
|
55 | this.request({
|
56 | method: 'GET',
|
57 | url: '/devices/',
|
58 | data: data
|
59 | }, callback);
|
60 | };
|
61 |
|
62 | TestingBot.prototype.takeScreenshot = function(callback, url, browsers, waitTime, resolution, fullPage, callbackURL) {
|
63 | var data = {};
|
64 | if (url) {
|
65 | data.url = url;
|
66 | }
|
67 | if (browsers) {
|
68 | data.browsers = browsers;
|
69 | }
|
70 | if (waitTime) {
|
71 | data.waitTime = waitTime;
|
72 | }
|
73 | if (resolution) {
|
74 | data.resolution = resolution;
|
75 | }
|
76 | if (fullPage) {
|
77 | data.fullPage = fullPage;
|
78 | }
|
79 | if (callbackURL) {
|
80 | data.callbackURL = callbackURL;
|
81 | }
|
82 | this.request({
|
83 | method: 'POST',
|
84 | url: '/screenshots',
|
85 | data: data
|
86 | }, callback);
|
87 | };
|
88 |
|
89 | TestingBot.prototype.retrieveScreenshots = function(screenshotId, callback) {
|
90 | var data = {};
|
91 | if (screenshotId) {
|
92 | data.screenshotId = screenshotId;
|
93 | }
|
94 | this.request({
|
95 | method: 'GET',
|
96 | url: '/screenshots',
|
97 | data: data
|
98 | }, callback);
|
99 | };
|
100 |
|
101 | TestingBot.prototype.getScreenshotList = function(callback, offset, limit) {
|
102 | if (!offset) {
|
103 | offset = 0;
|
104 | }
|
105 | if (!limit) {
|
106 | limit = 10;
|
107 | }
|
108 | this.request({
|
109 | method: 'GET',
|
110 | url: '/screenshots',
|
111 | data: { offset: offset, limit: limit }
|
112 | }, callback);
|
113 | };
|
114 |
|
115 | TestingBot.prototype.getBrowsers = function(callback, type) {
|
116 | var data = {};
|
117 | if (type) {
|
118 | data.type = type;
|
119 | }
|
120 | this.request({
|
121 | method: 'GET',
|
122 | url: '/browsers',
|
123 | data: data
|
124 | }, callback);
|
125 | };
|
126 |
|
127 | TestingBot.prototype.getUserInfo = function(callback) {
|
128 | this.request({
|
129 | method: 'GET',
|
130 | url: '/user'
|
131 | }, callback);
|
132 | };
|
133 |
|
134 | TestingBot.prototype.updateUserInfo = function(data, callback) {
|
135 | this.request({
|
136 | method: 'PUT',
|
137 | url: '/user',
|
138 | data: data
|
139 | }, callback);
|
140 | };
|
141 |
|
142 | TestingBot.prototype.getTests = function(callback, offset, limit) {
|
143 | if (!offset) {
|
144 | offset = 0;
|
145 | }
|
146 | if (!limit) {
|
147 | limit = 10;
|
148 | }
|
149 | this.request({
|
150 | method: 'GET',
|
151 | url: '/tests/',
|
152 | data: { offset: offset, limit: limit }
|
153 | }, callback);
|
154 | };
|
155 |
|
156 | TestingBot.prototype.getTestDetails = function(testID, callback) {
|
157 | this.request({
|
158 | method: 'GET',
|
159 | url: '/tests/' + testID
|
160 | }, callback);
|
161 | };
|
162 |
|
163 | TestingBot.prototype.updateTest = function(data, testID, callback) {
|
164 | this.request({
|
165 | method: 'PUT',
|
166 | url: '/tests/' + testID,
|
167 | data: data
|
168 | }, callback);
|
169 | };
|
170 |
|
171 | TestingBot.prototype.deleteTest = function(testID, callback) {
|
172 | this.request({
|
173 | method: 'DELETE',
|
174 | url: '/tests/' + testID
|
175 | }, callback);
|
176 | };
|
177 |
|
178 | TestingBot.prototype.stopTest = function(testID, callback) {
|
179 | this.request({
|
180 | method: 'PUT',
|
181 | url: '/tests/' + testID + '/stop'
|
182 | }, callback);
|
183 | };
|
184 |
|
185 | TestingBot.prototype.getTunnel = function(callback) {
|
186 | this.request({
|
187 | method: 'GET',
|
188 | url: '/tunnel'
|
189 | }, callback);
|
190 | };
|
191 |
|
192 | TestingBot.prototype.getTunnelList = function(callback) {
|
193 | this.request({
|
194 | method: 'GET',
|
195 | url: '/tunnel/list'
|
196 | }, callback);
|
197 | };
|
198 |
|
199 | TestingBot.prototype.deleteTunnel = function(tunnelId, callback) {
|
200 | this.request({
|
201 | method: 'DELETE',
|
202 | url: '/tunnel/' + tunnelId
|
203 | }, callback);
|
204 | };
|
205 |
|
206 | TestingBot.prototype.getLabTests = function(callback, offset, limit) {
|
207 | if (!offset) {
|
208 | offset = 0;
|
209 | }
|
210 | if (!limit) {
|
211 | limit = 10;
|
212 | }
|
213 | this.request({
|
214 | method: 'GET',
|
215 | url: '/lab',
|
216 | data: { offset: offset, limit: limit }
|
217 | }, callback);
|
218 | };
|
219 |
|
220 | TestingBot.prototype.updateLabTest = function(data, testID, callback) {
|
221 | this.request({
|
222 | method: 'PUT',
|
223 | url: '/lab/' + testID,
|
224 | data: data
|
225 | }, callback);
|
226 | };
|
227 |
|
228 | TestingBot.prototype.getBuilds = function(callback, offset, limit) {
|
229 | if (!offset) {
|
230 | offset = 0;
|
231 | }
|
232 | if (!limit) {
|
233 | limit = 10;
|
234 | }
|
235 |
|
236 | this.request({
|
237 | method: 'GET',
|
238 | url: '/builds?offset=' + offset + '&limit=' + limit
|
239 | }, callback);
|
240 | };
|
241 |
|
242 | TestingBot.prototype.getTestsForBuild = function(buildId, callback) {
|
243 | this.request({
|
244 | method: 'GET',
|
245 | url: '/builds/' + buildId
|
246 | }, callback);
|
247 | };
|
248 |
|
249 | TestingBot.prototype.deleteBuild = function(buildId, callback) {
|
250 | this.request({
|
251 | method: 'DELETE',
|
252 | url: '/builds/' + buildId
|
253 | }, callback);
|
254 | };
|
255 |
|
256 | TestingBot.prototype.deleteLabTest = function(testID, callback) {
|
257 | this.request({
|
258 | method: 'DELETE',
|
259 | url: '/lab/' + testID
|
260 | }, callback);
|
261 | };
|
262 |
|
263 | TestingBot.prototype.uploadFile = function(localFilePath, callback) {
|
264 | var requestOptions = {
|
265 | method: 'POST',
|
266 | uri: 'https://api.testingbot.com/v1/storage',
|
267 | auth: {
|
268 | user: this.options.api_key,
|
269 | pass: this.options.api_secret,
|
270 | sendImmediately: true
|
271 | },
|
272 | formData: {
|
273 | file: fs.createReadStream(localFilePath)
|
274 | }
|
275 | };
|
276 |
|
277 | request(requestOptions, function(error, response) {
|
278 | var responseBody = null;
|
279 | if (response) {
|
280 | if (response.body && typeof(response.body) === 'string') {
|
281 | response.body = JSON.parse(response.body);
|
282 | }
|
283 | if (response.statusCode.toString().substring(0, 1) === '2') {
|
284 | responseBody = response.body;
|
285 | } else {
|
286 | error = response.body;
|
287 | }
|
288 | }
|
289 |
|
290 | if (callback) {
|
291 | callback(error, responseBody);
|
292 | }
|
293 | });
|
294 | };
|
295 |
|
296 | TestingBot.prototype.getTeam = function(callback) {
|
297 | this.request({
|
298 | method: 'GET',
|
299 | url: '/team-management'
|
300 | }, callback);
|
301 | };
|
302 |
|
303 | TestingBot.prototype.getUsersInTeam = function(callback) {
|
304 | this.request({
|
305 | method: 'GET',
|
306 | url: '/team-management/users'
|
307 | }, callback);
|
308 | };
|
309 |
|
310 | TestingBot.prototype.getUserFromTeam = function(userId, callback) {
|
311 | this.request({
|
312 | method: 'GET',
|
313 | url: '/team-management/users/' + userId
|
314 | }, callback);
|
315 | };
|
316 |
|
317 | TestingBot.prototype.createUserInTeam = function(user, callback) {
|
318 | this.request({
|
319 | method: 'POST',
|
320 | url: '/team-management/users/',
|
321 | data: {
|
322 | user: user
|
323 | }
|
324 | }, callback);
|
325 | };
|
326 |
|
327 | TestingBot.prototype.updateUserInTeam = function(userId, userData, callback) {
|
328 | this.request({
|
329 | method: 'PUT',
|
330 | url: '/team-management/users/' + userId,
|
331 | data: {
|
332 | user: userData
|
333 | }
|
334 | }, callback);
|
335 | };
|
336 |
|
337 | TestingBot.prototype.resetCredentials = function(userId, callback) {
|
338 | this.request({
|
339 | method: 'POST',
|
340 | url: '/team-management/users/' + userId + '/reset-keys'
|
341 | }, callback);
|
342 | };
|
343 |
|
344 | TestingBot.prototype.getStorageFile = function(appUrl, callback) {
|
345 | this.request({
|
346 | method: 'GET',
|
347 | url: '/storage/' + appUrl
|
348 | }, callback);
|
349 | };
|
350 |
|
351 | TestingBot.prototype.getStorageFiles = function(callback, offset, limit) {
|
352 | if (!offset) {
|
353 | offset = 0;
|
354 | }
|
355 | if (!limit) {
|
356 | limit = 10;
|
357 | }
|
358 |
|
359 | this.request({
|
360 | method: 'GET',
|
361 | url: '/storage',
|
362 | data: { offset: offset, limit: limit }
|
363 | }, callback);
|
364 | };
|
365 |
|
366 | TestingBot.prototype.deleteStorageFile = function(appUrl, callback) {
|
367 | this.request({
|
368 | method: 'DELETE',
|
369 | url: '/storage/' + appUrl
|
370 | }, callback);
|
371 | };
|
372 |
|
373 | TestingBot.prototype.uploadRemoteFile = function(remoteUrl, callback) {
|
374 | this.request({
|
375 | method: 'POST',
|
376 | url: '/storage/',
|
377 | data: {
|
378 | url: remoteUrl
|
379 | }
|
380 | }, callback);
|
381 | };
|
382 |
|
383 | TestingBot.prototype.getAuthenticationHashForSharing = function(sessionId) {
|
384 | return crypto.createHash('md5').update(this.options.api_key + ':' + this.options.api_secret + ':' + sessionId).digest('hex');
|
385 | };
|
386 |
|
387 | TestingBot.prototype.request = function(req_data, callback) {
|
388 | var requestPath = '/v1' + req_data.url;
|
389 | if (req_data.method === 'GET' && req_data.data) {
|
390 | requestPath = requestPath + '?' + querystring.stringify(req_data.data);
|
391 | }
|
392 | var requestOptions = {
|
393 | method: req_data.method,
|
394 | uri: 'https://api.testingbot.com' + requestPath,
|
395 | auth: {
|
396 | user: this.options.api_key,
|
397 | pass: this.options.api_secret,
|
398 | sendImmediately: true
|
399 | }
|
400 | };
|
401 |
|
402 | if (req_data.method !== 'GET') {
|
403 | requestOptions.form = req_data.data;
|
404 | }
|
405 |
|
406 | request(requestOptions, function(error, response) {
|
407 | var responseBody = null;
|
408 | if (response) {
|
409 | if (response.body && typeof(response.body) === 'string') {
|
410 | response.body = JSON.parse(response.body);
|
411 | }
|
412 | if (response.statusCode.toString().substring(0, 1) === '2') {
|
413 | responseBody = response.body;
|
414 | } else {
|
415 | error = response.body;
|
416 | }
|
417 | }
|
418 | if (callback) {
|
419 | callback(error, responseBody);
|
420 | }
|
421 | });
|
422 | };
|
423 |
|
424 | module.exports = TestingBot;
|