Created
December 30, 2013 17:06
-
-
Save HoyaBoya/8184856 to your computer and use it in GitHub Desktop.
Spree Functional Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Spree::Api::OrdersController do | |
render_views | |
let(:user) { create(:user) } | |
before(:each) do | |
warden = double(:warden, user: user, authenticate: user) | |
controller.stub(:env).and_return({'warden' => warden}) | |
controller.request.env['warden'] = warden | |
# Required for routes testing to work. | |
@routes = Spree::Core::Engine.routes | |
end | |
describe 'apply_promo_code' do | |
stub_authorization! | |
# Borrowed from: | |
# https://github.com/spree/spree/blob/master/api/spec/controllers/spree/api/promotion_application_spec.rb | |
let(:order) { create(:order, state: 'payment', user: user) } | |
subject do | |
put :apply_coupon_code, id: order.to_param, coupon_code: coupon_code, order_token: order.token, format: 'json' | |
end | |
context 'when valid promo code' do | |
let(:coupon_code) { 'tenoff' } | |
# Create a promotion for testing. | |
# https://github.com/spree/spree/blob/2-1-stable/core/spec/lib/spree/promo/coupon_applicator_spec.rb | |
before(:each) do | |
flat_percent_calc = Spree::Calculator::FlatPercentItemTotal.create(:preferred_flat_percent => "10") | |
promo = Spree::Promotion.create(:name => "Discount", :event_name => "spree.checkout.coupon_code_added", :code => "tenoff", :usage_limit => "10", :starts_at => DateTime.yesterday, :expires_at => DateTime.tomorrow) | |
promo_rule = Spree::Promotion::Rules::ItemTotal.create(:preferred_operator => "gt", :preferred_amount => "1") | |
promo_rule.update_attribute(:activator_id, promo.id) | |
promo_action = Spree::Promotion::Actions::CreateAdjustment.create(:calculator_type => "Spree::Calculator::FlatPercentItemTotal") | |
promo_action.update_attribute(:activator_id, promo.id) | |
flat_percent_calc.update_attribute(:calculable_id, promo.id) | |
Spree::Order.any_instance.stub(:payment_required? => false) | |
Spree::Adjustment.any_instance.stub(:eligible => true) | |
end | |
it 'should should be_ok ' do | |
subject | |
response.should be_ok | |
end | |
it 'should have success JSON' do | |
subject | |
result = JSON.parse(response.body) | |
result['successful'].should be_true | |
result['success'].should == "The coupon code was successfully applied to your order." | |
result['error'].should be_nil | |
end | |
end | |
context 'when invalid promo code' do | |
let(:coupon_code) { 'xxxxx' } | |
it 'should should be_ok ' do | |
subject | |
response.code.to_i.should == 422 | |
end | |
it 'should have error JSON' do | |
subject | |
result = JSON.parse(response.body) | |
result['successful'].should be_false | |
result['error'].should == "The coupon code you entered doesn't exist. Please try again." | |
result['success'].should be_nil | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment