diff options
| -rw-r--r-- | billing/config/initializers/braintree.rb | 6 | ||||
| -rw-r--r-- | billing/lib/braintree_test_app.rb | 36 | ||||
| -rw-r--r-- | billing/test/integration/customer_creation_test.rb | 36 | ||||
| -rw-r--r-- | core/leap_web_core.gemspec | 2 | 
4 files changed, 62 insertions, 18 deletions
| diff --git a/billing/config/initializers/braintree.rb b/billing/config/initializers/braintree.rb index ee21088..523dbce 100644 --- a/billing/config/initializers/braintree.rb +++ b/billing/config/initializers/braintree.rb @@ -1,5 +1,11 @@ +require 'braintree_test_app' +  Braintree::Configuration.logger = Logger.new('log/braintree.log')  Braintree::Configuration.environment = :sandbox  Braintree::Configuration.merchant_id = "bwrdyczvjspmxjhb"  Braintree::Configuration.public_key = "jmw58nbmjg84prbp"  Braintree::Configuration.private_key = "SET_ME" + +if Rails.env.test? +  Rails.application.config.middleware.use BraintreeTestApp +end diff --git a/billing/lib/braintree_test_app.rb b/billing/lib/braintree_test_app.rb new file mode 100644 index 0000000..41c327d --- /dev/null +++ b/billing/lib/braintree_test_app.rb @@ -0,0 +1,36 @@ +# RackTest assumes all requests to be local. +# Braintree requests need to go out to a different server though. +# So we use a middleware to catch these and send them out again. + +class BraintreeTestApp +  def initialize(app) +    @app = app +  end + +  def call(env) +    @env = env +    config = Braintree::Configuration.instantiate +    if request.path =~ /\/merchants\/#{config.merchant_id}\/transparent_redirect_requests$/ +      #proxy post to braintree +      uri = URI.parse(config.protocol + "://" + config.server + ":" + +        config.port.to_s + request.path) +      http = Net::HTTP.new(uri.host, uri.port) +      res = http.post(uri.path, request.body.read) + +      if res.code == "303" +        header_hash = res.header.to_hash +        header_hash["location"].first.gsub!("http://localhost:3000/", "http://www.example.com/") +        [303, {"location" => header_hash["location"].first}, ""] +      else +        raise "unexpected response from Braintree: expected a 303" +      end +    else +      @app.call(env) +    end +  end + +  def request +    @request = Rack::Request.new(@env) +  end +end + diff --git a/billing/test/integration/customer_creation_test.rb b/billing/test/integration/customer_creation_test.rb index 50116db..3ab2e4f 100644 --- a/billing/test/integration/customer_creation_test.rb +++ b/billing/test/integration/customer_creation_test.rb @@ -8,17 +8,32 @@ class CustomerCreationTest < ActionDispatch::IntegrationTest    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 first request to Braintree is triggered outside of rails +  # The request to Braintree is triggered outside of rails    test "successfully confirms customer creation" do -    user = FactoryGirl.create(:user) -    login_as user      response = post_transparent_redirect :create_customer_data,        customer: FactoryGirl.attributes_for(:braintree_customer),        redirect_url: confirm_customer_url @@ -28,20 +43,7 @@ class CustomerCreationTest < ActionDispatch::IntegrationTest      end      assert_equal 200, status -    assert customer = Customer.find_by_user_id(user.id) -    assert customer.braintree_customer -  end - -  # let's test both steps together with capybara -  test "create customer with braintree" do -    user = FactoryGirl.create(:user) -    login_as user -    visit '/customer/new' -    assert_difference("Customer.count") do -      click_button 'Save Payment Info' -    end -    assert_equal 200, status -    assert customer = Customer.find_by_user_id(user.id) +    assert customer = Customer.find_by_user_id(@user.id)      assert customer.braintree_customer    end diff --git a/core/leap_web_core.gemspec b/core/leap_web_core.gemspec index e572200..9ec64e9 100644 --- a/core/leap_web_core.gemspec +++ b/core/leap_web_core.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s|    s.add_dependency "couchrest", "~> 1.1.3"    s.add_dependency "couchrest_model", "~> 2.0.0.beta2" -  s.add_dependency "couchrest_session_store", "~> 0.0.9" +  s.add_dependency "couchrest_session_store", "~> 0.1.2"    s.add_dependency "json"  end | 
