require 'spec_helper'

describe SessionsController, type: :api do

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

  def login(user)
    # login as fan
    post '/api/auth_session.json', { :email => user.email, :password => user.password }.to_json, "CONTENT_TYPE" => 'application/json'
    last_response.status.should == 200
    JSON.parse(last_response.body).should == { "success" => true }
  end

  describe "create_oauth" do

    describe "twitter" do


      before(:each) do

        OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
            'uid' => '100',
            'provider' => 'twitter',
            'credentials' => {
                'token' => 'twittertoken',
                'secret' => 'twittersecret'
            }
        })

        Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]

      end

      after(:each) do
        Rails.application.env_config.delete("omniauth.auth")
      end


      it "should update user_authorization for existing user" do
        login(user)

        get '/auth/twitter' #, {'omniauth.auth' => OmniAuth.config.mock_auth[:twitter]}
        redirect = last_response.headers['Location']
        get redirect
        puts last_response.body.inspect
        user.reload
        auth = user.user_authorization('twitter')
        auth.should_not be_nil
        auth.uid.should == '100'
        auth.token.should == 'twittertoken'
        auth.secret.should == 'twittersecret'

        # also verify that a second visit does *not* create another new user
        get '/auth/twitter'
        redirect = last_response.headers['Location']
        get redirect

        user.reload
        auth = user.user_authorization('twitter')
        auth.uid.should == '100'
        auth.token.should == 'twittertoken'
        auth.secret.should == 'twittersecret'
      end
    end

    describe "facebook" do
      before(:each) do
        OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
            'uid' => '100',
            'provider' => 'facebook',
            'info' => {
              'first_name' => 'FirstName',
              'last_name' => 'LastName',
              'email' => 'test_oauth@example.com',
              'location' => 'mylocation'
            },
            'credentials' => {
              'token' => 'facebooktoken',
              'expires_at' => 1000000000
            }
          })
      end

      it "should create a user when oauth comes in with a non-currently existing user" do
        pending "needs this fixed: https://jamkazam.atlassian.net/browse/VRFS-271"
        request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
        lambda do
          get '/auth/facebook'
        end.should change(User, :count).by(1)
        user = User.find_by_email('test_oauth@example.com')
        user.should_not be_nil
        user.first_name.should == "FirstName"
        response.should be_success

        # also verify that a second visit does *not* create another new user
        lambda do
          get '/auth/facebook'
        end.should change(User, :count).by(0)
      end
    end

    describe "google_login" do
      before(:each) do
        OmniAuth.config.mock_auth[:google_login] = OmniAuth::AuthHash.new({
            'uid' => '100',
            'provider' => 'google_login',
            'credentials' => {
                'token' => 'google_logintoken',
                'secret' => 'google_loginsecret',
                'expires_at' => 1000000000
            }
        })
      end

      it "should update user_authorization for existing user" do
        login(user)
        get '/auth/google_login'
        redirect = last_response.headers['Location']
        get redirect
        user.reload
        auth = user.user_authorization('google_login')
        auth.uid.should == '100'
        auth.token.should == 'google_logintoken'
        auth.secret.should == 'google_loginsecret'

        # also verify that a second visit does *not* create another new user
        get '/auth/google_login'
        redirect = last_response.headers['Location']
        get redirect

        user.reload
        auth = user.user_authorization('google_login')
        auth.uid.should == '100'
        auth.token.should == 'google_logintoken'
        auth.secret.should == 'google_loginsecret'
      end
    end

    

  end
  
  
end
