<commit_before>require 'spec_helper'
require 'spree/promo/coupon_applicator'

module Spree
  describe Api::CheckoutsController do
    render_views

    before(:each) do
      stub_authentication!
      Spree::Config[:track_inventory_levels] = false
      country_zone = create(:zone, :name => 'CountryZone')
      @state = create(:state)
      @country = @state.country
      country_zone.members.create(:zoneable => @country)
      create(:stock_location)

      @shipping_method = create(:shipping_method, :zones => [country_zone])
      @payment_method = create(:bogus_payment_method)
    end

    after do
      Spree::Config[:track_inventory_levels] = true
    end

    context "POST 'create'" do
      it "creates a new order when no parameters are passed" do
        api_post :create

        json_response['number'].should be_present
        response.status.should == 201
      end

      it "should not have a user by default" do
        api_post :create

        json_response['user_id'].should_not be_present
        response.status.should == 201
      end

      it "should not have an email by default" do
        api_post :create

        json_response['email'].should_not be_present
        response.status.should == 201
      end
    end

    context "PUT 'update'" do
      let(:order) { create(:order_with_line_items) }

      before(:each) do
        Order.any_instance.stub(:confirmation_required? => true)
        Order.any_instance.stub(:payment_required? => true)
      end

      it "will return an error if the recently created order cannot transition from cart to address" do
        order.state.should eq "cart"
        order.update_column(:email, nil) # email is necessary to transition from cart to address

        api_put :update, :id => order.to_param, :order_token => order.token

        # Order has not transitioned
        json_response['state'].should == 'cart'
      end

      it "should transition a recently created order from cart to address" do
        order.state.should eq "cart"
        order.email.should_not be_nil
        api_put :update, :id => order.to_param, :order_token => order.token
        order.reload.state.should eq "address"
      end

      it "can take line_items_attributes as a parameter" do
        line_item = order.line_items.first
        api_put :update, :id => order.to_param, :order_token => order.token,
                         :order => { :line_items_attributes => { line_item.id => { :quantity => 1 } } }
        response.status.should == 200
        order.reload.state.should eq "address"
      end

      it "can take line_items as a parameter" do
        line_item = order.line_items.first
        api_put :update, :id => order.to_param, :order_token => order.token,
                         :order => { :line_items => { line_item.id => { :quantity => 1 } } }
        response.status.should == 200
        order.reload.state.should eq "address"
      end

      it "will return an error if the order cannot transition" do
        order.bill_address = nil
        order.save
        order.update_column(:state, "address")
        api_put :update, :id => order.to_param, :order_token => order.token
        # Order has not transitioned
        response.status.should == 422
      end

      it "can update addresses and transition from address to delivery" do
        order.update_column(:state, "address")<commit_msg>[api] Remove test checking for now-impossible (see last commit) situation in CheckoutsController<commit_after>require 'spec_helper'
require 'spree/promo/coupon_applicator'

module Spree
  describe Api::CheckoutsController do
    render_views

    before(:each) do
      stub_authentication!
      Spree::Config[:track_inventory_levels] = false
      country_zone = create(:zone, :name => 'CountryZone')
      @state = create(:state)
      @country = @state.country
      country_zone.members.create(:zoneable => @country)
      create(:stock_location)

      @shipping_method = create(:shipping_method, :zones => [country_zone])
      @payment_method = create(:bogus_payment_method)
    end

    after do
      Spree::Config[:track_inventory_levels] = true
    end

    context "POST 'create'" do
      it "creates a new order when no parameters are passed" do
        api_post :create

        json_response['number'].should be_present
        response.status.should == 201
      end

      it "should not have a user by default" do
        api_post :create

        json_response['user_id'].should_not be_present
        response.status.should == 201
      end

      it "should not have an email by default" do
        api_post :create

        json_response['email'].should_not be_present
        response.status.should == 201
      end
    end

    context "PUT 'update'" do
      let(:order) { create(:order_with_line_items) }

      before(:each) do
        Order.any_instance.stub(:confirmation_required? => true)
        Order.any_instance.stub(:payment_required? => true)
      end

      it "will return an error if the recently created order cannot transition from cart to address" do
        order.state.should eq "cart"
        order.update_column(:email, nil) # email is necessary to transition from cart to address

        api_put :update, :id => order.to_param, :order_token => order.token

        # Order has not transitioned
        json_response['state'].should == 'cart'
      end

      it "should transition a recently created order from cart to address" do
        order.state.should eq "cart"
        order.email.should_not be_nil
        api_put :update, :id => order.to_param, :order_token => order.token
        order.reload.state.should eq "address"
      end

      it "can take line_items_attributes as a parameter" do
        line_item = order.line_items.first
        api_put :update, :id => order.to_param, :order_token => order.token,
                         :order => { :line_items_attributes => { line_item.id => { :quantity => 1 } } }
        response.status.should == 200
        order.reload.state.should eq "address"
      end

      it "can take line_items as a parameter" do
        line_item = order.line_items.first
        api_put :update, :id => order.to_param, :order_token => order.token,
                         :order => { :line_items => { line_item.id => { :quantity => 1 } } }
        response.status.should == 200
        order.reload.state.should eq "address"
      end

      it "can update addresses and transition from address to delivery" do
        order.update_column(:state, "address")
{'fraction_non_alphanumeric': 0.09608921478765658, 'fraction_numerical': 0.005652306752215093, 'mean_word_length': 2.5992303463441453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '4330656', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1820, 'n_words': 530}