Commit 72d8ae0c1987ec0269737996950a9d4b3b61bc4f
1 parent
212236ed
Exists in
master
and in
29 other branches
Finish fixing the tests.
Fixing the functional tests was easier than I though it would. :)
Showing
2 changed files
with
43 additions
and
38 deletions
Show diff stats
plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
@@ -278,7 +278,7 @@ class ShoppingCartPluginController < PublicController | @@ -278,7 +278,7 @@ class ShoppingCartPluginController < PublicController | ||
278 | 278 | ||
279 | after_filter :save_cookie | 279 | after_filter :save_cookie |
280 | def save_cookie | 280 | def save_cookie |
281 | - if @cart.nil? && cookies[cookie_key] | 281 | + if @cart.nil? |
282 | cookies.delete(cookie_key) | 282 | cookies.delete(cookie_key) |
283 | else | 283 | else |
284 | cookies[cookie_key] = { | 284 | cookies[cookie_key] = { |
plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb
@@ -16,23 +16,28 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -16,23 +16,28 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
16 | attr_reader :enterprise | 16 | attr_reader :enterprise |
17 | attr_reader :product | 17 | attr_reader :product |
18 | 18 | ||
19 | + should 'return no cookie for an empty cart' do | ||
20 | + get :get | ||
21 | + assert_nil @response.cookies[:_noosfero_plugin_shopping_cart] | ||
22 | + end | ||
23 | + | ||
19 | should 'add a new product to cart' do | 24 | should 'add a new product to cart' do |
20 | - get :add, :profile => enterprise.identifier, :id => product.id | 25 | + get :add, :id => product.id |
21 | 26 | ||
22 | assert product_in_cart?(product) | 27 | assert product_in_cart?(product) |
23 | assert_equal 1, product_quantity(product) | 28 | assert_equal 1, product_quantity(product) |
24 | end | 29 | end |
25 | 30 | ||
26 | should 'grow quantity through add' do | 31 | should 'grow quantity through add' do |
27 | - get :add, :profile => enterprise.identifier, :id => product.id | 32 | + get :add, :id => product.id |
28 | assert_equal 1, product_quantity(product) | 33 | assert_equal 1, product_quantity(product) |
29 | 34 | ||
30 | - get :add, :profile => enterprise.identifier, :id => product.id | 35 | + get :add, :id => product.id |
31 | assert_equal 2, product_quantity(product) | 36 | assert_equal 2, product_quantity(product) |
32 | end | 37 | end |
33 | 38 | ||
34 | should 'not add product to cart if it does not exists' do | 39 | should 'not add product to cart if it does not exists' do |
35 | - assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 } | 40 | + assert_nothing_raised { get :add, :id => 9999 } |
36 | 41 | ||
37 | assert !product_in_cart?(product) | 42 | assert !product_in_cart?(product) |
38 | assert !response_ok? | 43 | assert !response_ok? |
@@ -40,10 +45,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -40,10 +45,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
40 | end | 45 | end |
41 | 46 | ||
42 | should 'remove cart if the product being removed is the last one' do | 47 | should 'remove cart if the product being removed is the last one' do |
43 | - get :add, :profile => enterprise.identifier, :id => product.id | 48 | + get :add, :id => product.id |
44 | assert cart? | 49 | assert cart? |
45 | 50 | ||
46 | - get :remove, :profile => enterprise.identifier, :id => product.id | 51 | + get :remove, :id => product.id |
47 | assert !cart? | 52 | assert !cart? |
48 | end | 53 | end |
49 | 54 | ||
@@ -51,25 +56,25 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -51,25 +56,25 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
51 | instantiate_cart | 56 | instantiate_cart |
52 | assert !cart? | 57 | assert !cart? |
53 | 58 | ||
54 | - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | 59 | + assert_nothing_raised { get :remove, :id => 9999 } |
55 | assert !response_ok? | 60 | assert !response_ok? |
56 | assert_equal 2, reponse_error_code | 61 | assert_equal 2, reponse_error_code |
57 | end | 62 | end |
58 | 63 | ||
59 | should 'just remove product if there are other products on cart' do | 64 | should 'just remove product if there are other products on cart' do |
60 | another_product = fast_create(Product, :enterprise_id => enterprise.id) | 65 | 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 | 66 | + get :add, :id => product.id |
67 | + get :add, :id => another_product.id | ||
63 | 68 | ||
64 | - get :remove, :profile => enterprise.identifier, :id => product.id | 69 | + get :remove, :id => product.id |
65 | assert cart? | 70 | assert cart? |
66 | assert !product_in_cart?(product) | 71 | assert !product_in_cart?(product) |
67 | end | 72 | end |
68 | 73 | ||
69 | should 'not try to remove a product that is not in the cart' do | 74 | should 'not try to remove a product that is not in the cart' do |
70 | - get :add, :profile => enterprise.identifier, :id => product.id | 75 | + get :add, :id => product.id |
71 | assert cart? | 76 | assert cart? |
72 | - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | 77 | + assert_nothing_raised { get :remove, :id => 9999 } |
73 | 78 | ||
74 | assert !response_ok? | 79 | assert !response_ok? |
75 | assert_equal 4, reponse_error_code | 80 | assert_equal 4, reponse_error_code |
@@ -79,23 +84,23 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -79,23 +84,23 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
79 | instantiate_cart | 84 | instantiate_cart |
80 | assert !cart? | 85 | assert !cart? |
81 | 86 | ||
82 | - assert_nothing_raised { get :list, :profile => enterprise.identifier } | 87 | + assert_nothing_raised { get :list } |
83 | assert !response_ok? | 88 | assert !response_ok? |
84 | assert_equal 2, reponse_error_code | 89 | assert_equal 2, reponse_error_code |
85 | end | 90 | end |
86 | 91 | ||
87 | should 'list products without errors' do | 92 | should 'list products without errors' do |
88 | - get :add, :profile => enterprise.identifier, :id => product.id | 93 | + get :add, :id => product.id |
89 | 94 | ||
90 | - assert_nothing_raised { get :list, :profile => enterprise.identifier } | 95 | + assert_nothing_raised { get :list } |
91 | assert response_ok? | 96 | assert response_ok? |
92 | end | 97 | end |
93 | 98 | ||
94 | should 'update the quantity of a product' do | 99 | should 'update the quantity of a product' do |
95 | - get :add, :profile => enterprise.identifier, :id => product.id | 100 | + get :add, :id => product.id |
96 | assert 1, product_quantity(product) | 101 | assert 1, product_quantity(product) |
97 | 102 | ||
98 | - get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3 | 103 | + get :update_quantity, :id => product.id, :quantity => 3 |
99 | assert 3, product_quantity(product) | 104 | assert 3, product_quantity(product) |
100 | end | 105 | end |
101 | 106 | ||
@@ -103,55 +108,54 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -103,55 +108,54 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
103 | instantiate_cart | 108 | instantiate_cart |
104 | assert !cart? | 109 | assert !cart? |
105 | 110 | ||
106 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | 111 | + assert_nothing_raised { get :update_quantity, :id => 9999, :quantity => 3 } |
107 | assert !response_ok? | 112 | assert !response_ok? |
108 | assert_equal 2, reponse_error_code | 113 | assert_equal 2, reponse_error_code |
109 | end | 114 | end |
110 | 115 | ||
111 | should 'not try to update the quantity of a product that is not in the cart' do | 116 | 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 | 117 | + get :add, :id => product.id |
113 | assert cart? | 118 | assert cart? |
114 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | 119 | + assert_nothing_raised { get :update_quantity, :id => 9999, :quantity => 3 } |
115 | 120 | ||
116 | assert !response_ok? | 121 | assert !response_ok? |
117 | assert_equal 4, reponse_error_code | 122 | assert_equal 4, reponse_error_code |
118 | end | 123 | end |
119 | 124 | ||
120 | should 'not update the quantity of a product with a invalid value' do | 125 | should 'not update the quantity of a product with a invalid value' do |
121 | - get :add, :profile => enterprise.identifier, :id => product.id | 126 | + get :add, :id => product.id |
122 | 127 | ||
123 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1} | 128 | + assert_nothing_raised { get :update_quantity, :id => product.id, :quantity => -1} |
124 | assert !response_ok? | 129 | assert !response_ok? |
125 | assert_equal 5, reponse_error_code | 130 | assert_equal 5, reponse_error_code |
126 | 131 | ||
127 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'} | 132 | + assert_nothing_raised { get :update_quantity, :id => product.id, :quantity => 'asdf'} |
128 | assert !response_ok? | 133 | assert !response_ok? |
129 | assert_equal 5, reponse_error_code | 134 | assert_equal 5, reponse_error_code |
130 | end | 135 | end |
131 | 136 | ||
132 | should 'clean the cart' do | 137 | should 'clean the cart' do |
133 | another_product = fast_create(Product, :enterprise_id => enterprise.id) | 138 | 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 | 139 | + get :add, :id => product.id |
140 | + get :add, :id => another_product.id | ||
136 | 141 | ||
137 | - assert_nothing_raised { get :clean, :profile => enterprise.identifier } | 142 | + assert_nothing_raised { get :clean } |
138 | assert !cart? | 143 | assert !cart? |
139 | end | 144 | end |
140 | 145 | ||
141 | should 'not crash if there is no cart' do | 146 | should 'not crash if there is no cart' do |
142 | instantiate_cart | 147 | instantiate_cart |
143 | assert !cart? | 148 | assert !cart? |
144 | - assert_nothing_raised { get :clean, :profile => enterprise.identifier } | 149 | + assert_nothing_raised { get :clean } |
145 | end | 150 | end |
146 | 151 | ||
147 | should 'register order on send request' do | 152 | should 'register order on send request' do |
148 | product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) | 153 | product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) |
149 | product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) | 154 | product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) |
150 | - @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) | 155 | + @controller.stubs(:cart).returns({ :enterprise_id => enterprise.id, :items => {product1.id => 1, product2.id => 2}}) |
151 | assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | 156 | assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do |
152 | post :send_request, | 157 | post :send_request, |
153 | - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | ||
154 | - :profile => enterprise.identifier | 158 | + :customer => {:name => "Manuel", :email => "manuel@ceu.com"} |
155 | end | 159 | end |
156 | 160 | ||
157 | order = ShoppingCartPlugin::PurchaseOrder.last | 161 | order = ShoppingCartPlugin::PurchaseOrder.last |
@@ -165,11 +169,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -165,11 +169,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
165 | 169 | ||
166 | should 'register order on send request and not crash if product is not defined' do | 170 | should 'register order on send request and not crash if product is not defined' do |
167 | product1 = fast_create(Product, :enterprise_id => enterprise.id) | 171 | product1 = fast_create(Product, :enterprise_id => enterprise.id) |
168 | - @controller.stubs(:cart).returns({:items => {product1.id => 1}}) | 172 | + @controller.stubs(:cart).returns({ :enterprise_id => enterprise.id, :items => {product1.id => 1}}) |
169 | assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | 173 | assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do |
170 | post :send_request, | 174 | post :send_request, |
171 | - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | ||
172 | - :profile => enterprise.identifier | 175 | + :customer => {:name => "Manuel", :email => "manuel@ceu.com"} |
173 | end | 176 | end |
174 | 177 | ||
175 | order = ShoppingCartPlugin::PurchaseOrder.last | 178 | order = ShoppingCartPlugin::PurchaseOrder.last |
@@ -184,11 +187,13 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -184,11 +187,13 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
184 | end | 187 | end |
185 | 188 | ||
186 | def cart? | 189 | def cart? |
187 | - @controller.send(:cart).nil? | 190 | + !@controller.send(:cart).nil? |
188 | end | 191 | end |
189 | 192 | ||
190 | def product_in_cart?(product) | 193 | def product_in_cart?(product) |
191 | - @controller.send(:cart)[:items].has_key?(product.id) | 194 | + @controller.send(:cart) && |
195 | + @controller.send(:cart)[:items] && | ||
196 | + @controller.send(:cart)[:items].has_key?(product.id) | ||
192 | end | 197 | end |
193 | 198 | ||
194 | def product_quantity(product) | 199 | def product_quantity(product) |
@@ -206,8 +211,8 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | @@ -206,8 +211,8 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
206 | # temporary hack...if I don't do this the session stays as an Array instead | 211 | # temporary hack...if I don't do this the session stays as an Array instead |
207 | # of a TestSession | 212 | # of a TestSession |
208 | def instantiate_cart | 213 | def instantiate_cart |
209 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
210 | - get :remove, :profile => enterprise.identifier, :id => product.id | 214 | + get :add, :id => product.id |
215 | + get :remove, :id => product.id | ||
211 | end | 216 | end |
212 | 217 | ||
213 | end | 218 | end |