Commit 61dfd21faf76766294bc167ffeb5ef06f191c2b5
1 parent
a341e67e
Exists in
master
and in
29 other branches
Moving controller out of the profile scope
Showing
4 changed files
with
502 additions
and
501 deletions
Show diff stats
plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
0 → 100644
... | ... | @@ -0,0 +1,289 @@ |
1 | +require 'base64' | |
2 | + | |
3 | +class ShoppingCartPluginController < ProfileController | |
4 | + | |
5 | + include ShoppingCartPlugin::CartHelper | |
6 | + | |
7 | + append_view_path File.join(File.dirname(__FILE__) + '/../views') | |
8 | + before_filter :login_required, :only => [] | |
9 | + | |
10 | + before_filter :login_required, :only => [] | |
11 | + | |
12 | + def get | |
13 | + has_products = !cart.nil? && (cart[:items].keys.size > 0) || false | |
14 | + config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products } | |
15 | + render :text => config.to_json | |
16 | + end | |
17 | + | |
18 | + def add | |
19 | + self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? | |
20 | + if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) | |
21 | + self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? | |
22 | + self.cart[:items][product.id] += 1 | |
23 | + render :text => { | |
24 | + :ok => true, | |
25 | + :error => {:code => 0}, | |
26 | + :products => [{ | |
27 | + :id => product.id, | |
28 | + :name => product.name, | |
29 | + :price => get_price(product, profile.environment), | |
30 | + :description => product.description, | |
31 | + :picture => product.default_image(:minor), | |
32 | + :quantity => self.cart[:items][product.id] | |
33 | + }] | |
34 | + }.to_json | |
35 | + end | |
36 | + end | |
37 | + | |
38 | + def remove | |
39 | + id = params[:id].to_i | |
40 | + if validate_cart_presence && validate_cart_has_product(id) | |
41 | + self.cart[:items].delete(id) | |
42 | + self.cart = nil if self.cart[:items].empty? | |
43 | + render :text => { | |
44 | + :ok => true, | |
45 | + :error => {:code => 0}, | |
46 | + :product_id => id | |
47 | + }.to_json | |
48 | + end | |
49 | + end | |
50 | + | |
51 | + def list | |
52 | + if validate_cart_presence | |
53 | + products = self.cart[:items].collect do |id, quantity| | |
54 | + product = Product.find(id) | |
55 | + { :id => product.id, | |
56 | + :name => product.name, | |
57 | + :price => get_price(product, profile.environment), | |
58 | + :description => product.description, | |
59 | + :picture => product.default_image(:minor), | |
60 | + :quantity => quantity | |
61 | + } | |
62 | + end | |
63 | + render :text => { | |
64 | + :ok => true, | |
65 | + :error => {:code => 0}, | |
66 | + :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, | |
67 | + :products => products | |
68 | + }.to_json | |
69 | + end | |
70 | + end | |
71 | + | |
72 | + def update_quantity | |
73 | + quantity = params[:quantity].to_i | |
74 | + id = params[:id].to_i | |
75 | + if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) | |
76 | + product = Product.find(id) | |
77 | + self.cart[:items][product.id] = quantity | |
78 | + render :text => { | |
79 | + :ok => true, | |
80 | + :error => {:code => 0}, | |
81 | + :product_id => id, | |
82 | + :quantity => quantity | |
83 | + }.to_json | |
84 | + end | |
85 | + end | |
86 | + | |
87 | + def clean | |
88 | + self.cart = nil | |
89 | + render :text => { | |
90 | + :ok => true, | |
91 | + :error => {:code => 0} | |
92 | + }.to_json | |
93 | + end | |
94 | + | |
95 | + def buy | |
96 | + @environment = profile.environment | |
97 | + @cart = cart | |
98 | + render :layout => false | |
99 | + end | |
100 | + | |
101 | + def send_request | |
102 | + register_order(params[:customer], self.cart[:items]) | |
103 | + begin | |
104 | + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items]) | |
105 | + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items]) | |
106 | + render :text => { | |
107 | + :ok => true, | |
108 | + :message => _('Request sent successfully. Check your email.'), | |
109 | + :error => {:code => 0} | |
110 | + }.to_json | |
111 | + rescue Exception => exception | |
112 | + render :text => { | |
113 | + :ok => false, | |
114 | + :error => { | |
115 | + :code => 6, | |
116 | + :message => exception.message | |
117 | + } | |
118 | + }.to_json | |
119 | + end | |
120 | + end | |
121 | + | |
122 | + def visibility | |
123 | + render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json | |
124 | + end | |
125 | + | |
126 | + def show | |
127 | + begin | |
128 | + self.cart[:visibility] = true | |
129 | + render :text => { | |
130 | + :ok => true, | |
131 | + :message => _('Basket displayed.'), | |
132 | + :error => {:code => 0} | |
133 | + }.to_json | |
134 | + rescue Exception => exception | |
135 | + render :text => { | |
136 | + :ok => false, | |
137 | + :error => { | |
138 | + :code => 7, | |
139 | + :message => exception.message | |
140 | + } | |
141 | + }.to_json | |
142 | + end | |
143 | + end | |
144 | + | |
145 | + def hide | |
146 | + begin | |
147 | + self.cart[:visibility] = false | |
148 | + render :text => { | |
149 | + :ok => true, | |
150 | + :message => _('Basket hidden.'), | |
151 | + :error => {:code => 0} | |
152 | + }.to_json | |
153 | + rescue Exception => exception | |
154 | + render :text => { | |
155 | + :ok => false, | |
156 | + :error => { | |
157 | + :code => 8, | |
158 | + :message => exception.message | |
159 | + } | |
160 | + }.to_json | |
161 | + end | |
162 | + end | |
163 | + | |
164 | + private | |
165 | + | |
166 | + def validate_same_enterprise | |
167 | + if profile.id != self.cart[:enterprise_id] | |
168 | + render :text => { | |
169 | + :ok => false, | |
170 | + :error => { | |
171 | + :code => 1, | |
172 | + :message => _("Can't join items from different enterprises.") | |
173 | + } | |
174 | + }.to_json | |
175 | + return false | |
176 | + end | |
177 | + true | |
178 | + end | |
179 | + | |
180 | + def validate_cart_presence | |
181 | + if self.cart.nil? | |
182 | + render :text => { | |
183 | + :ok => false, | |
184 | + :error => { | |
185 | + :code => 2, | |
186 | + :message => _("There is no basket.") | |
187 | + } | |
188 | + }.to_json | |
189 | + return false | |
190 | + end | |
191 | + true | |
192 | + end | |
193 | + | |
194 | + def validate_enterprise_has_product(id) | |
195 | + begin | |
196 | + product = profile.products.find(id) | |
197 | + rescue | |
198 | + render :text => { | |
199 | + :ok => false, | |
200 | + :error => { | |
201 | + :code => 3, | |
202 | + :message => _("This enterprise doesn't have this product.") | |
203 | + } | |
204 | + }.to_json | |
205 | + return nil | |
206 | + end | |
207 | + product | |
208 | + end | |
209 | + | |
210 | + def validate_cart_has_product(id) | |
211 | + if !self.cart[:items].has_key?(id) | |
212 | + render :text => { | |
213 | + :ok => false, | |
214 | + :error => { | |
215 | + :code => 4, | |
216 | + :message => _("The basket doesn't have this product.") | |
217 | + } | |
218 | + }.to_json | |
219 | + return false | |
220 | + end | |
221 | + true | |
222 | + end | |
223 | + | |
224 | + def validate_item_quantity(quantity) | |
225 | + if quantity.to_i < 1 | |
226 | + render :text => { | |
227 | + :ok => false, | |
228 | + :error => { | |
229 | + :code => 5, | |
230 | + :message => _("Invalid quantity.") | |
231 | + } | |
232 | + }.to_json | |
233 | + return false | |
234 | + end | |
235 | + true | |
236 | + end | |
237 | + | |
238 | + def register_order(custumer, items) | |
239 | + new_items = {} | |
240 | + items.each do |id, quantity| | |
241 | + product = Product.find(id) | |
242 | + price = product.price || 0 | |
243 | + new_items[id] = {:quantity => quantity, :price => price, :name => product.name} | |
244 | + end | |
245 | + ShoppingCartPlugin::PurchaseOrder.create!( | |
246 | + :seller => profile, | |
247 | + :customer => user, | |
248 | + :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, | |
249 | + :products_list => new_items, | |
250 | + :customer_name => params[:customer][:name], | |
251 | + :customer_email => params[:customer][:email], | |
252 | + :customer_contact_phone => params[:customer][:contact_phone], | |
253 | + :customer_address => params[:customer][:address], | |
254 | + :customer_city => params[:customer][:city], | |
255 | + :customer_zip_code => params[:customer][:zip_code] | |
256 | + ) | |
257 | + end | |
258 | + | |
259 | + protected | |
260 | + | |
261 | + def cart | |
262 | + @cart ||= | |
263 | + begin | |
264 | + cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil | |
265 | + end | |
266 | + @cart | |
267 | + end | |
268 | + | |
269 | + def cart=(data) | |
270 | + @cart = data | |
271 | + end | |
272 | + | |
273 | + after_filter :save_cookie | |
274 | + def save_cookie | |
275 | + if @cart.nil? && cookies[cookie_key] | |
276 | + cookies.delete(cookie_key) | |
277 | + else | |
278 | + cookies[cookie_key] = { | |
279 | + :value => Base64.encode64(@cart.to_yaml), | |
280 | + :path => "/profile/#{profile.identifier}/plugin/shopping_cart" | |
281 | + } | |
282 | + end | |
283 | + end | |
284 | + | |
285 | + def cookie_key | |
286 | + :_noosfero_session_shopping_cart | |
287 | + end | |
288 | + | |
289 | +end | ... | ... |
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
... | ... | @@ -1,288 +0,0 @@ |
1 | -require 'base64' | |
2 | - | |
3 | -include ShoppingCartPlugin::CartHelper | |
4 | - | |
5 | -class ShoppingCartPluginProfileController < ProfileController | |
6 | - append_view_path File.join(File.dirname(__FILE__) + '/../views') | |
7 | - before_filter :login_required, :only => [] | |
8 | - | |
9 | - before_filter :login_required, :only => [] | |
10 | - | |
11 | - def get | |
12 | - has_products = !cart.nil? && (cart[:items].keys.size > 0) || false | |
13 | - config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products } | |
14 | - render :text => config.to_json | |
15 | - end | |
16 | - | |
17 | - def add | |
18 | - self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? | |
19 | - if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) | |
20 | - self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? | |
21 | - self.cart[:items][product.id] += 1 | |
22 | - render :text => { | |
23 | - :ok => true, | |
24 | - :error => {:code => 0}, | |
25 | - :products => [{ | |
26 | - :id => product.id, | |
27 | - :name => product.name, | |
28 | - :price => get_price(product, profile.environment), | |
29 | - :description => product.description, | |
30 | - :picture => product.default_image(:minor), | |
31 | - :quantity => self.cart[:items][product.id] | |
32 | - }] | |
33 | - }.to_json | |
34 | - end | |
35 | - end | |
36 | - | |
37 | - def remove | |
38 | - id = params[:id].to_i | |
39 | - if validate_cart_presence && validate_cart_has_product(id) | |
40 | - self.cart[:items].delete(id) | |
41 | - self.cart = nil if self.cart[:items].empty? | |
42 | - render :text => { | |
43 | - :ok => true, | |
44 | - :error => {:code => 0}, | |
45 | - :product_id => id | |
46 | - }.to_json | |
47 | - end | |
48 | - end | |
49 | - | |
50 | - def list | |
51 | - if validate_cart_presence | |
52 | - products = self.cart[:items].collect do |id, quantity| | |
53 | - product = Product.find(id) | |
54 | - { :id => product.id, | |
55 | - :name => product.name, | |
56 | - :price => get_price(product, profile.environment), | |
57 | - :description => product.description, | |
58 | - :picture => product.default_image(:minor), | |
59 | - :quantity => quantity | |
60 | - } | |
61 | - end | |
62 | - render :text => { | |
63 | - :ok => true, | |
64 | - :error => {:code => 0}, | |
65 | - :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, | |
66 | - :products => products | |
67 | - }.to_json | |
68 | - end | |
69 | - end | |
70 | - | |
71 | - def update_quantity | |
72 | - quantity = params[:quantity].to_i | |
73 | - id = params[:id].to_i | |
74 | - if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) | |
75 | - product = Product.find(id) | |
76 | - self.cart[:items][product.id] = quantity | |
77 | - render :text => { | |
78 | - :ok => true, | |
79 | - :error => {:code => 0}, | |
80 | - :product_id => id, | |
81 | - :quantity => quantity | |
82 | - }.to_json | |
83 | - end | |
84 | - end | |
85 | - | |
86 | - def clean | |
87 | - self.cart = nil | |
88 | - render :text => { | |
89 | - :ok => true, | |
90 | - :error => {:code => 0} | |
91 | - }.to_json | |
92 | - end | |
93 | - | |
94 | - def buy | |
95 | - @environment = profile.environment | |
96 | - @cart = cart | |
97 | - render :layout => false | |
98 | - end | |
99 | - | |
100 | - def send_request | |
101 | - register_order(params[:customer], self.cart[:items]) | |
102 | - begin | |
103 | - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items]) | |
104 | - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items]) | |
105 | - render :text => { | |
106 | - :ok => true, | |
107 | - :message => _('Request sent successfully. Check your email.'), | |
108 | - :error => {:code => 0} | |
109 | - }.to_json | |
110 | - rescue Exception => exception | |
111 | - render :text => { | |
112 | - :ok => false, | |
113 | - :error => { | |
114 | - :code => 6, | |
115 | - :message => exception.message | |
116 | - } | |
117 | - }.to_json | |
118 | - end | |
119 | - end | |
120 | - | |
121 | - def visibility | |
122 | - render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json | |
123 | - end | |
124 | - | |
125 | - def show | |
126 | - begin | |
127 | - self.cart[:visibility] = true | |
128 | - render :text => { | |
129 | - :ok => true, | |
130 | - :message => _('Basket displayed.'), | |
131 | - :error => {:code => 0} | |
132 | - }.to_json | |
133 | - rescue Exception => exception | |
134 | - render :text => { | |
135 | - :ok => false, | |
136 | - :error => { | |
137 | - :code => 7, | |
138 | - :message => exception.message | |
139 | - } | |
140 | - }.to_json | |
141 | - end | |
142 | - end | |
143 | - | |
144 | - def hide | |
145 | - begin | |
146 | - self.cart[:visibility] = false | |
147 | - render :text => { | |
148 | - :ok => true, | |
149 | - :message => _('Basket hidden.'), | |
150 | - :error => {:code => 0} | |
151 | - }.to_json | |
152 | - rescue Exception => exception | |
153 | - render :text => { | |
154 | - :ok => false, | |
155 | - :error => { | |
156 | - :code => 8, | |
157 | - :message => exception.message | |
158 | - } | |
159 | - }.to_json | |
160 | - end | |
161 | - end | |
162 | - | |
163 | - private | |
164 | - | |
165 | - def validate_same_enterprise | |
166 | - if profile.id != self.cart[:enterprise_id] | |
167 | - render :text => { | |
168 | - :ok => false, | |
169 | - :error => { | |
170 | - :code => 1, | |
171 | - :message => _("Can't join items from different enterprises.") | |
172 | - } | |
173 | - }.to_json | |
174 | - return false | |
175 | - end | |
176 | - true | |
177 | - end | |
178 | - | |
179 | - def validate_cart_presence | |
180 | - if self.cart.nil? | |
181 | - render :text => { | |
182 | - :ok => false, | |
183 | - :error => { | |
184 | - :code => 2, | |
185 | - :message => _("There is no basket.") | |
186 | - } | |
187 | - }.to_json | |
188 | - return false | |
189 | - end | |
190 | - true | |
191 | - end | |
192 | - | |
193 | - def validate_enterprise_has_product(id) | |
194 | - begin | |
195 | - product = profile.products.find(id) | |
196 | - rescue | |
197 | - render :text => { | |
198 | - :ok => false, | |
199 | - :error => { | |
200 | - :code => 3, | |
201 | - :message => _("This enterprise doesn't have this product.") | |
202 | - } | |
203 | - }.to_json | |
204 | - return nil | |
205 | - end | |
206 | - product | |
207 | - end | |
208 | - | |
209 | - def validate_cart_has_product(id) | |
210 | - if !self.cart[:items].has_key?(id) | |
211 | - render :text => { | |
212 | - :ok => false, | |
213 | - :error => { | |
214 | - :code => 4, | |
215 | - :message => _("The basket doesn't have this product.") | |
216 | - } | |
217 | - }.to_json | |
218 | - return false | |
219 | - end | |
220 | - true | |
221 | - end | |
222 | - | |
223 | - def validate_item_quantity(quantity) | |
224 | - if quantity.to_i < 1 | |
225 | - render :text => { | |
226 | - :ok => false, | |
227 | - :error => { | |
228 | - :code => 5, | |
229 | - :message => _("Invalid quantity.") | |
230 | - } | |
231 | - }.to_json | |
232 | - return false | |
233 | - end | |
234 | - true | |
235 | - end | |
236 | - | |
237 | - def register_order(custumer, items) | |
238 | - new_items = {} | |
239 | - items.each do |id, quantity| | |
240 | - product = Product.find(id) | |
241 | - price = product.price || 0 | |
242 | - new_items[id] = {:quantity => quantity, :price => price, :name => product.name} | |
243 | - end | |
244 | - ShoppingCartPlugin::PurchaseOrder.create!( | |
245 | - :seller => profile, | |
246 | - :customer => user, | |
247 | - :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, | |
248 | - :products_list => new_items, | |
249 | - :customer_name => params[:customer][:name], | |
250 | - :customer_email => params[:customer][:email], | |
251 | - :customer_contact_phone => params[:customer][:contact_phone], | |
252 | - :customer_address => params[:customer][:address], | |
253 | - :customer_city => params[:customer][:city], | |
254 | - :customer_zip_code => params[:customer][:zip_code] | |
255 | - ) | |
256 | - end | |
257 | - | |
258 | - protected | |
259 | - | |
260 | - def cart | |
261 | - @cart ||= | |
262 | - begin | |
263 | - cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil | |
264 | - end | |
265 | - @cart | |
266 | - end | |
267 | - | |
268 | - def cart=(data) | |
269 | - @cart = data | |
270 | - end | |
271 | - | |
272 | - after_filter :save_cookie | |
273 | - def save_cookie | |
274 | - if @cart.nil? && cookies[cookie_key] | |
275 | - cookies.delete(cookie_key) | |
276 | - else | |
277 | - cookies[cookie_key] = { | |
278 | - :value => Base64.encode64(@cart.to_yaml), | |
279 | - :path => "/profile/#{profile.identifier}/plugin/shopping_cart" | |
280 | - } | |
281 | - end | |
282 | - end | |
283 | - | |
284 | - def cookie_key | |
285 | - :_noosfero_session_shopping_cart | |
286 | - end | |
287 | - | |
288 | -end |
plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb
0 → 100644
... | ... | @@ -0,0 +1,213 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | |
2 | +require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_profile_controller' | |
3 | + | |
4 | +# Re-raise errors caught by the controller. | |
5 | +class ShoppingCartPluginController; def rescue_action(e) raise e end; end | |
6 | + | |
7 | +class ShoppingCartPluginControllerTest < ActionController::TestCase | |
8 | + | |
9 | + def setup | |
10 | + @controller = ShoppingCartPluginController.new | |
11 | + @request = ActionController::TestRequest.new | |
12 | + @response = ActionController::TestResponse.new | |
13 | + @enterprise = fast_create(Enterprise) | |
14 | + @product = fast_create(Product, :enterprise_id => @enterprise.id) | |
15 | + end | |
16 | + attr_reader :enterprise | |
17 | + attr_reader :product | |
18 | + | |
19 | + should 'add a new product to cart' do | |
20 | + get :add, :profile => enterprise.identifier, :id => product.id | |
21 | + | |
22 | + assert product_in_cart?(product) | |
23 | + assert_equal 1, product_quantity(product) | |
24 | + end | |
25 | + | |
26 | + should 'grow quantity through add' do | |
27 | + get :add, :profile => enterprise.identifier, :id => product.id | |
28 | + assert_equal 1, product_quantity(product) | |
29 | + | |
30 | + get :add, :profile => enterprise.identifier, :id => product.id | |
31 | + assert_equal 2, product_quantity(product) | |
32 | + end | |
33 | + | |
34 | + should 'not add product to cart if it does not exists' do | |
35 | + assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 } | |
36 | + | |
37 | + assert !product_in_cart?(product) | |
38 | + assert !response_ok? | |
39 | + assert 3, reponse_error_code | |
40 | + end | |
41 | + | |
42 | + should 'remove cart if the product being removed is the last one' do | |
43 | + get :add, :profile => enterprise.identifier, :id => product.id | |
44 | + assert cart? | |
45 | + | |
46 | + get :remove, :profile => enterprise.identifier, :id => product.id | |
47 | + assert !cart? | |
48 | + end | |
49 | + | |
50 | + should 'not try to remove a product if there is no cart' do | |
51 | + instantiate_cart | |
52 | + assert !cart? | |
53 | + | |
54 | + assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | |
55 | + assert !response_ok? | |
56 | + assert_equal 2, reponse_error_code | |
57 | + end | |
58 | + | |
59 | + should 'just remove product if there are other products on cart' do | |
60 | + another_product = fast_create(Product, :enterprise_id => enterprise.id) | |
61 | + get :add, :profile => enterprise.identifier, :id => product.id | |
62 | + get :add, :profile => enterprise.identifier, :id => another_product.id | |
63 | + | |
64 | + get :remove, :profile => enterprise.identifier, :id => product.id | |
65 | + assert cart? | |
66 | + assert !product_in_cart?(product) | |
67 | + end | |
68 | + | |
69 | + should 'not try to remove a product that is not in the cart' do | |
70 | + get :add, :profile => enterprise.identifier, :id => product.id | |
71 | + assert cart? | |
72 | + assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | |
73 | + | |
74 | + assert !response_ok? | |
75 | + assert_equal 4, reponse_error_code | |
76 | + end | |
77 | + | |
78 | + should 'not try to list the cart if there is no cart' do | |
79 | + instantiate_cart | |
80 | + assert !cart? | |
81 | + | |
82 | + assert_nothing_raised { get :list, :profile => enterprise.identifier } | |
83 | + assert !response_ok? | |
84 | + assert_equal 2, reponse_error_code | |
85 | + end | |
86 | + | |
87 | + should 'list products without errors' do | |
88 | + get :add, :profile => enterprise.identifier, :id => product.id | |
89 | + | |
90 | + assert_nothing_raised { get :list, :profile => enterprise.identifier } | |
91 | + assert response_ok? | |
92 | + end | |
93 | + | |
94 | + should 'update the quantity of a product' do | |
95 | + get :add, :profile => enterprise.identifier, :id => product.id | |
96 | + assert 1, product_quantity(product) | |
97 | + | |
98 | + get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3 | |
99 | + assert 3, product_quantity(product) | |
100 | + end | |
101 | + | |
102 | + should 'not try to update quantity the quantity of a product if there is no cart' do | |
103 | + instantiate_cart | |
104 | + assert !cart? | |
105 | + | |
106 | + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | |
107 | + assert !response_ok? | |
108 | + assert_equal 2, reponse_error_code | |
109 | + end | |
110 | + | |
111 | + should 'not try to update the quantity of a product that is not in the cart' do | |
112 | + get :add, :profile => enterprise.identifier, :id => product.id | |
113 | + assert cart? | |
114 | + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | |
115 | + | |
116 | + assert !response_ok? | |
117 | + assert_equal 4, reponse_error_code | |
118 | + end | |
119 | + | |
120 | + should 'not update the quantity of a product with a invalid value' do | |
121 | + get :add, :profile => enterprise.identifier, :id => product.id | |
122 | + | |
123 | + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1} | |
124 | + assert !response_ok? | |
125 | + assert_equal 5, reponse_error_code | |
126 | + | |
127 | + assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'} | |
128 | + assert !response_ok? | |
129 | + assert_equal 5, reponse_error_code | |
130 | + end | |
131 | + | |
132 | + should 'clean the cart' do | |
133 | + another_product = fast_create(Product, :enterprise_id => enterprise.id) | |
134 | + get :add, :profile => enterprise.identifier, :id => product.id | |
135 | + get :add, :profile => enterprise.identifier, :id => another_product.id | |
136 | + | |
137 | + assert_nothing_raised { get :clean, :profile => enterprise.identifier } | |
138 | + assert !cart? | |
139 | + end | |
140 | + | |
141 | + should 'not crash if there is no cart' do | |
142 | + instantiate_cart | |
143 | + assert !cart? | |
144 | + assert_nothing_raised { get :clean, :profile => enterprise.identifier } | |
145 | + end | |
146 | + | |
147 | + should 'register order on send request' do | |
148 | + product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) | |
149 | + product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) | |
150 | + @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) | |
151 | + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | |
152 | + post :send_request, | |
153 | + :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | |
154 | + :profile => enterprise.identifier | |
155 | + end | |
156 | + | |
157 | + order = ShoppingCartPlugin::PurchaseOrder.last | |
158 | + | |
159 | + assert_equal 1.99, order.products_list[product1.id][:price] | |
160 | + assert_equal 1, order.products_list[product1.id][:quantity] | |
161 | + assert_equal 2.23, order.products_list[product2.id][:price] | |
162 | + assert_equal 2, order.products_list[product2.id][:quantity] | |
163 | + assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status | |
164 | + end | |
165 | + | |
166 | + should 'register order on send request and not crash if product is not defined' do | |
167 | + product1 = fast_create(Product, :enterprise_id => enterprise.id) | |
168 | + @controller.stubs(:cart).returns({:items => {product1.id => 1}}) | |
169 | + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | |
170 | + post :send_request, | |
171 | + :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | |
172 | + :profile => enterprise.identifier | |
173 | + end | |
174 | + | |
175 | + order = ShoppingCartPlugin::PurchaseOrder.last | |
176 | + | |
177 | + assert_equal 0, order.products_list[product1.id][:price] | |
178 | + end | |
179 | + | |
180 | + private | |
181 | + | |
182 | + def json_response | |
183 | + ActiveSupport::JSON.decode @response.body | |
184 | + end | |
185 | + | |
186 | + def cart? | |
187 | + @controller.send(:cart).nil? | |
188 | + end | |
189 | + | |
190 | + def product_in_cart?(product) | |
191 | + @controller.send(:cart)[:items].has_key?(product.id) | |
192 | + end | |
193 | + | |
194 | + def product_quantity(product) | |
195 | + @controller.send(:cart)[:items][product.id] | |
196 | + end | |
197 | + | |
198 | + def response_ok? | |
199 | + json_response['ok'] | |
200 | + end | |
201 | + | |
202 | + def reponse_error_code | |
203 | + json_response['error']['code'] | |
204 | + end | |
205 | + | |
206 | + # temporary hack...if I don't do this the session stays as an Array instead | |
207 | + # of a TestSession | |
208 | + def instantiate_cart | |
209 | + get :add, :profile => enterprise.identifier, :id => product.id | |
210 | + get :remove, :profile => enterprise.identifier, :id => product.id | |
211 | + end | |
212 | + | |
213 | +end | ... | ... |
plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb
... | ... | @@ -1,213 +0,0 @@ |
1 | -require File.dirname(__FILE__) + '/../../../../test/test_helper' | |
2 | -require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_profile_controller' | |
3 | - | |
4 | -# Re-raise errors caught by the controller. | |
5 | -class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end | |
6 | - | |
7 | -class ShoppingCartPluginProfileControllerTest < ActionController::TestCase | |
8 | - | |
9 | - def setup | |
10 | - @controller = ShoppingCartPluginProfileController.new | |
11 | - @request = ActionController::TestRequest.new | |
12 | - @response = ActionController::TestResponse.new | |
13 | - @enterprise = fast_create(Enterprise) | |
14 | - @product = fast_create(Product, :enterprise_id => @enterprise.id) | |
15 | - end | |
16 | - attr_reader :enterprise | |
17 | - attr_reader :product | |
18 | - | |
19 | - should 'add a new product to cart' do | |
20 | - get :add, :profile => enterprise.identifier, :id => product.id | |
21 | - | |
22 | - assert product_in_cart?(product) | |
23 | - assert_equal 1, product_quantity(product) | |
24 | - end | |
25 | - | |
26 | - should 'grow quantity through add' do | |
27 | - get :add, :profile => enterprise.identifier, :id => product.id | |
28 | - assert_equal 1, product_quantity(product) | |
29 | - | |
30 | - get :add, :profile => enterprise.identifier, :id => product.id | |
31 | - assert_equal 2, product_quantity(product) | |
32 | - end | |
33 | - | |
34 | - should 'not add product to cart if it does not exists' do | |
35 | - assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 } | |
36 | - | |
37 | - assert !product_in_cart?(product) | |
38 | - assert !response_ok? | |
39 | - assert 3, reponse_error_code | |
40 | - end | |
41 | - | |
42 | - should 'remove cart if the product being removed is the last one' do | |
43 | - get :add, :profile => enterprise.identifier, :id => product.id | |
44 | - assert cart? | |
45 | - | |
46 | - get :remove, :profile => enterprise.identifier, :id => product.id | |
47 | - assert !cart? | |
48 | - end | |
49 | - | |
50 | - should 'not try to remove a product if there is no cart' do | |
51 | - instantiate_cart | |
52 | - assert !cart? | |
53 | - | |
54 | - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | |
55 | - assert !response_ok? | |
56 | - assert_equal 2, reponse_error_code | |
57 | - end | |
58 | - | |
59 | - should 'just remove product if there are other products on cart' do | |
60 | - another_product = fast_create(Product, :enterprise_id => enterprise.id) | |
61 | - get :add, :profile => enterprise.identifier, :id => product.id | |
62 | - get :add, :profile => enterprise.identifier, :id => another_product.id | |
63 | - | |
64 | - get :remove, :profile => enterprise.identifier, :id => product.id | |
65 | - assert cart? | |
66 | - assert !product_in_cart?(product) | |
67 | - end | |
68 | - | |
69 | - should 'not try to remove a product that is not in the cart' do | |
70 | - get :add, :profile => enterprise.identifier, :id => product.id | |
71 | - assert cart? | |
72 | - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | |
73 | - | |
74 | - assert !response_ok? | |
75 | - assert_equal 4, reponse_error_code | |
76 | - end | |
77 | - | |
78 | - should 'not try to list the cart if there is no cart' do | |
79 | - instantiate_cart | |
80 | - assert !cart? | |
81 | - | |
82 | - assert_nothing_raised { get :list, :profile => enterprise.identifier } | |
83 | - assert !response_ok? | |
84 | - assert_equal 2, reponse_error_code | |
85 | - end | |
86 | - | |
87 | - should 'list products without errors' do | |
88 | - get :add, :profile => enterprise.identifier, :id => product.id | |
89 | - | |
90 | - assert_nothing_raised { get :list, :profile => enterprise.identifier } | |
91 | - assert response_ok? | |
92 | - end | |
93 | - | |
94 | - should 'update the quantity of a product' do | |
95 | - get :add, :profile => enterprise.identifier, :id => product.id | |
96 | - assert 1, product_quantity(product) | |
97 | - | |
98 | - get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3 | |
99 | - assert 3, product_quantity(product) | |
100 | - end | |
101 | - | |
102 | - should 'not try to update quantity the quantity of a product if there is no cart' do | |
103 | - instantiate_cart | |
104 | - assert !cart? | |
105 | - | |
106 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | |
107 | - assert !response_ok? | |
108 | - assert_equal 2, reponse_error_code | |
109 | - end | |
110 | - | |
111 | - should 'not try to update the quantity of a product that is not in the cart' do | |
112 | - get :add, :profile => enterprise.identifier, :id => product.id | |
113 | - assert cart? | |
114 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | |
115 | - | |
116 | - assert !response_ok? | |
117 | - assert_equal 4, reponse_error_code | |
118 | - end | |
119 | - | |
120 | - should 'not update the quantity of a product with a invalid value' do | |
121 | - get :add, :profile => enterprise.identifier, :id => product.id | |
122 | - | |
123 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1} | |
124 | - assert !response_ok? | |
125 | - assert_equal 5, reponse_error_code | |
126 | - | |
127 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'} | |
128 | - assert !response_ok? | |
129 | - assert_equal 5, reponse_error_code | |
130 | - end | |
131 | - | |
132 | - should 'clean the cart' do | |
133 | - another_product = fast_create(Product, :enterprise_id => enterprise.id) | |
134 | - get :add, :profile => enterprise.identifier, :id => product.id | |
135 | - get :add, :profile => enterprise.identifier, :id => another_product.id | |
136 | - | |
137 | - assert_nothing_raised { get :clean, :profile => enterprise.identifier } | |
138 | - assert !cart? | |
139 | - end | |
140 | - | |
141 | - should 'not crash if there is no cart' do | |
142 | - instantiate_cart | |
143 | - assert !cart? | |
144 | - assert_nothing_raised { get :clean, :profile => enterprise.identifier } | |
145 | - end | |
146 | - | |
147 | - should 'register order on send request' do | |
148 | - product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) | |
149 | - product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) | |
150 | - @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) | |
151 | - assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | |
152 | - post :send_request, | |
153 | - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | |
154 | - :profile => enterprise.identifier | |
155 | - end | |
156 | - | |
157 | - order = ShoppingCartPlugin::PurchaseOrder.last | |
158 | - | |
159 | - assert_equal 1.99, order.products_list[product1.id][:price] | |
160 | - assert_equal 1, order.products_list[product1.id][:quantity] | |
161 | - assert_equal 2.23, order.products_list[product2.id][:price] | |
162 | - assert_equal 2, order.products_list[product2.id][:quantity] | |
163 | - assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status | |
164 | - end | |
165 | - | |
166 | - should 'register order on send request and not crash if product is not defined' do | |
167 | - product1 = fast_create(Product, :enterprise_id => enterprise.id) | |
168 | - @controller.stubs(:cart).returns({:items => {product1.id => 1}}) | |
169 | - assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | |
170 | - post :send_request, | |
171 | - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | |
172 | - :profile => enterprise.identifier | |
173 | - end | |
174 | - | |
175 | - order = ShoppingCartPlugin::PurchaseOrder.last | |
176 | - | |
177 | - assert_equal 0, order.products_list[product1.id][:price] | |
178 | - end | |
179 | - | |
180 | - private | |
181 | - | |
182 | - def json_response | |
183 | - ActiveSupport::JSON.decode @response.body | |
184 | - end | |
185 | - | |
186 | - def cart? | |
187 | - @controller.send(:cart).nil? | |
188 | - end | |
189 | - | |
190 | - def product_in_cart?(product) | |
191 | - @controller.send(:cart)[:items].has_key?(product.id) | |
192 | - end | |
193 | - | |
194 | - def product_quantity(product) | |
195 | - @controller.send(:cart)[:items][product.id] | |
196 | - end | |
197 | - | |
198 | - def response_ok? | |
199 | - json_response['ok'] | |
200 | - end | |
201 | - | |
202 | - def reponse_error_code | |
203 | - json_response['error']['code'] | |
204 | - end | |
205 | - | |
206 | - # temporary hack...if I don't do this the session stays as an Array instead | |
207 | - # of a TestSession | |
208 | - def instantiate_cart | |
209 | - get :add, :profile => enterprise.identifier, :id => product.id | |
210 | - get :remove, :profile => enterprise.identifier, :id => product.id | |
211 | - end | |
212 | - | |
213 | -end |