Skip to content

Commit 07163c9

Browse files
first commit to testingbot-api
1 parent d156398 commit 07163c9

File tree

5 files changed

+361
-1
lines changed

5 files changed

+361
-1
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
11
# testingbot-api
2-
NodeJS module to communicate with the TestingBot API
2+
3+
Wrapper around the TestingBot REST API for [Node.js](http://nodejs.org/).
4+
5+
## Install
6+
7+
```shell
8+
npm install testingbot-api
9+
```
10+
11+
## Using the wrapper
12+
13+
```javascript
14+
var testingbot = require('testingbot-api');
15+
16+
var myAccount = new SauceLabs({
17+
api_key: "your-tb-key",
18+
api_scret: "your-tb-secret"
19+
});
20+
21+
testingbot.getUserInfo(function(err, data) {
22+
console.log(data);
23+
});
24+
25+
26+
## More documentation
27+
28+
Check out the [TestingBot REST API](https://testingbot.com/support/api) for more information.
29+
30+
## License
31+
32+
The MIT License (MIT)
33+
34+
Copyright (c) 2015 TestingBot.com
35+
36+
Permission is hereby granted, free of charge, to any person obtaining a copy
37+
of this software and associated documentation files (the "Software"), to deal
38+
in the Software without restriction, including without limitation the rights
39+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40+
copies of the Software, and to permit persons to whom the Software is
41+
furnished to do so, subject to the following conditions:
42+
43+
The above copyright notice and this permission notice shall be included in
44+
all copies or substantial portions of the Software.
45+
46+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52+
THE SOFTWARE.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/api.js');

lib/api.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
'use strict';
2+
3+
var https = require('https');
4+
var querystring = require('qs');
5+
6+
module.exports = TestingBot;
7+
function TestingBot(options) {
8+
this.options = options || {};
9+
this.options.api_key = process.env.TESTINGBOT_KEY || this.options.api_key || null;
10+
this.options.api_secret = process.env.TESTINGBOT_SECRET || this.options.api_secret || null;
11+
}
12+
13+
TestingBot.prototype.getTestDetails = function(testID, callback) {
14+
this.api({
15+
method: 'GET',
16+
url: '/tests/' + testID
17+
}, callback);
18+
};
19+
20+
TestingBot.prototype.getBrowsers = function(callback, type) {
21+
var data = {};
22+
if (type) {
23+
data.type = type;
24+
}
25+
this.api({
26+
method: 'GET',
27+
url: '/browsers',
28+
data: data
29+
}, callback);
30+
};
31+
32+
TestingBot.prototype.getLabTestDetails = function(testID, callback) {
33+
this.api({
34+
method: 'GET',
35+
url: '/lab/' + testID
36+
}, callback);
37+
};
38+
39+
TestingBot.prototype.getTunnel = function(callback) {
40+
this.api({
41+
method: 'GET',
42+
url: '/tunnel'
43+
}, callback);
44+
};
45+
46+
TestingBot.prototype.getUserInfo = function(callback) {
47+
this.api({
48+
method: 'GET',
49+
url: '/user'
50+
}, callback);
51+
};
52+
53+
TestingBot.prototype.getTests = function(callback, offset, limit) {
54+
if (!offset) {
55+
offset = 0;
56+
}
57+
if (!limit) {
58+
limit = 10;
59+
}
60+
this.api({
61+
method: 'GET',
62+
url: '/tests/',
63+
data: { offset: offset, limit: limit }
64+
}, callback);
65+
};
66+
67+
TestingBot.prototype.getLabTests = function(callback, offset, limit) {
68+
if (!offset) {
69+
offset = 0;
70+
}
71+
if (!limit) {
72+
limit = 10;
73+
}
74+
this.api({
75+
method: 'GET',
76+
url: '/lab',
77+
data: { offset: offset, limit: limit }
78+
}, callback);
79+
};
80+
81+
TestingBot.prototype.updateUserInfo = function(data, callback) {
82+
this.api({
83+
method: 'PUT',
84+
url: '/user',
85+
data: data
86+
}, callback);
87+
};
88+
89+
TestingBot.prototype.updateTest = function(data, testID, callback) {
90+
this.api({
91+
method: 'PUT',
92+
url: '/tests/' + testID,
93+
data: data
94+
}, callback);
95+
};
96+
97+
TestingBot.prototype.updateLabTest = function(data, testID, callback) {
98+
this.api({
99+
method: 'PUT',
100+
url: '/lab/' + testID,
101+
data: data
102+
}, callback);
103+
};
104+
105+
TestingBot.prototype.deleteTest = function(testID, callback) {
106+
this.api({
107+
method: 'DELETE',
108+
url: '/tests/' + testID
109+
}, callback);
110+
};
111+
112+
TestingBot.prototype.deleteLabTest = function(testID, callback) {
113+
this.api({
114+
method: 'DELETE',
115+
url: '/lab/' + testID
116+
}, callback);
117+
};
118+
119+
TestingBot.prototype.api = function(req_data, callback) {
120+
121+
var self = this;
122+
123+
var url = req_data.url;
124+
if (req_data.method === 'GET' && req_data.data) {
125+
var path = + '?' + querystring.stringify(req_data.data);
126+
}
127+
128+
var req_options = {
129+
host: 'api.testingbot.com',
130+
port: 443,
131+
path: '/v1/' + url,
132+
method: req_data.method
133+
};
134+
135+
req_options.headers = req_data.headers || {};
136+
req_options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
137+
req_options.headers['Content-length'] =
138+
req_data.data ? querystring.stringify(req_data.data).length : 0;
139+
140+
req_options.auth = this.options.api_key + ':' + this.options.api_secret;
141+
142+
var req = https.request(req_options, function(res) {
143+
res.setEncoding('utf8');
144+
var response = '';
145+
res.on('data', function(chunk) {
146+
response += chunk;
147+
}).on('end', function() {
148+
if (typeof(callback) === 'function') {
149+
try {
150+
if (res.statusCode === 200) {
151+
callback(null, JSON.parse(response));
152+
} else {
153+
callback(JSON.parse(response), null);
154+
}
155+
} catch (err) {
156+
callback(new Error('Couldnt parse ' + response), null);
157+
}
158+
}
159+
});
160+
});
161+
162+
req.on('error', function(e) {
163+
var response = 'error';
164+
if (typeof(callback) === 'function') {
165+
callback(new Error(response), null);
166+
}
167+
});
168+
169+
if (self.options.method !== 'GET' && req_data.data) {
170+
// write data to request body
171+
req.write(querystring.stringify(req_data.data));
172+
}
173+
req.end();
174+
};

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"author": "TestingBot <[email protected]> (testingbot.com)",
3+
"name": "testingbot-api",
4+
"description": "A wrapper around TestingBot's REST API",
5+
"version": "1.0.0",
6+
"homepage": "https://github.com/testingbot/testingbot-api",
7+
"repository": {
8+
"type": "git",
9+
"url": "git://github.com/testingbot/testingbot-api.git"
10+
},
11+
"devDependencies": {
12+
"mocha": ""
13+
},
14+
"engines": {
15+
"node": "*"
16+
},
17+
"dependencies": {
18+
"qs": ""
19+
}
20+
}

test/api_test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
'use strict';
2+
var TbApi = require('../lib/api.js');
3+
4+
module.exports = {
5+
setUp: function(callback) {
6+
var fs = require('fs');
7+
try {
8+
var data = fs.readFileSync(process.env.HOME + '/.testingbot');
9+
if (data !== null) {
10+
var arr = data.toString().replace('\n', '').split(':');
11+
var api_key = arr[0];
12+
var api_secret = arr[1];
13+
this.api = new TbApi({ api_key: api_key, api_secret: api_secret });
14+
callback();
15+
}
16+
} catch (e) {
17+
console.log('couldn\'t read $HOME/.testingbot');
18+
console.log(e);
19+
}
20+
},
21+
tearDown: function(callback) {
22+
// clean up
23+
callback();
24+
},
25+
testInfoUser: function(test) {
26+
var userInfo = this.api.getUserInfo(function(err, response) {
27+
test.ok(true, (typeof(response.first_name) === 'String'));
28+
test.ok(true, (response.plan !== undefined));
29+
test.done();
30+
});
31+
},
32+
testWrongCredentials: function(test) {
33+
var api = new TbApi({api_key: 'bogus', api_secret: 'bogus'});
34+
var userInfo = api.getUserInfo(function(err, response) {
35+
test.equal(null, response);
36+
test.done();
37+
});
38+
},
39+
testUpdateInfoUser: function(test) {
40+
var firstName = 'testing' + Math.round(Math.random() * 100);
41+
42+
var data = {
43+
'user[first_name]' : firstName
44+
};
45+
var that = this;
46+
var userInfo = this.api.updateUserInfo(data, function(r) {
47+
var userInfo = that.api.getUserInfo(function(err, response) {
48+
test.equal(firstName,
49+
response.first_name);
50+
test.done();
51+
});
52+
});
53+
},
54+
testListTests: function(test) {
55+
var list = this.api.getTests(function(err, response) {
56+
test.ok(response.data && response.data.length > 0, true);
57+
test.done();
58+
});
59+
},
60+
testInfoSpecificTest: function(test) {
61+
var that = this;
62+
var testInfo = this.api.getTests(function(err, response) {
63+
test.ok(response.data && response.data.length > 0, true);
64+
var singleTest = response.data[0];
65+
66+
that.api.getTestDetails(singleTest.id,
67+
function(err, response) {
68+
test.equal(
69+
response.session_id,
70+
singleTest.session_id);
71+
test.done();
72+
});
73+
});
74+
},
75+
testInfoNotFoundTest: function(test) {
76+
var notFound = this.api.getTestDetails(324234234324, function(err, response) {
77+
test.equal(null, response);
78+
test.done();
79+
});
80+
},
81+
testUpdateTest: function(test) {
82+
var that = this;
83+
var testInfo = this.api.getTests(function(err, response) {
84+
test.ok(response.data && response.data.length > 0, true);
85+
var singleTest = response.data[0];
86+
87+
var newTestName = 'test' + Math.round(Math.random() * 100);
88+
var newTestData = {
89+
'test[name]' : newTestName
90+
};
91+
that.api.updateTest(newTestData, singleTest.id,
92+
function(err, response) {
93+
that.api.getTestDetails(
94+
singleTest.id,
95+
function(err, response) {
96+
test.equal(response.name,
97+
newTestName);
98+
test.done();
99+
});
100+
});
101+
});
102+
},
103+
testUpdateTestNotFound: function(test) {
104+
var newTestName = 'test' + Math.round(Math.random() * 100);
105+
var newTestData = {
106+
'test[name]' : newTestName
107+
};
108+
var update = this.api.updateTest(newTestData,
109+
324324234,
110+
function(err, response) {
111+
test.equal(null, response);
112+
test.done();
113+
});
114+
}
115+
};

0 commit comments

Comments
 (0)