Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Add a few tests using clucumber and fix bugs exposed.
Browse files Browse the repository at this point in the history
Now tests charge creation with an explicit card and a token.

Bugs exposed and fixed:

 * Declare that create-{charge,token,invoice-item} return the ID of
   objects created.
 * Use the correct resource to create a token.
  • Loading branch information
antifuchs committed Oct 13, 2011
1 parent 98816b5 commit 1fe6bff
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.fasl
bin
vendor/ruby
.bundle
1 change: 1 addition & 0 deletions .rbenv-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.8.7-p352
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gem 'clucumber'
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
GEM
remote: http://rubygems.org/
specs:
builder (3.0.0)
clucumber (0.4.1)
cucumber (~> 0.10.0)
cucumber (0.10.7)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.4.0)
json (>= 1.4.6)
term-ansicolor (>= 1.0.5)
diff-lcs (1.1.3)
gherkin (2.4.21)
json (>= 1.4.6)
json (1.6.1)
term-ansicolor (1.0.7)

PLATFORMS
ruby

DEPENDENCIES
clucumber
10 changes: 5 additions & 5 deletions cl-stripe.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@


;;; Charges
(def-api-call :create (:charge :http-resource :charges)
(def-api-call :create (:charge :http-resource :charges :return-id t)
(amount currency customer card description)
"https://stripe.com/api/docs#create_charge")

Expand All @@ -188,7 +188,7 @@


;;; Customers
(def-api-call :create (:customer :http-resource :customers)
(def-api-call :create (:customer :http-resource :customers :return-id t)
(card coupon email description plan trial-end)
"https://stripe.com/api/docs#create_customer")

Expand All @@ -206,7 +206,7 @@


;;; Card Tokens
(def-api-call :create :token (card amount currency)
(def-api-call :create (:token :http-resource :tokens :return-id t) (card amount currency)
"https://stripe.com/api/docs#create_token")

(def-api-call :retrieve (:token :id t) ()
Expand Down Expand Up @@ -250,7 +250,7 @@


;;; Invoice items
(def-api-call :create (:invoice-item :http-resource :invoice-items)
(def-api-call :create (:invoice-item :http-resource :invoice-items :return-id t)
(customer amount currency description)
"https://stripe.com/api/docs#create_invoiceitem")

Expand All @@ -264,4 +264,4 @@
"https://stripe.com/api/docs#delete_invoiceitem")

(def-api-call :list :invoice-items (customer count offset)
"https://stripe.com/api/docs#list_invoiceitems")
"https://stripe.com/api/docs#list_invoiceitems")
24 changes: 24 additions & 0 deletions features/charge.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Charges

As a user of stripe,
I want to create charges
so I can get paid by my customers.

Background:

When I use the API key "wOUwrSt8JrWsPyHCsfkCDhCCqGomv4kp"

Scenario: Create a charge with a credit card

When I charge $20 to a random credit card

Then the charge should exist
And the charge should be for $20

Scenario: Create a charge with a token

When I create a token for a random credit card
And I charge ct3033 to the token

Then the charge should exist
And the charge should be for ct3033
2 changes: 2 additions & 0 deletions features/step_definitions/api_steps.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(When* #?{^I use the API key "([^"]*)"$} (key)
(stripe:set-default-api-key key))
10 changes: 10 additions & 0 deletions features/step_definitions/card_steps.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(When* #?{^I create a token for a random credit card$} ()
(setf (var :card) *random-card*
(var :amount) nil
(var :currency) nil)
(multiple-value-bind (token id)
(stripe:create-token :card (var :card))
(assert (stringp id) (id)
"ID is not a string: ~s" id)
(setf (var :token) token
(var :token-id) id)))
49 changes: 49 additions & 0 deletions features/step_definitions/charge_steps.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
;; -*- lisp -*-

(defun lookup-currency-symbol (currency-symbol)
(values
(cdr (assoc currency-symbol '(("$" . "usd")
("ct" . "usd"))
:test 'string-equal))
(if (string-equal currency-symbol "$")
100
1)))

;; TODO: generate the card number randomly
(defvar *random-card* (list :number "4242424242424242" :exp_month "12" :exp_year "2012" :cvc "123"
:name "Tester Oberloisl"
:address-line1 "555 Random Avenue" :address-line2 "Apt 9"
:address-zip "55959" :address-state "Ohai-o"
:address_country "USA"))

(Then* #?{^the charge should exist$} ()
(assert (nth-value 1 (stripe:retrieve-charge (var :charge-id)))))

(Then* #?{^the charge should be for ([^0-9]+)\s*([0-9]+)$} (currency-symbol amount)
(let ((amount (parse-integer amount)))
(multiple-value-bind (currency multiplier) (lookup-currency-symbol currency-symbol)
(assert (string-equal (stripe:sstruct-get (var :charge) :currency) currency) ()
"Currencies are not equal: ~s != ~s" (stripe:sstruct-get :currency) currency)
(assert (= (stripe:sstruct-get (var :charge) :amount) (* amount multiplier)) ()
"Amounts are not equal: ~s != ~s"
(/ (stripe:sstruct-get (var :charge) :amount) multiplier) amount))))

(When* #?{^I charge ([^0-9]+)\s*([0-9]+) to (a random credit card|the token)$} (currency-symbol amount what)
(if (string= what "a random credit card")
(setf (var :card) *random-card*)
(setf (var :card) (var :token-id)))
(multiple-value-bind (currency multiplier) (lookup-currency-symbol currency-symbol)
(setf (var :currency) currency)
(setf (var :amount) (parse-integer amount :junk-allowed nil))
(setf (var :amount) (* multiplier (var :amount))))

(multiple-value-bind (charge id)
(stripe:create-charge :amount (var :amount)
:currency (var :currency)
:description (format nil "Cucumber test charge ~a" (get-universal-time))
:card (var :card))
(assert (stringp id) (id)
"ID is not a string: ~s" id)
(setf (var :charge) charge
(var :charge-id) id)))

2 changes: 2 additions & 0 deletions features/step_definitions/clucumber.wire
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host: 127.0.0.1
port: <%= ENV["CLUCUMBER_PORT"] || 42427 %>
13 changes: 13 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'clucumber'
begin
puts "Launching..."
(@main_clucumber = ClucumberSubprocess.launch(File.expand_path("../", File.dirname(__FILE__)))).listen <<-LISP
(load #p"#{File.expand_path("../../cl-stripe.asd", File.dirname(__FILE__))}")
(ql:quickload :cl-stripe)
LISP
STDERR.puts(@main_clucumber.output)
rescue PTY::ChildExited => e
STDERR.puts "Failure: #{@main_clucumber.inspect}: #{e.inspect}!"
STDERR.puts(@main_clucumber && @main_clucumber.output)
exit 1
end

0 comments on commit 1fe6bff

Please sign in to comment.