Skip to content

Commit ce7e3d9

Browse files
committed
tests now generate their own marketplace
1 parent 5eb1e03 commit ce7e3d9

File tree

3 files changed

+92
-59
lines changed

3 files changed

+92
-59
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The callback is called with an error (if any) and then the response body (if any
2929

3030
* `balanced`
3131
* `.account`
32-
* `.create(email, cb)` - creates a new balanced account, takes bank/card info/tokens in options
32+
* `.create(account, cb)` - creates a new balanced account, takes bank/card info/tokens in options
3333
* `.add_card(account_id, card_info_or_id, cb)` - adds a card to their account
3434
* `.debit(account_id, debit, cb)` - debits the accounts card
3535
* `.hold(account_id, hold, cb) ` - puts a hold on the accounts card
@@ -47,9 +47,7 @@ The callback is called with an error (if any) and then the response body (if any
4747

4848
## Tests
4949

50-
To run the tests, install mocha with `npm install mocha -g`, then run
51-
52-
`BALANCED_API_SECRET=your_test_api_secret BALANCED_MARKETPLACE_ID=your_test_marketplace_id npm test`
50+
To run the tests, install mocha with `npm install mocha -g`, then run `npm test`
5351

5452
## Author
5553

index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ module.exports = function(api_secret, marketplace_id) {
3131
})
3232
}
3333

34-
return balanced = {
35-
36-
client: client,
37-
38-
account: {
34+
return {
35+
36+
account: {
3937

4038
//creates a new balanced account
4139
create: function(account, cb){

tests/index.js

+87-50
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,101 @@
1-
if (!process.env.BALANCED_API_SECRET || !process.env.BALANCED_MARKETPLACE_ID) {
2-
console.log("You must have environment variables BALANCED_API_SECRET and BALANCED_MARKETPLACE_ID set with test data.\n")
3-
console.log("ex: BALANCED_API_SECRET=[your_test_api_secret] BALANCED_MARKETPLACE_ID=[your_test_marketplace_id] npm test\n")
4-
process.exit(2)
5-
}
1+
var request = require("request")
2+
var url = require("url")
3+
var assert = require("assert")
4+
5+
var api_secret = null
6+
var marketplace_id = null
7+
var balanced = null
8+
var test = {}
9+
10+
var client = function(method, uri, json, cb) {
611

7-
var balanced = require("../index")(process.env.BALANCED_API_SECRET, process.env.BALANCED_MARKETPLACE_ID);
8-
var assert = require("assert");
9-
10-
11-
var test = {
12-
email: function(){ return Math.random()+'[email protected]' },
13-
card_info: {
14-
card_number: "5105105105105100",
15-
expiration_month: "12",
16-
expiration_year: "2020",
17-
security_code: "123"
18-
},
19-
bank_info: {
20-
name: "Johann Bernoulli",
21-
account_number: "9900000001",
22-
routing_number: "121000358",
23-
type: "checking"
12+
//make json param optional
13+
if(typeof json === 'function' && cb === undefined){cb = json; json = null}
14+
15+
if(api_secret){
16+
uri = url.format({protocol: "https", host: "api.balancedpayments.com", auth: api_secret+':', pathname: uri})
17+
}else{
18+
uri = url.format({protocol: "https", host: "api.balancedpayments.com", pathname: uri})
2419
}
20+
21+
request({
22+
method: method,
23+
uri: uri,
24+
encoding: "utf-8",
25+
json: json || true
26+
}, function(err, response, body) {
27+
if (response.statusCode >= 400){
28+
29+
if(body !== undefined){
30+
err = new Error("Balanced call failed: "+response.statusCode+" - "+body.response)
31+
}else{
32+
err = new Error("Balanced call failed: "+response.statusCode)
33+
}
34+
}
35+
36+
cb(err, body)
37+
38+
})
2539
}
2640

41+
2742
before(function(done){
2843

29-
var count = 0
30-
var track = function(){if(++count === 2){done()}}
31-
32-
var marketplace_id = process.env.BALANCED_MARKETPLACE_ID
33-
34-
//create a token
35-
balanced.client("POST", "/v1/marketplaces/"+marketplace_id+"/cards", {
36-
card_number: "5105105105105100",
37-
expiration_month: "12",
38-
expiration_year: "2015",
39-
security_code: "123"
40-
}, function(err, res){
41-
if(err){
42-
done(err)
43-
}else{
44-
test.card_id = res.id
45-
track()
46-
}
47-
})
44+
//create a marketplace
45+
client("POST", "/v1/api_keys", function(err, res){
46+
if(err){return done(err)}
4847

49-
//create bank token
50-
balanced.client("POST", "/v1/bank_accounts", test.bank_info, function(err, res){
51-
if(err){
52-
done(err)
53-
}else{
54-
test.bank_id = res.id
55-
track()
56-
}
57-
})
48+
//set the api secret
49+
api_secret = res.secret
50+
51+
client("POST", "/v1/marketplaces", function(err, res){
52+
if(err){return done(err)}
53+
54+
marketplace_id = res.id
5855

56+
test.email = function(){ return Math.random()+'[email protected]' }
57+
test.card_info = {card_number: "5105105105105100", expiration_month: "12", expiration_year: "2020", security_code: "123"}
58+
test.bank_info = {name: "Johann Bernoulli", account_number: "9900000001", routing_number: "121000358", type: "checking"}
59+
60+
var count = 0
61+
var track = function(){
62+
if(++count === 2){
63+
balanced = require("../index")(api_secret, marketplace_id)
64+
done()
65+
}
66+
}
67+
//create a card token
68+
client("POST", "/v1/marketplaces/"+marketplace_id+"/cards", {
69+
card_number: "5105105105105100",
70+
expiration_month: "12",
71+
expiration_year: "2015",
72+
security_code: "123"
73+
}, function(err, res){
74+
if(err){
75+
done(err)
76+
}else{
77+
test.card_id = res.id
78+
track()
79+
}
80+
})
81+
//create bank token
82+
client("POST", "/v1/bank_accounts", test.bank_info, function(err, res){
83+
if(err){
84+
done(err)
85+
}else{
86+
test.bank_id = res.id
87+
track()
88+
}
89+
})
90+
})
91+
})
5992
})
6093

6194

95+
96+
97+
98+
//TESTS
6299
describe('balanced', function(){
63100

64101

0 commit comments

Comments
 (0)