solidus_backend 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of solidus_backend might be problematic. Click here for more details.

Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -0
  3. data/Rakefile +15 -0
  4. data/script/rails +9 -0
  5. data/solidus_backend.gemspec +30 -0
  6. data/spec/controllers/spree/admin/base_controller_spec.rb +24 -0
  7. data/spec/controllers/spree/admin/cancellations_controller_spec.rb +77 -0
  8. data/spec/controllers/spree/admin/customer_returns_controller_spec.rb +234 -0
  9. data/spec/controllers/spree/admin/general_settings_controller_spec.rb +41 -0
  10. data/spec/controllers/spree/admin/missing_products_controller_spec.rb +18 -0
  11. data/spec/controllers/spree/admin/orders/customer_details_controller_spec.rb +80 -0
  12. data/spec/controllers/spree/admin/orders_controller_spec.rb +460 -0
  13. data/spec/controllers/spree/admin/payment_methods_controller_spec.rb +49 -0
  14. data/spec/controllers/spree/admin/payments_controller_spec.rb +175 -0
  15. data/spec/controllers/spree/admin/product_properties_controller_spec.rb +69 -0
  16. data/spec/controllers/spree/admin/products_controller_spec.rb +162 -0
  17. data/spec/controllers/spree/admin/promotion_actions_controller_spec.rb +21 -0
  18. data/spec/controllers/spree/admin/promotion_codes_controller_spec.rb +18 -0
  19. data/spec/controllers/spree/admin/promotion_rules_controller_spec.rb +21 -0
  20. data/spec/controllers/spree/admin/promotions_controller_spec.rb +122 -0
  21. data/spec/controllers/spree/admin/refunds_controller_spec.rb +32 -0
  22. data/spec/controllers/spree/admin/reimbursements_controller_spec.rb +124 -0
  23. data/spec/controllers/spree/admin/reports_controller_spec.rb +134 -0
  24. data/spec/controllers/spree/admin/resource_controller_spec.rb +166 -0
  25. data/spec/controllers/spree/admin/return_authorizations_controller_spec.rb +240 -0
  26. data/spec/controllers/spree/admin/return_items_controller_spec.rb +27 -0
  27. data/spec/controllers/spree/admin/root_controller_spec.rb +41 -0
  28. data/spec/controllers/spree/admin/search_controller_spec.rb +104 -0
  29. data/spec/controllers/spree/admin/shipping_methods_controller_spec.rb +14 -0
  30. data/spec/controllers/spree/admin/stock_items_controller_spec.rb +50 -0
  31. data/spec/controllers/spree/admin/stock_locations_controller_spec.rb +41 -0
  32. data/spec/controllers/spree/admin/stock_transfers_controller_spec.rb +351 -0
  33. data/spec/controllers/spree/admin/store_credits_controller_spec.rb +309 -0
  34. data/spec/controllers/spree/admin/users_controller_spec.rb +257 -0
  35. data/spec/controllers/spree/admin/variants_controller_spec.rb +32 -0
  36. data/spec/features/admin/configuration/analytics_tracker_spec.rb +50 -0
  37. data/spec/features/admin/configuration/countries_spec.rb +22 -0
  38. data/spec/features/admin/configuration/general_settings_spec.rb +45 -0
  39. data/spec/features/admin/configuration/payment_methods_spec.rb +124 -0
  40. data/spec/features/admin/configuration/shipping_methods_spec.rb +64 -0
  41. data/spec/features/admin/configuration/states_spec.rb +64 -0
  42. data/spec/features/admin/configuration/stock_locations_spec.rb +50 -0
  43. data/spec/features/admin/configuration/tax_categories_spec.rb +56 -0
  44. data/spec/features/admin/configuration/tax_rates_spec.rb +30 -0
  45. data/spec/features/admin/configuration/taxonomies_spec.rb +52 -0
  46. data/spec/features/admin/configuration/zones_spec.rb +39 -0
  47. data/spec/features/admin/homepage_spec.rb +78 -0
  48. data/spec/features/admin/locale_spec.rb +30 -0
  49. data/spec/features/admin/orders/adjustments_promotions_spec.rb +53 -0
  50. data/spec/features/admin/orders/adjustments_spec.rb +126 -0
  51. data/spec/features/admin/orders/cancelling_and_resuming_spec.rb +48 -0
  52. data/spec/features/admin/orders/cancelling_inventory_spec.rb +48 -0
  53. data/spec/features/admin/orders/customer_details_spec.rb +163 -0
  54. data/spec/features/admin/orders/line_items_spec.rb +50 -0
  55. data/spec/features/admin/orders/listing_spec.rb +130 -0
  56. data/spec/features/admin/orders/log_entries_spec.rb +55 -0
  57. data/spec/features/admin/orders/new_order_spec.rb +185 -0
  58. data/spec/features/admin/orders/order_details_spec.rb +533 -0
  59. data/spec/features/admin/orders/payments_spec.rb +234 -0
  60. data/spec/features/admin/orders/risk_analysis_spec.rb +47 -0
  61. data/spec/features/admin/orders/shipments_spec.rb +65 -0
  62. data/spec/features/admin/payments/store_credits_spec.rb +21 -0
  63. data/spec/features/admin/products/edit/images_spec.rb +87 -0
  64. data/spec/features/admin/products/edit/products_spec.rb +66 -0
  65. data/spec/features/admin/products/edit/taxons_spec.rb +43 -0
  66. data/spec/features/admin/products/edit/variants_spec.rb +61 -0
  67. data/spec/features/admin/products/option_types_spec.rb +114 -0
  68. data/spec/features/admin/products/products_spec.rb +395 -0
  69. data/spec/features/admin/products/properties_spec.rb +139 -0
  70. data/spec/features/admin/products/prototypes_spec.rb +110 -0
  71. data/spec/features/admin/products/stock_management_spec.rb +82 -0
  72. data/spec/features/admin/products/variant_spec.rb +51 -0
  73. data/spec/features/admin/promotion_adjustments_spec.rb +220 -0
  74. data/spec/features/admin/promotions/option_value_rule_spec.rb +65 -0
  75. data/spec/features/admin/promotions/tiered_calculator_spec.rb +69 -0
  76. data/spec/features/admin/reports_spec.rb +61 -0
  77. data/spec/features/admin/stock_transfer_spec.rb +104 -0
  78. data/spec/features/admin/store_credits_spec.rb +82 -0
  79. data/spec/features/admin/taxons_spec.rb +31 -0
  80. data/spec/features/admin/users_spec.rb +275 -0
  81. data/spec/helpers/admin/base_helper_spec.rb +18 -0
  82. data/spec/helpers/admin/navigation_helper_spec.rb +73 -0
  83. data/spec/helpers/admin/reimbursements_helper_spec.rb +34 -0
  84. data/spec/helpers/admin/stock_movements_helper_spec.rb +29 -0
  85. data/spec/helpers/admin/store_credit_events_helper_spec.rb +95 -0
  86. data/spec/helpers/promotion_rules_helper_spec.rb +12 -0
  87. data/spec/spec_helper.rb +116 -0
  88. data/spec/support/appear_before_matcher.rb +8 -0
  89. data/spec/support/ror_ringer.jpeg +0 -0
  90. data/spec/test_views/spree/admin/widgets/edit.html.erb +1 -0
  91. data/spec/test_views/spree/admin/widgets/new.html.erb +1 -0
  92. metadata +96 -6
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ module Admin
5
+ describe ProductPropertiesController, type: :controller do
6
+ stub_authorization!
7
+
8
+ describe "#index" do
9
+ subject { spree_get :index, parameters }
10
+
11
+ context "no option values are provided" do
12
+ let(:product) { create(:product) }
13
+ let(:parameters) do
14
+ { product_id: product.to_param }
15
+ end
16
+
17
+ before { subject }
18
+
19
+ it "instantiates a new variant property rule" do
20
+ expect(assigns(:variant_property_rule)).to_not be_persisted
21
+ end
22
+
23
+ it "instantiates a new variant property rule value" do
24
+ expect(assigns(:variant_property_rule).values.size).to eq 1
25
+ expect(assigns(:variant_property_rule).values.first).to_not be_persisted
26
+ end
27
+ end
28
+
29
+ context "option values are provided" do
30
+ let(:size) { create(:option_type, name: 'size') }
31
+ let(:product) { create(:product, option_types: [size]) }
32
+ let(:size_small) { create(:option_value, name: 'small', option_type: size) }
33
+ let(:size_large) { create(:option_value, name: 'large', option_type: size) }
34
+ let!(:first_rule) { create(:variant_property_rule, product: product, option_value: size_small) }
35
+
36
+ context "no rules match the option values" do
37
+ let(:parameters) do
38
+ {
39
+ product_id: product.to_param,
40
+ ovi: [size_large.id]
41
+ }
42
+ end
43
+
44
+ before { subject }
45
+
46
+ it "instantiates a new variant property rule" do
47
+ expect(assigns(:variant_property_rule)).to_not be_persisted
48
+ end
49
+ end
50
+
51
+ context "a rule matches the option values" do
52
+ let(:parameters) do
53
+ {
54
+ product_id: product.to_param,
55
+ ovi: [size_small.id]
56
+ }
57
+ end
58
+
59
+ before { subject }
60
+
61
+ it "assigns the property rule to the only property rule that matches the option values" do
62
+ expect(assigns(:variant_property_rule)).to eq first_rule
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,162 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::ProductsController, :type => :controller do
4
+ stub_authorization!
5
+
6
+ context "#index" do
7
+ let(:ability_user) { stub_model(Spree::LegacyUser, :has_spree_role? => true) }
8
+
9
+ # Regression test for #1259
10
+ it "can find a product by SKU" do
11
+ product = create(:product, :sku => "ABC123")
12
+ spree_get :index, :q => { :sku_start => "ABC123" }
13
+ expect(assigns[:collection]).not_to be_empty
14
+ expect(assigns[:collection]).to include(product)
15
+ end
16
+ end
17
+
18
+ # regression test for #1370
19
+ context "adding properties to a product" do
20
+ let!(:product) { create(:product) }
21
+ specify do
22
+ spree_put :update, :id => product.to_param, :product => { :product_properties_attributes => { "1" => { :property_name => "Foo", :value => "bar" } } }
23
+ expect(flash[:success]).to eq("Product #{product.name.inspect} has been successfully updated!")
24
+ end
25
+ end
26
+
27
+ describe "creating variant property rules" do
28
+ let(:first_property) { create(:property) }
29
+ let(:second_property) { create(:property) }
30
+ let(:option_value) { create(:option_value) }
31
+ let!(:product) { create(:product, option_types: [option_value.option_type]) }
32
+ let(:payload) do
33
+ {
34
+ id: product.to_param,
35
+ product: {
36
+ id: product.id,
37
+ variant_property_rules_attributes: {
38
+ "0" => {
39
+ option_value_ids: option_value.id,
40
+ values_attributes: {
41
+ "0" => {
42
+ property_name: first_property.name,
43
+ value: "First"
44
+ },
45
+ "1" => {
46
+ property_name: second_property.name,
47
+ value: "Second"
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ end
55
+
56
+ subject { spree_put :update, payload }
57
+
58
+ it "creates a variant property rule" do
59
+ expect { subject }.to change { product.variant_property_rules.count }.by(1)
60
+ end
61
+
62
+ it "creates a variant property rule condition" do
63
+ expect { subject }.to change { product.variant_property_rule_conditions.count }.by(1)
64
+ end
65
+
66
+ it "creates a variant property rule value for the 'First' value" do
67
+ subject
68
+ expect(product.variant_property_rule_values.find_by(value: 'First')).to_not be_nil
69
+ end
70
+
71
+ it "creates a variant property rule value for the 'Second' value" do
72
+ subject
73
+ expect(product.variant_property_rule_values.find_by(value: 'Second')).to_not be_nil
74
+ end
75
+
76
+ it "redirects to the product properties page" do
77
+ subject
78
+ expect(response).to redirect_to(spree.admin_product_product_properties_path(product, ovi: [option_value.id]))
79
+ end
80
+ end
81
+
82
+ describe "updating variant property rules" do
83
+ let(:first_property) { create(:property) }
84
+ let(:second_property) { create(:property) }
85
+ let(:option_value) { create(:option_value) }
86
+ let(:original_option_value) { create(:option_value) }
87
+ let!(:product) { create(:product, option_types: [option_value.option_type]) }
88
+ let!(:rule) do
89
+ create(:variant_property_rule, product: product, option_value: original_option_value)
90
+ end
91
+ let(:payload) do
92
+ {
93
+ id: product.to_param,
94
+ product: {
95
+ id: product.id,
96
+ variant_property_rules_attributes: {
97
+ "0" => {
98
+ id: rule.id,
99
+ option_value_ids: option_value.id,
100
+ values_attributes: {
101
+ "0" => {
102
+ property_name: first_property.name,
103
+ value: "First Edit"
104
+ },
105
+ "1" => {
106
+ property_name: second_property.name,
107
+ value: "Second Edit"
108
+ }
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+ end
115
+
116
+ subject { spree_put :update, payload }
117
+
118
+ it "does not create any new rules" do
119
+ expect { subject }.to_not change { Spree::VariantPropertyRule.count }
120
+ end
121
+
122
+ it "replaces the rule's condition" do
123
+ expect { subject }.to change { rule.option_value_ids }.from([original_option_value.id]).to([option_value.id])
124
+ end
125
+
126
+ it "adds two values to the rule" do
127
+ expect { subject }.to change { rule.values.count }.by(2)
128
+ end
129
+
130
+ it "creates the 'First Edit' value" do
131
+ subject
132
+ expect(rule.values.find_by(value: 'First Edit')).to_not be_nil
133
+ end
134
+
135
+ it "creates the 'Second Edit' value" do
136
+ subject
137
+ expect(rule.values.find_by(value: 'Second Edit')).to_not be_nil
138
+ end
139
+
140
+ it "redirects to the product properties page" do
141
+ subject
142
+ expect(response).to redirect_to(spree.admin_product_product_properties_path(product, ovi: [option_value.id]))
143
+ end
144
+ end
145
+
146
+ # regression test for #801
147
+ context "destroying a product" do
148
+ let(:product) do
149
+ product = create(:product)
150
+ create(:variant, :product => product)
151
+ product
152
+ end
153
+
154
+ it "deletes all the variants (including master) for the product" do
155
+ spree_delete :destroy, :id => product
156
+ expect(product.reload.deleted_at).not_to be_nil
157
+ product.variants_including_master.each do |variant|
158
+ expect(variant.reload.deleted_at).not_to be_nil
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::PromotionActionsController, :type => :controller do
4
+ stub_authorization!
5
+
6
+ let!(:promotion) { create(:promotion) }
7
+
8
+ it "can create a promotion action of a valid type" do
9
+ spree_post :create, :promotion_id => promotion.id, :action_type => "Spree::Promotion::Actions::CreateAdjustment"
10
+ expect(response).to be_redirect
11
+ expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
12
+ expect(promotion.actions.count).to eq(1)
13
+ end
14
+
15
+ it "can not create a promotion action of an invalid type" do
16
+ spree_post :create, :promotion_id => promotion.id, :action_type => "Spree::InvalidType"
17
+ expect(response).to be_redirect
18
+ expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
19
+ expect(promotion.rules.count).to eq(0)
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::PromotionCodesController do
4
+ stub_authorization!
5
+ render_views
6
+
7
+ let!(:promotion) { create(:promotion) }
8
+ let!(:code1) { create(:promotion_code, promotion: promotion) }
9
+ let!(:code2) { create(:promotion_code, promotion: promotion) }
10
+ let!(:code3) { create(:promotion_code, promotion: promotion) }
11
+
12
+ it "can create a promotion rule of a valid type" do
13
+ spree_get :index, promotion_id: promotion.id, format: 'csv'
14
+ expect(response).to be_success
15
+ parsed = CSV.parse(response.body, headers: true)
16
+ expect(parsed.entries.map(&:to_h)).to eq([{"Code" => code1.value}, {"Code" => code2.value}, {"Code" => code3.value}])
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::PromotionRulesController, :type => :controller do
4
+ stub_authorization!
5
+
6
+ let!(:promotion) { create(:promotion) }
7
+
8
+ it "can create a promotion rule of a valid type" do
9
+ spree_post :create, :promotion_id => promotion.id, :promotion_rule => { :type => "Spree::Promotion::Rules::Product" }
10
+ expect(response).to be_redirect
11
+ expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
12
+ expect(promotion.rules.count).to eq(1)
13
+ end
14
+
15
+ it "can not create a promotion rule of an invalid type" do
16
+ spree_post :create, :promotion_id => promotion.id, :promotion_rule => { :type => "Spree::InvalidType" }
17
+ expect(response).to be_redirect
18
+ expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
19
+ expect(promotion.rules.count).to eq(0)
20
+ end
21
+ end
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::PromotionsController, :type => :controller do
4
+ stub_authorization!
5
+
6
+ let!(:promotion1) { create(:promotion, name: "name1", code: "code1", path: "path1") }
7
+ let!(:promotion2) { create(:promotion, name: "name2", code: "code2", path: "path2") }
8
+ let!(:category) { create :promotion_category }
9
+
10
+ describe "#index" do
11
+
12
+ it "succeeds" do
13
+ spree_get :index
14
+ expect(assigns[:promotions]).to match_array [promotion2, promotion1]
15
+ end
16
+
17
+ it "assigns promotion categories" do
18
+ spree_get :index
19
+ expect(assigns[:promotion_categories]).to match_array [category]
20
+ end
21
+
22
+ context "search" do
23
+ it "pages results" do
24
+ spree_get :index, per_page: '1'
25
+ expect(assigns[:promotions]).to eq [promotion2]
26
+ end
27
+
28
+ it "filters by name" do
29
+ spree_get :index, q: {name_cont: promotion1.name}
30
+ expect(assigns[:promotions]).to eq [promotion1]
31
+ end
32
+
33
+ it "filters by code" do
34
+ spree_get :index, q: {codes_value_cont: promotion1.codes.first.value }
35
+ expect(assigns[:promotions]).to eq [promotion1]
36
+ end
37
+
38
+ it "filters by path" do
39
+ spree_get :index, q: {path_cont: promotion1.path}
40
+ expect(assigns[:promotions]).to eq [promotion1]
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "#create" do
46
+ subject { spree_post :create, params }
47
+ let(:params) { {promotion: {name: 'some promo'}} }
48
+
49
+ context "it succeeds" do
50
+ it "creates a promotion" do
51
+ expect { subject }.to change { Spree::Promotion.count }.by(1)
52
+ end
53
+
54
+ it "sets the flash message" do
55
+ subject
56
+ expect(flash[:success]).to eq "Promotion has been successfully created!"
57
+ end
58
+
59
+ it "redirects to promotion edit" do
60
+ subject
61
+ expect(response).to redirect_to "http://test.host/admin/promotions/#{assigns(:promotion).id}/edit"
62
+ end
63
+
64
+ context "with one promo codes" do
65
+ let(:params) do
66
+ super().merge(promotion_builder: { base_code: 'abc', number_of_codes: 1 })
67
+ end
68
+
69
+ it "succeeds and creates one code" do
70
+ expect {
71
+ expect {
72
+ subject
73
+ }.to change { Spree::Promotion.count }.by(1)
74
+ }.to change { Spree::PromotionCode.count }.by(1)
75
+
76
+ expect(assigns(:promotion).codes.first.value).to eq ('abc')
77
+ end
78
+ end
79
+
80
+ context "with multiple promo codes" do
81
+ let(:params) do
82
+ super().merge(promotion_builder: { base_code: 'abc', number_of_codes: 2 })
83
+ end
84
+
85
+ it "succeeds and creates multiple codes" do
86
+ expect {
87
+ expect {
88
+ subject
89
+ }.to change { Spree::Promotion.count }.by(1)
90
+ }.to change { Spree::PromotionCode.count }.by(2)
91
+
92
+ codes = assigns(:promotion).codes.map(&:value).sort
93
+ expect(codes.length).to be 2
94
+ expect(codes[0]).to match(/abc_[a-z]{6}/)
95
+ expect(codes[1]).to match(/abc_[a-z]{6}/)
96
+ end
97
+ end
98
+ end
99
+
100
+ context "it fails" do
101
+ let(:params) { {} }
102
+
103
+ it "does not create a promotion" do
104
+ expect {
105
+ subject
106
+ }.not_to change { Spree::Promotion.count }
107
+ end
108
+
109
+ it "sets the flash error" do
110
+ subject
111
+ expect(flash[:error]).to eq "Name can't be blank"
112
+ end
113
+
114
+ it "renders new" do
115
+ subject
116
+ expect(response).to render_template :new
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::RefundsController do
4
+ stub_authorization!
5
+
6
+ describe "POST create" do
7
+ context "a Spree::Core::GatewayError is raised" do
8
+
9
+ let(:payment) { create(:payment) }
10
+
11
+ subject do
12
+ spree_post :create,
13
+ refund: { amount: "50.0", refund_reason_id: "1" },
14
+ order_id: payment.order_id,
15
+ payment_id: payment.id
16
+ end
17
+
18
+ before(:each) do
19
+ def controller.create
20
+ raise Spree::Core::GatewayError.new('An error has occurred')
21
+ end
22
+ end
23
+
24
+ it "sets an error message with the correct text" do
25
+ subject
26
+ expect(flash[:error]).to eq 'An error has occurred'
27
+ end
28
+
29
+ it { is_expected.to render_template(:new) }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Admin::ReimbursementsController, :type => :controller do
4
+ stub_authorization!
5
+
6
+ let!(:default_refund_reason) do
7
+ Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false)
8
+ end
9
+
10
+ describe '#edit' do
11
+ let(:reimbursement) { create(:reimbursement) }
12
+ let(:order) { reimbursement.order }
13
+ let!(:active_stock_location) { create(:stock_location, active: true) }
14
+ let!(:inactive_stock_location) { create(:stock_location, active: false) }
15
+
16
+ subject do
17
+ spree_get :edit, order_id: order.to_param, id: reimbursement.to_param
18
+ end
19
+
20
+ it "loads all the active stock locations" do
21
+ subject
22
+ expect(assigns(:stock_locations)).to include(active_stock_location)
23
+ expect(assigns(:stock_locations)).not_to include(inactive_stock_location)
24
+ end
25
+ end
26
+
27
+ describe '#create' do
28
+ let(:customer_return) { create(:customer_return, line_items_count: 1) }
29
+ let(:order) { customer_return.order }
30
+ let(:return_item) { customer_return.return_items.first }
31
+ let(:payment) { order.payments.first }
32
+ before { return_item.receive! }
33
+
34
+ subject do
35
+ spree_post :create, order_id: order.to_param, build_from_customer_return_id: customer_return.id
36
+ end
37
+
38
+ it 'creates the reimbursement' do
39
+ expect { subject }.to change { order.reimbursements.count }.by(1)
40
+ expect(assigns(:reimbursement).return_items.to_a).to eq customer_return.return_items.to_a
41
+ end
42
+
43
+ it 'redirects to the edit page' do
44
+ subject
45
+ expect(response).to redirect_to(spree.edit_admin_order_reimbursement_path(order, assigns(:reimbursement)))
46
+ end
47
+
48
+ context 'when create fails' do
49
+ before do
50
+ allow_any_instance_of(Spree::Reimbursement).to receive(:valid?) do |reimbursement, *args|
51
+ reimbursement.errors.add(:base, 'something bad happened')
52
+ false
53
+ end
54
+ end
55
+
56
+ context 'when a referer header is present' do
57
+ let(:referer) { spree.edit_admin_order_customer_return_path(order, customer_return) }
58
+
59
+ it 'redirects to the referer' do
60
+ request.env["HTTP_REFERER"] = referer
61
+ expect {
62
+ spree_post :create, order_id: order.to_param
63
+ }.to_not change { Spree::Reimbursement.count }
64
+ expect(response).to redirect_to(referer)
65
+ expect(flash[:error]).to eq("something bad happened")
66
+ end
67
+ end
68
+
69
+ context 'when a referer header is not present' do
70
+ it 'redirects to the admin root' do
71
+ expect {
72
+ spree_post :create, order_id: order.to_param
73
+ }.to_not change { Spree::Reimbursement.count }
74
+ expect(response).to redirect_to(spree.admin_path)
75
+ expect(flash[:error]).to eq("something bad happened")
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#perform" do
82
+ let(:reimbursement) { create(:reimbursement) }
83
+ let(:customer_return) { reimbursement.customer_return }
84
+ let(:order) { reimbursement.order }
85
+ let(:return_items) { reimbursement.return_items }
86
+ let(:payment) { order.payments.first }
87
+
88
+ subject do
89
+ spree_post :perform, order_id: order.to_param, id: reimbursement.to_param
90
+ end
91
+
92
+ it 'redirects to customer return page' do
93
+ subject
94
+ expect(response).to redirect_to spree.admin_order_reimbursement_path(order, reimbursement)
95
+ end
96
+
97
+ it 'performs the reimbursement' do
98
+ expect {
99
+ subject
100
+ }.to change { payment.refunds.count }.by(1)
101
+ expect(payment.refunds.last.amount).to be > 0
102
+ expect(payment.refunds.last.amount).to eq return_items.to_a.sum(&:total)
103
+ end
104
+
105
+ context "a Spree::Core::GatewayError is raised" do
106
+ before(:each) do
107
+ def controller.perform
108
+ raise Spree::Core::GatewayError.new('An error has occurred')
109
+ end
110
+ end
111
+
112
+ it "sets an error message with the correct text" do
113
+ subject
114
+ expect(flash[:error]).to eq 'An error has occurred'
115
+ end
116
+
117
+ it 'redirects to the edit page' do
118
+ subject
119
+ redirect_path = spree.edit_admin_order_reimbursement_path(order, assigns(:reimbursement))
120
+ expect(response).to redirect_to(redirect_path)
121
+ end
122
+ end
123
+ end
124
+ end