Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #20 from 2sidedfigure/master

Add Plans API test
  • Loading branch information...
commit 39c261096d87230a1166723d7eb156cd7b67e211 2 parents 78947c1 + cc62fe8
@abh abh authored
Showing with 278 additions and 0 deletions.
  1. +147 −0 test/invoices.js
  2. +131 −0 test/plans.js
View
147 test/invoices.js
@@ -0,0 +1,147 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ sys = require('sys');
+
+var api_key = process.env.STRIPE_API;
+
+if (!api_key) {
+ sys.puts('To run vows, you must have a STRIPE_API environment variable with a test api key');
+ process.exit(2)
+}
+
+var stripe = require('./../lib/main.js')(api_key);
+
+var customer, plan, charge;
+
+vows.describe("Invoice API").addBatch({
+ 'Create a subscription plan': {
+ topic: function() {
+ stripe.plans.create({
+ id: 'foobarbaz_plan',
+ amount: 2000,
+ currency: 'usd',
+ interval: 'month',
+ name: 'The super duper FooBarBaz subscription!'
+ }, this.callback);
+ },
+ 'returns a plan': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'plan');
+ assert.equal(response.id, 'foobarbaz_plan');
+
+ plan = response;
+ },
+ 'Create a customer': {
+ topic: function(plan_err, plan) {
+ var d = new Date();
+ stripe.customers.create({
+ email: "foo@example.com",
+ plan: plan.id,
+ card: { number: "4111111111111111",
+ exp_month: d.getMonth() + 1,
+ exp_year: d.getFullYear() + 1,
+ name: "T. Ester"
+ }
+ }, this.callback);
+ },
+ 'returns a customer': function(err, response) {
+ assert.isNull(err);
+
+ console.log("response", response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'customer');
+ assert.equal(response.email, "foo@example.com");
+
+ customer = response;
+ },
+ 'Create a charge to create the first invoice': {
+ topic: function(cust_err, customer) {
+ stripe.charges.create({
+ amount: 500,
+ currency: 'usd',
+ customer: customer.id
+ }, this.callback);
+ },
+ 'created a charge': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'charge');
+ assert.equal(response.paid, true);
+ },
+ 'List invoices': {
+ topic: function() {
+ stripe.invoices.list({ customer: customer.id }, this.callback);
+ },
+ 'receives a list of invoices': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.isArray(response.data);
+ assert.strictEqual(response.data.length > 0, true);
+
+ var invoice = response.data[0];
+ assert.isObject(invoice);
+ assert.equal(invoice.object, 'invoice');
+ },
+ 'Retrieve an existing invoice': {
+ topic: function(list_err, list) {
+ var invoice = list.data[0];
+ stripe.invoices.retrieve(invoice.id, this.callback);
+ },
+ 'received invoice': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'invoice');
+ assert.strictEqual(response.total > 0, true);
+ },
+ 'Retrieve an upcoming invoice': {
+ topic: function() {
+ stripe.invoices.upcoming(customer.id, this.callback);
+ },
+ 'received upcoming invoice': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'invoice');
+ assert.strictEqual(response.total > 0, true);
+ },
+ 'Delete the customer': {
+ topic: function() {
+ stripe.customers.del(customer.id, this.callback);
+ },
+ 'customer deleted': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.isTrue(response.deleted);
+ },
+ 'Delete the subscription plan': {
+ topic: function() {
+ stripe.plans.del(plan.id, this.callback);
+ },
+ 'deleted the subscription plan': function(err, response) {
+ assert.isNull(err);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.isTrue(response.deleted);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}).export(module, {error: false});
View
131 test/plans.js
@@ -0,0 +1,131 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ sys = require('sys');
+
+var api_key = process.env.STRIPE_API;
+
+if (!api_key) {
+ sys.puts('To run vows, you must have a STRIPE_API environment variable with a test api key');
+ process.exit(2)
+}
+
+var stripe = require('./../lib/main.js')(api_key);
+
+var plans = [], // hold the plans we create during the test
+ getFirstPlan = function(shift) {
+ var plan = shift ? plans.shift() : plans[0];
+
+ if (!plan) {
+ console.log('No plan was created!');
+ process.exit(2);
+ }
+
+ return plan;
+ };
+
+vows.describe("Subscription Plans API").addBatch({
+ 'Create subscription plan': {
+ topic: function() {
+ stripe.plans.create({
+ id: 'foobarbaz_plan',
+ amount: 2000,
+ currency: 'usd',
+ interval: 'year',
+ name: 'The super duper FooBarBaz subscription!'
+ }, this.callback);
+ },
+ 'returns a plan': function(err, response) {
+ assert.isNull(err);
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'plan');
+ assert.equal(response.id, 'foobarbaz_plan');
+ assert.equal(response.interval, 'year');
+ assert.equal(response.amount, 2000);
+
+ plans.push(response);
+ },
+ 'Retrieve plan': {
+ topic: function(create_err, plan) {
+ stripe.plans.retrieve(plan.id, this.callback);
+ },
+ 'returns the previously created plan': function(err, response) {
+ var p = getFirstPlan();
+
+ assert.isNull(err);
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.id, p.id);
+ }
+ },
+ 'Create another plan': {
+ topic: function() {
+ stripe.plans.create({
+ id: 'bazbarfoo_plan',
+ amount: 4000,
+ currency: 'usd',
+ interval: 'month',
+ name: 'The super duper BazBarFoo subscription!'
+ }, this.callback);
+ },
+ 'returns a plan': function(err, response) {
+ assert.isNull(err);
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.equal(response.object, 'plan');
+ assert.equal(response.id, 'bazbarfoo_plan');
+ assert.equal(response.interval, 'month');
+ assert.equal(response.amount, 4000);
+
+ plans.push(response);
+ },
+ 'List plans': {
+ topic: function() {
+ stripe.plans.list(null, null, this.callback);
+ },
+ 'returns a list of plans': function(err, response) {
+ assert.isNull(err);
+ console.log('response', response);
+
+ assert.isDefined(response);
+ assert.isNumber(response.count);
+ assert.strictEqual(response.count >= 2, true);
+ },
+ 'Delete plans': {
+ topic: function() {
+ var p = getFirstPlan();
+
+ stripe.plans.del(p.id, this.callback);
+ },
+ 'deleted the first plan': function(err, response) {
+ assert.isNull(err);
+
+ var p = getFirstPlan(true);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.isTrue(response.deleted);
+ assert.equal(response.id, p.id);
+ },
+ 'delete the next plan': {
+ topic: function() {
+ var p = getFirstPlan();
+
+ stripe.plans.del(p.id, this.callback);
+ },
+ 'deleted the next plan': function(err, response) {
+ assert.isNull(err);
+
+ var p = getFirstPlan(true);
+
+ console.log('response', response);
+ assert.isDefined(response);
+ assert.isTrue(response.deleted);
+ assert.equal(response.id, p.id);
+ }
+ }
+ }
+ }
+ }
+ }
+}).export(module, {error: false});
Please sign in to comment.
Something went wrong with that request. Please try again.