<commit_before>require 'spec_helper'

describe CheckoutController do
  let(:order) { mock_model(Order, :checkout_allowed? => true, :completed? => false, :update_attributes => true, :payment? => false, :insufficient_stock_lines => []).as_null_object }
  before { controller.stub :current_order => order, :current_user => nil }

  it "should understand checkout routes" do
    assert_routing("/checkout/delivery", {:controller => "checkout", :action => "edit", :state => "delivery"})
    assert_routing("/checkout/update/delivery", {:controller => "checkout", :action => "update", :state => "delivery"})
  end

  context "#edit" do

    it "should redirect to the cart path unless checkout_allowed?" do
      order.stub :checkout_allowed? => false
      get :edit, { :state => "delivery" }
      response.should redirect_to cart_path
    end

    it "should redirect to the cart path if current_order is nil" do
      controller.stub!(:current_order).and_return(nil)
      get :edit, { :state => "delivery" }
      response.should redirect_to cart_path
    end

    it "should change to the requested state" do
      order.should_receive(:state=).with("payment").and_return true
      get :edit, { :state => "payment" }
    end

    it "should redirect to cart if order is completed" do
      order.stub(:completed? => true)
      get :edit, {:state => "address"}
      response.should redirect_to(cart_path)
    end

  end

  context "#update" do

    context "save successful" do
      before do
        order.stub(:update_attribute).and_return true
        order.should_receive(:update_attributes).and_return true
      end

      it "should assign order" do
        post :update, {:state => "confirm"}
        assigns[:order].should_not be_nil
      end

      it "should change to requested state" do
        order.should_receive(:state=).with('confirm')
        post :update, {:state => "confirm"}
      end

      context "with next state" do
        before { order.stub :next => true }

        it "should advance the state" do
          order.should_receive(:next).and_return true
          post :update, {:state => "delivery"}
        end

        it "should redirect the next state" do
          order.stub :state => "payment"
          post :update, {:state => "delivery"}
          response.should redirect_to checkout_state_path("payment")
        end

        context "when in the confirm state" do
          before { order.stub :state => "complete" }

          it "should redirect to the order view" do
            post :update, {:state => "confirm"}
            response.should redirect_to order_path(order)
          end

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

          it "should remove completed order from the session" do
            post :update, {:state => "confirm"}, {:order_id => "foofah"}
            session[:order_id].should be_nil
          end

        end

      end
    end

    context "save unsuccessful" do
      before { order.should_receive(:update_attributes).and_return false }

      it "should assign order" do
        post :update, {:state => "confirm"}
        assigns[:order].should_not be_nil
      end

      it "should not change the order state" do
        order.should_not_receive(:update_attribute)
        post :update, { :state => 'confirm' }
      end

      it "should render the edit template" do
        post :update, { :state => 'confirm' }
        response.should render_template :edit
      end
    end

    context "when current_order is nil" do
      before { controller.stub! :current_order => nil }
      it "should not change the state if order is completed" do
        order.should_not_receive(:update_attribute)
        post :update, {:state => "confirm"}
      end

      it "should redirect to the cart_path" do
        post :update, {:state => "confirm"}
        response.should redirect_to cart_path
      end
    end

    context "Spree::GatewayError" do

      before do
        order.stub(:update_attributes).and_raise(Spree::GatewayError)
        post :update, {:state => "whatever"}
      end

      it "should render the edit template" do
        response.should render_template :edit
      end

      it "should set appropriate flash message" do
        flash[:error].should == I18n.t('spree_gateway_error_flash_for_checkout')
      end

    end

  end

  context "When last inventory item has been purchased and no backorders" do
    let(:product) { mock_model(Product, :name => "Amazing Object") }
    let(:variant) { mock_model(Variant, :on_hand => 0) }
    let(:line_item) { mock_model(LineItem, :variant => variant, :quantity => 1, :product => product) }
    let(:order) { Factory.new(:order) }

    before do
      order.stub(:line_items => [line_item])
      Spree::Config.set :track_inventory_levels => true
      Spree::Config.set :allow_backorders => false
    end

    context "back orders == false" do
      before do
        post :update, {:state => "payment"}
      end

      it "should render edit template" do
        response.should redirect_to cart_path<commit_msg>Simplified checkout_controller_spec<commit_after>require 'spec_helper'

describe CheckoutController do
  let(:order) { mock_model(Order, :checkout_allowed? => true, :completed? => false, :update_attributes => true, :payment? => false, :insufficient_stock_lines => []).as_null_object }
  before { controller.stub :current_order => order, :current_user => nil }

  it "should understand checkout routes" do
    assert_routing("/checkout/delivery", {:controller => "checkout", :action => "edit", :state => "delivery"})
    assert_routing("/checkout/update/delivery", {:controller => "checkout", :action => "update", :state => "delivery"})
  end

  context "#edit" do

    it "should redirect to the cart path unless checkout_allowed?" do
      order.stub :checkout_allowed? => false
      get :edit, { :state => "delivery" }
      response.should redirect_to cart_path
    end

    it "should redirect to the cart path if current_order is nil" do
      controller.stub!(:current_order).and_return(nil)
      get :edit, { :state => "delivery" }
      response.should redirect_to cart_path
    end

    it "should change to the requested state" do
      order.should_receive(:state=).with("payment").and_return true
      get :edit, { :state => "payment" }
    end

    it "should redirect to cart if order is completed" do
      order.stub(:completed? => true)
      get :edit, {:state => "address"}
      response.should redirect_to(cart_path)
    end

  end

  context "#update" do

    context "save successful" do
      before do
        order.stub(:update_attribute).and_return true
        order.should_receive(:update_attributes).and_return true
      end

      it "should assign order" do
        post :update, {:state => "confirm"}
        assigns[:order].should_not be_nil
      end

      it "should change to requested state" do
        order.should_receive(:state=).with('confirm')
        post :update, {:state => "confirm"}
      end

      context "with next state" do
        before { order.stub :next => true }

        it "should advance the state" do
          order.should_receive(:next).and_return true
          post :update, {:state => "delivery"}
        end

        it "should redirect the next state" do
          order.stub :state => "payment"
          post :update, {:state => "delivery"}
          response.should redirect_to checkout_state_path("payment")
        end

        context "when in the confirm state" do
          before { order.stub :state => "complete" }

          it "should redirect to the order view" do
            post :update, {:state => "confirm"}
            response.should redirect_to order_path(order)
          end

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

          it "should remove completed order from the session" do
            post :update, {:state => "confirm"}, {:order_id => "foofah"}
            session[:order_id].should be_nil
          end

        end

      end
    end

    context "save unsuccessful" do
      before { order.should_receive(:update_attributes).and_return false }

      it "should assign order" do
        post :update, {:state => "confirm"}
        assigns[:order].should_not be_nil
      end

      it "should not change the order state" do
        order.should_not_receive(:update_attribute)
        post :update, { :state => 'confirm' }
      end

      it "should render the edit template" do
        post :update, { :state => 'confirm' }
        response.should render_template :edit
      end
    end

    context "when current_order is nil" do
      before { controller.stub! :current_order => nil }
      it "should not change the state if order is completed" do
        order.should_not_receive(:update_attribute)
        post :update, {:state => "confirm"}
      end

      it "should redirect to the cart_path" do
        post :update, {:state => "confirm"}
        response.should redirect_to cart_path
      end
    end

    context "Spree::GatewayError" do

      before do
        order.stub(:update_attributes).and_raise(Spree::GatewayError)
        post :update, {:state => "whatever"}
      end

      it "should render the edit template" do
        response.should render_template :edit
      end

      it "should set appropriate flash message" do
        flash[:error].should == I18n.t('spree_gateway_error_flash_for_checkout')
      end

    end

  end

  context "When last inventory item has been purchased" do
    let(:product) { mock_model(Product, :name => "Amazing Object") }
    let(:variant) { mock_model(Variant, :on_hand => 0) }
    let(:line_item) { mock_model LineItem, :insufficient_stock? => true }
    let(:order) { Factory(:order) }

    before do
      order.stub(:line_items => [line_item])
      Spree::Config.set :track_inventory_levels => true
      Spree::Config.set :allow_backorders => false
    end

    context "and back orders == false" do
      before do
        post :update, {:state => "payment"}
      end

      it "should render edit template" do
        response.should redirect_to cart_path
{'fraction_non_alphanumeric': 0.1084511104701471, 'fraction_numerical': 0.0010575906162868954, 'mean_word_length': 2.667842031029619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 93, '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': '364898', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2871, 'n_words': 903}