Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Wednesday, February 16, 2011

Mocking Out Omniauth for Cucumber Testing

A few months ago some friends and I got together for Cleveland Startup Weekend and started building our "next big thing" DreamKumo. 

We had a couple challenges. First we knew from the get go that we wanted the site to be 100% facebook integrated. That meant implementing Facebook Connect and the Facebook Graph API. We also are a group of people who throughly believe in ATDD, TDD and agile development principals (and practices) in general. This led us to building up cucumber features for every area of the site. 

We hit a snag though when we tried to cuke our login process. We're asking our users to login through Facebook connect, which means when they click the login button they are redirected to facebook.com, and then sent back to us. Implementing the requirement was pretty simple using the omniauth gem with a little help from the folks at Rails Rumble. Cuking it is another story. Since facebook connect actually redirects the user we can't mock out the request using fakeweb and we don't want to cuke what happens on Facebook.com since 1) we can't control it, 2) it isn't consistent (sometimes you get a screen to accept the permissions that your app is requesting, sometimes you get the facebook login screen and sometimes you get redirected straight through) and 3) we don't want to setup a dummy facebook account for our cukes to use. 

So we needed to prevent the site from redirecting to facebook, and dummy up the site cookie so it looked like we were authenticated. We also needed the failure case to test what would happen if our user was sent to facebook and they decided they didn't want to grant us permission to their account. 

Enter monkey patching. 

Ruby has this wonderfully powerful ability to override the native functionality of a method or class at runtime. It's part of it being a dynamic language and it let's you do some really cool things, like manipulate the functionality of any class, including library classes like those in omniauth, at any time. So we dug through omniauth's code to find the point where it did the redirect to facebook and mocked out a response. We then had it immediatley redirect back to the callback url, or the url in our application that responds when facebook redirects back to us. So the result is we never hit facebook and omniauth sends along what we need to determine if the authentication was successful or not. 

Ok enough talk - here's the code. 

These steps are in a generic_context_steps.rb file in our step_definitions

 

Given /^I have valid facebook credentials$/ do
  module OmniAuth
    module Strategies
      class OAuth2
        def request_phase
          redirect callback_url
        end
        def callback_phase
          @env['rack.auth'] = {"provider"=>"facebook", "uid"=>"123", "user_info"=>{"first_name"=>"John", "last_name"=>"Doe", "nickname"=>"JohnDoe", "email"=>"john.doe@test.com"}, "credentials"=>{"token"=>"abc123"}}
          call_app!
        end
      end
    end
  end
end
Given /^I have invalid facebook credentials$/ do
  module OmniAuth
    module Strategies
      class OAuth2
        def request_phase
          redirect callback_url
        end
        def callback_phase
          fail!(:invalid_credentials, nil)
        end
      end
    end
  end
end
We simply call those in any feature where we want to authenticate the user and it continues on with the test as intended. 
There is a slight ATDD tradeoff. You aren't actually testing everything exactly as your user would see it since you're skipping the Facebook part. But In this case we borrow a bit from our TDD book and justify this by saying we only test our own code, and we didn't write facebook.com.
Hope this helps. 

 

Monday, August 16, 2010

New Gig

Today officially marks my first day at SB Stone. I thought a post was warranted to talk about what we are trying to do and how open source, .net, mobile, all the fancy buzzwords are part of it.

I am the first full time dev in what the company is calling our "interactive" division. SB as a whole really has 4 main markets, staff augmentation, IT services, support and this new interactive group.

So what does interactive mean? Well... really all it means is we want to build a "studio" style dev wing where we do both project work for third party clients, as well as build our own products to market and resell. We're hoping the project works supports the dev time needed for our own things until those become profitable. Also as with anything some will hit and some will miss.

So why did I come over here? Mainly because the new group wants to build a new brand and image for the company, a large part of which will be participating and contributing to the dev and tech community. That means participating at and speaking at conferences, sharing knowledge through blog posts / twitter / user groups, all of it.

From a tech standpoint our group is looking at a mix of .NET and Open Source tools, specifically Ruby on Rails (although we have PHP resources as well.) We're also training up on mobile including iOS and eventually Android so a whole lot of different tools. My role from a marketing perspective is mostly going to be focused on the Rails side though. So hopefully in the near future I'll be going to more ruby conferences and events to talk to people about what we're doing and how Rails is playing in the marketplace.

The thing i'm most excited about though is building out a proper agile dev team. Earlier today we committed to following Kanban via Agile Zen for my first project, and hopefully many more going forward. We're adhering to test driven development, continuous integration and emergent design as well. The only "hot" agile practice we aren't going quite yet is pairing, and only for lack of collocated people (and remote pairing, in my opinion, sucks.)

So that's what I'm up to. If your an independent or even a full timer looking to help us out let me know. We're always looking for contractors to keep available and we'll hopefully be building up more of an in house team in the coming months. Even more importantly if you know of any small businesses looking for rails / php / .net help LET ME KNOW :-)

Wednesday, August 11, 2010

So You Want To Be A .NET Dev

Before I open the flamegates please note this post is a tongue in cheek reply to Kevin Gisi's excellent "So You Want To Be A Ruby Dev". I personally work in both platforms and they each have their pluses and minuses. Kevin's post is both scathingly hilarious and sadly true I hope this rings the same way.

So You Want To Be A .NET Dev

Fantastic, welcome aboard! The .NET community is a somewhat fractured but enormous place to be. Some of us follow test-driven development (and a library or two might have tests if your lucky), configuration over convention and the Alt.NET guys have heard of agile methodologies. We have documentation tools like... comments.... and xml comments... which means that if the devs who wrote the library your working on have read Code Complete it might not be a bunch of spaghetti code. So leave your hippy language and development stack, and come join the corporate monolith - maintaining job security by using only the best tools from Microsoft (tm).

So, let's get you started! You'll want to grab a copy of the .NET Framework. Which Framework? Well seeing as how your probably going to be building new apps and maintaining apps from 2001 you should probably just install all of them. 1.1, 2.0, 3.5 and 4.0 are all available to download on microsoft.com There's also 1.0 but we don't speak of that, and mono but no one uses that. What's the difference? Well mostly features, we have a LOT of people to support after all so each new version shoehorns more and more stuff in until we can be everything for everyone. We wouldn't want to have to rely on the community to add new things... who would support it? Don't worry you can install all of them at the same time, they play nice, but I hope you have a nice big hard drive on your PC.

Oh, you're running OSX? Hmm... well, you could try install Windows? No.. well how about VMWare or Parallels. You know those virtualization products that make your computer work like Windows. IDE? Well that one's easy you're going to need Visual Studio, but which one? Well you could install Visual Studio 2010. With that you'll be able to maintain all those .NET 2.0 - 4.0 applications, but you're also going to need Visual Studio 2003 for those old pesky 1.1 "legacy" apps. That said you better hope your whole team is on 2010 otherwise your project files will get hosed and no one will be able to open your solution. What's a project or solution? Well let's talk about that later OK? Oh and you're going to want to buy Resharper, seriously all the best .NET people use it. It adds all the stuff to Visual Studio 2010 that Microsoft will "borrow" and add for 2012. None of us really use Macs...

Ready to get started? Alrighty, well which framework do you want to use? The two big web ones are Webforms and MVC. Webforms is typically used in huge applications for massive corporations that don't know any better. It has a great designer and you can build your entire high maintenance app without writing a single line of code, isn't that awesome? MVC is our newest pet project that all the big wigs in the "community" swear by. Its a distinctly Microsoft invention, you see they broke apart data access, application logic and view rendering and introduced unit testing. It's revolutionary, we didn't borrow the concept or project layout from anyone especially not those ruby on rails blokes. For windows client dev you can use WPF or Winforms but you should really use WPF. Sure it still feels like beta software after two major revisions, but its new and shiny and we promise not to depreciate it in a week in favor of something new. Speaking of something new there's also silverlight which is kinda like webforms and kinda like wpf and kinda like flash.

OK next up is our package management app. Hahahaha just kidding we don't have one. First off why would you ever want to use a tool not made by Microsoft? If you MUST there's lots of excellent control libraries our partners like DevExpress and Telerik have built, their sales staff would be happy to help you configure them. Sometimes different versions even play nice together. We have a really nice Word doc for you to read that explains all the dependencies you need to install, by next week you should be up and running.

Oh, by the way a lot of those third party libraries are now designed for .NET 4, but don't worry you can still use the old versions, if you can find them.

As far as database persistence goes well you'll be using SQL Server for sure. Best get that checkbook back out. Oh you mean code wise, well there's Datasets, Linq to SQL (but we already cancelled that), EF1 (but its universally panned) and EF4 CTP (but even our own MVPs admit that's 3 years behind what other communities have.) You could also use NHibernate but that's not built by Microsoft so good luck getting it by your CIO or MVP Architect.

Hmmm authentication? Well you'll probably want to use the built in authentication provider, and its actually pretty good it even supports security roles. Unfortunately if you're in MVC it doesn't really follow database conventions but it'll work.

Last thing, we'll have to set you up in our TFS system. TFS is this great version control tool Microsoft gave us that's light years ahead of their old tool SourceSafe, it only screws up a merge or loses information occasionally. You need to work offline? Well.. you CAN but you'll be prompted a few times to reconnect and sometimes your code won't merge back in to the server right. It's 2010 when would you not have an internet connection? Also just to limit those pesky merges we've turned on locking checkouts, we wouldn't want people working on the same file, that would be chaos.

Anyway welcome to the team, I'll swing by in a week and see if you've gotten anything done.

Tuesday, July 20, 2010

GiveCamp 2010

This past weekend I had the opportunity to be part of something the likes of which I had not yet seen in the Cleveland area. From Friday until Sunday the folks at Lean Dog Software in collaboration with a handful of organizers and sponsors hosted Cleveland's first GiveCamp.

GiveCamp is a national program that pairs up talented software developers, designers and other technical specialists with local non-profit organizations to complete technical projects that the non-profits would otherwise be unable to afford. This past weekend was the first time Cleveland has hosted one of these events and I think we did our geek brethren in other, much larger, cities proud. When it was all said and done over 20 charities were helped by over 100 volunteer making GiveCamp Cleveland the largest first GiveCamp ever. it wasn't quite the largest of all time, but maybe we'll take that record next year.

For those curious the weekend runs like this.

Friday night we were paired up with our charities and sat down with a sponsor from the charity to talk about their needs and what our goals for the weekend were. In most cases the plan for the weekend was mapped out in advance by a business analyst that was pre-assigned to each team. However, in the case of my charity, The League of Women Voters of the Cleveland Area, we came it knowing virtually nothing. You see the organizers had anticipated having enough people to help roughly 15 charities, but come the week of the event they had enough to add 7 more. The LWV Cleveland was one of the late additions so we had to scope and plan the project on day one instead of in advance.

Our sponsor Sherece was amazing. She understood our limited time frame and was willing to compromise when needed but also did a great job of explaining the purpose of her organization so we could build out the best site for her that the time and resources allowed.

From Friday night through all day Saturday the teams went to work, in most cases this meant building out and customizing a Wordpress installation for each charity. Something like 15 of the teams used Wordpress which is really a testament to the product and how flexible it is.

Throughout the weekend stand-ups were held to make sure the teams were on track. Also breakfast lunch and dinner were provided both Saturday and Sunday.

Sunday evening we trained out charities, presented the final projects and handed over the keys so to speak. The charities all seemed quite grateful for the work that was done, but in reality it was the volunteers who seemed most upbeat. I think I speak for all of us when I say we had an absolute blast using our abilities and skills in a way that we very rarely get to. I told some friends in advance of this weekend that I don't know how to build a house, but I know how to build a website, so getting a chance to do that for a great organization like the LWV was not only fun but an honor.

Just in case anyone is curious the site we built can be found at http://www.lwvdev.com. We had some issues connecting with their hosting provider so itll be switched over to their main site, lwvcef.org in the next week or so.

Some thanks to hand out. First off to all of the organizers especially Mark Schumann who acted as MC and leader of the organizers, Andrew Craze who served as the developer lead and Jon Stahl who's company Lean Dog hosted the event on their amazing workspace (the old Hornblowers boat on lake erie.) Also thanks to the IEEE for acting as a non-profit sponsor of the event and Burke Lakefront Airport for offering up the entire terminal to be used by the campers when the number of volunteers overflowed the boat.

On a personal note I moved to Cleveland Ohio 5 years ago because of a job offer after college. When you think of Cleveland you don't really expect to find a vibrant and exciting community of software developers and tech enthusiasts. This isn't Silicon Valley or Boston or Austin. Let me just say, if you believe that like I did you couldn't be more wrong. Over 100 people showed up this weekend from dozens of companies all over Northeast Ohio. There was a range of expertise that ranged from SQL to .NET and Ruby, Coldfusion, PHP you name it and it was represented. This is a great city to live in, with great people and one hell of a technical community, and that friends is why I'm #happyincle.

Anyway Thanks again to all those involved and I can't wait to do it all again next year.

Friday, June 18, 2010

RCov w/ RSpec-2

So I've spend the last couple weeks updating an old Rails application to Rails 3 and RSpec 2, both of which are currently still in beta.

Early on I noticed that the new rspec doesn't quite support rake spec:rcov yet. The new core framework has the rcov flag but there's no default rake task. RSpec-rails also removed the spec.rake task from your rails project and centralized it into your gem install, which confused me to no end. So in case anyone else wants to run rcov on your code I thought I might post what I did to make rake spec:rcov work again.

First a couple prereqs. I'm using an RVM install of all of this with ruby 1.8.7 (only because 1.9.1 gave me problems w/ rails 3) so in the steps below replace the paths I give with the path to your gem installation folder and you should be good.

1) Open ~/.rvm/rubies//lib/ruby/gems//gems/rspec-rails-2.XXXXX/lib/rspec/rails/tasks/rspec.task in your favorite editor.

2) Add this rake task

desc "Run all specs with rcov"
RSpec::Core::RakeTask.new(:rcov => spec_prereq) do |t|
t.rcov = true
end

That's it. Pretty simple.

A caveat. Right now this runs ALL of your specs in both your rvm ruby folder and your project. I haven't a clue why, hopefully someone smarter than I will comment. That means rcov take a while and when its done its cludgy. If someone does help me out I'll post an update, in the meantime this works.