ubivar
Advanced tools
Comparing version 0.1.3-beta1 to 0.1.3-beta10
/* | ||
* FACTORY | ||
* Error factory | ||
* @raw [Error] error | ||
*/ | ||
@@ -10,6 +11,12 @@ function _Error(raw){ | ||
/* | ||
* PROTOTYPES | ||
* Set default Error prototypes and type on _Error | ||
*/ | ||
_Error.prototype = Object.create(Error.prototype) | ||
_Error.prototype.type = "GenericError" | ||
/* | ||
* Prototype function that set attributes of the _Error | ||
* @type [String] the type of the error | ||
* @message [String] the message associated with the error | ||
*/ | ||
_Error.prototype.populate = function(type, message){ | ||
@@ -20,2 +27,6 @@ this.type = type | ||
/* | ||
* Generic extend mechanisms to create new _Error types | ||
* @sub [Object] | ||
*/ | ||
_Error.extend = function(sub) { | ||
@@ -41,5 +52,9 @@ var hasOwn = {}.hasOwnProperty | ||
/* | ||
* Map default Error attributes to _Error attributes | ||
*/ | ||
var UbivarError = _Error.UbivarError = _Error.extend({ | ||
"type" : "UbivarError" | ||
, "populate": function(raw){ | ||
/* Populate a raw error */ | ||
, "populate": function(raw){ | ||
this.type = this.type | ||
@@ -56,3 +71,6 @@ this.stack = (new Error(raw.message)).stack | ||
// Helper factory taking raw ubivar error that outputs wrapping instances | ||
/* | ||
* Helper factory that returns an instance of UbivarError | ||
* @rawUbivarError [Error] a raw error | ||
*/ | ||
UbivarError.generate = function(rawUbivarError){ | ||
@@ -69,3 +87,3 @@ switch (rawUbivarError.type) { | ||
/* | ||
* SPECIFIC ERROR TYPES | ||
* Generate _Error subtypes | ||
*/ | ||
@@ -77,2 +95,5 @@ _Error.UbivarInvalidRequestError = UbivarError.extend({"type": "UbivarInvalidRequestError" }) | ||
/* | ||
* Export the _Error factory | ||
*/ | ||
module.exports = _Error |
@@ -0,1 +1,5 @@ | ||
/* Ubivar API client factory | ||
* @token [String] required bearer-token from admin interface | ||
* @version [String] optional, default to 'latest' | ||
*/ | ||
module.exports = function(token, version){ | ||
@@ -2,0 +6,0 @@ var _ = require("lodash") |
@@ -5,3 +5,5 @@ var _ = require("lodash") | ||
/* | ||
* FACTORY | ||
* Resource factory wrapped within a Ubivar instance | ||
* @ubivar [Ubivar] the parent wrapping instance | ||
* @resourceName [String] the name of the resource being instantiated | ||
*/ | ||
@@ -16,37 +18,92 @@ function Resource(ubivar, resourceName){ | ||
/* | ||
* PROTOTYPES | ||
* Create an instance of type resource | ||
* @data [object] object defining this new resource | ||
* @next [function] an optional callback | ||
*/ | ||
Resource.prototype.create = function(data, next){ | ||
Resource.prototype.create = function(){ | ||
var data = arguments[0] | ||
, next = _.isFunction(arguments[1]) ? arguments[1] : function(){} | ||
this.request.call(this, {"method": "POST" | ||
, "data" : data | ||
, "path" : this.path | ||
, "data" : data | ||
, "path" : this.path | ||
}, function(err, res){ next(err, res) }) | ||
} | ||
Resource.prototype.retrieve = function(id, next){ | ||
/* | ||
* Retrieve a resource having a specific 'id' | ||
* @id [String, Number] resource id | ||
* @next [function] optional callback | ||
*/ | ||
Resource.prototype.retrieve = function(){ | ||
if(arguments.length === 2){ | ||
var id = arguments[0] | ||
, next = arguments[1] | ||
} else { | ||
var next = _.isFunction(arguments[0]) ? arguments[0] : function(){} | ||
, id = !_.isFunction(arguments[0]) ? arguments[0] : "" | ||
} | ||
this.request.call(this, {"method": "GET" | ||
, "path" : this.path + "/" + id | ||
, "path" : this.path + "/" + id | ||
}, function(err, res){ next(err, res) }) | ||
} | ||
Resource.prototype.update = function(id, data, next){ | ||
/* | ||
* Update a resource having a specific 'id' | ||
* @id [String, Number] resource id | ||
* @data [Object] new attributes that update the resource | ||
* @next [function] optional callback | ||
*/ | ||
Resource.prototype.update = function(){ | ||
var nArgs = arguments.length | ||
, idLast = nArgs - 1 | ||
, id = nArgs === 3 ? arguments[0] : "" | ||
, data = nArgs === 3 ? arguments[1] : arguments[0] | ||
, next = _.isFunction(arguments[idLast]) ? arguments[idLast] : function(){} | ||
this.request.call(this, {"method": "POST" | ||
, "data" : data | ||
, "path" : this.path + "/" + id | ||
, "data" : data | ||
, "path" : this.path + "/" + id | ||
}, function(err, res){ next(err, res) }) | ||
} | ||
Resource.prototype.del = function(id, next){ | ||
/* | ||
* Delete a resource having a specific 'id' | ||
* @id [String, Number] resource id | ||
* @next [function] optional callback | ||
*/ | ||
Resource.prototype.del = function(){ | ||
var nArgs = arguments.length | ||
, id = nArgs === 2 ? arguments[0] : "" | ||
, next = nArgs === 2 ? arguments[1] : arguments[0] | ||
this.request.call(this, {"method": "DELETE" | ||
, "path" : this.path + "/" + id | ||
, "path" : this.path + "/" + id | ||
}, function(err, res){ next(err, res) }) | ||
} | ||
Resource.prototype.list = function(parameters, next){ | ||
/* | ||
* List resources | ||
* @parameters [Object] query parameters to retrieve resources | ||
* @next [function] optional callback | ||
*/ | ||
Resource.prototype.list = function(){ | ||
var args = arguments | ||
, parameters = !_.isFunction(args[0]) ? args[0] : {} | ||
, next = _.isFunction(args[0]) ? args[0] : _.isFunction(args[1]) ? args[1] : function(){} | ||
this.request.call(this, {"method": "GET" | ||
, "data" : parameters | ||
, "path" : this.path | ||
, "data" : parameters | ||
, "path" : this.path | ||
}, function(err, res){ next(err, res) }) | ||
} | ||
/* | ||
* Generic mechanisms to handle: | ||
* - a request, | ||
* - a socket, | ||
* - a response, | ||
* - a timeout | ||
*/ | ||
_.each(fs.readdirSync(__dirname), function(name){ | ||
@@ -60,2 +117,5 @@ var isReserved= name[0] === "." | ||
/* | ||
* Export the Resource factory | ||
*/ | ||
module.exports = Resource |
@@ -0,1 +1,6 @@ | ||
/* | ||
* Generic request mechanism | ||
* @options [Object] parameters of the request | ||
* @next [function] optional, called on response or timeout | ||
*/ | ||
module.exports = function(options, next){ | ||
@@ -15,3 +20,3 @@ var content = !!options.data ? JSON.stringify(options.data) : "{}" | ||
opts.headers["Authorization"] = "Bearer " + ubivar.get("auth") | ||
opts.headers["Content-Length"] = content.length | ||
opts.headers["Content-Length"] = Buffer.byteLength(content) | ||
@@ -18,0 +23,0 @@ req = (protocol).request(opts) |
@@ -0,1 +1,6 @@ | ||
/* | ||
* Request response handler | ||
* @req [Object] the submitted request | ||
* @next [function] an optional callback | ||
*/ | ||
module.exports = function(req, next) { | ||
@@ -2,0 +7,0 @@ var self = this |
@@ -0,1 +1,7 @@ | ||
/* | ||
* Socket handler | ||
* @ubivar [Ubivar] the wrapping Ubivar instance | ||
* @content [String] stringified content to be written on the socket | ||
* @req [Object] the request | ||
*/ | ||
module.exports = function(ubivar, content, req){ | ||
@@ -2,0 +8,0 @@ return function(socket) { |
@@ -1,2 +0,8 @@ | ||
module.exports = function(timeout, req, callback) { | ||
/* | ||
* Timeout handler | ||
* @timeout [Number] in ms | ||
* @req [Object] the request | ||
* @next [function] an optional callback | ||
*/ | ||
module.exports = function(timeout, req, next) { | ||
var self = this | ||
@@ -12,3 +18,3 @@ , Error = require("../../Error") | ||
callback.call(self | ||
next.call(self | ||
, new Error.UbivarConnectionError({ | ||
@@ -15,0 +21,0 @@ "message" : "Request aborted due to timeout being reached (" + timeout + "ms)" |
@@ -5,3 +5,20 @@ var path = require("path") | ||
, _ = require("lodash") | ||
, RESOURCES = [ | ||
"accounts" | ||
, "transactions" | ||
, "orders" | ||
, "login" | ||
, "logout" | ||
, "items" | ||
, "labels" | ||
, "status" | ||
, "fx" | ||
, "me" | ||
] | ||
/* | ||
* Wrapper factory of the Ubivar API client | ||
* @token [String] required obtained from admin interface | ||
* @version [String] default to 'latest' | ||
*/ | ||
function Ubivar(token, version){ | ||
@@ -12,5 +29,4 @@ var http = require("http") | ||
, theTimeout = http.createServer().timeout | ||
, theVersion = version || require(pathPackage).version | ||
, theVersion = version || "latest" | ||
, theName = "Ubivar/v1 NodeBindings/" + theVersion | ||
, theResources = ["accounts","transactions","orders","login","logout","items","labels","fx"] | ||
, theRevokedCerts = fs.readFileSync(path.join(__dirname, pathFingerprints) | ||
@@ -22,3 +38,3 @@ , 'utf8').replace(/^\s+|\s+$/g, '').split('\n') | ||
, "timeout" : theTimeout | ||
, "resources" : theResources | ||
, "resources" : RESOURCES | ||
, "revokedCerts" : theRevokedCerts | ||
@@ -40,3 +56,13 @@ , "headers" : { | ||
/* | ||
* Accessor (GET) function for the Ubivar wrapper | ||
* @key [String] name of the private attribute | ||
*/ | ||
Ubivar.prototype.get = function(key ){ return this._api[key] } | ||
/* | ||
* Accessor (SET) function for the Ubivar wrapper | ||
* @key [String] name of the private attribute | ||
* @value [String, number, object] attribute value | ||
*/ | ||
Ubivar.prototype.set = function(key, value){ | ||
@@ -49,2 +75,5 @@ if(key === "timeout" && value === null){ | ||
/* | ||
* Export the wrapper factory of Ubivar | ||
*/ | ||
module.exports = Ubivar |
{ | ||
"name": "ubivar", | ||
"version": "0.1.3-beta1", | ||
"version": "0.1.3-beta10", | ||
"description": "API wrapper to Ubivar", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
207
README.md
@@ -1,77 +0,166 @@ | ||
# Ubivar node.js bindings [![Build Status](https://travis-ci.org/ubivar/ubivar-node.png?branch=master)](https://travis-ci.org/ubivar/ubivar-node) | ||
# Ubivar node.js bindings | ||
[![npm version](https://badge.fury.io/js/ubivar.svg)](http://badge.fury.io/js/ubivar) [![Build Status](https://travis-ci.org/ubivar/ubivar-node.png?branch=master)](https://travis-ci.org/ubivar/ubivar-node) [![Inline docs](http://inch-ci.org/github/ubivar/ubivar-node.svg?branch=master)](http://inch-ci.org/github/ubivar/ubivar-node) | ||
Ubivar is an API that takes over the hassle of automatically screening | ||
e-payment for frauds on e-commerce websites. | ||
Ubivar's purpose is to route e-commerce transactions given their estimated | ||
risk. By default, the three possible routing outcomes are a suggested | ||
rejection, a recommended manual verification, and a proposed acceptance of the | ||
transaction. Ubivar does not need all the business events (aka `resources`), | ||
however the more it has contextual information about the transactions, the | ||
better its accuracy. The two required resources are the `transactions` and the | ||
`labels` that categorize the `transactions` like fraud, not fraud. The API is | ||
simple: you provide Ubivar your request `token` and the bindings provide the | ||
hooks to send and receive resources to the API. | ||
# Installation | ||
# Install | ||
`git clone https://github.com/ubivar/ubivar-node.git` | ||
`npm install ubivar` | ||
# Documentation | ||
Documentation is available at https://ubivar.com/docs/nodejs | ||
Documentation is available at [https://ubivar.com/docs/nodejs](https://ubivar.com/docs/nodejs) | ||
# API Overview | ||
Every resource is accessed via your `ubivar` instance: | ||
Every resource is accessed via your `ubivar` instance and accepts an optional | ||
callback as the last argument. The sample code below retrieves your account | ||
information (as json) and updates the primary phone with a random value. | ||
``` | ||
## Quick Start | ||
### A. Init | ||
```js | ||
var Ubivar = require("ubivar") | ||
, clientId = "your-client-id" | ||
, secretKey = "your-secret-key" | ||
, apiVersion= "your-api-version" | ||
, ubivar = new Ubivar(clientId, secretKey, apiVersion) | ||
, ubivar = new Ubivar("YOUR_API_ACCESS_TOKEN", "latest") | ||
``` | ||
Every resource accepts an optional callback as the last argument. | ||
### B. Send an e-commerce transaction | ||
```js | ||
ubivar.transactions.create({ | ||
"user_id" : "test_phahr3Eit3_123" // the id of your client | ||
, "user_email" : "test_phahr3Eit3@gmail-123.com" // the email of your client | ||
, "type" : "sale" // the type of transaction | ||
, "status" : "success" // whether the transaction was authorized | ||
, "order_id" : "test_iiquoozeiroogi_123" // the shopping cart id | ||
, "tx_id" : "client_tx_id_123" // the transaction id of this transaction | ||
, "amount" : "43210" // the amount of the transaction in cents | ||
, "payment_method":{ | ||
"bin" :"123456" // the bank identification number of the card | ||
, "brand" :"Mastercard" // the brand of the card | ||
, "funding" :"credit" // the type of card | ||
, "country" :"US" // the iso country code of the card | ||
, "name" :"M Man" // the name of the card holder | ||
, "cvc_check":"pass" // whether the card passed the cvc check | ||
},"billing_address":{ | ||
"line1" :"123 Market Street" // the billing address | ||
, "line2" :"4th Floor" | ||
, "city" :"San Francisco" | ||
, "state" :"California" | ||
, "zip" :"94102" | ||
, "country" :"US" | ||
} | ||
}, function(err, res){ | ||
if(err) return err | ||
// something unexpected occurred | ||
txId = res.data[0].id | ||
// keep track of the transaction id | ||
}) | ||
``` | ||
ubivar.accounts.create( { | ||
"user_id" : "test_123" | ||
, "session_id" : "test_session_id_123" | ||
, "user_email" : "test_email@email-123.com" | ||
, "first_name" : "test_yourfirstname_123" | ||
, "last_name" : "test_yourlastname_123" | ||
, "primary_phone" : "+123456789-123" | ||
}, function(err, resource){ | ||
err // null if no error | ||
resource // the created resource (account) | ||
}) | ||
### C. Retrieve its status | ||
```js | ||
ubivar.labels.retrieve(txId, function(err, res){ | ||
if(err) return err | ||
// something unexpected occurred | ||
status = res.data[0].status | ||
// the status of the transaction | ||
}) | ||
``` | ||
# Available resources and methods | ||
## All resources and methods | ||
+ Account | ||
+ create(params) | ||
+ retrieve(id) | ||
+ update(id, params) | ||
+ del(id) | ||
+ list() | ||
+ Transaction | ||
+ create(params) | ||
+ retrieve(id) | ||
+ update(id, params) | ||
+ del(id) | ||
+ list() | ||
+ Login | ||
+ create(params) | ||
+ retrieve(id) | ||
+ update(id, params) | ||
+ del(id) | ||
+ list() | ||
+ Logout | ||
+ create(params) | ||
+ retrieve(id) | ||
+ update(id, params) | ||
+ del(id) | ||
+ list() | ||
+ Item | ||
+ create(params) | ||
+ retrieve(id) | ||
+ update(id, params) | ||
+ del(id) | ||
+ list() | ||
+ Label | ||
+ create(params) | ||
+ retrieve(id) | ||
+ update(id, params) | ||
+ del(id) | ||
+ list() | ||
+ [Me](https://www.ubivar.com/docs/nodejs#me) | ||
+ [retrieve()](https://www.ubivar.com/docs/nodejs#retrieve_your_information) | ||
+ [update(params)](https://www.ubivar.com/docs/nodejs#update_your_information) | ||
+ [Account](https://www.ubivar.com/docs/nodejs#accounts) | ||
+ [create(params)](https://www.ubivar.com/docs/nodejs#create_an_account) | ||
+ [retrieve(id)](https://www.ubivar.com/docs/nodejs#retrieve_an_account) | ||
+ [update(id, params)](https://www.ubivar.com/docs/nodejs#update_an_account) | ||
+ [del(id)](https://www.ubivar.com/docs/nodejs#delete_an_account) | ||
+ [list()](https://www.ubivar.com/docs/nodejs#list_accounts) | ||
+ [Transaction](https://www.ubivar.com/docs/nodejs#transactions) | ||
+ [create(params)](https://www.ubivar.com/docs/nodejs#create_a_transaction) | ||
+ [retrieve(id)](https://www.ubivar.com/docs/nodejs#retrieve_a_transaction) | ||
+ [update(id, params)](https://www.ubivar.com/docs/nodejs#update_a_transaction) | ||
+ [del(id)](https://www.ubivar.com/docs/nodejs#delete_a_transaction) | ||
+ [list()](https://www.ubivar.com/docs/nodejs#list_transactions) | ||
+ [Login](https://www.ubivar.com/docs/nodejs#login) | ||
+ [create(params)](https://www.ubivar.com/docs/nodejs#create_login_event) | ||
+ [retrieve(id)](https://www.ubivar.com/docs/nodejs#retrieve_login_event) | ||
+ [del(id)](https://www.ubivar.com/docs/nodejs#delete_login_event) | ||
+ [list()](https://www.ubivar.com/docs/nodejs#list_login_events) | ||
+ [Logout](https://www.ubivar.com/docs/nodejs#logout) | ||
+ [create(params)](https://www.ubivar.com/docs/nodejs#create_logout_event) | ||
+ [retrieve(id)](https://www.ubivar.com/docs/nodejs#retrieve_logout_event) | ||
+ [del(id)](https://www.ubivar.com/docs/nodejs#delete_logout_event) | ||
+ [list()](https://www.ubivar.com/docs/nodejs#list_logout_events) | ||
+ [Item](https://www.ubivar.com/docs/nodejs#items) | ||
+ [create(params)](https://www.ubivar.com/docs/nodejs#create_item) | ||
+ [retrieve(id)](https://www.ubivar.com/docs/nodejs#retrieve_item) | ||
+ [update(id, params)](https://www.ubivar.com/docs/nodejs#update_item) | ||
+ [del(id)](https://www.ubivar.com/docs/nodejs#delete_item) | ||
+ [list(params)](https://www.ubivar.com/docs/nodejs#list_items) | ||
+ [Label](https://www.ubivar.com/docs/nodejs#labels) | ||
+ [create(params)](https://www.ubivar.com/docs/nodejs#create_label) | ||
+ [retrieve(id)](https://www.ubivar.com/docs/nodejs#retrieve_label) | ||
+ [update(id, params)](https://www.ubivar.com/docs/nodejs#update_label) | ||
+ [del(id)](https://www.ubivar.com/docs/nodejs#delete_label) | ||
+ [list(params)](https://www.ubivar.com/docs/nodejs#list_labels) | ||
+ [Fx](https://www.ubivar.com/docs/nodejs#fx) | ||
+ [list(params)](https://www.ubivar.com/docs/nodejs#list_fx) | ||
```js | ||
ubivar.fx.list({ | ||
"cur_from" : "CAD" // default to EUR | ||
, "cur_to" : "GBP" // default to USD | ||
, "date" : "2015-01-01" // default to today | ||
}, function(err, res){ | ||
// returns the CAD/GBP FX of the day | ||
}) | ||
``` | ||
+ [Status](https://www.ubivar.com/docs/nodejs#status) | ||
+ [list(params)](https://www.ubivar.com/docs/nodejs#list_status) | ||
```js | ||
ubivar.status.list(function(err, res){ | ||
// returns uptime status of the web and API resources | ||
}) | ||
``` | ||
## Configuration | ||
+ `ubivar.set("auth", "your-api-token")` | ||
+ `ubivar.setTimeout(20000) // in ms`, node's default is `120000ms` | ||
# More information / wikis | ||
+ [In-depth documentation](https://www.ubivar.com/docs/nodejs) | ||
# Development | ||
To run the tests, you will need a Ubivar test API key (from your [Ubivar dashboard](https://my.ubivar.com)) | ||
``` | ||
export UBIVAR_TEST_TOKEN="your-test-api-key" | ||
npm install -g mocha | ||
npm test | ||
``` | ||
*Note: on Windows, use `SET` instead of `export` for setting the `UBIVAR_TEST_TOKEN` environment variable.* | ||
# Author | ||
Originally inspired from [stripe-node](https://github.com/stripe/stripe-node). Developed by [Fabrice Colas](https://fabricecolas.me) ([fabrice.colas@gmail.com](mailto:fabrice.colas@gmail.com)). Maintained by Ubivar. |
"use strict" | ||
var _ = require("lodash") | ||
, expect = require("chai").expect | ||
, ubivar = require("../ubivar") | ||
var _ = require("lodash") | ||
, expect = require("chai").expect | ||
, ubivar = require("../ubivar") | ||
, token = process.env.UBIVAR_TEST_TOKEN | ||
describe("Resources", function() { | ||
var rootProps = ["log","_api"] | ||
, subProps = ["auth","protocol","timeout","resources","revokedCerts","headers","request"] | ||
, methods = ["create", "retrieve", "update", "del", "list"] | ||
, resources = _.difference(Object.keys(ubivar), rootProps) | ||
var rootProps = ["log","_api"] | ||
, subProps = ["auth","protocol","timeout","resources","revokedCerts","headers","request"] | ||
, methods = ["create", "retrieve", "update", "del", "list"] | ||
, allResources = ubivar.get("resources") | ||
, specialResources = ["me", "fx", "status"] | ||
, genericResources = _.difference(allResources, specialResources) | ||
describe("Properties", function(){ | ||
it("Should have a name and path attribute", function() { | ||
_.each(resources, function(resource){ | ||
_.each(genericResources, function(resource){ | ||
_.each(methods, function(method){ | ||
@@ -23,3 +26,3 @@ expect(ubivar[resource]["path"]).to.exist | ||
it("Should link to parent (ubivar)", function() { | ||
_.each(resources, function(resource){ | ||
_.each(genericResources, function(resource){ | ||
_.each(methods, function(method){ | ||
@@ -32,3 +35,3 @@ expect(ubivar[resource]["ubivar"]).to.exist | ||
it("Should have a logger", function() { | ||
_.each(resources, function(resource){ | ||
_.each(genericResources, function(resource){ | ||
_.each(methods, function(method){ | ||
@@ -41,3 +44,3 @@ expect(ubivar[resource]["log"]).to.exist | ||
it("Should have CRUD(L) methods", function() { | ||
_.each(resources, function(resource){ | ||
_.each(genericResources, function(resource){ | ||
_.each(methods, function(method){ | ||
@@ -51,13 +54,15 @@ expect(ubivar[resource][method]).to.exist | ||
describe("Functional tests", function(){ | ||
describe("Authentication", function(){ | ||
var testIt = function(isToken, isAuthorized){ | ||
describe("Authentication [/me]", function(){ | ||
var testIt = function(token, isAuthorized){ | ||
var message = "Should be " + (isAuthorized?" ":"un") + "authorized " | ||
, pe = process.env | ||
, token = isToken ? pe.UBIVAR_TEST_TOKEN :(isToken !== false ? isToken : "unauthToken") | ||
, example = require("../data/accounts")[0] | ||
message += (isToken ?"OK":(isToken !== false ? isToken :"KO")) + " token" | ||
if(!!token){ | ||
message += "'"+ token.slice(0,5) +"...'" | ||
} else { | ||
message += "'" + token + "'" | ||
} | ||
it(message, function(done){ | ||
var ubivar = require("../../lib")(token, "latest") | ||
ubivar.accounts.create(example, function(gotError, res){ | ||
ubivar.me.retrieve(function(gotError, res){ | ||
if( isAuthorized && gotError) done((new Error(message))) | ||
@@ -71,10 +76,148 @@ if( isAuthorized && !gotError) done() | ||
testIt(false , false) | ||
testIt(null , false) | ||
testIt(undefined, false) | ||
testIt(true , true) | ||
testIt(null , false) | ||
testIt(undefined , false) | ||
testIt("unauthToken", false) | ||
testIt(token, true) | ||
it("Should update me", function(done){ | ||
var ubivar = require("../../lib")(token, "latest") | ||
ubivar.me.retrieve(function(err, res){ | ||
var me = res.data[0] | ||
, vrand = ""+Math.random() | ||
me.primary_phone= vrand | ||
ubivar.me.update(me, function(err, res){ | ||
if(!err && res.status === 200 && vrand === res.data[0]["primary_phone"]){ | ||
done() | ||
} else { | ||
console.log("\n\nError:", err | ||
, "\nResponse:" , res | ||
, "\nVrand:" , vrand) | ||
done(new Error("Should update me")) | ||
} | ||
}) | ||
}) | ||
}) | ||
it("Should fail to delete me", function(done){ | ||
var ubivar = require("../../lib")(token, "latest") | ||
ubivar.me.del(function(err, res){ | ||
if(err){ | ||
done() | ||
} else { | ||
done(new Error("Should not return any deleted resource")) | ||
} | ||
}) | ||
}) | ||
it("Should list a single me", function(done){ | ||
var ubivar = require("../../lib")(token, "latest") | ||
ubivar.me.list(function(err, res){ | ||
if(!err && res.status === 200 && res.data.length === 1){ | ||
done() | ||
} else { | ||
console.log("\n\nError:", err | ||
, "\nResponse:" , res) | ||
done(new Error("Should list a single me")) | ||
} | ||
}) | ||
}) | ||
}) | ||
_.each(resources.slice(0,7), function(resource){ | ||
describe(resource[0].toUpperCase() + resource.slice(1), function(){ | ||
describe("FX", function(){ | ||
it("Should retrieve FX", function(done){ | ||
ubivar.fx.retrieve(function(err, res){ | ||
if(err){ | ||
done(err) | ||
} else if(!(res.status === 200 && res.data.length === 1)){ | ||
done(new Error("Did not return FX of the day ")) | ||
} else{ | ||
var fx = res.data[0] | ||
if(fx.cur_from !== "EUR"){ | ||
done(new Error("Returned 'cur_from' does not default to EUR")) | ||
} else if(fx.cur_to !== "USD"){ | ||
done(new Error("Returned 'cur_to' does not default to USD")) | ||
} else if((new Date()-new Date(fx.date))/(1000*60*60*24) > 1){ | ||
done(new Error("Latest FX should be less than one day old")) | ||
} else if(!_.isNumber(fx.rate)) { | ||
done(new Error("Did not return a number")) | ||
} else { | ||
done() | ||
} | ||
} | ||
}) | ||
}) | ||
it("Should return exchange rate between non-default 'cur'", function(done){ | ||
var cur_from = "CAD" | ||
, cur_to = "GBP" | ||
ubivar.fx.list({"cur_from":cur_from, "cur_to":cur_to}, function(err, res){ | ||
if(err){ | ||
done(err) | ||
} else if(!(res.status === 200 && res.data.length === 1)){ | ||
done(new Error("Did not return FX of the day for two custom currencies")) | ||
} else { | ||
var fx = res.data[0] | ||
if(fx.cur_from !== cur_from){ | ||
done(new Error("Did not return the custom 'cur_from'")) | ||
} else if(fx.cur_to !== cur_to){ | ||
done(new Error("Did not return the custom 'cur_to'")) | ||
} else if(!_.isNumber(fx.rate)) { | ||
done(new Error("Did not return a number")) | ||
} else { | ||
done() | ||
} | ||
} | ||
}) | ||
}) | ||
it("Should return fx rate for a specific date", function(done){ | ||
var date = "2015-01-01" | ||
ubivar.fx.list({"date":date}, function(err, res){ | ||
if(err){ | ||
done(err) | ||
} else if(!(res.status === 200 && res.data.length === 1)){ | ||
done(new Error("Did not return FX of the day for two custom currencies")) | ||
} else { | ||
var fx = res.data[0] | ||
if((new Date(date)-new Date(fx.date))/(1000*60*60*24) > 1){ | ||
done(new Error("Did not return the custom 'date'")) | ||
} else if(!_.isNumber(fx.rate)) { | ||
done(new Error("Did not return a number")) | ||
} else if(fx.rate !== 1.2141) { | ||
console.log(res) | ||
done(new Error("Did not return the correct EUR/USD FX rate")) | ||
} else { | ||
done() | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
describe("Status", function(){ | ||
it("Should list of valid set of uptime statuses", function(done){ | ||
ubivar.status.list(function(err, res){ | ||
if(err){ | ||
done(err) | ||
} else if(res.data.length === 0 ){ done(new Error("Did not return results")) | ||
} else if(!res.data[0].id ){ done(new Error("Should have an id")) | ||
} else if(!res.data[0].timestamp){ done(new Error("Should have a timestamp")) | ||
} else if(!res.data[0].name ){ done(new Error("Should have a name")) | ||
} else if(!res.data[0].url ){ done(new Error("Should have a url")) | ||
} else if(!res.data[0].interval ){ done(new Error("Should have an interval")) | ||
} else if(!res.data[0].status ){ done(new Error("Should have a status")) | ||
} else if(!res.data[0].alltimeuptimeratio){ done(new Error("Should have an all time uptime ratio")) | ||
} else{ | ||
done() | ||
} | ||
}) | ||
}) | ||
}) | ||
_.each(genericResources, function(resource){ | ||
describe(resource[0].toUpperCase() + resource.slice(1) | ||
, function(){ | ||
var example = require("../data/"+resource) | ||
@@ -138,13 +281,119 @@ , idResource | ||
it("Should list the resources", function(done){ | ||
var params = {"start_after": 0, "end_before": 1} | ||
, nList = params.end_before - params.start_after | ||
describe("Should list and paginate", function(){ | ||
var ids = [] | ||
ubivar[resource].list(params, function(err, res){ | ||
if(res.status === 200 && res.data.length === nList){ | ||
before(function(done){ | ||
var example = require("../data/"+resource) | ||
this.timeout(5000) | ||
ubivar[resource].create(example[0], function(err, res){ | ||
if(err) return done(err) | ||
ids.push(res.data[0].id) | ||
ubivar[resource].create(example[0], function(err, res){ | ||
if(err) return done(err) | ||
ids.push(res.data[0].id) | ||
ubivar[resource].create(example[0], function(err, res){ | ||
if(err) return done(err) | ||
ids.push(res.data[0].id) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
it("Should limit retrieved resources to N=1", function(done){ | ||
var nLimit = 1 | ||
ubivar[resource].list({limit:nLimit}, function(err, res){ | ||
if(err) done(err) | ||
else if(res.data.length === nLimit) done() | ||
else done(new Error("Should 'limit' list to N="+nLimit+" resources")) | ||
}) | ||
}) | ||
it("Should limit retrieved resources to N=2", function(done){ | ||
var nLimit = 2 | ||
ubivar[resource].list({limit:nLimit}, function(err, res){ | ||
if(err) done(err) | ||
else if(res.data.length === nLimit) done() | ||
else done(new Error("Should 'limit' list to N="+nLimit+" resources")) | ||
}) | ||
}) | ||
it("Should 'start_after' when paginating", function(done){ | ||
var nLimit = 2 | ||
ubivar[resource].list({"start_after": ids[0], "limit":nLimit}, function(err, res){ | ||
if(err) return done(err) | ||
if(res.data.length !== nLimit) return done(new Error("Should return N="+nLimit)) | ||
var returnedIds = _.pluck(res.data, "id") | ||
if(_.contains(returnedIds, ids[0])){ | ||
return done(new Error("Should not return 'starting_after' id")) | ||
} | ||
done() | ||
} else { | ||
done(new Error("Did not return a list of " + resource)) | ||
} | ||
}) | ||
}) | ||
it("Should 'start_after' and 'end_before' when paginating", function(done){ | ||
var nLimit = 1 | ||
ubivar[resource].list({"end_before": ids[2], "start_after":ids[0]}, function(err, res){ | ||
if(err) return done(err) | ||
if(res.data.length !== nLimit){ | ||
console.log(res.data);return done(new Error("Should return N="+nLimit)) | ||
} | ||
var returnedIds = _.pluck(res.data, "id") | ||
if(_.contains(returnedIds, ids[1])) return done() | ||
else return done(new Error("Should not return 'starting_after' id")) | ||
}) | ||
}) | ||
it("Should list ids greater than (gt) a given id", function(done){ | ||
var nLimit = 2 | ||
, object = {"id":{"gt":ids[0]}, "limit":nLimit} | ||
ubivar[resource].list(object, function(err, res){ | ||
if(err) return done(err) | ||
if(res.data.length !== nLimit) return done(new Error("Should return N="+nLimit)) | ||
var returnedIds = _.pluck(res.data, "id") | ||
if(_.contains(returnedIds, ids[1]) && _.contains(returnedIds, ids[2])){ | ||
return done() | ||
} | ||
return done(new Error("Should contain the right ids")) | ||
}) | ||
}) | ||
it("Should list and order DESC the ids that are less than (lt) an id", function(done){ | ||
var nLimit = 2 | ||
ubivar[resource].list({"id":{"lt":ids[2]}, "order":"-id", "limit":nLimit}, function(err, res){ | ||
if(err) return done(err) | ||
if(res.data.length !== nLimit) return done(new Error("Should return N="+nLimit)) | ||
var returnedIds = _.pluck(res.data, "id") | ||
if(_.contains(returnedIds, ids[0]) && _.contains(returnedIds, ids[1]) && !_.contains(returnedIds,ids[2])){ | ||
return done() | ||
} | ||
return done(new Error("Should contain the right ids")) | ||
}) | ||
}) | ||
it("Should list ids greater than or equal (gte) to a given id", function(done){ | ||
var nLimit = 3 | ||
ubivar[resource].list({"id":{"gte":ids[0]}, "limit":nLimit}, function(err, res){ | ||
if(err) return done(err) | ||
if(res.data.length !== nLimit) return done(new Error("Should return N="+nLimit)) | ||
var returnedIds = _.pluck(res.data, "id") | ||
if(_.contains(returnedIds, ids[0]) && _.contains(returnedIds, ids[1]) && _.contains(returnedIds, ids[2])){ | ||
return done() | ||
} | ||
return done(new Error("Should contain the right ids")) | ||
}) | ||
}) | ||
it("Should list and order DESC the ids that are less than or equal (lte) an id", function(done){ | ||
var nLimit = 3 | ||
ubivar[resource].list({"id":{"lte":ids[2]}, "order":"-id", "limit":nLimit}, function(err, res){ | ||
if(err) return done(err) | ||
if(res.data.length !== nLimit) return done(new Error("Should return N="+nLimit)) | ||
var returnedIds = _.pluck(res.data, "id") | ||
if(_.contains(returnedIds, ids[0]) && _.contains(returnedIds, ids[1]) && _.contains(returnedIds,ids[2])){ | ||
return done() | ||
} | ||
return done(new Error("Should contain the right ids")) | ||
}) | ||
}) | ||
}) | ||
@@ -151,0 +400,0 @@ }) |
@@ -12,3 +12,3 @@ "use strict" | ||
, methods = ["create", "retrieve", "update", "del", "list"] | ||
, resources = _.difference(Object.keys(ubivar), rootProps) | ||
, resources = ubivar.get("resources") | ||
@@ -15,0 +15,0 @@ this.timeout(20000) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
44591
953
167
29