1 require "test/test_helper"
3 class BigIntegrationTest < ActionController::IntegrationTest
4 def random_purchase_amount
8 def test_the_happy_path
12 assert_difference "CreditCard.count" do
13 card = create_credit_card(:passphrase => passphrase)
15 assert_response :created
17 amount = random_purchase_amount
18 assert_difference 'Authorization.count' do
19 authorize_existing_card(amount, card, passphrase)
21 assert_response :created
22 authorization = assigns(:authorization)
24 assert_difference 'Capture.count' do
25 capture(amount, authorization)
27 assert_response :created
30 def test_authorizing_a_one_time_card
31 card = valid_cc_attributes
32 assert_difference 'Authorization.count' do
33 authorize_one_time_card random_purchase_amount, card
34 assert_response :created
38 def test_storing_a_bad_card
40 assert_no_difference "CreditCard.count" do
41 card = create_credit_card(:number => 'oh crap')
43 assert card.errors.on(:number)
46 def test_cant_create_card_with_no_salt_supplied
47 card = create_credit_card(:passphrase => '')
48 assert_equal card.errors.on(:passphrase), "can't be blank"
49 card = create_credit_card(:passphrase => nil)
50 assert_equal card.errors.on(:passphrase), "can't be blank"
53 def test_capturing_a_bad_amount_should_fail
55 card = create_credit_card(:passphrase => passphrase)
56 amount = random_purchase_amount
57 transaction_id = authorize_existing_card(amount, card, passphrase)
58 capture(amount.to_i + 1, transaction_id) # trying to capture with an amount too high
59 assert response.headers["X-CaptureSuccess"] != true
62 def test_authorizing_with_bad_passphrase_should_suck
63 amount = random_purchase_amount
65 bad_passphrase = '54321'
66 card = create_credit_card(:passphrase => passphrase)
68 assert_no_difference 'Authorization.count' do
69 authorize_existing_card(amount, card, bad_passphrase)
73 def test_voiding_an_authorized_order
74 card = create_credit_card
75 amount = random_purchase_amount
76 authorization = authorize_existing_card(amount, card)
77 assert_difference 'Void.count' do
82 def test_voiding_a_refund
83 card = create_credit_card
84 amount = random_purchase_amount
85 authorization = authorize_existing_card(amount, card)
86 capture(amount, authorization)
87 # TODO not sure how to test a refund, other than mocking out the calls to $gateway
88 #assert_difference 'Refund.count' do
89 #refund = refund(amount, authorization)
91 #assert_difference 'Void.count' do
98 def valid_cc_attributes
99 { :number => '4242424242424242',
100 :year => Time.now.year + 1,
101 :month => Time.now.month,
102 :name => "Joe Van Dyk",
103 :street_address => "123 Main",
108 :passphrase => 'blurb' }
111 def create_credit_card options={}
112 card_values = valid_cc_attributes
113 card_values.merge!(options)
114 post credit_cards_url(:credit_card => card_values, :format => 'xml')
115 assigns(:credit_card)
118 def authorize_existing_card amount, card, passphrase='blurb'
119 post authorizations_url(:authorization => { :amount => amount, :credit_card_id => card.id, :passphrase => passphrase })
120 assigns(:authorization)
123 def authorize_one_time_card amount, card
124 post authorizations_url(:authorization => { :amount => amount, :credit_card => card })
125 assigns(:authorization)
128 def capture amount, authorization
129 post captures_url(:capture => {:amount => amount, :authorization => authorization})
133 def refund amount, authorization
134 post refunds_url(:amount => amount, :authorization => authorization)
138 def void thing_to_void
139 post voids_url(:void => { :voidee_type => thing_to_void.class, :voidee_id => thing_to_void})