Upgraded Rails
[monkeycharger.git] / test / integration / big_test.rb
blob970a92b43c849749591fba1a62297e138815651f
1 require "test/test_helper"
3 class BigIntegrationTest < ActionController::IntegrationTest
4   def random_purchase_amount
5     (rand(10000) + 1).to_s
6   end
8    def test_the_happy_path
9       passphrase = '12345'
11       card = nil
12       assert_difference "CreditCard.count" do
13          card = create_credit_card(:passphrase => passphrase)
14       end
15       assert_response :created
17       amount = random_purchase_amount
18       assert_difference 'Authorization.count' do
19         authorize_existing_card(amount, card, passphrase)
20       end
21       assert_response :created
22       authorization = assigns(:authorization)
24       assert_difference 'Capture.count' do
25         capture(amount, authorization)
26       end
27       assert_response :created
28    end
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
35      end
36    end
38    def test_storing_a_bad_card
39       card = nil
40       assert_no_difference "CreditCard.count" do
41         card = create_credit_card(:number => 'oh crap')
42       end
43        assert card.errors.on(:number)
44    end
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"
51    end
53    def test_capturing_a_bad_amount_should_fail
54       passphrase = '12345'
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
60    end
62    def test_authorizing_with_bad_passphrase_should_suck
63       amount = random_purchase_amount
64       passphrase = '12345'
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)
70       end
71    end
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
78        void(authorization)
79      end
80    end
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)
90      #end
91      #assert_difference 'Void.count' do
92        #void(refund) 
93      #end
94    end
96    private
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",
104         :city           => "Seattle",
105         :state          => "WA",
106         :zip            => "98115", 
107         :country        => "USA",
108         :passphrase     => 'blurb' }
109    end
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)
116    end
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)
121    end
123    def authorize_one_time_card amount, card
124       post authorizations_url(:authorization => { :amount => amount, :credit_card => card })
125       assigns(:authorization)
126    end
128    def capture amount, authorization
129       post captures_url(:capture => {:amount => amount, :authorization => authorization})
130       assigns(:capture)
131    end
133    def refund amount, authorization
134       post refunds_url(:amount => amount, :authorization => authorization)
135       assigns(:refund)
136    end
138    def void thing_to_void
139      post voids_url(:void => { :voidee_type => thing_to_void.class, :voidee_id => thing_to_void})
140      assigns(:void)
141    end