summaryrefslogtreecommitdiff
path: root/billing/test/functional/payments_controller_test.rb
blob: 055a990ba25830b78aad2fddf25d5226ed2c14a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'test_helper'
require 'fake_braintree'

class PaymentsControllerTest < ActionController::TestCase

  test "payment when unauthorized" do
    get :new
    assert_not_nil assigns(:tr_data)
    assert_response :success
  end

  test "authenticated user must create account before making payment" do
    login
    get :new
    assert_response :redirect
    assert_equal new_customer_url, response.header['Location']
  end

  test "payment when authenticated as customer" do
    user = find_record :user
    customer = stub_record :customer_with_payment_info, user: user
    Customer.stubs(:find_by_user_id).with(user.id).returns(customer)
    login customer.user
    get :new
    assert_not_nil assigns(:tr_data)
    assert_response :success
  end

  test "successful confirmation renders confirm" do
    Braintree::TransparentRedirect.expects(:confirm).returns(success_response)
    get :confirm

    assert_response :success
    assert_template :confirm
  end

  test "failed confirmation renders new" do
    Braintree::TransparentRedirect.expects(:confirm).returns(failure_response)
    get :confirm

    assert_response :success
    assert_not_nil assigns(:tr_data)
    assert_template :new
  end

  def failure_response
    stub success?: false,
      errors: stub(for: nil, size: 0),
      params: {},
      transaction: stub(status: nil)
  end

  def success_response
    stub success?: true,
      transaction: stub_transaction
  end

  # that's what you get when not following the law of demeter...
  def stub_transaction
    stub amount: "100.00",
      id: "ASDF",
      customer_details: FactoryGirl.build(:braintree_customer),
      credit_card_details: FactoryGirl.build(:braintree_customer).credit_cards.first
  end

end