Skip to content

Commit 7c91c3e

Browse files
committed
Add TestingBot Storage functionality
1 parent dde975e commit 7c91c3e

File tree

5 files changed

+98
-21
lines changed

5 files changed

+98
-21
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ Deletes a single build
115115
api.deleteBuild(buildId, function(error, success) {});
116116
```
117117

118+
### uploadFile
119+
Uploads a local file to TestingBot Storage
120+
121+
```javascript
122+
api.uploadFile(localFilePath, function(error, appUrl) {});
123+
```
124+
125+
### uploadRemoteFile
126+
Uploads a remote file to TestingBot Storage
127+
128+
```javascript
129+
api.uploadFile(remoteFileUrl, function(error, appUrl) {});
130+
```
131+
118132
### getAuthenticationHashForSharing
119133
Calculates the authentication hash for sharing, pass the WebDriver's SessionID.
120134
This is used to [share a test's detail page on TestingBot](https://testingbot.com/support/other/sharing)

lib/api.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var crypto = require('crypto');
88

99
function TestingBot(options) {
1010
this.options = options || {};
11-
this.options.api_key = this.options.api_key || process.env.TESTINGBOT_KEY || null;
12-
this.options.api_secret = this.options.api_secret || process.env.TESTINGBOT_SECRET || null;
11+
this.options.api_key = this.options.api_key || process.env.TESTINGBOT_KEY || process.env.TB_KEY || null;
12+
this.options.api_secret = this.options.api_secret || process.env.TESTINGBOT_SECRET || process.env.TB_SECRET || null;
1313

1414
if (!this.options.api_key || !this.options.api_secret) {
1515
try {
@@ -176,6 +176,49 @@ TestingBot.prototype.deleteLabTest = function(testID, callback) {
176176
}, callback);
177177
};
178178

179+
TestingBot.prototype.uploadFile = function(localFilePath, callback) {
180+
var requestOptions = {
181+
method: 'POST',
182+
uri: 'https://api.testingbot.com/v1/storage',
183+
auth: {
184+
user: this.options.api_key,
185+
pass: this.options.api_secret,
186+
sendImmediately: true
187+
},
188+
formData: {
189+
file: fs.createReadStream(localFilePath)
190+
}
191+
};
192+
193+
request(requestOptions, function(error, response) {
194+
var responseBody = null;
195+
if (response) {
196+
if (response.body && typeof(response.body) === 'string') {
197+
response.body = JSON.parse(response.body);
198+
}
199+
if (response.statusCode.toString().substring(0, 1) === '2') {
200+
responseBody = response.body;
201+
} else {
202+
error = response.body;
203+
}
204+
}
205+
206+
if (callback) {
207+
callback(error, responseBody);
208+
}
209+
});
210+
};
211+
212+
TestingBot.prototype.uploadRemoteFile = function(remoteUrl, callback) {
213+
this.request({
214+
method: 'POST',
215+
url: '/storage/',
216+
data: {
217+
url: remoteUrl
218+
}
219+
}, callback);
220+
};
221+
179222
TestingBot.prototype.getAuthenticationHashForSharing = function(sessionId) {
180223
return crypto.createHash('md5').update(this.options.api_key + ':' + this.options.api_secret + ':' + sessionId).digest('hex');
181224
};
@@ -201,13 +244,15 @@ TestingBot.prototype.request = function(req_data, callback) {
201244

202245
request(requestOptions, function(error, response) {
203246
var responseBody = null;
204-
if (response.body && typeof(response.body) === 'string') {
205-
response.body = JSON.parse(response.body);
206-
}
207-
if (response.statusCode.toString().substring(0, 1) === '2') {
208-
responseBody = response.body;
209-
} else {
210-
error = response.body;
247+
if (response) {
248+
if (response.body && typeof(response.body) === 'string') {
249+
response.body = JSON.parse(response.body);
250+
}
251+
if (response.statusCode.toString().substring(0, 1) === '2') {
252+
responseBody = response.body;
253+
} else {
254+
error = response.body;
255+
}
211256
}
212257
if (callback) {
213258
callback(error, responseBody);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"author": "TestingBot <[email protected]> (testingbot.com)",
33
"name": "testingbot-api",
44
"description": "A wrapper around TestingBot's REST API",
5-
"version": "1.0.6",
5+
"version": "1.0.7",
66
"scripts": {
7-
"lint": "eslint .",
7+
"lint": "eslint lib/",
88
"test": "make test"
99
},
1010
"homepage": "https://github.com/testingbot/testingbot-api",
@@ -25,7 +25,7 @@
2525
"node": "*"
2626
},
2727
"dependencies": {
28-
"qs": "^6.6.0",
28+
"qs": "^6.7.0",
2929
"request": "^2.88.0"
3030
}
3131
}

test/api_test.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
2+
23
var TbApi = require('../lib/api.js');
34
var assert = require('assert');
5+
var path = require('path');
46

57
describe('Api Tests', function() {
68
beforeEach(function(done) {
@@ -17,7 +19,7 @@ describe('Api Tests', function() {
1719
});
1820

1921
it('should detect wrong credentials', function(done) {
20-
var api = new TbApi({api_key: 'bogus', api_secret: 'bogus'});
22+
var api = new TbApi({ api_key: 'bogus', api_secret: 'bogus' });
2123
api.getUserInfo(function(err, response) {
2224
assert.equal(response, null);
2325
assert.notEqual(err, null);
@@ -46,16 +48,16 @@ describe('Api Tests', function() {
4648
assert.equal(response.data && response.data.length > 0, true);
4749
var singleTest = response.data[0];
4850
that.api.getTestDetails(singleTest.id, function(err, response) {
49-
assert.notEqual(response, null);
51+
assert.notEqual(response, null);
5052
done();
5153
});
5254
});
5355
});
5456

5557
it('should update a user object', function(done) {
5658
var that = this;
57-
var newName = "name_" + Math.round(Math.random()*10000);
58-
this.api.updateUserInfo({ user: { first_name : newName } }, function(err, response) {
59+
var newName = 'name_' + Math.round(Math.random() * 10000);
60+
this.api.updateUserInfo({ user: { first_name: newName } }, function(err, response) {
5961
assert.equal(err, null);
6062
that.api.getUserInfo(function(err, response) {
6163
assert.notEqual(response, null);
@@ -78,10 +80,10 @@ describe('Api Tests', function() {
7880
var singleTest = response.data[0];
7981
that.api.getTestDetails(singleTest.id, function(err, response) {
8082
assert.notEqual(response, null);
81-
var payload = { "test[success]" : "1", "test[status_message]" : "test" };
83+
var payload = { 'test[success]': '1', 'test[status_message]': 'test' };
8284
that.api.updateTest(payload, singleTest.id, function(err) {
8385
that.api.getTestDetails(singleTest.id, function(err, response) {
84-
assert.equal(response.status_message, "test");
86+
assert.equal(response.status_message, 'test');
8587
done();
8688
});
8789
});
@@ -96,10 +98,10 @@ describe('Api Tests', function() {
9698
var singleTest = response.data[0];
9799
that.api.getTestDetails(singleTest.id, function(err, response) {
98100
assert.notEqual(response, null);
99-
var payload = { test: { success: 1, status_message: "test2" } };
101+
var payload = { test: { success: 1, status_message: 'test2' } };
100102
that.api.updateTest(payload, singleTest.id, function(err) {
101103
that.api.getTestDetails(singleTest.id, function(err, response) {
102-
assert.equal(response.status_message, "test2");
104+
assert.equal(response.status_message, 'test2');
103105
done();
104106
});
105107
});
@@ -121,4 +123,19 @@ describe('Api Tests', function() {
121123
});
122124
});
123125
});
124-
});
126+
127+
it('should be possible to upload a local file', function(done) {
128+
var testFilePath = path.resolve(__dirname, 'test.apk');
129+
this.api.uploadFile(testFilePath, function(err, response) {
130+
assert(response.app_url !== undefined);
131+
done();
132+
});
133+
});
134+
135+
it('should be possible to upload a remote file', function(done) {
136+
this.api.uploadRemoteFile('https://testingbot.com/appium/sample.apk', function(err, response) {
137+
assert(response.app_url !== undefined);
138+
done();
139+
});
140+
});
141+
});

test/test.apk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok

0 commit comments

Comments
 (0)