spree_core 3.0.5 → 3.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile +3 -0
  4. data/Rakefile +30 -0
  5. data/app/assets/javascripts/spree.js.coffee.erb +1 -1
  6. data/app/models/spree/ability.rb +1 -1
  7. data/app/models/spree/base.rb +3 -1
  8. data/app/models/spree/order_updater.rb +2 -1
  9. data/app/models/spree/price.rb +7 -12
  10. data/app/models/spree/product.rb +3 -2
  11. data/app/models/spree/reimbursement.rb +1 -1
  12. data/app/models/spree/state.rb +2 -0
  13. data/app/models/spree/zone.rb +1 -1
  14. data/lib/spree/core/version.rb +1 -1
  15. data/lib/spree/testing_support/shoulda_matcher_configuration.rb +6 -0
  16. data/script/rails +9 -0
  17. data/spec/fixtures/thinking-cat.jpg +0 -0
  18. data/spec/helpers/base_helper_spec.rb +137 -0
  19. data/spec/helpers/products_helper_spec.rb +224 -0
  20. data/spec/lib/calculated_adjustments_spec.rb +7 -0
  21. data/spec/lib/i18n_spec.rb +123 -0
  22. data/spec/lib/search/base_spec.rb +86 -0
  23. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +101 -0
  24. data/spec/lib/spree/core/controller_helpers/order_spec.rb +95 -0
  25. data/spec/lib/spree/core/controller_helpers/search_spec.rb +17 -0
  26. data/spec/lib/spree/core/controller_helpers/store_spec.rb +16 -0
  27. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +39 -0
  28. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +22 -0
  29. data/spec/lib/spree/core/importer/order_spec.rb +502 -0
  30. data/spec/lib/spree/core/validators/email_spec.rb +53 -0
  31. data/spec/lib/spree/localized_number_spec.rb +38 -0
  32. data/spec/lib/spree/migrations_spec.rb +34 -0
  33. data/spec/lib/spree/money_spec.rb +122 -0
  34. data/spec/lib/tasks/exchanges_spec.rb +136 -0
  35. data/spec/mailers/order_mailer_spec.rb +124 -0
  36. data/spec/mailers/reimbursement_mailer_spec.rb +47 -0
  37. data/spec/mailers/shipment_mailer_spec.rb +63 -0
  38. data/spec/mailers/test_mailer_spec.rb +24 -0
  39. data/spec/models/spree/ability_spec.rb +246 -0
  40. data/spec/models/spree/address_spec.rb +291 -0
  41. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +286 -0
  42. data/spec/models/spree/adjustment_spec.rb +163 -0
  43. data/spec/models/spree/app_configuration_spec.rb +23 -0
  44. data/spec/models/spree/asset_spec.rb +25 -0
  45. data/spec/models/spree/calculator/default_tax_spec.rb +127 -0
  46. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +25 -0
  47. data/spec/models/spree/calculator/flat_rate_spec.rb +47 -0
  48. data/spec/models/spree/calculator/flexi_rate_spec.rb +41 -0
  49. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +15 -0
  50. data/spec/models/spree/calculator/price_sack_spec.rb +30 -0
  51. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +51 -0
  52. data/spec/models/spree/calculator/shipping.rb +8 -0
  53. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +23 -0
  54. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +13 -0
  55. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +52 -0
  56. data/spec/models/spree/calculator/shipping/per_item_spec.rb +20 -0
  57. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +29 -0
  58. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +40 -0
  59. data/spec/models/spree/calculator/tiered_percent_spec.rb +51 -0
  60. data/spec/models/spree/calculator_spec.rb +69 -0
  61. data/spec/models/spree/classification_spec.rb +93 -0
  62. data/spec/models/spree/concerns/display_money_spec.rb +43 -0
  63. data/spec/models/spree/country_spec.rb +18 -0
  64. data/spec/models/spree/credit_card_spec.rb +324 -0
  65. data/spec/models/spree/customer_return_spec.rb +262 -0
  66. data/spec/models/spree/exchange_spec.rb +75 -0
  67. data/spec/models/spree/gateway/bogus_simple.rb +20 -0
  68. data/spec/models/spree/gateway/bogus_spec.rb +13 -0
  69. data/spec/models/spree/gateway_spec.rb +54 -0
  70. data/spec/models/spree/image_spec.rb +5 -0
  71. data/spec/models/spree/inventory_unit_spec.rb +242 -0
  72. data/spec/models/spree/line_item_spec.rb +267 -0
  73. data/spec/models/spree/option_type_spec.rb +14 -0
  74. data/spec/models/spree/option_value_spec.rb +13 -0
  75. data/spec/models/spree/order/address_spec.rb +50 -0
  76. data/spec/models/spree/order/adjustments_spec.rb +29 -0
  77. data/spec/models/spree/order/callbacks_spec.rb +42 -0
  78. data/spec/models/spree/order/checkout_spec.rb +764 -0
  79. data/spec/models/spree/order/currency_updater_spec.rb +32 -0
  80. data/spec/models/spree/order/finalizing_spec.rb +117 -0
  81. data/spec/models/spree/order/helpers_spec.rb +5 -0
  82. data/spec/models/spree/order/payment_spec.rb +214 -0
  83. data/spec/models/spree/order/risk_assessment_spec.rb +84 -0
  84. data/spec/models/spree/order/shipments_spec.rb +43 -0
  85. data/spec/models/spree/order/state_machine_spec.rb +216 -0
  86. data/spec/models/spree/order/tax_spec.rb +84 -0
  87. data/spec/models/spree/order/totals_spec.rb +24 -0
  88. data/spec/models/spree/order/updating_spec.rb +18 -0
  89. data/spec/models/spree/order/validations_spec.rb +15 -0
  90. data/spec/models/spree/order_contents_spec.rb +256 -0
  91. data/spec/models/spree/order_inventory_spec.rb +228 -0
  92. data/spec/models/spree/order_merger_spec.rb +133 -0
  93. data/spec/models/spree/order_spec.rb +954 -0
  94. data/spec/models/spree/order_updater_spec.rb +283 -0
  95. data/spec/models/spree/payment/gateway_options_spec.rb +119 -0
  96. data/spec/models/spree/payment_method_spec.rb +95 -0
  97. data/spec/models/spree/payment_spec.rb +926 -0
  98. data/spec/models/spree/preference_spec.rb +80 -0
  99. data/spec/models/spree/preferences/configuration_spec.rb +30 -0
  100. data/spec/models/spree/preferences/preferable_spec.rb +348 -0
  101. data/spec/models/spree/preferences/scoped_store_spec.rb +58 -0
  102. data/spec/models/spree/preferences/store_spec.rb +46 -0
  103. data/spec/models/spree/price_spec.rb +42 -0
  104. data/spec/models/spree/product/scopes_spec.rb +148 -0
  105. data/spec/models/spree/product_duplicator_spec.rb +103 -0
  106. data/spec/models/spree/product_filter_spec.rb +26 -0
  107. data/spec/models/spree/product_option_type_spec.rb +5 -0
  108. data/spec/models/spree/product_property_spec.rb +11 -0
  109. data/spec/models/spree/product_spec.rb +474 -0
  110. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +50 -0
  111. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +148 -0
  112. data/spec/models/spree/promotion/actions/create_line_items_spec.rb +86 -0
  113. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +36 -0
  114. data/spec/models/spree/promotion/rules/first_order_spec.rb +75 -0
  115. data/spec/models/spree/promotion/rules/item_total_spec.rb +282 -0
  116. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +42 -0
  117. data/spec/models/spree/promotion/rules/option_value_spec.rb +90 -0
  118. data/spec/models/spree/promotion/rules/product_spec.rb +143 -0
  119. data/spec/models/spree/promotion/rules/taxon_spec.rb +102 -0
  120. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +27 -0
  121. data/spec/models/spree/promotion/rules/user_spec.rb +37 -0
  122. data/spec/models/spree/promotion_action_spec.rb +10 -0
  123. data/spec/models/spree/promotion_category_spec.rb +17 -0
  124. data/spec/models/spree/promotion_handler/cart_spec.rb +102 -0
  125. data/spec/models/spree/promotion_handler/coupon_spec.rb +323 -0
  126. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +48 -0
  127. data/spec/models/spree/promotion_handler/page_spec.rb +44 -0
  128. data/spec/models/spree/promotion_rule_spec.rb +29 -0
  129. data/spec/models/spree/promotion_spec.rb +603 -0
  130. data/spec/models/spree/property_spec.rb +5 -0
  131. data/spec/models/spree/prototype_spec.rb +5 -0
  132. data/spec/models/spree/refund_spec.rb +195 -0
  133. data/spec/models/spree/reimbursement/credit_spec.rb +36 -0
  134. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +140 -0
  135. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +83 -0
  136. data/spec/models/spree/reimbursement_performer_spec.rb +30 -0
  137. data/spec/models/spree/reimbursement_spec.rb +215 -0
  138. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +51 -0
  139. data/spec/models/spree/reimbursement_type/credit_spec.rb +53 -0
  140. data/spec/models/spree/reimbursement_type/exchange_spec.rb +46 -0
  141. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +55 -0
  142. data/spec/models/spree/return_authorization_spec.rb +250 -0
  143. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +77 -0
  144. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +58 -0
  145. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +61 -0
  146. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +32 -0
  147. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +29 -0
  148. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +35 -0
  149. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +65 -0
  150. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +43 -0
  151. data/spec/models/spree/return_item_spec.rb +682 -0
  152. data/spec/models/spree/returns_calculator_spec.rb +14 -0
  153. data/spec/models/spree/shipment_spec.rb +740 -0
  154. data/spec/models/spree/shipping_calculator_spec.rb +45 -0
  155. data/spec/models/spree/shipping_category_spec.rb +5 -0
  156. data/spec/models/spree/shipping_method_spec.rb +88 -0
  157. data/spec/models/spree/shipping_rate_spec.rb +141 -0
  158. data/spec/models/spree/state_spec.rb +18 -0
  159. data/spec/models/spree/stock/availability_validator_spec.rb +36 -0
  160. data/spec/models/spree/stock/content_item_spec.rb +22 -0
  161. data/spec/models/spree/stock/coordinator_spec.rb +51 -0
  162. data/spec/models/spree/stock/differentiator_spec.rb +39 -0
  163. data/spec/models/spree/stock/estimator_spec.rb +154 -0
  164. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +38 -0
  165. data/spec/models/spree/stock/package_spec.rb +194 -0
  166. data/spec/models/spree/stock/packer_spec.rb +70 -0
  167. data/spec/models/spree/stock/prioritizer_spec.rb +125 -0
  168. data/spec/models/spree/stock/quantifier_spec.rb +97 -0
  169. data/spec/models/spree/stock/splitter/backordered_spec.rb +29 -0
  170. data/spec/models/spree/stock/splitter/base_spec.rb +21 -0
  171. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +47 -0
  172. data/spec/models/spree/stock/splitter/weight_spec.rb +32 -0
  173. data/spec/models/spree/stock_item_spec.rb +410 -0
  174. data/spec/models/spree/stock_location_spec.rb +243 -0
  175. data/spec/models/spree/stock_movement_spec.rb +56 -0
  176. data/spec/models/spree/stock_transfer_spec.rb +50 -0
  177. data/spec/models/spree/store_spec.rb +50 -0
  178. data/spec/models/spree/tax_category_spec.rb +27 -0
  179. data/spec/models/spree/tax_rate_spec.rb +382 -0
  180. data/spec/models/spree/taxon_spec.rb +74 -0
  181. data/spec/models/spree/taxonomy_spec.rb +18 -0
  182. data/spec/models/spree/tracker_spec.rb +21 -0
  183. data/spec/models/spree/user_spec.rb +130 -0
  184. data/spec/models/spree/validations/db_maximum_length_validator_spec.rb +24 -0
  185. data/spec/models/spree/variant_spec.rb +523 -0
  186. data/spec/models/spree/zone_spec.rb +444 -0
  187. data/spec/spec_helper.rb +74 -0
  188. data/spec/support/big_decimal.rb +5 -0
  189. data/spec/support/concerns/adjustment_source_spec.rb +23 -0
  190. data/spec/support/concerns/default_price_spec.rb +28 -0
  191. data/spec/support/rake.rb +13 -0
  192. data/spec/support/test_gateway.rb +2 -0
  193. data/spree_core.gemspec +48 -0
  194. metadata +185 -4
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'email_spec'
3
+
4
+ describe Spree::ReimbursementMailer, :type => :mailer do
5
+ include EmailSpec::Helpers
6
+ include EmailSpec::Matchers
7
+
8
+ let(:reimbursement) { create(:reimbursement) }
9
+
10
+ context ":from not set explicitly" do
11
+ it "falls back to spree config" do
12
+ message = Spree::ReimbursementMailer.reimbursement_email(reimbursement)
13
+ expect(message.from).to eq [Spree::Store.current.mail_from_address]
14
+ end
15
+ end
16
+
17
+ it "accepts a reimbursement id as an alternative to a Reimbursement object" do
18
+ expect(Spree::Reimbursement).to receive(:find).with(reimbursement.id).and_return(reimbursement)
19
+
20
+ expect {
21
+ Spree::ReimbursementMailer.reimbursement_email(reimbursement.id).body
22
+ }.not_to raise_error
23
+ end
24
+
25
+ context "emails must be translatable" do
26
+ context "reimbursement_email" do
27
+ context "pt-BR locale" do
28
+ before do
29
+ I18n.enforce_available_locales = false
30
+ pt_br_shipped_email = { :spree => { :reimbursement_mailer => { :reimbursement_email => { :dear_customer => 'Caro Cliente,' } } } }
31
+ I18n.backend.store_translations :'pt-BR', pt_br_shipped_email
32
+ I18n.locale = :'pt-BR'
33
+ end
34
+
35
+ after do
36
+ I18n.locale = I18n.default_locale
37
+ I18n.enforce_available_locales = true
38
+ end
39
+
40
+ specify do
41
+ reimbursement_email = Spree::ReimbursementMailer.reimbursement_email(reimbursement)
42
+ expect(reimbursement_email.body).to include("Caro Cliente,")
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+ require 'email_spec'
3
+
4
+ describe Spree::ShipmentMailer, :type => :mailer do
5
+ include EmailSpec::Helpers
6
+ include EmailSpec::Matchers
7
+
8
+ before { create(:store) }
9
+
10
+ let(:shipment) do
11
+ order = stub_model(Spree::Order)
12
+ product = stub_model(Spree::Product, :name => %Q{The "BEST" product})
13
+ variant = stub_model(Spree::Variant, :product => product)
14
+ line_item = stub_model(Spree::LineItem, :variant => variant, :order => order, :quantity => 1, :price => 5)
15
+ shipment = stub_model(Spree::Shipment)
16
+ allow(shipment).to receive_messages(:line_items => [line_item], :order => order)
17
+ allow(shipment).to receive_messages(:tracking_url => "TRACK_ME")
18
+ shipment
19
+ end
20
+
21
+ context ":from not set explicitly" do
22
+ it "falls back to spree config" do
23
+ message = Spree::ShipmentMailer.shipped_email(shipment)
24
+ expect(message.from).to eq([Spree::Store.current.mail_from_address])
25
+ end
26
+ end
27
+
28
+ # Regression test for #2196
29
+ it "doesn't include out of stock in the email body" do
30
+ shipment_email = Spree::ShipmentMailer.shipped_email(shipment)
31
+ expect(shipment_email.body).not_to include(%Q{Out of Stock})
32
+ end
33
+
34
+ it "shipment_email accepts an shipment id as an alternative to an Shipment object" do
35
+ expect(Spree::Shipment).to receive(:find).with(shipment.id).and_return(shipment)
36
+ expect {
37
+ Spree::ShipmentMailer.shipped_email(shipment.id).body
38
+ }.not_to raise_error
39
+ end
40
+
41
+ context "emails must be translatable" do
42
+ context "shipped_email" do
43
+ context "pt-BR locale" do
44
+ before do
45
+ I18n.enforce_available_locales = false
46
+ pt_br_shipped_email = { :spree => { :shipment_mailer => { :shipped_email => { :dear_customer => 'Caro Cliente,' } } } }
47
+ I18n.backend.store_translations :'pt-BR', pt_br_shipped_email
48
+ I18n.locale = :'pt-BR'
49
+ end
50
+
51
+ after do
52
+ I18n.locale = I18n.default_locale
53
+ I18n.enforce_available_locales = true
54
+ end
55
+
56
+ specify do
57
+ shipped_email = Spree::ShipmentMailer.shipped_email(shipment)
58
+ expect(shipped_email).to have_body_text("Caro Cliente,")
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'email_spec'
3
+
4
+ describe Spree::TestMailer, :type => :mailer do
5
+ include EmailSpec::Helpers
6
+ include EmailSpec::Matchers
7
+
8
+ before { create(:store) }
9
+
10
+ let(:user) { create(:user) }
11
+
12
+ context ":from not set explicitly" do
13
+ it "falls back to spree config" do
14
+ message = Spree::TestMailer.test_email('test@example.com')
15
+ expect(message.from).to eq([Spree::Store.current.mail_from_address])
16
+ end
17
+ end
18
+
19
+ it "confirm_email accepts a user id as an alternative to a User object" do
20
+ expect {
21
+ test_email = Spree::TestMailer.test_email('test@example.com')
22
+ }.not_to raise_error
23
+ end
24
+ end
@@ -0,0 +1,246 @@
1
+ require 'spec_helper'
2
+ require 'cancan/matchers'
3
+ require 'spree/testing_support/ability_helpers'
4
+ require 'spree/testing_support/bar_ability'
5
+
6
+ # Fake ability for testing registration of additional abilities
7
+ class FooAbility
8
+ include CanCan::Ability
9
+
10
+ def initialize(user)
11
+ # allow anyone to perform index on Order
12
+ can :index, Spree::Order
13
+ # allow anyone to update an Order with id of 1
14
+ can :update, Spree::Order do |order|
15
+ order.id == 1
16
+ end
17
+ end
18
+ end
19
+
20
+ describe Spree::Ability, :type => :model do
21
+ let(:user) { create(:user) }
22
+ let(:ability) { Spree::Ability.new(user) }
23
+ let(:token) { nil }
24
+
25
+ before do
26
+ user.spree_roles.clear
27
+ end
28
+
29
+ TOKEN = 'token123'
30
+
31
+ after(:each) {
32
+ Spree::Ability.abilities = Set.new
33
+ user.spree_roles = []
34
+ }
35
+
36
+ context 'register_ability' do
37
+ it 'should add the ability to the list of abilties' do
38
+ Spree::Ability.register_ability(FooAbility)
39
+ expect(Spree::Ability.new(user).abilities).not_to be_empty
40
+ end
41
+
42
+ it 'should apply the registered abilities permissions' do
43
+ Spree::Ability.register_ability(FooAbility)
44
+ expect(Spree::Ability.new(user).can?(:update, mock_model(Spree::Order, :id => 1))).to be true
45
+ end
46
+ end
47
+
48
+ context 'for general resource' do
49
+ let(:resource) { Object.new }
50
+
51
+ context 'with admin user' do
52
+ before(:each) { allow(user).to receive(:has_spree_role?).and_return(true) }
53
+ it_should_behave_like 'access granted'
54
+ it_should_behave_like 'index allowed'
55
+ end
56
+
57
+ context 'with customer' do
58
+ it_should_behave_like 'access denied'
59
+ it_should_behave_like 'no index allowed'
60
+ end
61
+ end
62
+
63
+ context 'for admin protected resources' do
64
+ let(:resource) { Object.new }
65
+ let(:resource_shipment) { Spree::Shipment.new }
66
+ let(:resource_product) { Spree::Product.new }
67
+ let(:resource_user) { Spree.user_class.new }
68
+ let(:resource_order) { Spree::Order.new }
69
+ let(:fakedispatch_user) { Spree.user_class.new }
70
+ let(:fakedispatch_ability) { Spree::Ability.new(fakedispatch_user) }
71
+
72
+ context 'with admin user' do
73
+ it 'should be able to admin' do
74
+ user.spree_roles << Spree::Role.find_or_create_by(name: 'admin')
75
+ expect(ability).to be_able_to :admin, resource
76
+ expect(ability).to be_able_to :index, resource_order
77
+ expect(ability).to be_able_to :show, resource_product
78
+ expect(ability).to be_able_to :create, resource_user
79
+ end
80
+ end
81
+
82
+ context 'with fakedispatch user' do
83
+ it 'should be able to admin on the order and shipment pages' do
84
+ user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
85
+
86
+ Spree::Ability.register_ability(BarAbility)
87
+
88
+ expect(ability).not_to be_able_to :admin, resource
89
+
90
+ expect(ability).to be_able_to :admin, resource_order
91
+ expect(ability).to be_able_to :index, resource_order
92
+ expect(ability).not_to be_able_to :update, resource_order
93
+ # ability.should_not be_able_to :create, resource_order # Fails
94
+
95
+ expect(ability).to be_able_to :admin, resource_shipment
96
+ expect(ability).to be_able_to :index, resource_shipment
97
+ expect(ability).to be_able_to :create, resource_shipment
98
+
99
+ expect(ability).not_to be_able_to :admin, resource_product
100
+ expect(ability).not_to be_able_to :update, resource_product
101
+ # ability.should_not be_able_to :show, resource_product # Fails
102
+
103
+ expect(ability).not_to be_able_to :admin, resource_user
104
+ expect(ability).not_to be_able_to :update, resource_user
105
+ expect(ability).to be_able_to :update, user
106
+ # ability.should_not be_able_to :create, resource_user # Fails
107
+ # It can create new users if is has access to the :admin, User!!
108
+
109
+ # TODO change the Ability class so only users and customers get the extra premissions?
110
+
111
+ Spree::Ability.remove_ability(BarAbility)
112
+ end
113
+ end
114
+
115
+ context 'with customer' do
116
+ it 'should not be able to admin' do
117
+ expect(ability).not_to be_able_to :admin, resource
118
+ expect(ability).not_to be_able_to :admin, resource_order
119
+ expect(ability).not_to be_able_to :admin, resource_product
120
+ expect(ability).not_to be_able_to :admin, resource_user
121
+ end
122
+ end
123
+ end
124
+
125
+ context 'as Guest User' do
126
+
127
+ context 'for Country' do
128
+ let(:resource) { Spree::Country.new }
129
+ context 'requested by any user' do
130
+ it_should_behave_like 'read only'
131
+ end
132
+ end
133
+
134
+ context 'for OptionType' do
135
+ let(:resource) { Spree::OptionType.new }
136
+ context 'requested by any user' do
137
+ it_should_behave_like 'read only'
138
+ end
139
+ end
140
+
141
+ context 'for OptionValue' do
142
+ let(:resource) { Spree::OptionType.new }
143
+ context 'requested by any user' do
144
+ it_should_behave_like 'read only'
145
+ end
146
+ end
147
+
148
+ context 'for Order' do
149
+ let(:resource) { Spree::Order.new }
150
+
151
+ context 'requested by same user' do
152
+ before(:each) { resource.user = user }
153
+ it_should_behave_like 'access granted'
154
+ it_should_behave_like 'no index allowed'
155
+ end
156
+
157
+ context 'requested by other user' do
158
+ before(:each) { resource.user = Spree.user_class.new }
159
+ it_should_behave_like 'create only'
160
+ end
161
+
162
+ context 'requested with proper token' do
163
+ let(:token) { 'TOKEN123' }
164
+ before(:each) { allow(resource).to receive_messages guest_token: 'TOKEN123' }
165
+ it_should_behave_like 'access granted'
166
+ it_should_behave_like 'no index allowed'
167
+ end
168
+
169
+ context 'requested with inproper token' do
170
+ let(:token) { 'FAIL' }
171
+ before(:each) { allow(resource).to receive_messages guest_token: 'TOKEN123' }
172
+ it_should_behave_like 'create only'
173
+ end
174
+ end
175
+
176
+ context 'for Product' do
177
+ let(:resource) { Spree::Product.new }
178
+ context 'requested by any user' do
179
+ it_should_behave_like 'read only'
180
+ end
181
+ end
182
+
183
+ context 'for ProductProperty' do
184
+ let(:resource) { Spree::Product.new }
185
+ context 'requested by any user' do
186
+ it_should_behave_like 'read only'
187
+ end
188
+ end
189
+
190
+ context 'for Property' do
191
+ let(:resource) { Spree::Product.new }
192
+ context 'requested by any user' do
193
+ it_should_behave_like 'read only'
194
+ end
195
+ end
196
+
197
+ context 'for State' do
198
+ let(:resource) { Spree::State.new }
199
+ context 'requested by any user' do
200
+ it_should_behave_like 'read only'
201
+ end
202
+ end
203
+
204
+ context 'for Taxons' do
205
+ let(:resource) { Spree::Taxon.new }
206
+ context 'requested by any user' do
207
+ it_should_behave_like 'read only'
208
+ end
209
+ end
210
+
211
+ context 'for Taxonomy' do
212
+ let(:resource) { Spree::Taxonomy.new }
213
+ context 'requested by any user' do
214
+ it_should_behave_like 'read only'
215
+ end
216
+ end
217
+
218
+ context 'for User' do
219
+ context 'requested by same user' do
220
+ let(:resource) { user }
221
+ it_should_behave_like 'access granted'
222
+ it_should_behave_like 'no index allowed'
223
+ end
224
+ context 'requested by other user' do
225
+ let(:resource) { Spree.user_class.new }
226
+ it_should_behave_like 'create only'
227
+ end
228
+ end
229
+
230
+ context 'for Variant' do
231
+ let(:resource) { Spree::Variant.new }
232
+ context 'requested by any user' do
233
+ it_should_behave_like 'read only'
234
+ end
235
+ end
236
+
237
+ context 'for Zone' do
238
+ let(:resource) { Spree::Zone.new }
239
+ context 'requested by any user' do
240
+ it_should_behave_like 'read only'
241
+ end
242
+ end
243
+
244
+ end
245
+
246
+ end
@@ -0,0 +1,291 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Address, :type => :model do
4
+
5
+ subject { Spree::Address }
6
+
7
+ describe "clone" do
8
+ it "creates a copy of the address with the exception of the id, updated_at and created_at attributes" do
9
+ state = create(:state)
10
+ original = create(:address,
11
+ :address1 => 'address1',
12
+ :address2 => 'address2',
13
+ :alternative_phone => 'alternative_phone',
14
+ :city => 'city',
15
+ :country => Spree::Country.first,
16
+ :firstname => 'firstname',
17
+ :lastname => 'lastname',
18
+ :company => 'company',
19
+ :phone => 'phone',
20
+ :state_id => state.id,
21
+ :state_name => state.name,
22
+ :zipcode => '10001')
23
+
24
+ cloned = original.clone
25
+
26
+ expect(cloned.address1).to eq(original.address1)
27
+ expect(cloned.address2).to eq(original.address2)
28
+ expect(cloned.alternative_phone).to eq(original.alternative_phone)
29
+ expect(cloned.city).to eq(original.city)
30
+ expect(cloned.country_id).to eq(original.country_id)
31
+ expect(cloned.firstname).to eq(original.firstname)
32
+ expect(cloned.lastname).to eq(original.lastname)
33
+ expect(cloned.company).to eq(original.company)
34
+ expect(cloned.phone).to eq(original.phone)
35
+ expect(cloned.state_id).to eq(original.state_id)
36
+ expect(cloned.state_name).to eq(original.state_name)
37
+ expect(cloned.zipcode).to eq(original.zipcode)
38
+
39
+ expect(cloned.id).not_to eq(original.id)
40
+ expect(cloned.created_at).not_to eq(original.created_at)
41
+ expect(cloned.updated_at).not_to eq(original.updated_at)
42
+ end
43
+ end
44
+
45
+ context "aliased attributes" do
46
+ let(:address) { Spree::Address.new }
47
+
48
+ it "first_name" do
49
+ address.firstname = "Ryan"
50
+ expect(address.first_name).to eq("Ryan")
51
+ end
52
+
53
+ it "last_name" do
54
+ address.lastname = "Bigg"
55
+ expect(address.last_name).to eq("Bigg")
56
+ end
57
+ end
58
+
59
+ context "validation" do
60
+ before do
61
+ configure_spree_preferences do |config|
62
+ config.address_requires_state = true
63
+ end
64
+ end
65
+
66
+ let(:country) { mock_model(Spree::Country, :states => [state], :states_required => true) }
67
+ let(:state) { stub_model(Spree::State, :name => 'maryland', :abbr => 'md') }
68
+ let(:address) { build(:address, :country => country) }
69
+
70
+ before do
71
+ allow(country.states).to receive_messages :find_all_by_name_or_abbr => [state]
72
+ end
73
+
74
+ it "state_name is not nil and country does not have any states" do
75
+ address.state = nil
76
+ address.state_name = 'alabama'
77
+ expect(address).to be_valid
78
+ end
79
+
80
+ it "errors when state_name is nil" do
81
+ address.state_name = nil
82
+ address.state = nil
83
+ expect(address).not_to be_valid
84
+ end
85
+
86
+ it "full state name is in state_name and country does contain that state" do
87
+ address.state_name = 'alabama'
88
+ # called by state_validate to set up state_id.
89
+ # Perhaps this should be a before_validation instead?
90
+ expect(address).to be_valid
91
+ expect(address.state).not_to be_nil
92
+ expect(address.state_name).to be_nil
93
+ end
94
+
95
+ it "state abbr is in state_name and country does contain that state" do
96
+ address.state_name = state.abbr
97
+ expect(address).to be_valid
98
+ expect(address.state_id).not_to be_nil
99
+ expect(address.state_name).to be_nil
100
+ end
101
+
102
+ it "state is entered but country does not contain that state" do
103
+ address.state = state
104
+ address.country = stub_model(Spree::Country, :states_required => true)
105
+ address.valid?
106
+ expect(address.errors["state"]).to eq(['is invalid'])
107
+ end
108
+
109
+ it "both state and state_name are entered but country does not contain the state" do
110
+ address.state = state
111
+ address.state_name = 'maryland'
112
+ address.country = stub_model(Spree::Country, :states_required => true)
113
+ expect(address).to be_valid
114
+ expect(address.state_id).to be_nil
115
+ end
116
+
117
+ it "both state and state_name are entered and country does contain the state" do
118
+ address.state = state
119
+ address.state_name = 'maryland'
120
+ expect(address).to be_valid
121
+ expect(address.state_name).to be_nil
122
+ end
123
+
124
+ it "address_requires_state preference is false" do
125
+ Spree::Config.set :address_requires_state => false
126
+ address.state = nil
127
+ address.state_name = nil
128
+ expect(address).to be_valid
129
+ end
130
+
131
+ it "requires phone" do
132
+ address.phone = ""
133
+ address.valid?
134
+ expect(address.errors["phone"]).to eq(["can't be blank"])
135
+ end
136
+
137
+ it "requires zipcode" do
138
+ address.zipcode = ""
139
+ address.valid?
140
+ expect(address.errors['zipcode']).to include("can't be blank")
141
+ end
142
+
143
+ context "zipcode validation" do
144
+ it "validates the zipcode" do
145
+ allow(address.country).to receive(:iso).and_return('US')
146
+ address.zipcode = 'abc'
147
+ address.valid?
148
+ expect(address.errors['zipcode']).to include('is invalid')
149
+ end
150
+
151
+ it "accepts a zip code with surrounding white space" do
152
+ allow(address.country).to receive(:iso).and_return('US')
153
+ address.zipcode = ' 12345 '
154
+ address.valid?
155
+ expect(address.errors['zipcode']).not_to include('is invalid')
156
+ end
157
+
158
+ context 'does not validate' do
159
+ it 'does not have a country' do
160
+ address.country = nil
161
+ address.valid?
162
+ expect(address.errors['zipcode']).not_to include('is invalid')
163
+ end
164
+
165
+ it 'does not have an iso' do
166
+ allow(address.country).to receive(:iso).and_return(nil)
167
+ address.valid?
168
+ expect(address.errors['zipcode']).not_to include('is invalid')
169
+ end
170
+
171
+ it 'does not have a zipcode' do
172
+ address.zipcode = ""
173
+ address.valid?
174
+ expect(address.errors['zipcode']).not_to include('is invalid')
175
+ end
176
+
177
+ it 'does not have a supported country iso' do
178
+ allow(address.country).to receive(:iso).and_return('BO')
179
+ address.valid?
180
+ expect(address.errors['zipcode']).not_to include('is invalid')
181
+ end
182
+ end
183
+ end
184
+
185
+ context "phone not required" do
186
+ before { allow(address).to receive_messages require_phone?: false }
187
+
188
+ it "shows no errors when phone is blank" do
189
+ address.phone = ""
190
+ address.valid?
191
+ expect(address.errors[:phone].size).to eq 0
192
+ end
193
+ end
194
+
195
+ context "zipcode not required" do
196
+ before { allow(address).to receive_messages require_zipcode?: false }
197
+
198
+ it "shows no errors when phone is blank" do
199
+ address.zipcode = ""
200
+ address.valid?
201
+ expect(address.errors[:zipcode].size).to eq 0
202
+ end
203
+ end
204
+ end
205
+
206
+ context ".default" do
207
+ context "no user given" do
208
+ before do
209
+ @default_country_id = Spree::Config[:default_country_id]
210
+ new_country = create(:country)
211
+ Spree::Config[:default_country_id] = new_country.id
212
+ end
213
+
214
+ after do
215
+ Spree::Config[:default_country_id] = @default_country_id
216
+ end
217
+
218
+ it "sets up a new record with Spree::Config[:default_country_id]" do
219
+ expect(Spree::Address.default.country).to eq(Spree::Country.find(Spree::Config[:default_country_id]))
220
+ end
221
+
222
+ # Regression test for #1142
223
+ it "uses the first available country if :default_country_id is set to an invalid value" do
224
+ Spree::Config[:default_country_id] = "0"
225
+ expect(Spree::Address.default.country).to eq(Spree::Country.first)
226
+ end
227
+ end
228
+
229
+ context "user given" do
230
+ let(:bill_address) { Spree::Address.new(phone: Time.now.to_i) }
231
+ let(:ship_address) { double("ShipAddress") }
232
+ let(:user) { double("User", bill_address: bill_address, ship_address: ship_address) }
233
+
234
+ it "returns a copy of that user bill address" do
235
+ expect(subject.default(user).phone).to eq bill_address.phone
236
+ end
237
+
238
+ it "falls back to build default when user has no address" do
239
+ allow(user).to receive_messages(bill_address: nil)
240
+ expect(subject.default(user)).to eq subject.build_default
241
+ end
242
+ end
243
+ end
244
+
245
+ context '#full_name' do
246
+ context 'both first and last names are present' do
247
+ let(:address) { stub_model(Spree::Address, :firstname => 'Michael', :lastname => 'Jackson') }
248
+ specify { expect(address.full_name).to eq('Michael Jackson') }
249
+ end
250
+
251
+ context 'first name is blank' do
252
+ let(:address) { stub_model(Spree::Address, :firstname => nil, :lastname => 'Jackson') }
253
+ specify { expect(address.full_name).to eq('Jackson') }
254
+ end
255
+
256
+ context 'last name is blank' do
257
+ let(:address) { stub_model(Spree::Address, :firstname => 'Michael', :lastname => nil) }
258
+ specify { expect(address.full_name).to eq('Michael') }
259
+ end
260
+
261
+ context 'both first and last names are blank' do
262
+ let(:address) { stub_model(Spree::Address, :firstname => nil, :lastname => nil) }
263
+ specify { expect(address.full_name).to eq('') }
264
+ end
265
+
266
+ end
267
+
268
+ context '#state_text' do
269
+ context 'state is blank' do
270
+ let(:address) { stub_model(Spree::Address, :state => nil, :state_name => 'virginia') }
271
+ specify { expect(address.state_text).to eq('virginia') }
272
+ end
273
+
274
+ context 'both name and abbr is present' do
275
+ let(:state) { stub_model(Spree::State, :name => 'virginia', :abbr => 'va') }
276
+ let(:address) { stub_model(Spree::Address, :state => state) }
277
+ specify { expect(address.state_text).to eq('va') }
278
+ end
279
+
280
+ context 'only name is present' do
281
+ let(:state) { stub_model(Spree::State, :name => 'virginia', :abbr => nil) }
282
+ let(:address) { stub_model(Spree::Address, :state => state) }
283
+ specify { expect(address.state_text).to eq('virginia') }
284
+ end
285
+ end
286
+
287
+ context "defines require_phone? helper method" do
288
+ let(:address) { stub_model(Spree::Address) }
289
+ specify { expect(address.instance_eval{ require_phone? }).to be true}
290
+ end
291
+ end