summaryrefslogtreecommitdiff
path: root/billing/test/functional/customer_controller_test.rb
blob: 878ed48650889e951c108d77326121f4636b26a1 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'test_helper'
require 'fake_braintree'

class CustomerControllerTest < ActionController::TestCase

  test "new assigns redirect url" do
    login
    get :new

    assert_response :success
    assert assigns(:tr_data)
    tr_data = Braintree::Util.parse_query_string(assigns(:tr_data))
    assert_equal confirm_customer_url, tr_data[:redirect_url]
  end

  test "new requires login" do
    get :new

    assert_response :redirect
    assert_redirected_to login_path
  end

  test "edit uses params[:id]" 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 :edit, id: customer.user.id

    assert_response :success
    assert assigns(:tr_data)
    tr_data = Braintree::Util.parse_query_string(assigns(:tr_data))
    assert_equal customer.braintree_customer_id, tr_data[:customer_id]
    assert_equal confirm_customer_url, tr_data[:redirect_url]
  end

  test "confirm user creation" do
    login
    Braintree::TransparentRedirect.expects(:confirm).returns(success_response)
    # to_confirm = prepare_confirmation :create_customer_data,
    #   customer: FactoryGirl.attributes_for(:braintree_customer),
    #   redirect_url: confirm_customer_url

    assert_difference("Customer.count") do
      post :confirm, braintree: :query
    end

    assert_response :success
    assert result = assigns(:result)
    assert result.success?
    assert result.customer.id
  end

  test "customer update" do
    user = find_record :user
    customer = stub_record :customer_with_payment_info, user: user
    customer.expects(:save)
    Customer.stubs(:find_by_user_id).with(user.id).returns(customer)
    login customer.user
    Braintree::TransparentRedirect.expects(:confirm).
      returns(success_response(customer))

    assert_no_difference("Customer.count") do
      post :confirm, query: :from_braintree
    end

    assert_response :success
    assert result = assigns(:result)
    assert result.success?
    assert_equal customer.braintree_customer, result.customer
  end

  test "failed user creation" do
    skip "can't get user creation to fail"
    login
    FakeBraintree.decline_all_cards!
    # what is prepare_confirmation ?? this method isn't found
    to_confirm = prepare_confirmation :create_customer_data,
      customer: FactoryGirl.attributes_for(:broken_customer),
      redirect_url: confirm_customer_url
    post :confirm, to_confirm

    FakeBraintree.clear!
    assert_response :success
    assert result = assigns(:result)
    assert !result.success?
  end

  test "failed user creation with stubbing" do
    login
    Braintree::TransparentRedirect.expects(:confirm).returns(failure_response)
    post :confirm, bla: :blub

    assert_response :success
    assert_template :new
  end

  test "failed user update with stubbing" 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
    Braintree::TransparentRedirect.expects(:confirm).returns(failure_response)
    post :confirm, bla: :blub

    assert_response :success
    assert_template :edit
  end

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

  def success_response(customer = nil)
    stub success?: true,
      customer: braintree_customer(customer)
  end

  def braintree_customer(customer)
    if customer
      customer.braintree_customer
    else
      FactoryGirl.build :braintree_customer
    end
  end

end