wordjelly-auth 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/lib/auth/version.rb +1 -1
  3. data/spec/auth_test.rb +7 -0
  4. data/spec/controllers/auth/admin_create_users_controller_spec.rb +141 -0
  5. data/spec/controllers/auth/clients_controller_spec.rb +223 -0
  6. data/spec/controllers/auth/search_controller_spec.rb +5 -0
  7. data/spec/controllers/auth/shopping/discounts_controller_spec.rb +54 -0
  8. data/spec/controllers/auth/users/profiles_controller_spec.rb +5 -0
  9. data/spec/dummy/README.rdoc +28 -0
  10. data/spec/dummy/Rakefile +6 -0
  11. data/spec/dummy/app/assets/javascripts/activity.js +2 -0
  12. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  13. data/spec/dummy/app/assets/javascripts/home.js +2 -0
  14. data/spec/dummy/app/assets/javascripts/pollymer.min.js +170 -0
  15. data/spec/dummy/app/assets/javascripts/tests.js +2 -0
  16. data/spec/dummy/app/assets/javascripts/topics.js +2 -0
  17. data/spec/dummy/app/assets/stylesheets/activity.css +4 -0
  18. data/spec/dummy/app/assets/stylesheets/application.scss +16 -0
  19. data/spec/dummy/app/assets/stylesheets/home.css +4 -0
  20. data/spec/dummy/app/assets/stylesheets/tests.css +4 -0
  21. data/spec/dummy/app/assets/stylesheets/topics.css +4 -0
  22. data/spec/dummy/app/controllers/activity_controller.rb +3 -0
  23. data/spec/dummy/app/controllers/admins/confirmations_controller.rb +28 -0
  24. data/spec/dummy/app/controllers/admins/omniauth_callbacks_controller.rb +28 -0
  25. data/spec/dummy/app/controllers/admins/passwords_controller.rb +32 -0
  26. data/spec/dummy/app/controllers/admins/registrations_controller.rb +60 -0
  27. data/spec/dummy/app/controllers/admins/sessions_controller.rb +5 -0
  28. data/spec/dummy/app/controllers/admins/unlocks_controller.rb +28 -0
  29. data/spec/dummy/app/controllers/application_controller.rb +49 -0
  30. data/spec/dummy/app/controllers/home_controller.rb +13 -0
  31. data/spec/dummy/app/controllers/otp_controller.rb +3 -0
  32. data/spec/dummy/app/controllers/shopping/cart_items_controller.rb +28 -0
  33. data/spec/dummy/app/controllers/shopping/carts_controller.rb +4 -0
  34. data/spec/dummy/app/controllers/shopping/discounts_controller.rb +3 -0
  35. data/spec/dummy/app/controllers/shopping/payments_controller.rb +5 -0
  36. data/spec/dummy/app/controllers/shopping/products_controller.rb +2 -0
  37. data/spec/dummy/app/controllers/tests_controller.rb +58 -0
  38. data/spec/dummy/app/controllers/topics_controller.rb +63 -0
  39. data/spec/dummy/app/controllers/users/sessions_controller.rb +3 -0
  40. data/spec/dummy/app/controllers/webhooks_controller.rb +13 -0
  41. data/spec/dummy/app/helpers/activity_helper.rb +2 -0
  42. data/spec/dummy/app/helpers/api/v1/token_auth_helper.rb +2 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/helpers/home_helper.rb +2 -0
  45. data/spec/dummy/app/helpers/tests_helper.rb +2 -0
  46. data/spec/dummy/app/helpers/topics_helper.rb +2 -0
  47. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  48. data/spec/dummy/app/mailers/new.rb +3 -0
  49. data/spec/dummy/app/models/activity.rb +3 -0
  50. data/spec/dummy/app/models/admin.rb +12 -0
  51. data/spec/dummy/app/models/noti.rb +48 -0
  52. data/spec/dummy/app/models/noti_response.rb +9 -0
  53. data/spec/dummy/app/models/shopping/cart.rb +3 -0
  54. data/spec/dummy/app/models/shopping/cart_item.rb +7 -0
  55. data/spec/dummy/app/models/shopping/discount.rb +3 -0
  56. data/spec/dummy/app/models/shopping/payment.rb +16 -0
  57. data/spec/dummy/app/models/shopping/product.rb +6 -0
  58. data/spec/dummy/app/models/test.rb +5 -0
  59. data/spec/dummy/app/models/topic.rb +75 -0
  60. data/spec/dummy/app/models/user.rb +95 -0
  61. data/spec/dummy/app/views/home/index.html.erb +6 -0
  62. data/spec/dummy/app/views/home/send_notification.html.erb +2 -0
  63. data/spec/dummy/app/views/layouts/application.html.erb +26 -0
  64. data/spec/dummy/app/views/layouts/mailer.html.erb +8 -0
  65. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  66. data/spec/dummy/app/views/new/notification.html.erb +1 -0
  67. data/spec/dummy/app/views/new/notification.text.erb +1 -0
  68. data/spec/dummy/app/views/shopping/_navigation_options.html.erb +1 -0
  69. data/spec/dummy/app/views/tests/_form.html.erb +25 -0
  70. data/spec/dummy/app/views/tests/edit.html.erb +6 -0
  71. data/spec/dummy/app/views/tests/index.html.erb +33 -0
  72. data/spec/dummy/app/views/tests/new.html.erb +5 -0
  73. data/spec/dummy/app/views/tests/show.html.erb +14 -0
  74. data/spec/dummy/app/views/topics/_form.html.erb +25 -0
  75. data/spec/dummy/app/views/topics/edit.html.erb +6 -0
  76. data/spec/dummy/app/views/topics/index.html.erb +29 -0
  77. data/spec/dummy/app/views/topics/new.html.erb +5 -0
  78. data/spec/dummy/app/views/topics/show.html.erb +14 -0
  79. data/spec/dummy/app/views/users/sessions/create.js.erb +6 -0
  80. data/spec/dummy/bin/bundle +3 -0
  81. data/spec/dummy/bin/rails +4 -0
  82. data/spec/dummy/bin/rake +4 -0
  83. data/spec/dummy/bin/setup +29 -0
  84. data/spec/dummy/config.ru +8 -0
  85. data/spec/dummy/config/application.rb +31 -0
  86. data/spec/dummy/config/boot.rb +5 -0
  87. data/spec/dummy/config/environment.rb +4 -0
  88. data/spec/dummy/config/environments/development.rb +57 -0
  89. data/spec/dummy/config/environments/production.rb +92 -0
  90. data/spec/dummy/config/environments/test.rb +44 -0
  91. data/spec/dummy/config/initializers/assets.rb +11 -0
  92. data/spec/dummy/config/initializers/aws.rb +15 -0
  93. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  94. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  95. data/spec/dummy/config/initializers/devise.rb +273 -0
  96. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  97. data/spec/dummy/config/initializers/inflections.rb +16 -0
  98. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  99. data/spec/dummy/config/initializers/preinitializer.rb +167 -0
  100. data/spec/dummy/config/initializers/redis.rb +9 -0
  101. data/spec/dummy/config/initializers/session_store.rb +3 -0
  102. data/spec/dummy/config/initializers/wrap_parameters.rb +9 -0
  103. data/spec/dummy/config/locales/devise.en.yml +62 -0
  104. data/spec/dummy/config/locales/en.yml +25 -0
  105. data/spec/dummy/config/mongoid.yml +142 -0
  106. data/spec/dummy/config/redis.yml +14 -0
  107. data/spec/dummy/config/routes.rb +38 -0
  108. data/spec/dummy/config/secrets.yml +22 -0
  109. data/spec/dummy/db/seeds.rb +48 -0
  110. data/spec/dummy/lib/admin/parameter_sanitizer.rb +9 -0
  111. data/spec/dummy/lib/assets/files/test_names.json +1 -0
  112. data/spec/dummy/lib/user/parameter_sanitizer.rb +8 -0
  113. data/spec/dummy/public/404.html +67 -0
  114. data/spec/dummy/public/422.html +67 -0
  115. data/spec/dummy/public/500.html +66 -0
  116. data/spec/dummy/public/favicon.ico +0 -0
  117. data/spec/dummy/shoryuken.yml +5 -0
  118. data/spec/dummy/sidekiq.yml +9 -0
  119. data/spec/dummy/sidekiq_prep.sh +6 -0
  120. data/spec/dummy/spec/controllers/activity_controller_spec.rb +5 -0
  121. data/spec/dummy/spec/controllers/tests_controller_spec.rb +141 -0
  122. data/spec/dummy/spec/factories/tests.rb +6 -0
  123. data/spec/dummy/spec/fixtures/new/send_notification +3 -0
  124. data/spec/dummy/spec/helpers/activity_helper_spec.rb +15 -0
  125. data/spec/dummy/spec/helpers/tests_helper_spec.rb +15 -0
  126. data/spec/dummy/spec/mailers/new_spec.rb +18 -0
  127. data/spec/dummy/spec/mailers/previews/new_preview.rb +13 -0
  128. data/spec/dummy/spec/models/test_spec.rb +5 -0
  129. data/spec/dummy/spec/requests/tests_spec.rb +10 -0
  130. data/spec/dummy/spec/routing/tests_routing_spec.rb +39 -0
  131. data/spec/dummy/spec/views/tests/edit.html.erb_spec.rb +21 -0
  132. data/spec/dummy/spec/views/tests/index.html.erb_spec.rb +22 -0
  133. data/spec/dummy/spec/views/tests/new.html.erb_spec.rb +21 -0
  134. data/spec/dummy/spec/views/tests/show.html.erb_spec.rb +16 -0
  135. data/spec/dummy/test/controllers/api/v1/token_auth_controller_test.rb +7 -0
  136. data/spec/dummy/test/controllers/home_controller_test.rb +9 -0
  137. data/spec/dummy/test/controllers/oauth_tests_controller_test.rb +7 -0
  138. data/spec/dummy/test/fixtures/oauth_tests.yml +11 -0
  139. data/spec/dummy/test/fixtures/users.yml +7 -0
  140. data/spec/dummy/test/models/oauth_test_test.rb +7 -0
  141. data/spec/dummy/test/models/user_test.rb +7 -0
  142. data/spec/factories/client_factory.rb +77 -0
  143. data/spec/features/additional_login_param_feature_spec.rb +63 -0
  144. data/spec/features/extension_spec.rb +680 -0
  145. data/spec/features/redirect_disable_spec.rb +74 -0
  146. data/spec/fixtures/auth/clients.yml +13 -0
  147. data/spec/helpers/auth/admin_create_users_helper_spec.rb +15 -0
  148. data/spec/helpers/auth/resource_helper_spec.rb +15 -0
  149. data/spec/helpers/auth/search_helper_spec.rb +15 -0
  150. data/spec/helpers/auth/users/profiles_helper_spec.rb +15 -0
  151. data/spec/integration/navigation_test.rb +8 -0
  152. data/spec/mailers/auth/notifier_spec.rb +5 -0
  153. data/spec/mailers/previews/auth/notifier_preview.rb +4 -0
  154. data/spec/models/auth/admin_create_user_spec.rb +5 -0
  155. data/spec/models/auth/client_test.rb +9 -0
  156. data/spec/models/auth/shopping/discount_spec.rb +5 -0
  157. data/spec/rails_helper.rb +70 -0
  158. data/spec/requests/admin/confirmation_request_spec.rb +164 -0
  159. data/spec/requests/admin/password_request_spec.rb +191 -0
  160. data/spec/requests/admin/registration_request_spec.rb +574 -0
  161. data/spec/requests/admin/session_request_spec.rb +352 -0
  162. data/spec/requests/admin/unlock_request_spec.rb +178 -0
  163. data/spec/requests/admin_create_user_request_spec.rb +303 -0
  164. data/spec/requests/background_job_request_spec.rb +28 -0
  165. data/spec/requests/cart_item_request_spec.rb +381 -0
  166. data/spec/requests/cart_request_spec.rb +451 -0
  167. data/spec/requests/client_request_spec.rb +65 -0
  168. data/spec/requests/discount_request_spec.rb +589 -0
  169. data/spec/requests/payment_request_spec.rb +1254 -0
  170. data/spec/requests/search_request_spec.rb +173 -0
  171. data/spec/requests/topic_request_spec.rb +138 -0
  172. data/spec/requests/user/additional_login_param_and_email_validation_spec.rb +673 -0
  173. data/spec/requests/user/confirmation_request_spec.rb +189 -0
  174. data/spec/requests/user/omniauth_callbacks_request_spec.rb +682 -0
  175. data/spec/requests/user/otp_basic_flow_request_spec.rb +512 -0
  176. data/spec/requests/user/password_request_spec.rb +216 -0
  177. data/spec/requests/user/registration_request_spec.rb +615 -0
  178. data/spec/requests/user/session_request_spec.rb +361 -0
  179. data/spec/requests/user/unlock_request_spec.rb +208 -0
  180. data/spec/routing/admin/admin_routes_spec.rb +15 -0
  181. data/spec/routing/auth/admin_create_users_routing_spec.rb +39 -0
  182. data/spec/routing/clients_controller_routes_spec.rb +75 -0
  183. data/spec/routing/engine_routes_spec.rb +16 -0
  184. data/spec/spec_helper.rb +138 -0
  185. data/spec/support/factory_girl.rb +3 -0
  186. data/spec/support/omniauth_macros.rb +195 -0
  187. data/spec/support/payumoney_support.rb +11 -0
  188. data/spec/support/sign_in_support.rb +361 -0
  189. data/spec/support/wait_for_ajax.rb +11 -0
  190. data/spec/views/auth/admin_create_users/edit.html.erb_spec.rb +14 -0
  191. data/spec/views/auth/admin_create_users/index.html.erb_spec.rb +14 -0
  192. data/spec/views/auth/admin_create_users/new.html.erb_spec.rb +14 -0
  193. data/spec/views/auth/admin_create_users/show.html.erb_spec.rb +11 -0
  194. data/spec/views/auth/shopping/discounts/create.html.erb_spec.rb +5 -0
  195. data/spec/views/auth/shopping/discounts/destroy.html.erb_spec.rb +5 -0
  196. data/spec/views/auth/shopping/discounts/edit.html.erb_spec.rb +5 -0
  197. data/spec/views/auth/shopping/discounts/index.html.erb_spec.rb +5 -0
  198. data/spec/views/auth/shopping/discounts/new.html.erb_spec.rb +5 -0
  199. data/spec/views/auth/shopping/discounts/show.html.erb_spec.rb +5 -0
  200. data/spec/views/auth/shopping/discounts/update.html.erb_spec.rb +5 -0
  201. data/spec/views/profile/show.html.erb_spec.rb +5 -0
  202. metadata +401 -3
@@ -0,0 +1,1254 @@
1
+ require "rails_helper"
2
+
3
+ RSpec.describe "payment request spec",:payment => true, :shopping => true, :type => :request do
4
+
5
+ before(:all) do
6
+ Shopping::Product.delete_all
7
+ @product = Shopping::Product.new(attributes_for(:product))
8
+ @product.save
9
+ ActionController::Base.allow_forgery_protection = false
10
+ User.delete_all
11
+ Auth::Client.delete_all
12
+ @u = User.new(attributes_for(:user_confirmed))
13
+ @u.save
14
+
15
+ @c = Auth::Client.new(:resource_id => @u.id, :api_key => "test", :app_ids => ["test_app_id"])
16
+ @c.redirect_urls = ["http://www.google.com"]
17
+ @c.versioned_create
18
+ @u.client_authentication["test_app_id"] = "test_es_token"
19
+ @u.save
20
+ @ap_key = @c.api_key
21
+ @headers = { "CONTENT_TYPE" => "application/json" , "ACCEPT" => "application/json", "X-User-Token" => @u.authentication_token, "X-User-Es" => @u.client_authentication["test_app_id"], "X-User-Aid" => "test_app_id"}
22
+
23
+
24
+ ### CREATE ONE ADMIN USER
25
+
26
+ ### It will use the same client as the user.
27
+ @admin = Admin.new(attributes_for(:admin_confirmed))
28
+ @admin.client_authentication["test_app_id"] = "test_es_token"
29
+ @admin.save
30
+ @admin_headers = { "CONTENT_TYPE" => "application/json" , "ACCEPT" => "application/json", "X-Admin-Token" => @admin.authentication_token, "X-Admin-Es" => @admin.client_authentication["test_app_id"], "X-Admin-Aid" => "test_app_id"}
31
+
32
+ end
33
+
34
+
35
+ context " -- web app requests -- " do
36
+
37
+
38
+ context " -- gateway payment -- ", :payumoney => true do
39
+
40
+ before(:example) do
41
+ ## create cart items
42
+ ## create cart
43
+ ## create a gateway payment.
44
+ ## then simulate a post request as if its coming from the payment gateway
45
+ @created_cart_item_ids = []
46
+ @cart = Shopping::Cart.new
47
+ @cart.resource_id = @u.id.to_s
48
+ @cart.resource_class = @u.class.name
49
+ @cart.save
50
+
51
+ 5.times do
52
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
53
+ cart_item.resource_id = @u.id.to_s
54
+ cart_item.resource_class = @u.class.name
55
+ cart_item.parent_id = @cart.id
56
+ cart_item.signed_in_resource = @u
57
+ cart_item.save
58
+ @created_cart_item_ids << cart_item.id.to_s
59
+ end
60
+
61
+ @cart.prepare_cart
62
+
63
+ ## creating payment.
64
+ @payment = Shopping::Payment.new
65
+ @payment.payment_type = "gateway"
66
+ @payment.cart_id = @cart.id.to_s
67
+ @payment.amount = @cart.cart_price
68
+ @payment.signed_in_resource = @u
69
+ @payment.resource_class = @u.class.name
70
+ @payment.resource_id = @u.id.to_s
71
+ @payment.firstname = "bhargav"
72
+ @payment.email = "bhargav.r.raut@gmail.com"
73
+ @payment.phone = "9561137096"
74
+ @payment.productinfo = "hello world"
75
+
76
+ puts "payment save result:"
77
+ puts @payment.save.to_s
78
+ puts @payment.errors.full_messages.to_s
79
+
80
+
81
+
82
+ end
83
+
84
+ after(:example) do
85
+ Shopping::Cart.delete_all
86
+ Shopping::CartItem.delete_all
87
+ Shopping::Payment.delete_all
88
+ end
89
+
90
+ context " -- gateway does post call to the app -- " do
91
+
92
+ before(:example) do
93
+ expect_any_instance_of(Shopping::Payment).to receive(:gateway_callback).and_return(true)
94
+ expect_any_instance_of(Shopping::Payment).to receive(:verify_payment).and_return(nil)
95
+ end
96
+
97
+
98
+ it " -- triggers gateway callback, but returns from the verify_payment callback -- ", :first_gateway_test do
99
+
100
+ sign_in @u
101
+
102
+
103
+ post "/shopping/payments/#{@payment.id.to_s}", PayumoneySupport.payment_callback_params(@payment)
104
+
105
+
106
+ end
107
+
108
+
109
+ end
110
+
111
+ context " -- user does refresh payment -- " do
112
+
113
+ before(:example) do
114
+ expect_any_instance_of(Shopping::Payment).to receive(:gateway_callback).and_return(nil)
115
+ expect_any_instance_of(Shopping::Payment).to receive(:verify_payment).and_return(true)
116
+ end
117
+
118
+ it " -- does not trigger gateway callback , because .verify_payment attr_accessor is set -- " do
119
+
120
+ sign_in @u
121
+
122
+ put "/shopping/payments/#{@payment.id.to_s}", {:is_verify_payment => true}
123
+
124
+ end
125
+
126
+ end
127
+
128
+
129
+
130
+ context " -- user interrupts callback -- " do
131
+
132
+
133
+ it " -- calls verify_payment if the payment status is pending, and the user passes is_verify_payment -- " do
134
+
135
+ expect_any_instance_of(Shopping::Payment).to receive(:verify_payment).and_return(true)
136
+
137
+ expect(@payment.payment_status).to be_nil
138
+
139
+ sign_in @u
140
+
141
+ put "/shopping/payments/#{@payment.id.to_s}", {:is_verify_payment => true}
142
+
143
+ end
144
+
145
+ it " -- doesnt call verify_payment if the status is not pending, and even if the user passes is_verify_payment -- ", :sss => true do
146
+
147
+ ## change the payment status to 0
148
+ ## and then calling this will not work.
149
+
150
+
151
+ @payment.payment_status = 0
152
+ @payment.signed_in_resource = @admin
153
+ @payment.resource_id = @u
154
+ @payment.resource_class = @u.class.to_s
155
+ ## setting this as true so that it doesn't do the gateway callback(that happens automatically otherwise, and it will lead to an error.)
156
+ ## just set this is_verify_payment to avoid the gateway callback, there is no other significance, i would have liked to do skip_callback somehow, but too tired.
157
+ @payment.is_verify_payment = true
158
+
159
+ sign_in @u
160
+
161
+ expect_any_instance_of(Shopping::Payment).to receive(:verify_payment).and_return(nil)
162
+
163
+ put "/shopping/payments/#{@payment.id.to_s}", {:is_verify_payment => true}
164
+
165
+ end
166
+ end
167
+
168
+ end
169
+
170
+ end
171
+
172
+
173
+
174
+ context " -- cash, card, cheque payment -- " do
175
+
176
+ before(:example) do
177
+ Shopping::CartItem.delete_all
178
+ Shopping::Cart.delete_all
179
+ Shopping::Payment.delete_all
180
+
181
+
182
+ @created_cart_item_ids = []
183
+ @cart = Shopping::Cart.new
184
+ @cart.resource_id = @u.id.to_s
185
+ @cart.resource_class = @u.class.name
186
+ @cart.save
187
+
188
+ 5.times do
189
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
190
+ cart_item.resource_id = @u.id.to_s
191
+ cart_item.resource_class = @u.class.name
192
+ cart_item.parent_id = @cart.id
193
+ cart_item.signed_in_resource = @u
194
+ cart_item.save
195
+ puts cart_item.errors.full_messages
196
+ @created_cart_item_ids << cart_item.id.to_s
197
+ end
198
+
199
+ end
200
+
201
+ after(:example) do
202
+ Shopping::CartItem.delete_all
203
+ Shopping::Cart.delete_all
204
+ Shopping::Payment.delete_all
205
+ end
206
+
207
+
208
+ it " -- creates a payment to the cart-- ", :cr => true do
209
+
210
+
211
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: 10, :api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
212
+
213
+ expect(Shopping::Payment.count).to eq(1)
214
+
215
+ end
216
+
217
+ it " -- produces a validation error if payment is less than minimum payment amount -- " do
218
+
219
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: 5, :api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
220
+
221
+ payment_made = assigns(:auth_shopping_payment)
222
+ expect(payment_made.errors.full_messages).not_to be_empty
223
+
224
+ expect(Shopping::Payment.count).to eq(0)
225
+
226
+ end
227
+
228
+ it " -- sets all cart items as accepted, if payment amount is sufficient for all the cart items. ",:br => true do
229
+
230
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: 50.00, :api_key => @ap_key, :current_app_id => "test_app_id", :payment_status => 1, :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
231
+
232
+ puts response.body.to_s
233
+
234
+ expect(Shopping::Payment.count).to eq(1)
235
+
236
+ @created_cart_item_ids.each do |id|
237
+ cart_item = Shopping::CartItem.find(id)
238
+ expect(cart_item.accepted).to be_truthy
239
+ end
240
+
241
+
242
+ end
243
+
244
+
245
+
246
+ it " -- if payment status is set as failed, cart item statuses are also updated -- ", :payment_cart_item_reversal do
247
+
248
+ payment = Shopping::Payment.new
249
+ payment.payment_type = "cash"
250
+ payment.amount = 50.00
251
+ payment.resource_id = @u.id.to_s
252
+ payment.resource_class = @u.class.name.to_s
253
+ payment.cart_id = @cart.id.to_s
254
+ ## need to set the signed in resource as admin, because we are setting the payment status, and that cannot be done as user.
255
+ payment.signed_in_resource = @admin
256
+ ## this is setting the payment as successfully.
257
+ payment.payment_status = 1
258
+ ps = payment.save
259
+
260
+
261
+ ## now all the cart items should be accepted
262
+ @created_cart_item_ids.each do |id|
263
+ cart_item = Shopping::CartItem.find(id)
264
+ expect(cart_item.accepted).to be_truthy
265
+ end
266
+
267
+
268
+ put shopping_payment_path({:id => payment.id}), {payment: {payment_status: 0},:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
269
+
270
+
271
+
272
+ @created_cart_item_ids.each do |id|
273
+ cart_item = Shopping::CartItem.find(id)
274
+ expect(cart_item.accepted).not_to be_truthy
275
+ end
276
+
277
+ end
278
+
279
+
280
+ it " -- payment update can retrospectively accept cart items -- ", :retro => true do
281
+
282
+ ## now make a payment to the cart of the amount equal to all the cart items.
283
+ payment = Shopping::Payment.new
284
+ payment.payment_type = "cash"
285
+ payment.amount = 10.00
286
+ payment.resource_id = @u.id.to_s
287
+ payment.resource_class = @u.class.name.to_s
288
+ payment.cart_id = @cart.id.to_s
289
+ payment.signed_in_resource = @admin
290
+ payment.payment_status = 1
291
+ ps = payment.save
292
+ puts payment.errors.full_messages.to_s
293
+ expect(ps).to be_truthy
294
+
295
+ ## then remove some cart items from the cart
296
+ cart_item_to_remove = Shopping::CartItem.first
297
+ cart_item_to_remove.signed_in_resource = @admin
298
+ k = cart_item_to_remove.unset_cart
299
+ expect(cart_item_to_remove.parent_id).to be_nil
300
+ expect(k).to be_truthy
301
+
302
+ ## now add one cart item back into the cart
303
+ ## what if we add back the same cart item.
304
+ ## it will already be accepted
305
+ ## so when you add a cart item to the cart, set its accepted as null by default.
306
+ cart_item_to_remove = Shopping::CartItem.find(cart_item_to_remove.id.to_s)
307
+ cart_item_to_remove.signed_in_resource = @admin
308
+ r = cart_item_to_remove.set_cart_and_resource(@cart)
309
+
310
+ expect(r).to be_truthy
311
+
312
+ ## now this cart item will be accepted = null, even though the cart has enough credit in it.
313
+ cart_item_to_remove = Shopping::CartItem.find(cart_item_to_remove.id.to_s)
314
+ expect(cart_item_to_remove.accepted).to be_nil
315
+ expect(cart_item_to_remove.parent_id).to eq(@cart.id.to_s)
316
+
317
+
318
+ res = payment.save
319
+ expect(res).to be_truthy
320
+
321
+ Shopping::CartItem.all.each do |citem|
322
+ if citem.parent_id == @cart.id.to_s
323
+ expect(citem.accepted).to be_truthy
324
+ end
325
+ end
326
+
327
+ ## add another cart item.
328
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
329
+ cart_item.resource_id = @u.id.to_s
330
+ cart_item.resource_class = @u.class.name
331
+ cart_item.parent_id = @cart.id
332
+ cart_item.signed_in_resource = @u
333
+ res = cart_item.save
334
+ #puts cart_item.errors.full_messages
335
+ expect(res).to be_truthy
336
+
337
+ ## now call update on the payment, and the last cart item should be marked as accepted.
338
+ put shopping_payment_path({:id => payment.id}), {:api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
339
+
340
+ expect(response.code).to eq("204")
341
+
342
+ ## this is failing for some reason.
343
+ latest_added_citem = Shopping::CartItem.find(cart_item.id.to_s)
344
+ expect(latest_added_citem.accepted).not_to be_truthy
345
+
346
+ end
347
+
348
+
349
+ it " -- sequence -- ", :sequence => true do
350
+
351
+ ## make a successfull payment.
352
+ payment = Shopping::Payment.new
353
+ payment.payment_type = "cash"
354
+ payment.amount = 50.00
355
+ payment.resource_id = @u.id.to_s
356
+ payment.resource_class = @u.class.name.to_s
357
+ payment.cart_id = @cart.id.to_s
358
+ payment.signed_in_resource = @admin
359
+ ## this is setting the payment as successfully.
360
+ payment.payment_status = 1
361
+ ps = payment.save
362
+ expect(ps).to be_truthy
363
+
364
+ ## now remove an item from the cart.
365
+ ## you cannot remove an item once a payment has been made, unless you are an admin.
366
+ cart_item_to_remove = Shopping::CartItem.first
367
+ cart_item_to_remove.signed_in_resource = @admin
368
+ k = cart_item_to_remove.unset_cart
369
+
370
+ ## now make a refund request.
371
+ refund_request = Shopping::Payment.new
372
+ refund_request.payment_type = "cash"
373
+ refund_request.refund = true
374
+ ## the amount doesnt matter.
375
+ refund_request.amount = -10
376
+ refund_request.resource_id = @u.id.to_s
377
+ refund_request.resource_class = @u.class.name.to_s
378
+ refund_request.cart_id = @cart.id.to_s
379
+ refund_request.signed_in_resource = @u
380
+ ## this is setting the payment as successfully.
381
+ ref_res = refund_request.save
382
+ ## then accept that request.
383
+ #puts refund_request.errors.full_messages.to_s
384
+ expect(ref_res).to be_truthy
385
+
386
+ ## now suppose earlier payment fails.
387
+ ## all cart items should be set as accepted false.
388
+
389
+ ## only admin can update a payment.
390
+ #payment = Shopping::Payment.find(payment.id)
391
+ #payment.payment_type = "cash"
392
+ #payment.amount = 50.00
393
+ #payment.resource_id = @u.id.to_s
394
+ #payment.resource_class = @u.class.name.to_s
395
+ #payment.cart_id = @cart.id.to_s
396
+ #payment.signed_in_resource = @admin
397
+ ## this is setting the payment as successfully.
398
+ #payment.payment_status = 0
399
+ #ps = payment.save
400
+
401
+ puts "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
402
+
403
+ put shopping_payment_path({:id => payment.id}), {payment: {payment_status: 0},:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
404
+
405
+ #expect(ps).to be_truthy
406
+ payment = Shopping::Payment.find(payment.id)
407
+ expect(payment.payment_status).to eq(0)
408
+
409
+
410
+
411
+ @cart = Shopping::Cart.find(payment.cart_id)
412
+ @cart.prepare_cart
413
+ ## now all the remaining items should be set as not accepted.
414
+ @cart.get_cart_items.each do |citem|
415
+ expect(citem.accepted).not_to be_truthy
416
+ end
417
+
418
+ ## so first we made a payment of 50, then removed one item, now payment has gone false, so now the pending balance should be -40
419
+ cart = Shopping::Cart.find(@cart.id)
420
+ cart.prepare_cart
421
+ expect(cart.cart_pending_balance).to eq(40)
422
+
423
+ end
424
+
425
+
426
+
427
+ end
428
+
429
+
430
+ context " -- excess payment -- " do
431
+ before(:example) do
432
+ Shopping::CartItem.delete_all
433
+ Shopping::Cart.delete_all
434
+ Shopping::Payment.delete_all
435
+ @created_cart_item_ids = []
436
+ @cart = Shopping::Cart.new
437
+ @cart.resource_id = @u.id.to_s
438
+ @cart.resource_class = @u.class.name
439
+ @cart.save
440
+
441
+ 5.times do
442
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
443
+ cart_item.resource_id = @u.id.to_s
444
+ cart_item.resource_class = @u.class.name
445
+ cart_item.parent_id = @cart.id
446
+ cart_item.price = 10.00
447
+ cart_item.signed_in_resource = @u
448
+ cart_item.save
449
+ @created_cart_item_ids << cart_item.id.to_s
450
+ end
451
+ end
452
+
453
+ after(:example) do
454
+ Shopping::CartItem.delete_all
455
+ Shopping::Cart.delete_all
456
+ Shopping::Payment.delete_all
457
+ end
458
+
459
+ it " -- cash payment should set amount to pending balance, and pass, with change being calculated -- ", :cash_payment_excess => true do
460
+
461
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: 55.00, :api_key => @ap_key, :current_app_id => "test_app_id", :payment_status => 1, :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
462
+
463
+ expect(Shopping::Payment.count).to eq(1)
464
+
465
+ payment = assigns(:auth_shopping_payment)
466
+ expect(payment.cash_change).to eq(5.00)
467
+
468
+ end
469
+
470
+ it " -- cheque payment should fail, saying amount is excessive -- ", :cheque_payment_excess => true do
471
+
472
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cheque", amount: 55.00, :api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
473
+
474
+ puts response.body.to_s
475
+ expect(Shopping::Payment.count).to eq(0)
476
+
477
+ payment = assigns(:auth_shopping_payment)
478
+ expect(payment.errors.full_messages).not_to be_empty
479
+
480
+ end
481
+
482
+ it " -- card payment should fail, saying amount is excessive -- ", :card_payment_excess do
483
+
484
+ ## this is same as for cheque, just change the payment type.
485
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "card", amount: 55.00, :api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
486
+
487
+ puts response.body.to_s
488
+ expect(Shopping::Payment.count).to eq(0)
489
+
490
+ payment = assigns(:auth_shopping_payment)
491
+ expect(payment.errors.full_messages).not_to be_empty
492
+
493
+ end
494
+
495
+ end
496
+
497
+
498
+
499
+
500
+ context " -- payment validations. -- ", :payment_validations => true do
501
+
502
+ before(:example) do
503
+ Shopping::CartItem.delete_all
504
+ Shopping::Cart.delete_all
505
+ Shopping::Payment.delete_all
506
+ @created_cart_item_ids = []
507
+ @cart = Shopping::Cart.new
508
+ @cart.resource_id = @u.id.to_s
509
+ @cart.resource_class = @u.class.name
510
+ @cart.save
511
+
512
+
513
+ 5.times do
514
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
515
+ cart_item.resource_id = @u.id.to_s
516
+ cart_item.resource_class = @u.class.name
517
+ cart_item.parent_id = @cart.id
518
+ cart_item.signed_in_resource = @u
519
+ cart_item.save
520
+ @created_cart_item_ids << cart_item.id.to_s
521
+ end
522
+
523
+
524
+ end
525
+
526
+ after(:example) do
527
+ Shopping::CartItem.delete_all
528
+ Shopping::Cart.delete_all
529
+ Shopping::Payment.delete_all
530
+ end
531
+
532
+ it " -- doesnt allow payment to be created if cart is empty -- ", :payment_into_empty_cart do
533
+
534
+ #Shopping::CartItem.all.each do |citem|
535
+ # citem.signed_in_resource = @admin
536
+ # citem.unset_cart
537
+ #end
538
+
539
+ new_cart = Shopping::Cart.new
540
+ new_cart.resource_id = @u.id.to_s
541
+ new_cart.resource_class = @u.class.name
542
+ new_cart.save
543
+
544
+
545
+ post shopping_payments_path, {cart_id: new_cart.id.to_s,payment_type: "cash", amount: 10.00, :api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
546
+
547
+ expect(Shopping::Payment.count).to eq(0)
548
+
549
+ puts response.body.to_s
550
+
551
+ #expect(payment.errors.full_messages).not_to be_empty
552
+
553
+
554
+ end
555
+
556
+ it " -- payment amount must be greater than zero. -- ", :payment_greater_than => true do
557
+
558
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: -10.00, :api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
559
+
560
+ expect(Shopping::Payment.count).to eq(0)
561
+
562
+ payment = assigns(:auth_shopping_payment)
563
+
564
+ expect(payment.errors.full_messages).not_to be_empty
565
+
566
+ end
567
+
568
+
569
+
570
+ end
571
+
572
+
573
+
574
+ ## note also need to check this in case of cart controller, when we add or remove cart items.
575
+ ## set cart item accepted specs.
576
+ context " -- transactional issues in multiple cart items being marked as accepted -- " do
577
+
578
+ before(:example) do
579
+
580
+ Shopping::CartItem.delete_all
581
+ Shopping::Cart.delete_all
582
+ Shopping::Payment.delete_all
583
+ @created_cart_item_ids = []
584
+ @cart = Shopping::Cart.new
585
+ @cart.resource_id = @u.id.to_s
586
+ @cart.resource_class = @u.class.name
587
+ @cart.save
588
+
589
+ 5.times do
590
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
591
+ cart_item.resource_id = @u.id.to_s
592
+ cart_item.resource_class = @u.class.name
593
+ cart_item.parent_id = @cart.id
594
+ cart_item.signed_in_resource = @u
595
+ cart_item.price = 10.00
596
+ cart_item.save
597
+ @created_cart_item_ids << cart_item.id.to_s
598
+ end
599
+
600
+
601
+ allow_any_instance_of(Shopping::CartItem).to receive(:set_accepted).and_return(false)
602
+
603
+
604
+ end
605
+
606
+ after(:example) do
607
+ Shopping::CartItem.delete_all
608
+ Shopping::Cart.delete_all
609
+ Shopping::Payment.delete_all
610
+
611
+
612
+
613
+ end
614
+
615
+ it " -- shows a validation error if, all cart items cannot be successfully updated ", :cart_item_update_payment_failure do
616
+
617
+ ## so how to simulate this?
618
+ ## override set_accepted method in payment_concern to return false.
619
+
620
+
621
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: 50, :api_key => @ap_key, :current_app_id => "test_app_id", :payment_status => 1, :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
622
+
623
+ expect(Shopping::Payment.count).to eq(0)
624
+
625
+ payment = assigns(:auth_shopping_payment)
626
+ expect(payment.errors.full_messages).not_to be_empty
627
+ end
628
+
629
+ it " -- can recover from situation where all cart items couldnt be accepted -- " do
630
+
631
+ ## so basically the cart item gets marked as being accepted by a payment that doesnt exist.
632
+
633
+ ## so basically should have a function that can check for such a scene.
634
+
635
+ ## should be able to call update on a cart item, and in doing so, search for the payment, if it doesn't exist, then it should update the cart item as not being accepted.
636
+
637
+
638
+
639
+ end
640
+
641
+ end
642
+
643
+ context " -- payment receipt -- " do
644
+
645
+ before(:example) do
646
+
647
+ Shopping::CartItem.delete_all
648
+ Shopping::Cart.delete_all
649
+ Shopping::Payment.delete_all
650
+ @created_cart_item_ids = []
651
+ @cart = Shopping::Cart.new
652
+ @cart.resource_id = @u.id.to_s
653
+ @cart.resource_class = @u.class.name
654
+ @cart.save
655
+
656
+ 5.times do
657
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
658
+ cart_item.resource_id = @u.id.to_s
659
+ cart_item.resource_class = @u.class.name
660
+ cart_item.parent_id = @cart.id
661
+ cart_item.signed_in_resource = @u
662
+ cart_item.price = 10.00
663
+ cart_item.save
664
+ @created_cart_item_ids << cart_item.id.to_s
665
+ end
666
+
667
+
668
+
669
+
670
+
671
+ end
672
+
673
+ after(:example) do
674
+ Shopping::CartItem.delete_all
675
+ Shopping::Cart.delete_all
676
+ Shopping::Payment.delete_all
677
+
678
+
679
+
680
+ end
681
+
682
+
683
+ it " -- generates receipt after payment is saved -- ", :failure => true do
684
+
685
+ ## proposed plan for successfull payment.
686
+ ## cart should prepare cart
687
+ ## it should also show the receipt of the latest payment.
688
+ ## so basically a hash with two keys should be generated
689
+ ## cart => {with all its attributes}
690
+ ## curr_payment => {with cart item ids, that it has paid for.}
691
+
692
+ ## expect the payment_receipt hash to be present.
693
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", amount: 50.00, :api_key => @ap_key, :current_app_id => "test_app_id", :payment_status => 1, :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
694
+
695
+ assigned_p = assigns(:auth_shopping_payment)
696
+
697
+ expect(Shopping::Payment.count).to eq(1)
698
+
699
+ @created_cart_item_ids.each do |id|
700
+ cart_item = Shopping::CartItem.find(id)
701
+ expect(cart_item.accepted).to be_truthy
702
+ end
703
+
704
+ puts JSON.pretty_generate(assigned_p.payment_receipt)
705
+
706
+
707
+
708
+ end
709
+ end
710
+
711
+ context " -- refund -- ", :refund => true do
712
+
713
+ before(:example) do
714
+
715
+ Shopping::CartItem.delete_all
716
+ Shopping::Cart.delete_all
717
+ Shopping::Payment.delete_all
718
+
719
+ ## create a cart
720
+ @cart = Shopping::Cart.new
721
+ @cart.resource_id = @u.id.to_s
722
+ @cart.resource_class = @u.class.name
723
+ @cart.save
724
+
725
+
726
+ ## create five cart items and add them to the cart.
727
+ @created_cart_item_ids = []
728
+ 5.times do
729
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
730
+ cart_item.resource_id = @u.id.to_s
731
+ cart_item.resource_class = @u.class.name
732
+ cart_item.parent_id = @cart.id
733
+ cart_item.signed_in_resource = @u
734
+ cart_item.price = 10.00
735
+ cart_item.save
736
+ @created_cart_item_ids << cart_item.id.to_s
737
+ end
738
+
739
+ ## create a payment to the cart
740
+ payment = Shopping::Payment.new
741
+ payment.payment_type = "cash"
742
+ payment.amount = 50.00
743
+ payment.resource_id = @u.id.to_s
744
+ payment.resource_class = @u.class.name.to_s
745
+ payment.cart_id = @cart.id.to_s
746
+ payment.signed_in_resource = @admin
747
+ payment.payment_status = 1
748
+ ps = payment.save
749
+
750
+
751
+ end
752
+
753
+ after(:example) do
754
+ Shopping::CartItem.delete_all
755
+ Shopping::Cart.delete_all
756
+ Shopping::Payment.delete_all
757
+ end
758
+
759
+
760
+
761
+
762
+ it " -- creates a refund request if the cart pending balance is negative -- ", :create_refund do
763
+
764
+ @cart = Shopping::Cart.find(@cart.id)
765
+ @cart.prepare_cart
766
+ expect(@cart.cart_pending_balance).to eq(0.00)
767
+
768
+
769
+ ## now basically remove a cart item from the cart.
770
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
771
+ ## only admin can unset an item , after a payment has already been made
772
+ last_cart_item.signed_in_resource = @admin
773
+ last_cart_item.unset_cart
774
+
775
+ ## now the pending balance should be -ve
776
+ @cart.prepare_cart
777
+ expect(@cart.cart_pending_balance).to eq(-10.00)
778
+
779
+
780
+ ## so basically here we are just creating a refund request.
781
+ ## here it only checks that the amount is negative and not what is the amount sent in by the refund.
782
+ ## this has to be equal to the cart_pending_balance at the time of accepting the refund.
783
+ ## so here sent as 10 but this is actually wrong.
784
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", refund: true, amount: 10, :api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
785
+
786
+ puts response.body.to_s
787
+
788
+ expect(response.code).to eq("201")
789
+
790
+
791
+
792
+ end
793
+
794
+ it " -- does not create a refund request if the cart pending balance is positive or zero -- ", :pending_balance do
795
+
796
+ ## add an item to the cart.
797
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
798
+ cart_item.resource_id = @u.id.to_s
799
+ cart_item.resource_class = @u.class.name
800
+ cart_item.parent_id = @cart.id
801
+ cart_item.signed_in_resource = @u
802
+ cart_item.price = 10.00
803
+ cart_item.save
804
+
805
+ ## prepare the cart again.
806
+ @cart.prepare_cart
807
+ expect(@cart.cart_pending_balance).to eq(10.00)
808
+
809
+ ## now try to create a refund request, and it should fail
810
+
811
+ post shopping_payments_path, {cart_id: @cart.id.to_s,payment_type: "cash", refund: true, amount: 10, :api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
812
+
813
+
814
+
815
+
816
+ expect(response.code).to eq("422")
817
+
818
+
819
+ end
820
+
821
+
822
+ it " -- accepts a valid refund if the user is an administrator -- ", :pd => true do
823
+
824
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
825
+ last_cart_item.signed_in_resource = @admin
826
+ last_cart_item.unset_cart
827
+
828
+ ## create a new refund request and set it as pending.
829
+ payment = Shopping::Payment.new
830
+
831
+ ## this doesn't matter while creating refunds.
832
+ payment.payment_type = "cheque"
833
+
834
+
835
+ payment.amount = -10.00
836
+ payment.refund = true
837
+ payment.resource_id = @u.id.to_s
838
+ payment.resource_class = @u.class.name.to_s
839
+ payment.cart_id = @cart.id.to_s
840
+
841
+ ## need to assign this because we are bypassing the controller.
842
+ payment.signed_in_resource = @u
843
+ ps = payment.save
844
+
845
+ ##now first assert that a refund exists whose refund status is null.
846
+ payment = Shopping::Payment.find(payment.id.to_s)
847
+ expect(payment.payment_status).to eq(nil)
848
+
849
+ ## now use the admin user to create a put request to the above payment.
850
+
851
+ ## it should set the status of the payment as accepted.
852
+
853
+ put shopping_payment_path({:id => payment.id}), {payment: {payment_status: 1, amount: -10},:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
854
+
855
+ puts response.body.to_s
856
+
857
+ payment = Shopping::Payment.find(payment.id)
858
+
859
+ expect(payment.payment_status).to eq(1)
860
+
861
+ end
862
+
863
+
864
+ it " -- before accepting a refund as an administrator it checks that there is negative pending balance -- ", :admin_negative => true do
865
+
866
+ ## remove an item from the cart.
867
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
868
+ ## only admin can unset, once payment has already been made.
869
+ last_cart_item.signed_in_resource = @admin
870
+ last_cart_item.unset_cart
871
+
872
+ ## create a new refund request and set it as pending.
873
+ payment = Shopping::Payment.new
874
+
875
+ ## this doesn't matter while creating refunds.
876
+ payment.payment_type = "cheque"
877
+
878
+ ## this also doesnt matter while creating refunds.
879
+ payment.amount = -10.00
880
+ payment.refund = true
881
+ payment.resource_id = @u.id.to_s
882
+ payment.resource_class = @u.class.name.to_s
883
+ payment.cart_id = @cart.id.to_s
884
+
885
+ ## need to assign this because we are bypassing the controller.
886
+ payment.signed_in_resource = @u
887
+ ps = payment.save
888
+
889
+ ##now first assert that a refund exists whose refund status is null.
890
+ payment = Shopping::Payment.find(payment.id.to_s)
891
+ expect(payment.payment_status).to eq(nil)
892
+
893
+ ## at this stage, check the cart and expect it to have a negative balance : i.e the user deserves a refund.
894
+ @cart = Shopping::Cart.find(@cart.id)
895
+ @cart.prepare_cart
896
+ expect(@cart.cart_pending_balance < 0).to be_truthy
897
+
898
+
899
+ ## now again add another item to the cart so that the balance is no longer negative.
900
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
901
+ cart_item.resource_id = @u.id.to_s
902
+ cart_item.resource_class = @u.class.name
903
+ cart_item.parent_id = @cart.id
904
+ cart_item.price = 10.00
905
+ cart_item.signed_in_resource = @u
906
+ cart_item.save
907
+
908
+
909
+ ## now use the admin user to create a put request to the above payment.
910
+
911
+ ## it should not allow the payment status to be changed to anything, since the balance is now positive. basically wont allow the admin to change the status to
912
+
913
+ put shopping_payment_path({:id => payment.id}), {payment: {payment_status: 1},:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
914
+
915
+ puts response.body.to_s
916
+
917
+ payment = Shopping::Payment.find(payment.id)
918
+
919
+ expect(payment.payment_status).to be_nil
920
+
921
+
922
+ end
923
+
924
+
925
+ it " -- after accepting a refund as an administrator, will update all pending refunds as failed" do
926
+
927
+
928
+ ## remove an item from the cart.
929
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
930
+ last_cart_item.signed_in_resource = @admin
931
+ last_cart_item.unset_cart
932
+
933
+
934
+ ## now we create three refunds
935
+ refunds_expected_to_have_failed = []
936
+ 3.times do
937
+ payment = Shopping::Payment.new
938
+ payment.payment_type = "cheque"
939
+ payment.amount = -10
940
+ payment.refund = true
941
+ payment.resource_id = @u.id.to_s
942
+ payment.resource_class = @u.class.name.to_s
943
+ payment.cart_id = @cart.id.to_s
944
+ payment.signed_in_resource = @u
945
+ ps = payment.save
946
+ refunds_expected_to_have_failed << payment
947
+ end
948
+
949
+
950
+ ## then we accept the last one, by sending in a put request as the admin.
951
+
952
+ put shopping_payment_path({:id => refunds_expected_to_have_failed.last.id}), {payment: {payment_status: 1},:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
953
+
954
+
955
+ ## we only keep the first two because the last one will not be deleted, we are going to be accepting the last one.
956
+ refunds_expected_to_have_failed.map!{|c| c = c.id.to_s}
957
+ refunds_expected_to_have_failed.pop
958
+ ## all previous payments should be marked as successfull.
959
+ ## for this an after update callback can be added?
960
+ ## but then that will be
961
+ refunds_expected_to_have_failed.each do |ref_id|
962
+ expect(Shopping::Payment.find(ref_id).payment_status).to eq(0)
963
+ end
964
+
965
+ end
966
+
967
+
968
+ it " -- attempting to 'show' a refund, if a subsequent refund was accepted, then will redirect to update this refund as failed -- ", :only_failure => true do
969
+
970
+ ## remove an item from the cart.
971
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
972
+ last_cart_item.signed_in_resource = @admin
973
+ last_cart_item.unset_cart
974
+
975
+
976
+ ## now we create three refunds
977
+ refunds_expected_to_have_failed = []
978
+ 3.times do
979
+ payment = Shopping::Payment.new
980
+ payment.payment_type = "cheque"
981
+ payment.amount = -10.00
982
+ payment.refund = true
983
+ payment.resource_id = @u.id.to_s
984
+ payment.resource_class = @u.class.name.to_s
985
+ payment.cart_id = @cart.id.to_s
986
+ payment.signed_in_resource = @u
987
+ ps = payment.save
988
+ refunds_expected_to_have_failed << payment
989
+ end
990
+
991
+ ## sleep so that there is some time lag between creating the refunds and updating the last one as a success.
992
+ sleep(2)
993
+
994
+
995
+ ## update the last refund as successfull.
996
+ successfull_refund = refunds_expected_to_have_failed.pop
997
+ successfull_refund.amount = -10
998
+ successfull_refund.signed_in_resource = @admin
999
+ successfull_refund.refund_success
1000
+
1001
+ res1 = successfull_refund.save
1002
+
1003
+
1004
+
1005
+ ## convert the remaining two refunds into their ids.
1006
+ refunds_expected_to_have_failed.map!{|c| c = c.id.to_s}
1007
+
1008
+ ## now update the last one as nil, i.e we simulate that somehow it didnt get converted to a failure payment status when the successfull refund was accepted.
1009
+ last_refund = Shopping::Payment.find(refunds_expected_to_have_failed.last)
1010
+ last_refund.payment_status = nil
1011
+ last_refund.signed_in_resource = @admin
1012
+ last_refund.skip_callbacks = {:before_save => true, :after_save => true}
1013
+ res = last_refund.save
1014
+ puts "second res: #{res.to_s}"
1015
+ ## now do update on this last refund, so that it will call refund_refresh, and we now expect it to have a 0 payment status.
1016
+ put shopping_payment_path({:id => last_refund.id}), {:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @headers
1017
+
1018
+ puts response.body.to_s
1019
+
1020
+ last_refund = Shopping::Payment.find(last_refund.id.to_s)
1021
+
1022
+ expect(last_refund.payment_status).to eq(0)
1023
+
1024
+ end
1025
+
1026
+
1027
+ it " -- remaining cart items are not affected after refund is approved. -- ", :refund_cart_item_status do
1028
+
1029
+
1030
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
1031
+ last_cart_item.signed_in_resource = @admin
1032
+ last_cart_item.unset_cart
1033
+
1034
+ ## create a new refund request and set it as pending.
1035
+ payment = Shopping::Payment.new
1036
+
1037
+ ## this doesn't matter while creating refunds.
1038
+ payment.payment_type = "cheque"
1039
+
1040
+
1041
+ payment.amount = -10
1042
+ payment.refund = true
1043
+ payment.resource_id = @u.id.to_s
1044
+ payment.resource_class = @u.class.name.to_s
1045
+ payment.cart_id = @cart.id.to_s
1046
+
1047
+ ## need to assign this because we are bypassing the controller.
1048
+ payment.signed_in_resource = @u
1049
+ ps = payment.save
1050
+
1051
+ ##now first assert that a refund exists whose refund status is null.
1052
+ payment = Shopping::Payment.find(payment.id.to_s)
1053
+ expect(payment.payment_status).to eq(nil)
1054
+
1055
+ put shopping_payment_path({:id => payment.id}), {payment: {payment_status: 1},:api_key => @ap_key, :current_app_id => "test_app_id", :proxy_resource_class => @u.class.name.to_s, :proxy_resource_id => @u.id.to_s}.to_json, @admin_headers
1056
+
1057
+ ## take all the cart items.
1058
+ payment = Shopping::Payment.find(payment.id.to_s)
1059
+ expect(payment.payment_status).to eq(1)
1060
+
1061
+ Shopping::CartItem.all.each do |c_item|
1062
+ if c_item.id.to_s != last_cart_item.id.to_s
1063
+ expect(c_item.accepted).to be_truthy
1064
+ end
1065
+ end
1066
+
1067
+ end
1068
+
1069
+
1070
+ end
1071
+
1072
+ context " --- update, delete, payment and refund by user -- ", :update_payment do
1073
+
1074
+ before(:example) do
1075
+ Shopping::CartItem.delete_all
1076
+ Shopping::Cart.delete_all
1077
+ Shopping::Payment.delete_all
1078
+
1079
+ ## create a cart
1080
+ @cart = Shopping::Cart.new
1081
+ @cart.resource_id = @u.id.to_s
1082
+ @cart.resource_class = @u.class.name
1083
+ @cart.save
1084
+
1085
+
1086
+ ## create five cart items and add them to the cart.
1087
+ @created_cart_item_ids = []
1088
+ 5.times do
1089
+ cart_item = Shopping::CartItem.new(attributes_for(:cart_item))
1090
+ cart_item.resource_id = @u.id.to_s
1091
+ cart_item.resource_class = @u.class.name
1092
+ cart_item.parent_id = @cart.id
1093
+ cart_item.price = 10.00
1094
+ cart_item.signed_in_resource = @u
1095
+ cart_item.save
1096
+ @created_cart_item_ids << cart_item.id.to_s
1097
+ end
1098
+ end
1099
+
1100
+
1101
+
1102
+ it " -- user cannot update a payment once made -- ", :update_payment => true do
1103
+
1104
+ ## create a payment.
1105
+ payment = Shopping::Payment.new
1106
+ payment.payment_type = "cash"
1107
+ payment.amount = 50.00
1108
+ payment.resource_id = @u.id.to_s
1109
+ payment.resource_class = @u.class.name.to_s
1110
+ payment.signed_in_resource = @u
1111
+ payment.cart_id = @cart.id.to_s
1112
+ ps = payment.save
1113
+
1114
+
1115
+ ## now try to update it.
1116
+ put shopping_payment_path({:id => payment.id}), {payment: {amount: 10.00},:api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
1117
+
1118
+
1119
+
1120
+ payment = Shopping::Payment.find(payment.id.to_s)
1121
+ expect(payment.amount).to eq(50.00)
1122
+
1123
+
1124
+
1125
+ end
1126
+
1127
+ it " -- user cannot update a refund once created -- ", :update_refund => true do
1128
+
1129
+ ## create a payment.
1130
+ payment = Shopping::Payment.new
1131
+ payment.payment_type = "cash"
1132
+ payment.amount = 50.00
1133
+ payment.resource_id = @u.id.to_s
1134
+ payment.resource_class = @u.class.name.to_s
1135
+ payment.cart_id = @cart.id.to_s
1136
+ payment.signed_in_resource = @admin
1137
+ payment.payment_status = 1
1138
+ ps = payment.save
1139
+ expect(ps).to be_truthy
1140
+
1141
+
1142
+ ## remove an item from the cart
1143
+ last_cart_item = Shopping::CartItem.find(@created_cart_item_ids.last.to_s)
1144
+ last_cart_item.signed_in_resource = @admin
1145
+ last_cart_item.unset_cart
1146
+
1147
+ cart = Shopping::Cart.find(payment.cart_id)
1148
+ cart.prepare_cart
1149
+ #puts "teh cart pending balance. is."
1150
+ #puts cart.cart_pending_balance.to_s
1151
+
1152
+ ## create a refund
1153
+ ref = Shopping::Payment.new
1154
+ ref.payment_type = "cash"
1155
+ ref.amount = 50.00
1156
+ ref.refund = true
1157
+ ref.resource_id = @u.id.to_s
1158
+ ref.resource_class = @u.class.name.to_s
1159
+ ref.signed_in_resource = @u
1160
+ ref.cart_id = @cart.id.to_s
1161
+ rs = ref.save
1162
+ #puts ref.errors.full_messages.to_s
1163
+ expect(rs).to be_truthy
1164
+
1165
+ ## now try to update it.
1166
+ put shopping_payment_path({:id => ref.id}), {payment: {refund: false},:api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
1167
+
1168
+ ref = Shopping::Payment.find(ref.id.to_s)
1169
+ expect(ref.refund).to be_truthy
1170
+
1171
+ end
1172
+
1173
+ it " -- user cannot delete a payment once made -- ", :delete_payment do
1174
+
1175
+ ## create a payment.
1176
+ payment = Shopping::Payment.new
1177
+ payment.payment_type = "cash"
1178
+ payment.amount = 50.00
1179
+ payment.resource_id = @u.id.to_s
1180
+ payment.resource_class = @u.class.name.to_s
1181
+ payment.cart_id = @cart.id.to_s
1182
+ payment.signed_in_resource = @admin
1183
+ ps = payment.save
1184
+ expect(ps).to be_truthy
1185
+
1186
+ ## now try to update it.
1187
+ delete shopping_payment_path({:id => payment.id}),{:api_key => @ap_key, :current_app_id => "test_app_id"}.to_json, @headers
1188
+
1189
+ expect(Shopping::Payment.count).to eq(1)
1190
+
1191
+ end
1192
+
1193
+ end
1194
+
1195
+ context " -- payment stores accepted cart items -- " do
1196
+
1197
+
1198
+ end
1199
+
1200
+ end
1201
+
1202
+
1203
+ =begin
1204
+
1205
+ -so basically it boils down to this->
1206
+
1207
+ -1.
1208
+ before_remove - check if status is accepted, if yes, then
1209
+ it cannot be removed, provide a hook to override this method as per the needs.
1210
+
1211
+ - here only there is fork - that if removal fails, then provide:
1212
+ on_cannot_remove_item_hook
1213
+
1214
+ -2.
1215
+ after_remove - this is not done automatically. He has to request a refund, by creating a refund request.
1216
+
1217
+ -3. provide a method to cancel all the tests in the cart, this is done by calling destroy cart - if call destroy cart, then where will we show the refund?
1218
+ - refund should also be shown in the cart.
1219
+
1220
+ so you cannot destroy the cart if payments have already been made to it.
1221
+ you can only remove items from the cart, i.e all the items.
1222
+ and there you can show the refunds.
1223
+
1224
+ so before destroy cart - check if payments have been made, and if yes, then don't destroy the cart.
1225
+
1226
+
1227
+ -4. basically we have to make a payment to the user.
1228
+ thats the essence.
1229
+ so the payment should have a pay_to
1230
+ it will also have a type, this will be the same as the last payment made by the user.
1231
+ it will have to be done by the business seperately.
1232
+ notification behaviour has to be defined, for eg:
1233
+
1234
+ refund can be by cheque only at this stage.
1235
+ the refund is successfull if the cheque is ready.
1236
+
1237
+ After refund success, a callback has to notify the payee, that his cheque can be picked up,
1238
+
1239
+ at this stage the acknowledgement proof can be picked up, saying that his payment was successfully received.
1240
+
1241
+ - 5. refund lifecycle.
1242
+
1243
+ a. first the refund is created, by the user.
1244
+ b. before_creating the refund, check if the pending_balance is -ve, only then create the refund, otherwise not.
1245
+ c. refund now has a status of 0, i.e pending.
1246
+ d. before_updating refund, if the refund is being set as accepted, then it has to check if there is any negative balance and only then update it.
1247
+ while changing such a status, it has to set the amount of the refund, and also delete any other refund requests, created before now.
1248
+
1249
+ =end
1250
+
1251
+
1252
+
1253
+
1254
+