<commit_before>require 'spec_helper'

describe Spree::CheckoutController do
  let(:order) { Spree::Order.new }
  let(:user) { mock_model Spree::User }
  let(:token) { '<PASSWORD>' }

  before do
    order.stub :checkout_allowed? => true, :user => user, :new_record? => false
    controller.stub :current_order => order
    controller.stub :current_user => nil
  end

  context '#edit' do
    context 'when registration step enabled' do
      before do
        controller.stub :check_authorization
        Spree::Auth::Config.set(:registration_step => true)
      end

      context 'when authenticated as registered user' do
        before { controller.stub :current_user => user }

        it 'should proceed to the first checkout step' do
          get :edit, { :state => 'confirm' }
          response.should render_template :edit
        end
      end

      context 'when authenticated as guest' do
        before { controller.stub :auth_user => user }

        it 'should redirect to registration step' do
          get :edit, { :state => 'confirm' }
          response.should redirect_to spree.checkout_registration_path
        end
      end
    end

    context 'when registration step disabled' do
      before do
        Spree::Auth::Config.set(:registration_step => false)
        controller.stub :check_authorization
      end

      context 'when authenticated as registered' do
        before { controller.stub :current_user => user }

        it 'should proceed to the first checkout step' do
          get :edit, { :state => 'confirm' }
          response.should render_template :edit
        end
      end

      context 'when authenticated as guest' do
        before { controller.stub :auth_user => user }

        it 'should proceed to the first checkout step' do
          get :edit, { :state => 'confirm' }
          response.should render_template :edit
        end
      end

    end

    it 'should check if the user is authorized for :edit' do
      controller.should_receive(:authorize!).with(:edit, order, token)
      get :edit, { :state => 'confirm' }, { :access_token => token }
    end
  end

  context '#update' do
    it 'should check if the user is authorized for :edit' do
      controller.should_receive(:authorize!).with(:edit, order, token)
      post :update, { :state => 'confirm' }, { :access_token => token }
    end

    context 'when save successful' do
      before do
        controller.stub :check_authorization
        order.stub(:update_attribute).and_return true
        order.should_receive(:update_attributes).and_return true
      end

      context 'when in the confirm state' do
        before do
          order.stub :next => true
          order.stub :state => 'complete'
          order.stub :number => 'R123'
        end

        context 'with a guest user' do
          before do
            order.stub :token => 'ABC'
            user.stub :has_role? => true
            controller.stub :current_user => nil
          end

          it 'should redirect to the tokenized order view' do
            post :update, { :state => 'confirm' }
            response.should redirect_to token_order_path('R123', 'ABC')
          end

          it 'should populate the flash message' do
            post :update, { :state => 'confirm' }
            flash.notice.should == I18n.t(:order_processed_successfully)
          end
        end

        context 'with a registered user' do
          before do
            user.stub :has_role? => true
            controller.stub :current_user => mock_model(Spree::User, :has_role? => true)
          end

          it 'should redirect to the standard order view' do
            post :update, { :state => 'confirm' }
            response.should redirect_to Spree::Core::Engine.routes.url_helpers.order_path('R123')
          end
        end
      end
    end
  end

  context '#registration' do<commit_msg>token_order_path is inside the spree engine<commit_after>require 'spec_helper'

describe Spree::CheckoutController do
  let(:order) { Spree::Order.new }
  let(:user) { mock_model Spree::User }
  let(:token) { 'some_token' }

  before do
    order.stub :checkout_allowed? => true, :user => user, :new_record? => false
    controller.stub :current_order => order
    controller.stub :current_user => nil
  end

  context '#edit' do
    context 'when registration step enabled' do
      before do
        controller.stub :check_authorization
        Spree::Auth::Config.set(:registration_step => true)
      end

      context 'when authenticated as registered user' do
        before { controller.stub :current_user => user }

        it 'should proceed to the first checkout step' do
          get :edit, { :state => 'confirm' }
          response.should render_template :edit
        end
      end

      context 'when authenticated as guest' do
        before { controller.stub :auth_user => user }

        it 'should redirect to registration step' do
          get :edit, { :state => 'confirm' }
          response.should redirect_to spree.checkout_registration_path
        end
      end
    end

    context 'when registration step disabled' do
      before do
        Spree::Auth::Config.set(:registration_step => false)
        controller.stub :check_authorization
      end

      context 'when authenticated as registered' do
        before { controller.stub :current_user => user }

        it 'should proceed to the first checkout step' do
          get :edit, { :state => 'confirm' }
          response.should render_template :edit
        end
      end

      context 'when authenticated as guest' do
        before { controller.stub :auth_user => user }

        it 'should proceed to the first checkout step' do
          get :edit, { :state => 'confirm' }
          response.should render_template :edit
        end
      end

    end

    it 'should check if the user is authorized for :edit' do
      controller.should_receive(:authorize!).with(:edit, order, token)
      get :edit, { :state => 'confirm' }, { :access_token => token }
    end
  end

  context '#update' do
    it 'should check if the user is authorized for :edit' do
      controller.should_receive(:authorize!).with(:edit, order, token)
      post :update, { :state => 'confirm' }, { :access_token => token }
    end

    context 'when save successful' do
      before do
        controller.stub :check_authorization
        order.stub(:update_attribute).and_return true
        order.should_receive(:update_attributes).and_return true
      end

      context 'when in the confirm state' do
        before do
          order.stub :next => true
          order.stub :state => 'complete'
          order.stub :number => 'R123'
        end

        context 'with a guest user' do
          before do
            order.stub :token => 'ABC'
            user.stub :has_role? => true
            controller.stub :current_user => nil
          end

          it 'should redirect to the tokenized order view' do
            post :update, { :state => 'confirm' }
            response.should redirect_to spree.token_order_path('R123', 'ABC')
          end

          it 'should populate the flash message' do
            post :update, { :state => 'confirm' }
            flash.notice.should == I18n.t(:order_processed_successfully)
          end
        end

        context 'with a registered user' do
          before do
            user.stub :has_role? => true
            controller.stub :current_user => mock_model(Spree::User, :has_role? => true)
          end

          it 'should redirect to the standard order view' do
            post :update, { :state => 'confirm' }
            response.should redirect_to Spree::Core::Engine.routes.url_helpers.order_path('R123')
          end
        end
      end
    end
  end

  context '#registration' do
{'fraction_non_alphanumeric': 0.092360756259581, 'fraction_numerical': 0.002810424118548799, 'mean_word_length': 2.2337876910367616, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3742347', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2100, 'n_words': 686}