Commit 29f6b68e5b527e4d0d94d4b4501b8e212cff723a

Authored by Rodrigo Souto
1 parent ddcf8bea

rails3: fix enterprise tests

PS: still tests failin
app/models/enterprise.rb
@@ -53,8 +53,9 @@ class Enterprise < Organization @@ -53,8 +53,9 @@ class Enterprise < Organization
53 super + FIELDS 53 super + FIELDS
54 end 54 end
55 55
56 - def validate  
57 - super 56 + validate :presence_of_required_fieds
  57 +
  58 + def presence_of_required_fieds
58 self.required_fields.each do |field| 59 self.required_fields.each do |field|
59 if self.send(field).blank? 60 if self.send(field).blank?
60 self.errors.add_on_blank(field) 61 self.errors.add_on_blank(field)
@@ -103,7 +104,7 @@ class Enterprise < Organization @@ -103,7 +104,7 @@ class Enterprise < Organization
103 if environment.replace_enterprise_template_when_enable 104 if environment.replace_enterprise_template_when_enable
104 apply_template(template) 105 apply_template(template)
105 end 106 end
106 - save_without_validation! 107 + save(:validate => false)
107 end 108 end
108 109
109 def question 110 def question
test/unit/enterprise_test.rb
@@ -41,7 +41,7 @@ class EnterpriseTest < ActiveSupport::TestCase @@ -41,7 +41,7 @@ class EnterpriseTest < ActiveSupport::TestCase
41 end 41 end
42 42
43 def test_belongs_to_environment_and_has_default 43 def test_belongs_to_environment_and_has_default
44 - assert_equal Environment.default, Enterprise.create!(:name => 'my test environment', :identifier => 'mytestenvironment').environment 44 + assert_equal Environment.default, create(Enterprise, :name => 'my test environment', :identifier => 'mytestenvironment').environment
45 end 45 end
46 46
47 def test_cannot_rename 47 def test_cannot_rename
@@ -62,8 +62,8 @@ class EnterpriseTest < ActiveSupport::TestCase @@ -62,8 +62,8 @@ class EnterpriseTest < ActiveSupport::TestCase
62 62
63 should 'remove products when removing enterprise' do 63 should 'remove products when removing enterprise' do
64 e = fast_create(Enterprise, :name => "My enterprise", :identifier => 'myenterprise') 64 e = fast_create(Enterprise, :name => "My enterprise", :identifier => 'myenterprise')
65 - e.products.create!(:name => 'One product', :product_category => @product_category)  
66 - e.products.create!(:name => 'Another product', :product_category => @product_category) 65 + create(Product, :enterprise => e, :name => 'One product', :product_category => @product_category)
  66 + create(Product, :enterprise => e, :name => 'Another product', :product_category => @product_category)
67 67
68 assert_difference Product, :count, -2 do 68 assert_difference Product, :count, -2 do
69 e.destroy 69 e.destroy
@@ -71,15 +71,16 @@ class EnterpriseTest < ActiveSupport::TestCase @@ -71,15 +71,16 @@ class EnterpriseTest < ActiveSupport::TestCase
71 end 71 end
72 72
73 should 'create a default set of articles' do 73 should 'create a default set of articles' do
74 - Enterprise.any_instance.expects(:default_set_of_articles).returns([Blog.new(:name => 'Blog')])  
75 - enterprise = Enterprise.create!(:name => 'my test enterprise', :identifier => 'myenterprise') 74 + blog = build(Blog)
  75 + Enterprise.any_instance.expects(:default_set_of_articles).returns([blog])
  76 + enterprise = create(Enterprise, :name => 'my test enterprise', :identifier => 'myenterprise')
76 77
77 - assert_kind_of Blog, enterprise.articles.find_by_path('blog')  
78 - assert_kind_of RssFeed, enterprise.articles.find_by_path('blog/feed') 78 + assert_kind_of Blog, enterprise.articles.find_by_path(blog.path)
  79 + assert_kind_of RssFeed, enterprise.articles.find_by_path(blog.feed.path)
79 end 80 end
80 81
81 should 'create default set of blocks' do 82 should 'create default set of blocks' do
82 - e = Enterprise.create(:name => 'my new community', :identifier => 'mynewcommunity') 83 + e = create(Enterprise, :name => 'my new community', :identifier => 'mynewcommunity')
83 84
84 assert !e.boxes[0].blocks.empty?, 'person must have blocks in area 1' 85 assert !e.boxes[0].blocks.empty?, 'person must have blocks in area 1'
85 assert !e.boxes[1].blocks.empty?, 'person must have blocks in area 2' 86 assert !e.boxes[1].blocks.empty?, 'person must have blocks in area 2'
@@ -101,6 +102,7 @@ class EnterpriseTest < ActiveSupport::TestCase @@ -101,6 +102,7 @@ class EnterpriseTest < ActiveSupport::TestCase
101 enterprise.add_member(member) 102 enterprise.add_member(member)
102 103
103 person = fast_create(Person) 104 person = fast_create(Person)
  105 + enterprise.stubs(:notification_emails).returns(['sample@example.org'])
104 enterprise.add_member(person) 106 enterprise.add_member(person)
105 107
106 assert_equal false, person.is_member_of?(enterprise) 108 assert_equal false, person.is_member_of?(enterprise)
@@ -183,7 +185,7 @@ class EnterpriseTest < ActiveSupport::TestCase @@ -183,7 +185,7 @@ class EnterpriseTest < ActiveSupport::TestCase
183 inactive_template.boxes << Box.new 185 inactive_template.boxes << Box.new
184 inactive_template.save! 186 inactive_template.save!
185 187
186 - active_template = Enterprise.create!(:name => 'enteprise template', :identifier => 'enterprise_template') 188 + active_template = create(Enterprise, :name => 'enteprise template', :identifier => 'enterprise_template')
187 assert_equal 3, active_template.boxes.size 189 assert_equal 3, active_template.boxes.size
188 190
189 e = Environment.default 191 e = Environment.default
@@ -191,7 +193,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -191,7 +193,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
191 e.enterprise_template = active_template 193 e.enterprise_template = active_template
192 e.save! 194 e.save!
193 195
194 - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) 196 + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false)
195 197
196 p = create_user('test_user').person 198 p = create_user('test_user').person
197 ent.enable(p) 199 ent.enable(p)
@@ -201,13 +203,13 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -201,13 +203,13 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
201 203
202 should 'create EnterpriseActivation task when creating with enabled = false' do 204 should 'create EnterpriseActivation task when creating with enabled = false' do
203 EnterpriseActivation.delete_all 205 EnterpriseActivation.delete_all
204 - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) 206 + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false)
205 assert_equal [ent], EnterpriseActivation.find(:all).map(&:enterprise) 207 assert_equal [ent], EnterpriseActivation.find(:all).map(&:enterprise)
206 end 208 end
207 209
208 should 'create EnterpriseActivation with 7-characters codes' do 210 should 'create EnterpriseActivation with 7-characters codes' do
209 EnterpriseActivation.delete_all 211 EnterpriseActivation.delete_all
210 - Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) 212 + create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false)
211 assert_equal 7, EnterpriseActivation.find(:first).code.size 213 assert_equal 7, EnterpriseActivation.find(:first).code.size
212 end 214 end
213 215
@@ -236,7 +238,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -236,7 +238,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
236 should 'list product categories full name' do 238 should 'list product categories full name' do
237 subcategory = fast_create(ProductCategory, :name => 'Products subcategory', :parent_id => @product_category.id) 239 subcategory = fast_create(ProductCategory, :name => 'Products subcategory', :parent_id => @product_category.id)
238 ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') 240 ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent')
239 - p = ent.products.create!(:name => 'test prod', :product_category => subcategory) 241 + p = create(Product, :name => 'test prod', :product_category => subcategory, :enterprise => ent)
240 242
241 assert_equal [p.category_full_name], ent.product_categories 243 assert_equal [p.category_full_name], ent.product_categories
242 end 244 end
@@ -256,7 +258,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -256,7 +258,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
256 end 258 end
257 259
258 should 'have a default enterprise template' do 260 should 'have a default enterprise template' do
259 - env = Environment.create!(:name => 'test env') 261 + env = create(Environment, :name => 'test env')
260 p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => env.id) 262 p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => env.id)
261 assert_kind_of Enterprise, p.template 263 assert_kind_of Enterprise, p.template
262 end 264 end
@@ -270,7 +272,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -270,7 +272,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
270 e.inactive_enterprise_template = inactive_template 272 e.inactive_enterprise_template = inactive_template
271 e.save! 273 e.save!
272 274
273 - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :template => another_template, :environment => e) 275 + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :template => another_template, :environment => e)
274 assert_equal inactive_template, ent.template 276 assert_equal inactive_template, ent.template
275 end 277 end
276 278
@@ -282,7 +284,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -282,7 +284,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
282 should 'return active_enterprise_fields' do 284 should 'return active_enterprise_fields' do
283 e = Environment.default 285 e = Environment.default
284 e.expects(:active_enterprise_fields).returns(['contact_phone', 'contact_email']).at_least_once 286 e.expects(:active_enterprise_fields).returns(['contact_phone', 'contact_email']).at_least_once
285 - ent = Enterprise.new(:environment => e) 287 + ent = build(Enterprise, :environment => e)
286 288
287 assert_equal e.active_enterprise_fields, ent.active_fields 289 assert_equal e.active_enterprise_fields, ent.active_fields
288 end 290 end
@@ -290,7 +292,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -290,7 +292,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
290 should 'return required_enterprise_fields' do 292 should 'return required_enterprise_fields' do
291 e = Environment.default 293 e = Environment.default
292 e.expects(:required_enterprise_fields).returns(['contact_phone', 'contact_email']).at_least_once 294 e.expects(:required_enterprise_fields).returns(['contact_phone', 'contact_email']).at_least_once
293 - enterprise = Enterprise.new(:environment => e) 295 + enterprise = build(Enterprise, :environment => e)
294 296
295 assert_equal e.required_enterprise_fields, enterprise.required_fields 297 assert_equal e.required_enterprise_fields, enterprise.required_fields
296 end 298 end
@@ -298,7 +300,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -298,7 +300,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
298 should 'require fields if enterprise needs' do 300 should 'require fields if enterprise needs' do
299 e = Environment.default 301 e = Environment.default
300 e.expects(:required_enterprise_fields).returns(['contact_phone']).at_least_once 302 e.expects(:required_enterprise_fields).returns(['contact_phone']).at_least_once
301 - enterprise = Enterprise.new(:environment => e) 303 + enterprise = build(Enterprise, :environment => e)
302 assert ! enterprise.valid? 304 assert ! enterprise.valid?
303 assert enterprise.errors[:contact_phone.to_s].present? 305 assert enterprise.errors[:contact_phone.to_s].present?
304 306
@@ -308,28 +310,28 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -308,28 +310,28 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
308 end 310 end
309 311
310 should 'enable contact' do 312 should 'enable contact' do
311 - enterprise = Enterprise.new(:enable_contact_us => false) 313 + enterprise = build(Enterprise, :enable_contact_us => false)
312 assert !enterprise.enable_contact? 314 assert !enterprise.enable_contact?
313 enterprise.enable_contact_us = true 315 enterprise.enable_contact_us = true
314 assert enterprise.enable_contact? 316 assert enterprise.enable_contact?
315 end 317 end
316 318
317 should 'save organization_website with http' do 319 should 'save organization_website with http' do
318 - p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') 320 + p = build(Enterprise, :name => 'test_ent', :identifier => 'test_ent')
319 p.organization_website = 'website.without.http' 321 p.organization_website = 'website.without.http'
320 p.save! 322 p.save!
321 assert_equal 'http://website.without.http', p.organization_website 323 assert_equal 'http://website.without.http', p.organization_website
322 end 324 end
323 325
324 should 'save not add http to empty organization_website' do 326 should 'save not add http to empty organization_website' do
325 - p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') 327 + p = build(Enterprise, :name => 'test_ent', :identifier => 'test_ent')
326 p.organization_website = '' 328 p.organization_website = ''
327 p.save! 329 p.save!
328 assert_equal '', p.organization_website 330 assert_equal '', p.organization_website
329 end 331 end
330 332
331 should 'save organization_website as typed if has http' do 333 should 'save organization_website as typed if has http' do
332 - p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') 334 + p = build(Enterprise, :name => 'test_ent', :identifier => 'test_ent')
333 p.organization_website = 'http://website.with.http' 335 p.organization_website = 'http://website.with.http'
334 p.save 336 p.save
335 assert_equal 'http://website.with.http', p.organization_website 337 assert_equal 'http://website.with.http', p.organization_website
@@ -340,7 +342,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -340,7 +342,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
340 e.enable('enterprises_are_disabled_when_created') 342 e.enable('enterprises_are_disabled_when_created')
341 e.save! 343 e.save!
342 344
343 - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') 345 + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent')
344 assert_equal false, Enterprise['test_ent'].enabled? 346 assert_equal false, Enterprise['test_ent'].enabled?
345 end 347 end
346 348
@@ -349,12 +351,12 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -349,12 +351,12 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
349 351
350 e.enable('enterprises_are_validated_when_created') 352 e.enable('enterprises_are_validated_when_created')
351 e.save 353 e.save
352 - enterprise = Enterprise.create(:name => 'test enteprise', :identifier => 'test_ent1') 354 + enterprise = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent1')
353 assert enterprise.validated 355 assert enterprise.validated
354 356
355 e.disable('enterprises_are_validated_when_created') 357 e.disable('enterprises_are_validated_when_created')
356 e.save 358 e.save
357 - enterprise = Enterprise.create(:name => 'test enteprise', :identifier => 'test_ent2') 359 + enterprise = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent2')
358 assert !enterprise.validated 360 assert !enterprise.validated
359 end 361 end
360 362
@@ -369,7 +371,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -369,7 +371,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
369 e.inactive_enterprise_template = inactive_template 371 e.inactive_enterprise_template = inactive_template
370 e.save! 372 e.save!
371 373
372 - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') 374 + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent')
373 assert_equal 1, ent.boxes.size 375 assert_equal 1, ent.boxes.size
374 end 376 end
375 377
@@ -384,23 +386,23 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -384,23 +386,23 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
384 e.inactive_enterprise_template = inactive_template 386 e.inactive_enterprise_template = inactive_template
385 e.save! 387 e.save!
386 388
387 - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') 389 + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent')
388 assert_equal 3, ent.boxes.size 390 assert_equal 3, ent.boxes.size
389 end 391 end
390 392
391 should 'collect the highlighted products with image' do 393 should 'collect the highlighted products with image' do
392 env = Environment.default 394 env = Environment.default
393 e1 = fast_create(Enterprise) 395 e1 = fast_create(Enterprise)
394 - p1 = e1.products.create!(:name => 'test_prod1', :product_category_id => @product_category.id) 396 + p1 = create(Product, :name => 'test_prod1', :product_category_id => @product_category.id, :enterprise => e1)
395 products = [] 397 products = []
396 3.times {|n| 398 3.times {|n|
397 - products.push(Product.create!(:name => "product #{n}", :enterprise_id => e1.id, 399 + products.push(create(Product, :name => "product #{n}", :enterprise_id => e1.id,
398 :highlighted => true, :product_category_id => @product_category.id, 400 :highlighted => true, :product_category_id => @product_category.id,
399 :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } 401 :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }
400 )) 402 ))
401 } 403 }
402 - Product.create!(:name => "product 4", :enterprise_id => e1.id, :product_category_id => @product_category.id, :highlighted => true)  
403 - Product.create!(:name => "product 5", :enterprise_id => e1.id, :product_category_id => @product_category.id, :image_builder => { 404 + create(Product, :name => "product 4", :enterprise_id => e1.id, :product_category_id => @product_category.id, :highlighted => true)
  405 + create(Product, :name => "product 5", :enterprise_id => e1.id, :product_category_id => @product_category.id, :image_builder => {
404 :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') 406 :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
405 }) 407 })
406 assert_equal products, e1.highlighted_products_with_image 408 assert_equal products, e1.highlighted_products_with_image
@@ -409,8 +411,8 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -409,8 +411,8 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
409 should 'has many inputs through products' do 411 should 'has many inputs through products' do
410 enterprise = fast_create(Enterprise) 412 enterprise = fast_create(Enterprise)
411 product = fast_create(Product, :enterprise_id => enterprise.id, :product_category_id => @product_category.id) 413 product = fast_create(Product, :enterprise_id => enterprise.id, :product_category_id => @product_category.id)
412 - product.inputs << Input.new(:product_category => @product_category)  
413 - product.inputs << Input.new(:product_category => @product_category) 414 + product.inputs << build(Input, :product_category => @product_category)
  415 + product.inputs << build(Input, :product_category => @product_category)
414 416
415 assert_equal product.inputs, enterprise.inputs 417 assert_equal product.inputs, enterprise.inputs
416 end 418 end
@@ -451,7 +453,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase @@ -451,7 +453,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
451 453
452 454
453 activity = ActionTracker::Record.last 455 activity = ActionTracker::Record.last
454 - scrap = Scrap.create!(defaults_for_scrap(:sender => person, :receiver => enterprise, :content => 'A scrap')) 456 + scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => enterprise, :content => 'A scrap'))
455 457
456 assert_equal [scrap], enterprise.activities.map { |a| a.klass.constantize.find(a.id) } 458 assert_equal [scrap], enterprise.activities.map { |a| a.klass.constantize.find(a.id) }
457 end 459 end