Commit 72d8ae0c1987ec0269737996950a9d4b3b61bc4f

Authored by Antonio Terceiro
1 parent 212236ed

Finish fixing the tests.

Fixing the functional tests was easier than I though it would. :)
plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
... ... @@ -278,7 +278,7 @@ class ShoppingCartPluginController < PublicController
278 278  
279 279 after_filter :save_cookie
280 280 def save_cookie
281   - if @cart.nil? && cookies[cookie_key]
  281 + if @cart.nil?
282 282 cookies.delete(cookie_key)
283 283 else
284 284 cookies[cookie_key] = {
... ...
plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb
... ... @@ -16,23 +16,28 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
16 16 attr_reader :enterprise
17 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 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 27 assert product_in_cart?(product)
23 28 assert_equal 1, product_quantity(product)
24 29 end
25 30  
26 31 should 'grow quantity through add' do
27   - get :add, :profile => enterprise.identifier, :id => product.id
  32 + get :add, :id => product.id
28 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 36 assert_equal 2, product_quantity(product)
32 37 end
33 38  
34 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 42 assert !product_in_cart?(product)
38 43 assert !response_ok?
... ... @@ -40,10 +45,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
40 45 end
41 46  
42 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 49 assert cart?
45 50  
46   - get :remove, :profile => enterprise.identifier, :id => product.id
  51 + get :remove, :id => product.id
47 52 assert !cart?
48 53 end
49 54  
... ... @@ -51,25 +56,25 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
51 56 instantiate_cart
52 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 60 assert !response_ok?
56 61 assert_equal 2, reponse_error_code
57 62 end
58 63  
59 64 should 'just remove product if there are other products on cart' do
60 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 70 assert cart?
66 71 assert !product_in_cart?(product)
67 72 end
68 73  
69 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 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 79 assert !response_ok?
75 80 assert_equal 4, reponse_error_code
... ... @@ -79,23 +84,23 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
79 84 instantiate_cart
80 85 assert !cart?
81 86  
82   - assert_nothing_raised { get :list, :profile => enterprise.identifier }
  87 + assert_nothing_raised { get :list }
83 88 assert !response_ok?
84 89 assert_equal 2, reponse_error_code
85 90 end
86 91  
87 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 96 assert response_ok?
92 97 end
93 98  
94 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 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 104 assert 3, product_quantity(product)
100 105 end
101 106  
... ... @@ -103,55 +108,54 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
103 108 instantiate_cart
104 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 112 assert !response_ok?
108 113 assert_equal 2, reponse_error_code
109 114 end
110 115  
111 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 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 121 assert !response_ok?
117 122 assert_equal 4, reponse_error_code
118 123 end
119 124  
120 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 129 assert !response_ok?
125 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 133 assert !response_ok?
129 134 assert_equal 5, reponse_error_code
130 135 end
131 136  
132 137 should 'clean the cart' do
133 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 143 assert !cart?
139 144 end
140 145  
141 146 should 'not crash if there is no cart' do
142 147 instantiate_cart
143 148 assert !cart?
144   - assert_nothing_raised { get :clean, :profile => enterprise.identifier }
  149 + assert_nothing_raised { get :clean }
145 150 end
146 151  
147 152 should 'register order on send request' do
148 153 product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99)
149 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 156 assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do
152 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 159 end
156 160  
157 161 order = ShoppingCartPlugin::PurchaseOrder.last
... ... @@ -165,11 +169,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
165 169  
166 170 should 'register order on send request and not crash if product is not defined' do
167 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 173 assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do
170 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 176 end
174 177  
175 178 order = ShoppingCartPlugin::PurchaseOrder.last
... ... @@ -184,11 +187,13 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
184 187 end
185 188  
186 189 def cart?
187   - @controller.send(:cart).nil?
  190 + !@controller.send(:cart).nil?
188 191 end
189 192  
190 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 197 end
193 198  
194 199 def product_quantity(product)
... ... @@ -206,8 +211,8 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase
206 211 # temporary hack...if I don't do this the session stays as an Array instead
207 212 # of a TestSession
208 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 216 end
212 217  
213 218 end
... ...