Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
unikey_oauth / spec / controllers / oauth_controller_spec.rb
Size: Mime:
require 'rails_helper'

RSpec.describe User::OauthController, type: :controller do

  # subject { User::OauthController }

  let(:user) { MockUser.new }
  let(:user_params) { { email: 'cmclean@unikey.com', password: 'password' } }
  let(:third_party_params) { MockOauthParams.new.body }

  before do
    session[:user] = nil
    session[:expiry_time] = Time.zone.now

    allow(Api).to receive(:request_oauth_authorization_code).and_return(MockAuthorizationCode.new)
    allow(Api).to receive(:application_permission).and_return(MockApplicationPermission.new)
    allow(Api).to receive(:delete_authorized_locks).and_return(MockAuthorizedLocks.new)
    allow(Api).to receive(:get_user_locks).and_return(MockLocks.new)
  end


  describe 'index' do
    it 'should redirect to the grant step if you are already logged in' do
      session[:user] = user
      get :index
      expect(subject).to redirect_to user_oauth_grant_path
    end
  end


  describe 'authorizing' do
    describe 'should be here' do
      it 'should progress to the next step on successful authorization' do
        post :authorizing, user_params
        expect(subject).to redirect_to user_oauth_grant_path
      end
    end

    describe 'invalid' do
      Errors = Struct.new('User', :errors)
      errors = Errors.new base: ['this is an error']

      before do
        allow(User)
          .to receive(:authorize)
          .with('cmclean@unikey.com', 'password', request.remote_ip)
          .and_return errors

        post :authorizing, user_params
      end

      it 'should display report errors from failed calls' do
        expect(flash[:error].size).to eq 1
      end

      it 'should navigate to the previous step' do
        expect(subject).to redirect_to user_oauth_index_path
      end
    end
  end


  describe 'grant' do
    describe 'shouldnt be here' do
      it 'should redirct to the index page if you are not authenticated' do
        get :grant, third_party_params
        expect(subject).to redirect_to user_oauth_index_path
      end

      it 'should redirect to the index page if you not come from an application' do
        session[:user] = user
        get :grant, {}
        expect(subject).to redirect_to user_oauth_index_path
      end

      it 'should set a flash error if you didnt come from an application' do
        session[:user] = user
        post :grant, {}
        expect(flash[:error].present?).to be true
      end
    end

    describe 'invalid query params' do
      before do
        allow(Api).to receive(:get_application).and_return( MockBadApplicationDataResponse.new )
      end

      it 'should raise a RoutingError exception if the associated response contains an error' do
        session[:user] = user
        
        expect{ get :grant, third_party_params }.to raise_error
      end
    end
  end

  describe 'granting' do
    describe 'shouldnt be here' do
      it 'should redirct to the index page if you are not authenticated' do
        post :granting, third_party_params
        expect(subject).to redirect_to user_oauth_index_path
      end


      it 'should redirect to the index page if you not come from an application' do
        session[:user] = user
        get :granting, {}
        expect(subject).to redirect_to user_oauth_index_path
      end


      it 'should set a flash error if you didnt come from an application' do
        session[:user] = user
        post :granting, {}
        expect(flash[:error].present?).to be true
      end
    end

    describe 'should be here' do
      it 'should not have errors set if locks are selected' do
        session[:user] = user
        post :granting, third_party_params
        expect(flash[:error].present?).to be false
      end

    end
  end
end