manage_products_controller_test.rb
23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
require_relative "../test_helper"
require 'manage_products_controller'
class ManageProductsControllerTest < ActionController::TestCase
all_fixtures
def setup
@controller = ManageProductsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@enterprise = fast_create(Enterprise, :name => 'teste', :identifier => 'test_ent')
@user = create_user_with_permission('test_user', 'manage_products', @enterprise)
@environment = @enterprise.environment
@environment.enable('products_for_enterprises')
@product_category = fast_create(ProductCategory)
login_as :test_user
end
should "not have permission" do
u = create_user('user_test')
login_as :user_test
get 'index', :profile => @enterprise.identifier
assert :success
assert_template 'shared/access_denied'
end
should "get index" do
get 'index', :profile => @enterprise.identifier
assert_response :success
assert assigns(:products)
end
should "get new form" do
get 'new', :profile => @enterprise.identifier
assert_response :success
assert assigns(:product)
assert_template 'new'
assert_tag :tag => 'form', :attributes => { :action => /new/ }
end
should "create new product" do
assert_difference 'Product.count' do
post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'}, :selected_category_id => @product_category.id
assert assigns(:product)
refute assigns(:product).new_record?
end
end
should "not create invalid product" do
assert_no_difference 'Product.count' do
post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'}
assert_response :success
assert assigns(:product)
assert assigns(:product).new_record?
end
end
should "get edit name form" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get 'edit', :profile => @enterprise.identifier, :id => product.id, :field => 'name'
assert_response :success
assert assigns(:product)
assert_tag :tag => 'form', :attributes => { :action => /myprofile\/#{@enterprise.identifier}\/manage_products\/edit\/#{product.id}\?field=name/ }
end
should "get edit info form" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get 'edit', :profile => @enterprise.identifier, :id => product.id, :field => 'info'
assert_response :success
assert assigns(:product)
assert_tag :tag => 'form', :attributes => { :action => /myprofile\/#{@enterprise.identifier}\/manage_products\/edit\/#{product.id}\?field=info/ }
end
should "get edit image form" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get 'edit', :profile => @enterprise.identifier, :id => product.id, :field => 'image'
assert_response :success
assert assigns(:product)
assert_tag :tag => 'form', :attributes => { :action => /myprofile\/#{@enterprise.identifier}\/manage_products\/edit\/#{product.id}\?field=image/ }
end
should "edit product name" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
post :edit, :profile => @enterprise.identifier, :product => {:name => 'new test product'}, :id => product.id, :field => 'name'
assert_response :success
assert assigns(:product)
refute assigns(:product).new_record?
assert_equal product, Product.find_by(name: 'new test product')
end
should "edit product description" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id, :description => 'My product is very good')
post :edit, :profile => @enterprise.identifier, :product => {:description => 'A very good product!'}, :id => product.id, :field => 'info'
assert_response :success
assert assigns(:product)
refute assigns(:product).new_record?
assert_equal 'A very good product!', Product.find_by(name: 'test product').description
end
should "edit product image" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
post :edit, :profile => @enterprise.identifier, :product => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }, :id => product.id, :field => 'image'
assert_response :success
assert assigns(:product)
refute assigns(:product).new_record?
assert_equal 'rails.png', Product.find_by(name: 'test product').image.filename
end
should "not edit to invalid parameters" do
product = fast_create(Product, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
post 'edit_category', :profile => @enterprise.identifier, :selected_category_id => nil, :id => product.id
assert_response :success
assert_template 'shared/_dialog_error_messages'
end
should "not crash if product has no category" do
product = fast_create(Product, :profile_id => @enterprise.id)
assert_nothing_raised do
post 'edit_category', :profile => @enterprise.identifier, :id => product.id
end
end
should "destroy product" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
assert_difference 'Product.count', -1 do
post 'destroy', :profile => @enterprise.identifier, :id => product.id
assert_response :redirect
assert_redirected_to :action => 'index'
assert assigns(:product)
refute Product.find_by(name: 'test product')
end
end
should "fail to destroy product" do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
Product.any_instance.stubs(:destroy).returns(false)
assert_no_difference 'Product.count' do
post 'destroy', :profile => @enterprise.identifier, :id => product.id
assert_response :redirect
assert_redirected_to :controller => "manage_products", :profile => @enterprise.identifier, :action => 'show', :id => product.id
assert assigns(:product)
assert Product.find_by(name: 'test product')
end
end
should 'show categories selection' do
category1 = fast_create(ProductCategory, :name => 'Category 1')
category2 = fast_create(ProductCategory, :name => 'Category 2', :parent_id => category1.id)
category3 = fast_create(ProductCategory, :name => 'Category 3', :parent_id => category2.id)
get :new, :profile => @enterprise.identifier
assert_tag :tag => 'select', :attributes => { :id => 'category_id' }, :descendant => { :tag => 'option', :content => category1.name }
end
should "create new product categorized" do
category1 = fast_create(ProductCategory, :name => 'Category 1')
category2 = fast_create(ProductCategory, :name => 'Category 2', :parent_id => category1)
assert_difference 'Product.count' do
post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product' }, :selected_category_id => category2.id
assert_equal category2, assigns(:product).product_category
end
end
should 'not create a new product with an invalid category' do
category1 = fast_create(Category, :name => 'Category 1')
category2 = fast_create(Category, :name => 'Category 2', :parent_id => category1)
assert_raise ActiveRecord::AssociationTypeMismatch do
post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product' }, :selected_category_id => category2.id
end
end
should 'filter html from name of product' do
category = fast_create(ProductCategory, :name => 'Category 1')
post 'new', :profile => @enterprise.identifier, :product => { :name => "<b id='html_name'>name bold</b>" }, :selected_category_id => category.id
assert_sanitized assigns(:product).name
end
should 'filter html with white list from description of product' do
product = fast_create(Product, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
post 'edit', :profile => @enterprise.identifier, :id => product.id, :field => 'info', :product => { :name => 'name', :description => "<b id=\"html_descr\">descr bold</b>" }
assert_equal "<b id=\"html_descr\">descr bold</b>", assigns(:product).description
end
should 'not let users in if environment do not let' do
env = Environment.default
env.disable('products_for_enterprises')
env.save!
@enterprise.environment = env
@enterprise.save!
get :index, :profile => @enterprise.identifier
assert_template 'not_found'
end
should 'show top level product categories for the user to choose' do
pc1 = fast_create(ProductCategory, :name => 'test_category1')
pc2 = fast_create(ProductCategory, :name => 'test_category2')
get :new, :profile => @enterprise.identifier
assert_equivalent ProductCategory.top_level_for(pc1.environment), assigns(:categories)
end
should 'increase level while navigate on hierarchy categories' do
category_level0 = fast_create(ProductCategory, :name => 'Products', :environment_id => @environment.id)
category_level1 = fast_create(ProductCategory, :parent_id => category_level0.id, :name => 'Shoes', :environment_id => @environment.id)
category_level2 = fast_create(ProductCategory, :parent_id => category_level1.id, :name => 'Athletic Shoes', :environment_id => @environment.id)
get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category_level0.id
assert_equal 0, assigns(:level)
get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category_level1.id
assert_equal 1, assigns(:level)
get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category_level2.id
assert_equal 2, assigns(:level)
end
should 'remember the selected category' do
category0 = fast_create(ProductCategory, :name => 'Products', :environment_id => @environment.id)
category1 = fast_create(ProductCategory, :name => 'Shoes', :environment_id => @environment.id)
get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category0.id
assert_equal category0, assigns(:category)
get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category1.id
assert_equal category1, assigns(:category)
end
should 'list top level categories when has no category_id' do
get :categories_for_selection, :profile => @enterprise.identifier
assert_equal ProductCategory.top_level_for(@environment), assigns(:categories)
end
should 'render dialog_error_messages template for invalid product' do
post :new, :profile => @enterprise.identifier, :product => { :name => 'Invalid product' }
assert_template 'shared/_dialog_error_messages'
end
should 'render redirect_via_javascript template after save' do
assert_difference 'Product.count' do
post :new, :profile => @enterprise.identifier, :product => { :name => 'Invalid product' }, :selected_category_id => @product_category.id
assert_template 'shared/_redirect_via_javascript'
end
end
should 'show product of enterprise' do
prod = @enterprise.products.create!(:name => 'Product test', :product_category => @product_category)
get :show, :id => prod.id, :profile => @enterprise.identifier
assert_tag :tag => 'h2', :content => /#{prod.name}/
end
should 'link back to index from product show' do
enterprise = create(Enterprise, :name => 'test_enterprise_1', :identifier => 'test_enterprise_1', :environment => Environment.default)
prod = enterprise.products.create!(:name => 'Product test', :product_category => @product_category)
get :show, :id => prod.id, :profile => enterprise.identifier
assert_tag({
:tag => 'div',
:attributes => {
:class => /main-block/
},
:descendant => {
:tag => 'a',
:attributes => {
:href => "/catalog/#{enterprise.identifier}",
},
:content => /Back to the product listing/
}
})
end
should 'not show product price when showing product if not informed' do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_no_tag :tag => 'span', :attributes => { :class => 'product_price' }, :content => /Price:/
end
should 'show product price when showing product if unit was informed' do
product = fast_create(Product, :name => 'test product', :price => 50.00, :unit_id => fast_create(Unit).id, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /Price:/
assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 50.00'
end
should 'show product price when showing product if discount was informed' do
product = fast_create(Product, :name => 'test product', :price => 50.00, :unit_id => fast_create(Unit).id, :discount => 3.50, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /List price:/
assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 50.00'
assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /On sale:/
assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 46.50'
end
should 'show product price when showing product if unit not informed' do
product = fast_create(Product, :name => 'test product', :price => 50.00, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /Price:/
assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 50.00'
end
should 'display button to add input when product has no input' do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'div', :attributes => { :id => 'display-add-input-button'},
:descendant => {:tag => 'a', :attributes => { :href => "/myprofile/#{@enterprise.identifier}/manage_products/add_input/#{product.id}", :id => 'add-input-button'}, :content => 'Add the inputs or raw material used by this product'}
end
should 'has instance of input list when showing product' do
product = fast_create(Product, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_equal [], assigns(:inputs)
end
should 'remove input of a product' do
product = fast_create(Product, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
assert_equal [input], product.inputs
post :remove_input, :id => input.id, :profile => @enterprise.identifier
product.reload
assert_equal [], product.inputs
end
should 'save inputs order' do
product = fast_create(Product, :profile_id => @enterprise.id)
first = Input.create!(:product => product, :product_category => fast_create(ProductCategory))
second = Input.create!(:product => product, :product_category => fast_create(ProductCategory))
third = Input.create!(:product => product, :product_category => fast_create(ProductCategory))
assert_equal [first, second, third], product.inputs(true)
get :order_inputs, :profile => @enterprise.identifier, :id => product, :input => [second.id, third.id, first.id]
assert_template nil
assert_equal [second, third, first], product.inputs(true)
end
should 'render partial certifiers for selection' do
xhr :get, :certifiers_for_selection, :profile => @enterprise.identifier
assert_template '_certifiers_for_selection'
end
should 'not list all the products of enterprise' do
@enterprise.products = []
1.upto(12) do |n|
fast_create(Product, :name => "test product_#{n}", :profile_id => @enterprise.id, :product_category_id => @product_category.id)
end
get :index, :profile => @enterprise.identifier
assert_equal 10, assigns(:products).size
end
should 'paginate the manage products list of enterprise' do
@enterprise.products = []
1.upto(12) do |n|
fast_create(Product, :name => "test product_#{n}", :profile_id => @enterprise.id, :product_category_id => @product_category.id)
end
get :index, :profile => @enterprise.identifier
assert_tag :tag => 'a', :attributes => { :rel => 'next', :href => "/myprofile/#{@enterprise.identifier}/manage_products?page=2" }
get :index, :profile => @enterprise.identifier, :page => 2
assert_equal 2, assigns(:products).size
end
should 'display tabs even if description and inputs are empty and user is allowed' do
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }
end
should 'not display tabs if description and inputs are empty and user is not allowed' do
create_user('foo')
login_as 'foo'
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_no_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }
end
should 'not display tabs if description and inputs are empty and user is not logged in' do
logout
product = fast_create(Product, :name => 'test product', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_no_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }
end
should 'display only description tab if inputs are empty and user is not allowed' do
create_user('foo')
login_as 'foo'
product = fast_create(Product, :description => 'This product is very good', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }, :descendant => {:tag => 'a', :attributes => {:href => '#product-description'}, :content => 'Description'}
assert_no_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }, :descendant => {:tag => 'a', :attributes => {:href => '#inputs'}, :content => 'Inputs and raw material'}
end
should 'display only inputs tab if description is empty and user is not allowed' do
create_user 'foo'
login_as 'foo'
product = fast_create(Product, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_no_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }, :descendant => {:tag => 'a', :attributes => {:href => '#product-description'}, :content => 'Description'}
assert_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }, :descendant => {:tag => 'a', :attributes => {:href => '#product-inputs'}, :content => 'Inputs and raw material'}
end
should 'display tabs if description and inputs are not empty and user is not allowed' do
create_user('foo')
login_as 'foo'
product = fast_create(Product, :description => 'This product is very good', :profile_id => @enterprise.id, :product_category_id => @product_category.id)
input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }, :descendant => {:tag => 'a', :attributes => {:href => '#product-description'}, :content => 'Description'}
assert_tag :tag => 'div', :attributes => { :id => "product-#{product.id}-tabs" }, :descendant => {:tag => 'a', :attributes => {:href => '#product-inputs'}, :content => 'Inputs and raw material'}
end
should 'include extra content supplied by plugins on products info extras' do
class TestProductInfoExtras1Plugin < Noosfero::Plugin
def product_info_extras(p)
proc {"<span id='plugin1'>This is Plugin1 speaking!</span>".html_safe}
end
end
class TestProductInfoExtras2Plugin < Noosfero::Plugin
def product_info_extras(p)
proc { "<span id='plugin2'>This is Plugin2 speaking!</span>".html_safe }
end
end
product = fast_create(Product, :profile_id => @enterprise.id)
Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestProductInfoExtras1Plugin.new, TestProductInfoExtras2Plugin.new])
get :show, :id => product.id, :profile => @enterprise.identifier
assert_tag :tag => 'span', :content => 'This is Plugin1 speaking!', :attributes => {:id => 'plugin1'}
assert_tag :tag => 'span', :content => 'This is Plugin2 speaking!', :attributes => {:id => 'plugin2'}
end
should 'not allow product creation for profiles that can\'t do it' do
class SpecialEnterprise < Enterprise
def create_product?
false
end
end
enterprise = SpecialEnterprise.create!(:identifier => 'special-enterprise', :name => 'Special Enterprise')
get 'new', :profile => enterprise.identifier
assert_response 403
end
should 'remove price detail of a product' do
product = fast_create(Product, :profile_id => @enterprise.id, :product_category_id => @product_category.id)
cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'Environment')
detail = product.price_details.create(:production_cost_id => cost.id, :price => 10)
assert_equal [detail], product.price_details
post :remove_price_detail, :id => detail.id, :product => product, :profile => @enterprise.identifier
product.reload
assert_equal [], product.price_details
end
should 'create a production cost for enterprise' do
get :create_production_cost, :profile => @enterprise.identifier, :id => 'Taxes'
assert_equal ['Taxes'], Enterprise.find(@enterprise.id).production_costs.map(&:name)
resp = ActiveSupport::JSON.decode(@response.body)
assert_equal 'Taxes', resp['name']
assert resp['id'].kind_of?(Integer)
assert resp['ok']
assert_nil resp['error_msg']
end
should 'display error if production cost has no name' do
get :create_production_cost, :profile => @enterprise.identifier
resp = ActiveSupport::JSON.decode(@response.body)
assert_nil resp['name']
assert_nil resp['id']
refute resp['ok']
assert_match /blank/, resp['error_msg']
end
should 'display error if name of production cost is too long' do
get :create_production_cost, :profile => @enterprise.identifier, :id => 'a'*60
resp = ActiveSupport::JSON.decode(@response.body)
assert_nil resp['name']
assert_nil resp['id']
refute resp['ok']
assert_match /too long/, resp['error_msg']
end
end