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 10 @community = Community.new(params[:community])
11 11 @community.environment = environment
12 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 14 redirect_to :action => 'index'
15 15 return
16 16 end
... ...
app/models/community.rb
... ... @@ -18,12 +18,18 @@ class Community < Organization
18 18 community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default')
19 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 25 def self.create_after_moderation(requestor, attributes = {})
  26 + environment = attributes.delete(:environment)
22 27 community = Community.new(attributes)
  28 + community.environment = environment
23 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 31 else
26   - community = Community.create(attributes)
  32 + community.save!
27 33 community.add_admin(requestor)
28 34 end
29 35 community
... ... @@ -39,8 +45,9 @@ class Community < Organization
39 45 super + FIELDS
40 46 end
41 47  
42   - def validate
43   - super
  48 + validate :presence_of_required_fieds
  49 +
  50 + def presence_of_required_fieds
44 51 self.required_fields.each do |field|
45 52 if self.send(field).blank?
46 53 self.errors.add_on_blank(field)
... ...
app/models/profile.rb
... ... @@ -343,9 +343,14 @@ class Profile < ActiveRecord::Base
343 343 def copy_blocks_from(profile)
344 344 self.boxes.destroy_all
345 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 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 354 end
350 355 end
351 356 end
... ... @@ -373,7 +378,7 @@ class Profile &lt; ActiveRecord::Base
373 378 self.public_profile = template.public_profile
374 379  
375 380 # flush
376   - self.save_without_validation!
  381 + self.save(:validate => false)
377 382 end
378 383  
379 384 def apply_type_specific_template(template)
... ...
app/models/user.rb
... ... @@ -310,6 +310,6 @@ class User &lt; ActiveRecord::Base
310 310 end
311 311  
312 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 314 end
315 315 end
... ...
test/unit/community_test.rb
... ... @@ -13,19 +13,20 @@ class CommunityTest &lt; ActiveSupport::TestCase
13 13 end
14 14  
15 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 18 assert_equal 'My shiny new Community', c.name
18 19 assert_equal 'my-shiny-new-community', c.identifier
19 20 end
20 21  
21 22 should 'have a description attribute' do
22   - c = Community.new(:environment => Environment.default)
  23 + c = build(Community, :environment => Environment.default)
23 24 c.description = 'the description of the community'
24 25 assert_equal 'the description of the community', c.description
25 26 end
26 27  
27 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 31 assert !c.boxes[0].blocks.empty?, 'person must have blocks in area 1'
31 32 assert !c.boxes[1].blocks.empty?, 'person must have blocks in area 2'
... ... @@ -33,15 +34,16 @@ class CommunityTest &lt; ActiveSupport::TestCase
33 34 end
34 35  
35 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 43 end
42 44  
43 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 47 assert_respond_to community, :contact_person
46 48 end
47 49  
... ... @@ -86,20 +88,20 @@ class CommunityTest &lt; ActiveSupport::TestCase
86 88  
87 89 should 'have a community template' do
88 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 92 assert_equal template, p.template
91 93 end
92 94  
93 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 98 assert_kind_of Community, p.template
97 99 end
98 100  
99 101 should 'return active_community_fields' do
100 102 e = Environment.default
101 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 106 assert_equal e.active_community_fields, ent.active_fields
105 107 end
... ... @@ -107,7 +109,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
107 109 should 'return required_community_fields' do
108 110 e = Environment.default
109 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 114 assert_equal e.required_community_fields, community.required_fields
113 115 end
... ... @@ -115,7 +117,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
115 117 should 'require fields if community needs' do
116 118 e = Environment.default
117 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 121 assert ! community.valid?
120 122 assert community.errors[:contact_phone.to_s].present?
121 123  
... ... @@ -127,7 +129,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
127 129 should 'return newest text articles as news' do
128 130 c = fast_create(Community, :name => 'test_com')
129 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 133 older_t = fast_create(TinyMceArticle, :name => 'old news', :profile_id => c.id)
132 134 t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id)
133 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 154 end
153 155  
154 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 159 assert_sanitized c.description
158 160 end
159 161  
160 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 165 assert_sanitized c.name
164 166 end
... ... @@ -166,6 +168,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
166 168 should 'create a task when creating a community if feature is enabled' do
167 169 env = Environment.default
168 170 env.enable('admin_must_approve_new_communities')
  171 + person.stubs(:notification_emails).returns(['sample@example.org'])
169 172  
170 173 assert_difference CreateCommunity, :count do
171 174 Community.create_after_moderation(person, {:environment => env, :name => 'Example'})
... ... @@ -181,11 +184,11 @@ class CommunityTest &lt; ActiveSupport::TestCase
181 184 env.disable('admin_must_approve_new_communities')
182 185  
183 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 188 end
186 189  
187 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 192 end
190 193 end
191 194  
... ... @@ -216,6 +219,8 @@ class CommunityTest &lt; ActiveSupport::TestCase
216 219  
217 220 community.add_member(fast_create(Person))
218 221  
  222 + community.stubs(:notification_emails).returns(['sample@example.org'])
  223 +
219 224 assert_difference AddMember, :count do
220 225 community.add_member(person)
221 226 end
... ... @@ -290,7 +295,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
290 295 article = create(TextileArticle, :profile_id => community.id)
291 296 time = article.activity.updated_at
292 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 299 process_delayed_job_queue
295 300 assert_equal time, article.activity.updated_at
296 301 end
... ... @@ -348,7 +353,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
348 353 person = fast_create(Person)
349 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 357 activity = ActionTracker::Record.last
353 358  
354 359 assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) }
... ... @@ -359,7 +364,9 @@ class CommunityTest &lt; ActiveSupport::TestCase
359 364 community = fast_create(Community)
360 365  
361 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 371 assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) }
365 372 end
... ...