Older blog entries for Skud (starting at number 214)

Rails controller specs: matching on redirect URLs

Yet another thing I figured out painstakingly, by finding random bits of documentation in places other than the documentation, because apparently that’s how Rails rolls.

Here’s my situation. I have a controller spec like this:

  describe "GET checkout" do
    it "redirects to Paypal" do
      member = FactoryGirl.create(:member)
      sign_in member
      order = Order.create!(:member_id => member.id)
      get :checkout, {:id => order.to_param}
      response.should redirect_to "http://paypal.com/..."

Basically I want to check that when someone checks out, they get sent to Paypal. Easy peasy, right? The thing is, I don’t know the exact URL they’ll get sent to — it’ll be big and complicated and have lots of parameters on it, and it’ll be different in test anyway, and umm… ok, well, basically I just want to test whether I get redirected, and whether it’s to somewhere paypal-like. Ideally I want to do something like:

# doesn't work
response.should redirect_to /paypal\.com/

… and have it do a match against the URL it’s redirecting to. But that doesn’t work.

I googled every-fucking-where trying to find out what else I could do with the response object, but it doesn’t appear to be documented anywhere official. In the end I found this blog post on Response object methods by Jeff Roush. Thanks, Jeff!

In conclusion, here’s how I’m testing for a redirect that matches against a regexp:

response.status.should eq 302
response.redirect_url.should match /paypal\.com/

(There’s also a redirect_url_match? but it seems to be deprecated.)

Hope that helps someone else, and that they find the answer more quickly than I did.

Syndicated 2013-05-31 02:11:54 from Infotropism

Testing PayPal Express with ActiveMerchant’s BogusGateway (and how to make it work)

If you’re writing a Rails app with PayPal Express checkout, and having trouble testing because ActiveMerchant’s BogusGateway doesn’t support PayPal-specific methods like setup_purchase, here’s a fix. I spent a while figuring this out the other day so I thought I’d note it here in case someone else is googling.

The problem

Presumably you are writing a Rails app, and are following the instructions in the RailsCast #146 Paypal Express Checkout, or something along those lines.

In your config/environments/test.rb you have:

config.after_initialize do
  ActiveMerchant::Billing::Base.mode = :test
  ::STANDARD_GATEWAY = ActiveMerchant::Billing::BogusGateway.new
  ::EXPRESS_GATEWAY = ActiveMerchant::Billing::BogusGateway.new

The BogusGateway just mocks the behaviour of a real payment gateway, so that you can run your tests without connecting to a real one — even in test mode. This means you can run your tests offline, for example.

So far so good. Then in your controller you have something like:

response = EXPRESS_GATEWAY.setup_purchase(order.total,
    :ip                => request.remote_ip,
    :return_url        => new_order_url,
    :cancel_return_url => products_url

When you try to test the checkout method in your controller, however, you see an error message like:

undefined method `setup_purchase' for #<:billing::bogusgateway:0x000001296aca50/>

The problem is that ActiveMerchant::Billing::PayPalExpressGateway provides a number of methods that BogusGateway doesn’t duplicate. Why not? Well, apparently PayPal Express is a bit of an odd duck in the ActiveMerchant world, and mumble mumble no real reason.

The solution

There’s a pull request by sideshowcoder submitted to ActiveMerchant which creates a PayPalBogusGateway that does all the necessary PayPal-Express-specific things, but it got turned down as “not a common enough use case”. Personally I think that ActiveMerchant shouldn’t have shipped the Paypal Express functionality in an untestable form, but whatevs. You can use sideshowcoder’s fix in your own app, as follows.

First of all, in order to monkeypatch in this way, you’re going to have to take a copy of the gem and put it in your vendor/ directory. I did so following the instructions in this blog post, specifically:

  • Download the activemerchant gem from rubygems.org
  • gem unpack ~/activemerchant-1.33.0.gem --target vendor/gems
  • Repeat for the active_utils gem, which is also required.

Then in my Gemfile, I have:

gem 'activemerchant', '1.33.0',
  :path => 'vendor/gems/activemerchant-1.33.0',
  :require => 'active_merchant'
gem 'active_utils', '1.0.5',
  :path => 'vendor/gems/active_utils-1.0.5'

Finally, after running “bundle install” to make sure that all worked, I dropped paypal_bogus.rb into the appropriate place in my vendors/ directory.

Congratulations, you can now test your Rails app’s interaction with PayPal Express. Hopefully the googles will take note, and the next person searching for this won’t have to pound their head against it quite as hard as I did.

(Thanks also to Ben Bleything for some help on Twitter with the vendored gem part of this.)

Syndicated 2013-05-31 01:57:09 from Infotropism

Fed up with meetups

I went to a tech meetup the other night, an introductory session about a technology I’m interested in using. It was advertised for 6pm, so I arrived at 5:59, and found a note on the door saying it was starting at 6:30. I wasted half an hour wandering around and went back, to find a bunch of people standing around drinking beer. I didn’t know any of them, I was cranky about having shovelled in an early dinner and hauled ass into the city to be on time when it wasn’t necessary, and I don’t drink beer, so instead of socialising I decided to just grab a seat and mess around on my phone. The seats were epically uncomfortable. I looked around and saw a couple of nicer seats, so I moved to one of those. Better. I checked my email and read some of my RSS feeds.

The event organiser — friendly guy, and good on him for being sociable, I’m not complaining about that — came over and asked where I was from. I was momentarily discombobulated. Melbourne? Thornbury? I’ve just come all the way from Thornbury to be here. What did he want to know? I looked confused, and he rephrased: where did I work? Oh. I told him about Growstuff, and why I was interested in the technology the meetup was about. We chatted for a minute and he moved on.

The first talk was interesting, I guess, though I found it hard to enjoy because I’d been kicked off my comfy seat by someone who “owned” it — he worked at the company hosting the meetup and had brought his desk chair to sit in — and I was in one of the back-breaking plastic monstrosities they’d set out for the group. The talk seemed to mostly be a tour of one of the basic examples/tutorials for the technology in question, but with added commentary/advice/thoughts from someone who really knew his way around it. That was good. But was it good enough to make up for the pain and crankiness and general unpleasantness? I had a headache coming on. I took out my knitting, which at least is soothing and lets me listen without otherwise fidgeting. It helped a bit.

After the first talk there was a break, with pizza. They hadn’t advertised the pizza or mentioned it in the meetup announcement. Had I known, I wouldn’t have rushed to eat something before 6pm, and now I wasn’t hungry. There was still nothing to drink except for beer. Everyone was standing around chatting and they were all cooler and trendier and smarter than me and I wanted to cry.

I went home early.

Syndicated 2013-05-31 00:18:53 from Infotropism

An interesting documentary

A couple of weeks ago I was reading an article about Israeli politics and found myself turning to Wikipedia to fill in some of the gaps in my knowledge. Approximately 200 Firefox tabs later I found myself watching orthodox Jewish women’s headscarf howtos on Youtube, learning about Third Order Franciscan monks and nuns, and — eventually — watching documentaries about Anabaptist sects in North America.

This is one of the most interesting I found: