Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bogus gateway to test PayPal #424

Closed
wants to merge 3 commits into from
@sideshowcoder

PayPal has some methods untestable via the normal Bogus Gateway
like setup_authorization, and handles authorization differently, the
PaypalBogusGateway behaves more like the actual PayPal Gateway to enable
simple tests without remote interaction for setup_authorization as well as authorize.

@sideshowcoder sideshowcoder Bogus gateway to test PayPal
Since PayPal has some methods untestable via the normal Bogus Gateway
like setup_authorization, and handles authorization differently, the
PaypalBogusGateway behaves more like the actual PayPal Gateway to enable
simple tests without remote interaction
192f497
@travisbot

This pull request fails (merged 192f497 into 7cad106).

@travisbot

This pull request fails (merged 17747b9 into 7cad106).

@travisbot

This pull request passes (merged 4478947 into 7cad106).

@ntalbott
Collaborator

It seems like the general usefulness of the BogusGateway is that it is a generic stand-in for any gateway; how would you see the PaypalBogusGateway being used?

@sideshowcoder

Actually the BogusGateway does not allow testing the Paypal specific methods like setup_authorization, since the Application I am working on only works with Paypal only I wanted to be able to test my code with these methods in place as well. That was the reason to extend the Bogus Gateway to also include these methods and give the correct responses.

So in my code the PaypalBogusGateway stands in for a PaypalExpressGateway and responds to the setup_* methods in the form a Paypal Gateway would.

@jduff
Admin
@ntalbott
Collaborator

I'm with @jduff; the PayPal Express gateway is actually kind of an oddity in the ActiveMerchant codebase. Until/unless other gateways start using a common API with it, it's probably best to just mock out yourself locally.

@ntalbott ntalbott closed this
@adamtao

Even though this commit didn't get merged, I wanted to thank you for posting it. I was able to use it to test paypal express in my own rails app. Just what I needed.

Thanks :) happy to be helpful.

@adamwaite

I'd like to thank you for this too. This guy too: http://infotrope.net/2013/05/31/testing-paypal-express-with-activemerchants-bogusgateway-and-how-to-make-it-work/

Shows people have a need for it, no?

@sideshowcoder

Well if more people find it useful I am happy to bring this up to speed with the changes to active_merchant since the pr was closed.

@zmillman

I just ran into this issue as while writing rspec tests for purchasing by PayPal Express

@nleo

+1

@ch000

+1

@katgironpe

+1
Though this was not merged, it helped me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 31, 2012
  1. @sideshowcoder

    Bogus gateway to test PayPal

    sideshowcoder authored
    Since PayPal has some methods untestable via the normal Bogus Gateway
    like setup_authorization, and handles authorization differently, the
    PaypalBogusGateway behaves more like the actual PayPal Gateway to enable
    simple tests without remote interaction
  2. @sideshowcoder
  3. @sideshowcoder
This page is out of date. Refresh to see the latest.
View
52 lib/active_merchant/billing/gateways/paypal_bogus.rb
@@ -0,0 +1,52 @@
+require File.dirname(__FILE__) + '/paypal/paypal_common_api'
+require File.dirname(__FILE__) + '/paypal/paypal_express_response'
+require File.dirname(__FILE__) + '/paypal_express_common'
+
+module ActiveMerchant #:nodoc:
+ module Billing #:nodoc:
+ class PaypalBogusGateway < BogusGateway
+
+ REDIRECT_URL = "https://bogus.paypal.com"
+
+ def setup_authorization money, options = {}
+ requires!(options, :return_url, :cancel_return_url)
+
+ PaypalExpressResponse.new true, SUCCESS_MESSAGE, { :Token => AUTHORIZATION }, :test => true
+ end
+
+ def setup_purchase money, options = {}
+ requires!(options, :return_url, :cancel_return_url)
+
+ PaypalExpressResponse.new true, SUCCESS_MESSAGE, { :Token => AUTHORIZATION }, :test => true
+ end
+
+ def authorize money, options = {}
+ requires!(options, :token, :payer_id)
+
+ case normalize(options[:token])
+ when '1'
+ PaypalExpressResponse.new false, FAILURE_MESSAGE, {:authorized_amount => money}, :test => true
+ else
+ PaypalExpressResponse.new true, SUCCESS_MESSAGE, {:authorized_amount => money}, :test => true, :authorization => AUTHORIZATION
+ end
+ end
+
+ def purchase money, options = {}
+ requires!(options, :token, :payer_id)
+
+ case normalize(options[:token])
+ when '1'
+ PaypalExpressResponse.new false, FAILURE_MESSAGE, {:amount => money}, :test => true
+ else
+ PaypalExpressResponse.new true, SUCCESS_MESSAGE, {:amount => money}, :test => true, :authorization => AUTHORIZATION
+ end
+ end
+
+ def redirect_url_for token
+ REDIRECT_URL
+ end
+
+ end
+ end
+end
+
View
29 test/unit/gateways/paypal_bogus_test.rb
@@ -0,0 +1,29 @@
+require 'test_helper'
+
+class PaypalBogusTest < Test::Unit::TestCase
+ def setup
+ @gateway = PaypalBogusGateway.new(
+ :login => 'bogus',
+ :password => 'bogus'
+ )
+ @return_url = "http://foo.com"
+ @cancel_return_url = "http://foo.com"
+ end
+
+ def test_setup_authorization
+ assert_raise(ArgumentError) { @gateway.setup_authorization 1000 }
+ assert @gateway.setup_authorization(1000, { :cancel_return_url => @cancel_return_url,
+ :return_url => @return_url }).success?
+ end
+
+ def test_redirect_url_for
+ assert @gateway.redirect_url_for '1'
+ end
+
+ def authorize
+ assert_raise(ArgumentError) { @gateway.authorize 1000 }
+ assert !@gateway.authorize(1000, { :token => '1', :payer_id => '42' }).success?
+ assert @gateway.authorize(1000, { :token => '2', :payer_id => '42' }).success?
+ end
+
+end
Something went wrong with that request. Please try again.