summaryrefslogtreecommitdiff
path: root/billing/test/integration/customer_creation_test.rb
blob: 3ab2e4fa44e5ec728cc4c4ea02398396909650b2 (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
require 'test_helper'
require 'fake_braintree'
require 'capybara/rails'

class CustomerCreationTest < ActionDispatch::IntegrationTest
  include Warden::Test::Helpers
  include Capybara::DSL

  setup do
    Warden.test_mode!
    @user = FactoryGirl.create(:user)
    login_as @user
  end

  teardown do
    Warden.test_reset!
  end

  # Let's test both steps together with capybara
  #
  # This test is nice and clean but also a bit fragil:
  # RackTest assumes all requests to be local. So we need
  # BraintreeTestApp for the braintree transparent redirect to work.
  test "create customer with braintree" do
    visit '/customer/new'
    assert_difference("Customer.count") do
      click_button 'Save Payment Info'
    end

    assert customer = Customer.find_by_user_id(@user.id)
    assert customer.braintree_customer
  end

  # We only test the confirmation here.
  # The request to Braintree is triggered outside of rails
  test "successfully confirms customer creation" do
    response = post_transparent_redirect :create_customer_data,
      customer: FactoryGirl.attributes_for(:braintree_customer),
      redirect_url: confirm_customer_url

    assert_difference("Customer.count") do
      post response['Location']
    end

    assert_equal 200, status
    assert customer = Customer.find_by_user_id(@user.id)
    assert customer.braintree_customer
  end

  def post_transparent_redirect(type, data)
    params = data.dup
    params[:tr_data] = Braintree::TransparentRedirect.send(type, params)
    post_transparent_redirect_params(params)
  end

  def post_transparent_redirect_params(params)
    uri = URI.parse(Braintree::TransparentRedirect.url)
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.post(uri.path, Rack::Utils.build_nested_query(params))
    end
  end

end