muck-commerce 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. data/README.rdoc +6 -0
  2. data/VERSION +1 -1
  3. data/app/controllers/muck/billing_informations_controller.rb +1 -1
  4. data/app/controllers/muck/orders_controller.rb +22 -10
  5. data/app/controllers/muck/subscriptions_controller.rb +1 -1
  6. data/db/migrate/20100408035338_move_customer_profile_id.rb +18 -0
  7. data/lib/action_controller/muck_billing_application.rb +2 -2
  8. data/lib/active_record/acts/muck_billing_information.rb +2 -2
  9. data/lib/active_record/acts/muck_cart_item.rb +38 -0
  10. data/lib/active_record/acts/muck_commerce_user.rb +5 -0
  11. data/lib/active_record/acts/muck_order.rb +5 -3
  12. data/lib/active_record/acts/muck_order_transaction.rb +117 -73
  13. data/lib/test/muck_commerce_factories.rb +26 -17
  14. data/locales/en.yml +6 -1
  15. data/muck-commerce.gemspec +10 -2
  16. data/test/rails_root/app/models/billing_information.rb +36 -1
  17. data/test/rails_root/app/models/cart.rb +12 -0
  18. data/test/rails_root/app/models/cart_coupon.rb +11 -0
  19. data/test/rails_root/app/models/coupon.rb +21 -1
  20. data/test/rails_root/app/models/coupon_type.rb +9 -1
  21. data/test/rails_root/app/models/order.rb +21 -1
  22. data/test/rails_root/app/models/order_coupon.rb +12 -1
  23. data/test/rails_root/app/models/order_transaction.rb +20 -1
  24. data/test/rails_root/app/models/profile.rb +23 -0
  25. data/test/rails_root/app/models/subscription.rb +22 -1
  26. data/test/rails_root/app/models/subscription_plan.rb +15 -1
  27. data/test/rails_root/app/models/user.rb +33 -1
  28. data/test/rails_root/config/environment.rb +1 -1
  29. data/test/rails_root/db/migrate/20100408035338_move_customer_profile_id.rb +18 -0
  30. data/test/rails_root/remote/functional/remote_orders_controller_test.rb +58 -58
  31. data/test/rails_root/remote/functional/remote_subscriptions_controller_test.rb +9 -0
  32. data/test/rails_root/remote/remote_helper.rb +44 -15
  33. data/test/rails_root/remote/unit/remote_order_test.rb +7 -8
  34. data/test/rails_root/remote/unit/remote_order_transaction_test.rb +60 -34
  35. data/test/rails_root/test/functional/admin/billing_informations_controller_test.rb +7 -8
  36. data/test/rails_root/test/unit/billing_information_test.rb +78 -0
  37. data/test/rails_root/test/unit/cart_coupon_test.rb +12 -1
  38. data/test/rails_root/test/unit/coupon_test.rb +21 -1
  39. data/test/rails_root/test/unit/coupon_type_test.rb +9 -1
  40. data/test/rails_root/test/unit/order_test.rb +21 -1
  41. data/test/rails_root/test/unit/order_transaction_test.rb +19 -0
  42. data/test/rails_root/test/unit/subscription_test.rb +40 -9
  43. data/test/rails_root/test/unit/user_test.rb +33 -1
  44. metadata +12 -4
@@ -1,4 +1,4 @@
1
- RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
1
+ RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
2
2
 
3
3
  require File.join(File.dirname(__FILE__), 'boot')
4
4
 
@@ -0,0 +1,18 @@
1
+ class MoveCustomerProfileId < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :customer_profile_id, :string, :limit => 512
4
+ BillingInformation.all.each do |bi|
5
+ bi.billable.update_attribute(:customer_profile_id, bi.customer_profile_id)
6
+ end
7
+ remove_column :billing_informations, :customer_profile_id
8
+ end
9
+
10
+ def self.down
11
+ add_column :billing_informations, :customer_profile_id, :string, :limit => 512
12
+ BillingInformation.all.each do |bi|
13
+ bi.update_attribute(:customer_profile_id, bi.billable.customer_profile_id)
14
+ end
15
+ remove_column :users, :customer_profile_id
16
+ end
17
+
18
+ end
@@ -7,28 +7,38 @@ class RemoteOrdersControllerTest < ActionController::TestCase
7
7
  should_require_login :index => :get, :login_url => '/login'
8
8
 
9
9
  context "orders controller" do
10
+
10
11
  setup do
11
12
  MuckCommerce::Configure.enable_live_test_gateway
12
13
  end
13
-
14
+
15
+ teardown do
16
+ MuckCommerce::Configure.disable_live_test_gateway
17
+ end
18
+
14
19
  context "logged in without billing information" do
15
-
20
+
16
21
  setup do
17
22
  @user = Factory(:user)
18
23
  populate_cart(@user)
24
+ activate_authlogic
19
25
  login_as @user
20
26
  end
21
-
27
+
22
28
  context "POST create order with new billing information" do
23
29
  setup do
24
30
  post_create_live_billing
25
31
  end
26
- should_redirect_to("user_order_path(@user, assigns['order'])") { user_order_path(@user, assigns['order']) }
32
+ teardown do
33
+ cleanup_cim(assigns(:billing_information))
34
+ end
35
+ should_not_set_the_flash
36
+ should_redirect_to("new order path") { user_order_path(@user, assigns(:order), :new_order => true) }
27
37
  should "not have errors on the billing object" do
28
- assert assigns['billing'].errors.empty?, assigns['billing'].errors.full_messages.to_sentence
38
+ assert assigns(:billing_information).errors.empty?, assigns(:billing_information).errors.full_messages.to_sentence
29
39
  end
30
40
  end
31
-
41
+
32
42
  context "POST create with bad verification value" do
33
43
  setup do
34
44
  post_create_live_billing(:verification_value => '')
@@ -36,10 +46,10 @@ class RemoteOrdersControllerTest < ActionController::TestCase
36
46
  should_respond_with :success
37
47
  should_render_template :new
38
48
  should "have errors on the billing object" do
39
- assert assigns['billing'].errors.on(:verification_value), assigns['billing'].errors.full_messages.to_sentence
49
+ assert assigns(:billing_information).errors.on(:verification_value), assigns(:billing_information).errors.full_messages.to_sentence
40
50
  end
41
51
  end
42
-
52
+
43
53
  context "POST create with bad credit card" do
44
54
  setup do
45
55
  post_create_live_billing(:credit_card_number => '')
@@ -47,85 +57,75 @@ class RemoteOrdersControllerTest < ActionController::TestCase
47
57
  should_respond_with :success
48
58
  should_render_template :new
49
59
  should "have errors on the billing object" do
50
- assert assigns['billing'].errors.on(:credit_card_number), assigns['billing'].errors.full_messages.to_sentence
60
+ assert assigns(:billing_information).errors.on(:credit_card_number), assigns(:billing_information).errors.full_messages.to_sentence
51
61
  end
52
62
  end
53
-
63
+
54
64
  end
55
-
65
+
56
66
  context "user logged in with existing billing information" do
57
67
 
58
68
  setup do
59
- @user = Factory(:user)
69
+ @user, @billing_information = setup_live_user_and_billing
70
+ # send billing info to the CIM
71
+ response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) }
72
+ assert response.success?, response.message
73
+ assert @user.customer_profile_id
74
+ assert @billing_information.customer_payment_profile_id
75
+ assert @billing_information.has_billing_profile?
76
+ @user.order_transactions.push(response)
77
+ @user.reload
60
78
  populate_cart(@user)
61
- @billing = Factory.build(:live_billing_information, :billable => @user)
62
- @user.billing_informations << @billing
63
- @billing.vault_id = '1234'
64
- @billing.save!
65
-
79
+ activate_authlogic
66
80
  login_as @user
67
81
  end
68
-
82
+ teardown do
83
+ cleanup_cim(@billing_information)
84
+ end
85
+
69
86
  should "assign billing information" do
70
- assert @billing.billable == @user, "Billing user is #{@billing.billable.login} but user is #{@user}"
87
+ assert @billing_information.billable == @user, "Billing user is #{@billing_information.billable.login} but user is #{@user}"
71
88
  end
72
-
89
+
73
90
  context "POST create order with existing billing information" do
74
91
  setup do
75
- post :create
92
+ try_cim_post do
93
+ post :create, :amount => 1000
94
+ end
95
+ end
96
+ should_not_set_the_flash
97
+ should "have valid CIM profile ids" do
98
+ assert @user.billing_information, "User does not have any billing information."
99
+ assert @billing_information.has_billing_profile?
76
100
  end
77
- should_redirect_to("user_order_path(@user, assigns['order'])") { user_order_path(@user, assigns['order']) }
101
+ should_redirect_to("user order path") { user_order_path(@user, assigns(:order), :new_order => true) }
78
102
  should "not have errors on the billing object" do
79
- assert assigns['billing'].errors.empty?, assigns['billing'].errors.full_messages.to_sentence
103
+ assert assigns(:billing_information).errors.empty?, assigns(:billing_information).errors.full_messages.to_sentence
80
104
  end
81
105
  end
82
-
106
+
83
107
  context "POST create order with updated billing information" do
84
108
  setup do
85
- post :create, :billing_information => {
86
- :first_name => 'test',
87
- :last_name => 'guy',
88
- :address1 => 'P.O. Box 377',
89
- :city => 'Somewhere',
90
- :state_id => Factory(:state).id,
91
- :zip => 99999,
92
- :country_id => Factory(:country).id,
93
- :telephone => '777-777-7777',
94
- :payment_method => 'CC',
95
- :credit_card_type => 'visa',
96
- :credit_card_number => MuckCommerceTestDefinitions::VISA,
97
- :verification_value => '999',
98
- :credit_card_expiration => Time.now },
99
- :changed_billing => 'true'
109
+ post_create_live_billing({:telephone => '777-555-2222'}, true)
110
+ end
111
+ teardown do
112
+ cleanup_cim(assigns(:billing_information))
100
113
  end
101
- should_redirect_to("user order path") { user_order_path(assigns(:user), assigns(:order)) }
114
+ should_not_set_the_flash
115
+ should_redirect_to("user order path") { user_order_path(assigns(:user), assigns(:order), :new_order => true) }
102
116
  should "not have errors on the billing object" do
103
- assert assigns['billing_information'].errors.empty?, assigns['billing_information'].errors.full_messages.to_sentence
117
+ assert assigns(:billing_information).errors.empty?, assigns(:billing_information).errors.full_messages.to_sentence
104
118
  end
105
119
  end
106
-
120
+
107
121
  context "POST create order with bad credit card" do
108
122
  setup do
109
- post :create, :billing => {
110
- :first_name => 'test',
111
- :last_name => 'guy',
112
- :address1 => 'P.O. Box 377',
113
- :city => 'Somewhere',
114
- :state_id => Factory(:state).id,
115
- :zip => 99999,
116
- :country_id => Factory(:country).id,
117
- :telephone => '777-777-7777',
118
- :payment_method => GlobalConfig.payment_methods[0][1],
119
- :credit_card_type => 'bogus',
120
- :credit_card_number => '2232',
121
- :verification_value => '999',
122
- :credit_card_expiration => Time.now },
123
- :changed_billing => 'true'
123
+ post_create_live_billing({:credit_card_type => 'bogus', :credit_card_number => '2232'}, true)
124
124
  end
125
125
  should_respond_with :success
126
126
  should_render_template :new
127
127
  should "have errors on the billing object" do
128
- assert assigns['billing'].errors.on(:credit_card_number), assigns['billing'].errors.full_messages.to_sentence
128
+ assert assigns(:billing_information).errors.on(:credit_card_number), assigns(:billing_information).errors.full_messages.to_sentence
129
129
  end
130
130
  end
131
131
 
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/../remote_helper'
2
+
3
+ class RemoteSubscriptionsControllerTest < ActionController::TestCase
4
+
5
+ tests Muck::SubscriptionsController
6
+
7
+ should_require_login :edit => :get, :update => :put, :destroy => :delete, :login_url => '/login'
8
+
9
+ end
@@ -1,20 +1,49 @@
1
1
  require File.dirname(__FILE__) + '/../test/test_helper'
2
2
 
3
- def post_create_live_billing(options = {})
4
- post :create, :billing => {
5
- :first_name => 'test',
6
- :last_name => 'guy',
7
- :address1 => '33 commerce way',
8
- :city => 'San Jose',
9
- :state_id => Factory(:state).id,
10
- :zip => 99999,
11
- :country_id => Factory(:country).id,
12
- :telephone => '777-777-7777',
13
- :payment_method => 'CC',
14
- :credit_card_type => 'visa',
15
- :credit_card_number => MuckCommerceTestDefinitions::PAYPAL_VISA,
16
- :verification_value => '999',
17
- :credit_card_expiration => Time.now }.merge(options)
3
+ def post_create_live_billing(options = {}, changed_billing = false, amount = 1000)
4
+ try_cim_post do
5
+ post :create, :billing_information => Factory.attributes_for(:live_billing_information).merge(options),
6
+ :changed_billing => changed_billing,
7
+ :amount => amount
8
+ end
9
+ end
10
+
11
+ # Once a customer is setup in the CIM their record remains. This can cause problems with the
12
+ # tests if a CIM user is not deleted for some reason. This method will attempt to find
13
+ # and delete a CIM user if needed after which the user is again setup.
14
+ def try_cim(user, billing_information)
15
+ response = yield(user, billing_information)
16
+ if !response.success?
17
+ if response.params['messages']['message']['code'] == 'E00039'
18
+ billing_information.billable.customer_profile_id = parse_customer_profile_id(response.message)
19
+ response = OrderTransaction.cim_delete(billing_information.billable.customer_profile_id)
20
+ assert response.success?, response.message
21
+ response = yield(user, billing_information)
22
+ end
23
+ end
24
+ response
25
+ end
26
+
27
+ def try_cim_post
28
+ yield
29
+ if flash[:error] && flash[:error].include?('A duplicate record with id')
30
+ message = flash[:error]
31
+ flash.clear
32
+ response = OrderTransaction.cim_delete_by_customer_profile_id(parse_customer_profile_id(message))
33
+ assert response.success?, response.message
34
+ yield
35
+ end
36
+ end
37
+
38
+ def parse_customer_profile_id(message)
39
+ message[/\d+/]
40
+ end
41
+
42
+ def cleanup_cim(billing_information)
43
+ if billing_information.has_billing_profile?
44
+ response = OrderTransaction.cim_delete(billing_information.billable.customer_profile_id)
45
+ #assert response.success?, response.message
46
+ end
18
47
  end
19
48
 
20
49
  # Switch to paypal test gateway
@@ -17,15 +17,14 @@ class RemoteOrderTest < ActiveSupport::TestCase
17
17
  context "valid credit card" do
18
18
  setup do
19
19
  @order = Order.new
20
- @user, @billing = setup_live_user_and_billing
20
+ @user, @billing_information = setup_live_user_and_billing
21
21
  end
22
22
  teardown do
23
- # Have to get rid of records in the CIM
24
- OrderTransaction.cim_delete(@billing) if MuckCommerce::Configure.cim_enabled?
23
+ cleanup_cim(@billing_information)
25
24
  end
26
25
  should "succesfully checkout" do
27
26
  assert_difference "Order.count", 1 do
28
- success, purchase_made, purchase = @order.checkout(@billing, @amount, @coupons, @options)
27
+ success, purchase_made, purchase = @order.checkout(@billing_information, @amount, @coupons, @options)
29
28
  assert success
30
29
  assert purchase_made
31
30
  assert purchase.success?
@@ -41,14 +40,14 @@ class RemoteOrderTest < ActiveSupport::TestCase
41
40
  # In response the gateway will return declined instead of throwing an exception
42
41
  setup do
43
42
  @order = Order.new
44
- @user, @billing = setup_live_user_and_billing(:credit_card_number => MuckCommerceTestDefinitions::BRAIN_MASTERCARD)
43
+ @user, @billing_information = setup_live_user_and_billing(:credit_card_number => MuckCommerceTestDefinitions::BRAIN_MASTERCARD)
45
44
  end
46
45
 
47
46
  should "fail checkout" do
48
47
  assert_no_difference "Order.count" do
49
48
  assert_no_difference "OrderTransaction.count" do
50
49
  assert_raise(MuckCommerce::Exceptions::PaymentGatewayError, 'There was a problem processing your order') do
51
- success, purchase_made, purchase = @order.checkout(@billing, @amount, @coupons, @options)
50
+ success, purchase_made, purchase = @order.checkout(@billing_information, @amount, @coupons, @options)
52
51
  assert !success
53
52
  assert !purchase_made
54
53
  assert !purchase.success?
@@ -62,13 +61,13 @@ class RemoteOrderTest < ActiveSupport::TestCase
62
61
  context "exception credit card" do
63
62
  setup do
64
63
  @order = Order.new
65
- @user, @billing = setup_live_user_and_billing(:credit_card_number => MuckCommerceTestDefinitions::EXCEPTION_CARD)
64
+ @user, @billing_information = setup_live_user_and_billing(:credit_card_number => MuckCommerceTestDefinitions::EXCEPTION_CARD)
66
65
  end
67
66
 
68
67
  should "throw exception during checkout" do
69
68
  assert_no_difference "Order.count" do
70
69
  assert_raise(MuckCommerce::Exceptions::PaymentGatewayError, 'Invalid credit card information') do
71
- @order.checkout(@billing, @amount, @coupons, @options)
70
+ @order.checkout(@billing_information, @amount, @coupons, @options)
72
71
  end
73
72
  end
74
73
 
@@ -135,53 +135,92 @@ class RemoteOrderTransactionTest < ActiveSupport::TestCase
135
135
  context "management actions" do
136
136
  should " create and delete cim entry" do
137
137
  # create entry
138
- response = OrderTransaction.cim_create(@billing_information)
139
- puts response.message if !response.success?
140
- assert response.success?
141
- assert_equal @billing_information.customer_profile_id, response.reference
138
+ response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) }
139
+ assert response.success?, response.message
140
+ assert_equal @user.customer_profile_id, response.reference
142
141
  assert_equal @billing_information.customer_payment_profile_id, response.params["customer_payment_profile_id_list"]["numeric_string"]
143
142
  # delete entry
144
- response = OrderTransaction.cim_delete(@billing_information)
143
+ response = OrderTransaction.cim_delete(@billing_information.billable.customer_profile_id)
145
144
  assert response.success?, response.message
146
145
  end
147
146
  context "cim entry actions" do
148
147
  setup do
149
- response = OrderTransaction.cim_create(@billing_information)
148
+ response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) }
149
+ @customer_profile_id = response.params['customer_profile_id']
150
+ @customer_payment_profile_id = response.params["customer_payment_profile_id_list"]["numeric_string"]
150
151
  assert response.success?, response.message
152
+ assert @billing_information.has_billing_profile?
153
+ assert_equal @customer_profile_id, @billing_information.billable.customer_profile_id
154
+ assert_equal @customer_payment_profile_id, @billing_information.customer_payment_profile_id
151
155
  end
152
156
  teardown do
153
- response = OrderTransaction.cim_delete(@billing_information)
157
+ cleanup_cim(@billing_information)
154
158
  end
155
159
  should "update cim" do
156
160
  # update entry
157
- @billing_information.first_name = 'something'
158
- @billing_information.last_name = 'else'
159
- response = OrderTransaction.cim_update(@billing_information)
161
+ @user.email = 'somerandomthing@example.com'
162
+ response = OrderTransaction.cim_update(@user)
163
+ assert response.test?
164
+ assert response.success?, response.message
165
+ response = OrderTransaction.cim_get_customer_profile(@user.customer_profile_id)
166
+ assert response.test?
167
+ assert response.success?, response.message
168
+ assert_equal @user.email, response.params['profile']['email']
169
+ end
170
+ should "call cim_create_update and determine to update entry" do
171
+ # update entry
172
+ @billing_information.first_name = 'two'
173
+ @billing_information.last_name = 'first'
174
+ response = OrderTransaction.cim_create_update(@user, @billing_information)
160
175
  assert response.success?, response.message
176
+ response = OrderTransaction.cim_get_customer_profile(@user.customer_profile_id)
177
+ assert_equal @billing_information.first_name, response.params['profile']['payment_profiles']['bill_to']['first_name']
178
+ assert_equal @billing_information.last_name, response.params['profile']['payment_profiles']['bill_to']['last_name']
161
179
  end
162
180
  should "validate user cim profile" do
163
- response = OrderTransaction.cim_validate_user(@billing_information)
164
- puts response.message if !response.success?
181
+ assert_equal @user, @billing_information.billable
182
+ assert_equal @customer_profile_id, @billing_information.billable.customer_profile_id
183
+ assert_equal @customer_payment_profile_id, @billing_information.customer_payment_profile_id
184
+ response = OrderTransaction.cim_validate_customer_payment_profile(@billing_information)
185
+ assert response.success?, response.message
186
+ assert_equal I18n.translate('muck.commerce.cim_validate_profile'), response.action
187
+ end
188
+ should "get cim payment profile" do
189
+ response = OrderTransaction.cim_get_customer_payment_profile(@user.customer_profile_id, @billing_information.customer_payment_profile_id)
190
+ assert response.success?, response.message
191
+ assert response.params['payment_profile']
192
+ end
193
+ should "update cim payment profile" do
194
+ # update entry
195
+ @billing_information.first_name = 'payment'
196
+ @billing_information.last_name = 'update'
197
+ response = OrderTransaction.cim_update_payment_profile(@billing_information)
198
+ assert response.test?
199
+ assert response.success?, response.message
200
+ response = OrderTransaction.cim_get_customer_payment_profile(@user.customer_profile_id, @billing_information.customer_payment_profile_id)
201
+ assert response.test?
165
202
  assert response.success?, response.message
203
+ assert_equal @billing_information.first_name, response.params['payment_profile']['bill_to']['first_name']
204
+ assert_equal @billing_information.last_name, response.params['payment_profile']['bill_to']['last_name']
166
205
  end
167
206
  end
168
- should "determine whether to create or update entry" do
207
+ should "call cim_create_update and determine to create a new entry" do
169
208
  # create entry
170
- response = OrderTransaction.cim_create_update(@billing_information)
171
- assert response.success?
172
- # update entry
173
- @billing_information.first_name = 'something'
174
- @billing_information.last_name = 'else'
175
- response = OrderTransaction.cim_create_update(@billing_information)
209
+ assert !@billing_information.has_billing_profile?
210
+ response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create_update(user, billing_information) }
211
+ assert response.success?, response.message
212
+ response = OrderTransaction.cim_get_customer_profile(@user.customer_profile_id)
176
213
  assert response.success?, response.message
214
+ cleanup_cim(@billing_information)
177
215
  end
178
216
  end
179
217
  context "payment actions" do
180
218
  setup do
181
- response = OrderTransaction.cim_create(@billing_information)
219
+ response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) }
220
+ assert @billing_information.has_billing_profile?
182
221
  end
183
222
  teardown do
184
- response = OrderTransaction.cim_delete(@billing_information)
223
+ cleanup_cim(@billing_information)
185
224
  end
186
225
  should "purchase" do
187
226
  payment = OrderTransaction.cim_purchase(1234, @billing_information, @options)
@@ -194,19 +233,6 @@ class RemoteOrderTransactionTest < ActiveSupport::TestCase
194
233
  assert_equal I18n.translate('muck.commerce.authorization_cim'), payment.action
195
234
  end
196
235
  end
197
- context "validation" do
198
- setup do
199
- response = OrderTransaction.cim_create(@billing_information)
200
- end
201
- teardown do
202
- response = OrderTransaction.cim_delete(@billing_information)
203
- end
204
- should "validate the user" do
205
- response = OrderTransaction.cim_validate_user(@billing_information)
206
- assert response.success?
207
- assert_equal I18n.translate('muck.commerce.cim_validate_profile'), response.action
208
- end
209
- end
210
236
  end
211
237
 
212
238
  end