Sha256: 10cfb94a76dfe36e3c111d632a4b462967191521a1ae476dde6bbe704af980f2

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

require 'spec_helper'

module Spree::Api
  describe OrdersController do
    render_views

    before do
      stub_authentication!
    end

    context "with an available promotion" do
      let!(:order) { create(:order_with_line_items, :line_items_count => 1) }
      let!(:promotion) do 
        promotion = Spree::Promotion.create(name: "10% off", code: "10off")
        calculator = Spree::Calculator::FlatPercentItemTotal.create(preferred_flat_percent: "10")
        action = Spree::Promotion::Actions::CreateItemAdjustments.create(calculator: calculator)
        promotion.actions << action
        promotion
      end

      it "can apply a coupon code to the order" do
        order.total.should == 110.00
        api_put :apply_coupon_code, :id => order.to_param, :coupon_code => "10off", :order_token => order.guest_token
        response.status.should == 200
        order.reload.total.should == 109.00
        json_response["success"].should == "The coupon code was successfully applied to your order."
        json_response["error"].should be_blank
        json_response["successful"].should be_true
      end

      context "with an expired promotion" do
        before do
          promotion.starts_at = 2.weeks.ago
          promotion.expires_at = 1.week.ago
          promotion.save
        end

        it "fails to apply" do
          api_put :apply_coupon_code, :id => order.to_param, :coupon_code => "10off", :order_token => order.guest_token
          response.status.should == 422
          json_response["success"].should be_blank
          json_response["error"].should == "The coupon code is expired"
          json_response["successful"].should be_false
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_api-2.3.2 spec/controllers/spree/api/promotion_application_spec.rb
spree_api-2.3.1 spec/controllers/spree/api/promotion_application_spec.rb
spree_api-2.3.0 spec/controllers/spree/api/promotion_application_spec.rb