Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omniauth Rspec testing problem

I followed the rails cast of omniauth to create authentication for twitter ( http://railscasts.com/episodes/235-omniauth-part-1?view=comments ). It works fine in development but I can't get rspec to detect that I have created the authentication. Here is my snippet for create function in my Authentication controller:

def create
  begin

    auth_hash = request.env["omniauth.auth"]
    @auth = current_user.authentications.build( :provider     => auth_hash['provider'], 
                                              :uid          => auth_hash['uid'],
                                              :name         => auth_hash['user_info']['name'],
                                              :nickname     => auth_hash['user_info']['nickname'],
                                              :image        => auth_hash['user_info']['image'] 
                                              )

    if @auth.provider.downcase == "twitter"
      @auth.auth_token      = auth_hash['credentials']['token']
      @auth.secret_token    = auth_hash['credentials']['secret']
      @auth.site            = auth_hash['user_info']['urls']['Twitter']
    elsif @auth.provider == "Facebook"

    end

  rescue
    redirect_to current_user, :flash => { :error => "Missing oauth data!! Check with administrator!"}
  else
    if @auth.save
      msg = "Authentication success"
    else
      msg = "Already have authentication"
    end
    redirect_to current_user, :notice => msg
  end
end

included in my routes:

match '/auth/:provider/callback' => 'authentications#create'

I have the following setup in my rspec_helper:

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:twitter, {  :provider    => "twitter", 
                                  :uid         => "1234", 
                                  :user_info   => {   :name       => "Bob hope",
                                                      :nickname   => "bobby",
                                                      :urls       => {:Twitter => "www.twitter.com/bobster"}},
                                  :credentials => {   :auth_token => "lk2j3lkjasldkjflk3ljsdf"} })

Here is my rspec code that is not working:

  describe "Post 'create'" do
    before(:each) do
      @user = Factory(:user)
      sign_in @user
    end

    describe "success" do
      before(:each) do
        request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
      end


      it "should create authentication" do
        lambda do
          post :create, :provider => "twitter"
          response.should redirect_to(@user)
        end.should change(Authentication, :count).by(1)
      end
    end
  end

error i get is:

1) AuthenticationsController Post 'create' success should create authentication Failure/Error: lambda do count should have been changed by 1, but was changed by 0 # ./spec/controllers/authentications_controller_spec.rb:57

I've checked everything and cannot figure what I am doing wrong. Can anyone help?

like image 805
Whereisccguys Avatar asked Sep 01 '11 01:09

Whereisccguys


1 Answers

i finally figured what was wrong. in my mock :auth_token is suppose to be :token. That was causing the failed validation.

like image 60
Whereisccguys Avatar answered Nov 10 '22 13:11

Whereisccguys