Commit e5dfdd1575c7957e1d8fbe594f9062adc87eddf6

Authored by Rodrigo Souto
1 parent 5de7fb8b

rails3: fix community tests

PS: still failing on test related to
ActionTracker::Record#current_user_from_model
app/controllers/my_profile/memberships_controller.rb
@@ -10,7 +10,7 @@ class MembershipsController < MyProfileController @@ -10,7 +10,7 @@ class MembershipsController < MyProfileController
10 @community = Community.new(params[:community]) 10 @community = Community.new(params[:community])
11 @community.environment = environment 11 @community.environment = environment
12 if request.post? && @community.valid? 12 if request.post? && @community.valid?
13 - @community = Community.create_after_moderation(user, {:environment => environment}.merge(params[:community])) 13 + @community = Community.create_after_moderation(user, params[:community].merge({:environment => environment}))
14 redirect_to :action => 'index' 14 redirect_to :action => 'index'
15 return 15 return
16 end 16 end
app/models/community.rb
@@ -18,12 +18,18 @@ class Community < Organization @@ -18,12 +18,18 @@ class Community < Organization
18 community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default') 18 community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default')
19 end 19 end
20 20
  21 + # Since we it's not a good idea to add the environment as accessible through
  22 + # mass-assignment, we set it manually here. Note that this requires that the
  23 + # places that call this method are safe from mass-assignment by setting the
  24 + # environment key themselves.
21 def self.create_after_moderation(requestor, attributes = {}) 25 def self.create_after_moderation(requestor, attributes = {})
  26 + environment = attributes.delete(:environment)
22 community = Community.new(attributes) 27 community = Community.new(attributes)
  28 + community.environment = environment
23 if community.environment.enabled?('admin_must_approve_new_communities') 29 if community.environment.enabled?('admin_must_approve_new_communities')
24 - CreateCommunity.create(attributes.merge(:requestor => requestor)) 30 + CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment))
25 else 31 else
26 - community = Community.create(attributes) 32 + community.save!
27 community.add_admin(requestor) 33 community.add_admin(requestor)
28 end 34 end
29 community 35 community
@@ -39,8 +45,9 @@ class Community < Organization @@ -39,8 +45,9 @@ class Community < Organization
39 super + FIELDS 45 super + FIELDS
40 end 46 end
41 47
42 - def validate  
43 - super 48 + validate :presence_of_required_fieds
  49 +
  50 + def presence_of_required_fieds
44 self.required_fields.each do |field| 51 self.required_fields.each do |field|
45 if self.send(field).blank? 52 if self.send(field).blank?
46 self.errors.add_on_blank(field) 53 self.errors.add_on_blank(field)
app/models/profile.rb
@@ -343,9 +343,14 @@ class Profile < ActiveRecord::Base @@ -343,9 +343,14 @@ class Profile < ActiveRecord::Base
343 def copy_blocks_from(profile) 343 def copy_blocks_from(profile)
344 self.boxes.destroy_all 344 self.boxes.destroy_all
345 profile.boxes.each do |box| 345 profile.boxes.each do |box|
346 - self.boxes << Box.new(:position => box.position) 346 + new_box = Box.new
  347 + new_box.position = box.position
  348 + self.boxes << new_box
347 box.blocks.each do |block| 349 box.blocks.each do |block|
348 - self.boxes[-1].blocks << block.class.new(:title => block[:title], :settings => block.settings, :position => block.position) 350 + new_block = block.class.new(:title => block[:title])
  351 + new_block.settings = block.settings
  352 + new_block.position = block.position
  353 + self.boxes[-1].blocks << new_block
349 end 354 end
350 end 355 end
351 end 356 end
@@ -373,7 +378,7 @@ class Profile &lt; ActiveRecord::Base @@ -373,7 +378,7 @@ class Profile &lt; ActiveRecord::Base
373 self.public_profile = template.public_profile 378 self.public_profile = template.public_profile
374 379
375 # flush 380 # flush
376 - self.save_without_validation! 381 + self.save(:validate => false)
377 end 382 end
378 383
379 def apply_type_specific_template(template) 384 def apply_type_specific_template(template)
app/models/user.rb
@@ -310,6 +310,6 @@ class User &lt; ActiveRecord::Base @@ -310,6 +310,6 @@ class User &lt; ActiveRecord::Base
310 end 310 end
311 311
312 def delay_activation_check 312 def delay_activation_check
313 - Delayed::Job.enqueue(UserActivationJob.new(self.id), 0, 72.hours.from_now) 313 + Delayed::Job.enqueue(UserActivationJob.new(self.id), {:priority => 0, :run_at => 72.hours.from_now})
314 end 314 end
315 end 315 end
test/unit/community_test.rb
@@ -13,19 +13,20 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -13,19 +13,20 @@ class CommunityTest &lt; ActiveSupport::TestCase
13 end 13 end
14 14
15 should 'convert name into identifier' do 15 should 'convert name into identifier' do
16 - c = Community.new(:environment => Environment.default, :name =>'My shiny new Community') 16 + c = Community.new
  17 + c.name ='My shiny new Community'
17 assert_equal 'My shiny new Community', c.name 18 assert_equal 'My shiny new Community', c.name
18 assert_equal 'my-shiny-new-community', c.identifier 19 assert_equal 'my-shiny-new-community', c.identifier
19 end 20 end
20 21
21 should 'have a description attribute' do 22 should 'have a description attribute' do
22 - c = Community.new(:environment => Environment.default) 23 + c = build(Community, :environment => Environment.default)
23 c.description = 'the description of the community' 24 c.description = 'the description of the community'
24 assert_equal 'the description of the community', c.description 25 assert_equal 'the description of the community', c.description
25 end 26 end
26 27
27 should 'create default set of blocks' do 28 should 'create default set of blocks' do
28 - c = Community.create!(:environment => Environment.default, :name => 'my new community') 29 + c = create(Community, :environment => Environment.default, :name => 'my new community')
29 30
30 assert !c.boxes[0].blocks.empty?, 'person must have blocks in area 1' 31 assert !c.boxes[0].blocks.empty?, 'person must have blocks in area 1'
31 assert !c.boxes[1].blocks.empty?, 'person must have blocks in area 2' 32 assert !c.boxes[1].blocks.empty?, 'person must have blocks in area 2'
@@ -33,15 +34,16 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -33,15 +34,16 @@ class CommunityTest &lt; ActiveSupport::TestCase
33 end 34 end
34 35
35 should 'create a default set of articles' do 36 should 'create a default set of articles' do
36 - Community.any_instance.stubs(:default_set_of_articles).returns([Blog.new(:name => 'blog')])  
37 - community = Community.create!(:environment => Environment.default, :name => 'my new community') 37 + blog = build(Blog)
  38 + Community.any_instance.stubs(:default_set_of_articles).returns([blog])
  39 + community = create(Community, :environment => Environment.default, :name => 'my new community')
38 40
39 - assert_kind_of Blog, community.articles.find_by_path('blog')  
40 - assert_kind_of RssFeed, community.articles.find_by_path('blog/feed') 41 + assert_kind_of Blog, community.articles.find_by_path(blog.path)
  42 + assert_kind_of RssFeed, community.articles.find_by_path(blog.feed.path)
41 end 43 end
42 44
43 should 'have contact_person' do 45 should 'have contact_person' do
44 - community = Community.new(:environment => Environment.default, :name => 'my new community') 46 + community = build(Community, :environment => Environment.default, :name => 'my new community')
45 assert_respond_to community, :contact_person 47 assert_respond_to community, :contact_person
46 end 48 end
47 49
@@ -86,20 +88,20 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -86,20 +88,20 @@ class CommunityTest &lt; ActiveSupport::TestCase
86 88
87 should 'have a community template' do 89 should 'have a community template' do
88 template = fast_create(Community, :is_template => true) 90 template = fast_create(Community, :is_template => true)
89 - p = Community.create!(:name => 'test_com', :identifier => 'test_com', :template => template) 91 + p = create(Community, :name => 'test_com', :identifier => 'test_com', :template => template)
90 assert_equal template, p.template 92 assert_equal template, p.template
91 end 93 end
92 94
93 should 'have a default community template' do 95 should 'have a default community template' do
94 - env = Environment.create!(:name => 'test env')  
95 - p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env) 96 + env = create(Environment, :name => 'test env')
  97 + p = create(Community, :name => 'test_com', :identifier => 'test_com', :environment => env)
96 assert_kind_of Community, p.template 98 assert_kind_of Community, p.template
97 end 99 end
98 100
99 should 'return active_community_fields' do 101 should 'return active_community_fields' do
100 e = Environment.default 102 e = Environment.default
101 e.expects(:active_community_fields).returns(['contact_phone', 'contact_email']).at_least_once 103 e.expects(:active_community_fields).returns(['contact_phone', 'contact_email']).at_least_once
102 - ent = Community.new(:environment => e) 104 + ent = build(Community, :environment => e)
103 105
104 assert_equal e.active_community_fields, ent.active_fields 106 assert_equal e.active_community_fields, ent.active_fields
105 end 107 end
@@ -107,7 +109,7 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -107,7 +109,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
107 should 'return required_community_fields' do 109 should 'return required_community_fields' do
108 e = Environment.default 110 e = Environment.default
109 e.expects(:required_community_fields).returns(['contact_phone', 'contact_email']).at_least_once 111 e.expects(:required_community_fields).returns(['contact_phone', 'contact_email']).at_least_once
110 - community = Community.new(:environment => e) 112 + community = build(Community, :environment => e)
111 113
112 assert_equal e.required_community_fields, community.required_fields 114 assert_equal e.required_community_fields, community.required_fields
113 end 115 end
@@ -115,7 +117,7 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -115,7 +117,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
115 should 'require fields if community needs' do 117 should 'require fields if community needs' do
116 e = Environment.default 118 e = Environment.default
117 e.expects(:required_community_fields).returns(['contact_phone']).at_least_once 119 e.expects(:required_community_fields).returns(['contact_phone']).at_least_once
118 - community = Community.new(:name => 'My community', :environment => e) 120 + community = build(Community, :name => 'My community', :environment => e)
119 assert ! community.valid? 121 assert ! community.valid?
120 assert community.errors[:contact_phone.to_s].present? 122 assert community.errors[:contact_phone.to_s].present?
121 123
@@ -127,7 +129,7 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -127,7 +129,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
127 should 'return newest text articles as news' do 129 should 'return newest text articles as news' do
128 c = fast_create(Community, :name => 'test_com') 130 c = fast_create(Community, :name => 'test_com')
129 f = fast_create(Folder, :name => 'folder', :profile_id => c.id) 131 f = fast_create(Folder, :name => 'folder', :profile_id => c.id)
130 - u = UploadedFile.create!(:profile => c, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) 132 + u = create(UploadedFile, :profile => c, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
131 older_t = fast_create(TinyMceArticle, :name => 'old news', :profile_id => c.id) 133 older_t = fast_create(TinyMceArticle, :name => 'old news', :profile_id => c.id)
132 t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id) 134 t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id)
133 t_in_f = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id, :parent_id => f.id) 135 t_in_f = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id, :parent_id => f.id)
@@ -152,13 +154,13 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -152,13 +154,13 @@ class CommunityTest &lt; ActiveSupport::TestCase
152 end 154 end
153 155
154 should 'sanitize description' do 156 should 'sanitize description' do
155 - c = Community.create!(:name => 'test_com', :description => '<b>new</b> community') 157 + c = create(Community, :name => 'test_com', :description => '<b>new</b> community')
156 158
157 assert_sanitized c.description 159 assert_sanitized c.description
158 end 160 end
159 161
160 should 'sanitize name' do 162 should 'sanitize name' do
161 - c = Community.create!(:name => '<b>test_com</b>') 163 + c = create(Community, :name => '<b>test_com</b>')
162 164
163 assert_sanitized c.name 165 assert_sanitized c.name
164 end 166 end
@@ -166,6 +168,7 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -166,6 +168,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
166 should 'create a task when creating a community if feature is enabled' do 168 should 'create a task when creating a community if feature is enabled' do
167 env = Environment.default 169 env = Environment.default
168 env.enable('admin_must_approve_new_communities') 170 env.enable('admin_must_approve_new_communities')
  171 + person.stubs(:notification_emails).returns(['sample@example.org'])
169 172
170 assert_difference CreateCommunity, :count do 173 assert_difference CreateCommunity, :count do
171 Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) 174 Community.create_after_moderation(person, {:environment => env, :name => 'Example'})
@@ -181,11 +184,11 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -181,11 +184,11 @@ class CommunityTest &lt; ActiveSupport::TestCase
181 env.disable('admin_must_approve_new_communities') 184 env.disable('admin_must_approve_new_communities')
182 185
183 assert_difference Community, :count do 186 assert_difference Community, :count do
184 - Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) 187 + Community.create_after_moderation(person, {:environment => env, :name => 'Example 1'})
185 end 188 end
186 189
187 assert_no_difference CreateCommunity, :count do 190 assert_no_difference CreateCommunity, :count do
188 - Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) 191 + Community.create_after_moderation(person, {:environment => env, :name => 'Example 2'})
189 end 192 end
190 end 193 end
191 194
@@ -216,6 +219,8 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -216,6 +219,8 @@ class CommunityTest &lt; ActiveSupport::TestCase
216 219
217 community.add_member(fast_create(Person)) 220 community.add_member(fast_create(Person))
218 221
  222 + community.stubs(:notification_emails).returns(['sample@example.org'])
  223 +
219 assert_difference AddMember, :count do 224 assert_difference AddMember, :count do
220 community.add_member(person) 225 community.add_member(person)
221 end 226 end
@@ -290,7 +295,7 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -290,7 +295,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
290 article = create(TextileArticle, :profile_id => community.id) 295 article = create(TextileArticle, :profile_id => community.id)
291 time = article.activity.updated_at 296 time = article.activity.updated_at
292 Time.stubs(:now).returns(time + 1.day) 297 Time.stubs(:now).returns(time + 1.day)
293 - Comment.create!(:source_id => article.id, :title => 'some', :body => 'some', :author_id => p2.id) 298 + create(Comment, :source_id => article.id, :title => 'some', :body => 'some', :author_id => p2.id)
294 process_delayed_job_queue 299 process_delayed_job_queue
295 assert_equal time, article.activity.updated_at 300 assert_equal time, article.activity.updated_at
296 end 301 end
@@ -348,7 +353,7 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -348,7 +353,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
348 person = fast_create(Person) 353 person = fast_create(Person)
349 community = fast_create(Community) 354 community = fast_create(Community)
350 355
351 - scrap = Scrap.create!(defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap')) 356 + scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap'))
352 activity = ActionTracker::Record.last 357 activity = ActionTracker::Record.last
353 358
354 assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) } 359 assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) }
@@ -359,7 +364,9 @@ class CommunityTest &lt; ActiveSupport::TestCase @@ -359,7 +364,9 @@ class CommunityTest &lt; ActiveSupport::TestCase
359 community = fast_create(Community) 364 community = fast_create(Community)
360 365
361 UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once 366 UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
362 - article = create(TinyMceArticle, :profile => community, :name => 'An article about free software') 367 + assert_difference ActionTracker::Record, :count, 1 do
  368 + article = create(TinyMceArticle, :profile => community, :name => 'An article about free software')
  369 + end
363 370
364 assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) } 371 assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) }
365 end 372 end