Testing Omniauth in Rails

We're starting to develop a new website using Rails 3, RSpec 2, and OmniAuth 2. We wanted to follow TDD with RSpec to write authentication, but we really don't know where to start. We do not want to test the gem, since it has already been tested, but we want to test the application flow, and it was correctly routed according to the result of the callback.

The best so far we have classified the problem in two stages: 1- Before the callback: fake services such as facebook and twitter to return the calls 2- After the callback: getting the results and creating the user and the service associated with it

Please guide us and throw us some light :)

+4
source share
2 answers

Have you checked the wiki ?

** Change **

Maybe something like (untested):

before do OmniAuth.config.mock_auth[:twitter] = { 'uid' => '123545' } end it "sets a session variable to the OmniAuth auth hash" do controller.session[:auth_hash]['uid'].should == '123545' end 
+10
source

I am having trouble apologizing for RSpec for the OmniauthCallbacksController, do some research on this and it works for me. I add my codes here if someone finds it necessary. Tests for a bon voyage :)

 require 'spec_helper' describe OmniauthCallbacksController do describe "#linkedin" do let(:current_user) { Fabricate(:user) } before(:each) do OmniAuth.config.test_mode = true OmniAuth.config.mock_auth[:linkedin] = OmniAuth::AuthHash.new({provider: :linkedin, uid: '12345', credentials: {token: 'linkedin-token', secret: 'linkedin-secret'}}) request.env["devise.mapping"] = Devise.mappings[:user] @controller.stub!(:env).and_return({"omniauth.auth" => OmniAuth.config.mock_auth[:linkedin]}) User.stub(:from_auth).and_return(current_user) end describe "#linkedin" do context "with a new linkedin user" do before { get :linkedin } it "should authenticate user" do warden.authenticated?(:user).should == true end it "should set current_user" do subject.current_user.should_not be_nil end it "should redirect to root_path" do response.should redirect_to(root_path) end end end end end 
+1
source

All Articles