Creating Rspec Tests with Omniauth

Rails noob is here. I'm having trouble understanding how / what to check for authentication using Omniauth-Facebook. Pretty much the same setup for railscast relays. I have a test environment configured in the same way as described in previous questions and on the Gem wiki.

A couple of questions. When you create a factory user, how do you get the resulting object to fake authentication?

And what happens when the following code is run?

before do 
  request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
  visit '/auth/facebook'
end

Is the object saved in the database?

I have added some sample specifications below and a factory specification. Again, the installation file spec_helperhas a test mode configuration set to true.

Setup add_mock:

OmniAuth.config.add_mock(:facebook,
                         { :provider => 'facebook',
                           :uid => '1234567',
                           :info => { :name => 'Jonathan', :image => 'http://graph.facebook.com/1234567/picture?type=square'},
                           :credentials => {
                             :expires_at => 1351270850,
                             :token=> 'AAADzk0b791YBAHCNhBI3n6ScmWvuXTY4yIUqXr9WiZCg1R808RYzaHxsHnrbn62IwrIgZCfSBZAVIP6ptF41nm8YtRtZCeBbxbbz1mF8RQZDZD'
                            } })  

User_pages_spec:

describe "user pages" do

  let(:user) { Factory.create(:user) }

  describe "sign_in" do
    before { visit '/' }

    it "should add a user to the User model" do 
      expect { click_link "sign_in" }.to change(User, :count).by(1) 
    end

    it "should route to the appropriate page if email is nil" do
      click_link "sign_in"
      page.should have_selector('h5', text: 'Edit Email')
    end

    it "should redirect to the show page upon" do
      user.email = 'example@stanford.edu'
      user.save!
      click_link 'sign_in'
      page.should have_selector('div', text: user.name)
    end
   end
end

Factory

FactoryGirl.define do
  factory :user do
    provider  "facebook"
    uid       '1234567'
    name      'Jonathan'
  end
end
+5
source share

All Articles