Commit a997e8fce5bda46d9700a0fb39d264ada7296943

Authored by Victor Costa
1 parent 26f85615

rails3: fix mass-assignment errors

app/models/approve_comment.rb
... ... @@ -6,7 +6,11 @@ class ApproveComment < Task
6 6 validates_presence_of :comment_attributes
7 7  
8 8 def comment
9   - @comment ||= Comment.new(ActiveSupport::JSON.decode(self.comment_attributes)) unless self.comment_attributes.nil?
  9 + unless @comment || self.comment_attributes.nil?
  10 + @comment = Comment.new
  11 + @comment.assign_attributes(ActiveSupport::JSON.decode(self.comment_attributes), :without_protection => true)
  12 + end
  13 + @comment
10 14 end
11 15  
12 16 def requestor_name
... ...
app/models/profile.rb
... ... @@ -3,7 +3,7 @@
3 3 # which by default is the one returned by Environment:default.
4 4 class Profile < ActiveRecord::Base
5 5  
6   - attr_accessible :name, :identifier
  6 + attr_accessible :name, :identifier, :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone
7 7  
8 8 # use for internationalizable human type names in search facets
9 9 # reimplement on subclasses
... ...
app/models/thumbnail.rb
... ... @@ -6,4 +6,6 @@ class Thumbnail &lt; ActiveRecord::Base
6 6 sanitize_filename
7 7  
8 8 postgresql_attachment_fu
  9 +
  10 + attr_accessible :uploaded_data
9 11 end
... ...
test/unit/action_tracker_notification_test.rb
... ... @@ -65,13 +65,13 @@ class ActionTrackerNotificationTest &lt; ActiveSupport::TestCase
65 65 at = fast_create(ActionTracker::Record)
66 66 person = fast_create(Person)
67 67 assert_equal [], at.action_tracker_notifications
68   - at.action_tracker_notifications<< ActionTrackerNotification.new(:profile => person)
  68 + at.action_tracker_notifications<< build(ActionTrackerNotification, :profile => person)
69 69 at.reload
70 70  
71 71 assert_equal 1, at.action_tracker_notifications.count
72 72 last_notification = at.action_tracker_notifications.first
73 73  
74   - at.action_tracker_notifications<< ActionTrackerNotification.new(:profile => person)
  74 + at.action_tracker_notifications<< build(ActionTrackerNotification, :profile => person)
75 75 at.reload
76 76 assert_equal [last_notification], at.action_tracker_notifications
77 77 end
... ...
test/unit/approve_comment_test.rb
... ... @@ -9,7 +9,7 @@ class ApproveCommentTest &lt; ActiveSupport::TestCase
9 9 @profile = create_user('test_user', :email => "someone@anyhost.com").person
10 10 @article = fast_create(TextileArticle, :profile_id => @profile.id, :name => 'test name', :abstract => 'Lead of article', :body => 'This is my article')
11 11 @community = create(Community, :contact_email => "someone@anyhost.com")
12   - @comment = @article.comments.build(:title => 'any comment', :body => "any text", :author => create_user('someperson').person)
  12 + @comment = build(Comment, :article => @article, :title => 'any comment', :body => "any text", :author => create_user('someperson').person)
13 13 end
14 14  
15 15 attr_reader :profile, :article, :community
... ...
test/unit/article_test.rb
... ... @@ -1766,7 +1766,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1766 1766  
1767 1767 should 'save image on create article' do
1768 1768 assert_difference Article, :count do
1769   - p = Article.create!(:name => 'test', :image_builder => {
  1769 + p = create(Article, :name => 'test', :image_builder => {
1770 1770 :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
1771 1771 }, :profile_id => @profile.id)
1772 1772 assert_equal p.image(true).filename, 'rails.png'
... ...
test/unit/blog_archives_block_test.rb
... ... @@ -59,7 +59,7 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
59 59 should 'order years' do
60 60 blog = profile.blog
61 61 for year in 2005..2009
62   - post = TextileArticle.create!(:name => "post #{year}", :profile => profile, :parent => blog, :published_at => Date.new(year, 1, 1))
  62 + post = create(TextileArticle, :name => "post #{year}", :profile => profile, :parent => blog, :published_at => Date.new(year, 1, 1))
63 63 end
64 64 block = BlogArchivesBlock.new
65 65 block.stubs(:owner).returns(profile)
... ... @@ -69,7 +69,7 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
69 69 should 'order months from later to former' do
70 70 blog = profile.blog
71 71 for month in 1..3
72   - post = TextileArticle.create!(:name => "post #{month}", :profile => profile, :parent => blog, :published_at => Date.new(2009, month, 1))
  72 + post = create(TextileArticle, :name => "post #{month}", :profile => profile, :parent => blog, :published_at => Date.new(2009, month, 1))
73 73 end
74 74 block = BlogArchivesBlock.new
75 75 block.stubs(:owner).returns(profile)
... ...
test/unit/blog_test.rb
... ... @@ -221,7 +221,7 @@ class BlogTest &lt; ActiveSupport::TestCase
221 221  
222 222 should 'set cover image' do
223 223 profile = fast_create(Profile)
224   - blog = Blog.create(:profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
  224 + blog = create(Blog, :profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
225 225 blog.save!
226 226 blog.reload
227 227 assert_equal blog.image(true).filename, 'rails.png'
... ... @@ -229,7 +229,7 @@ class BlogTest &lt; ActiveSupport::TestCase
229 229  
230 230 should 'remove cover image' do
231 231 profile = fast_create(Profile)
232   - blog = Blog.create(:profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
  232 + blog = create(Blog, :profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
233 233 blog.save!
234 234 blog.reload
235 235  
... ... @@ -241,7 +241,7 @@ class BlogTest &lt; ActiveSupport::TestCase
241 241  
242 242 should 'update cover image' do
243 243 profile = fast_create(Profile)
244   - blog = Blog.create(:profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
  244 + blog = create(Blog, :profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
245 245 blog.save!
246 246 blog.reload
247 247  
... ...
test/unit/box_test.rb
... ... @@ -100,7 +100,7 @@ class BoxTest &lt; ActiveSupport::TestCase
100 100 end
101 101 Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SomePlugin.new])
102 102  
103   - blocks = Box.new(:position => 1).acceptable_blocks
  103 + blocks = build(Box, :position => 1).acceptable_blocks
104 104 assert blocks.include?('plugin-block')
105 105 end
106 106  
... ... @@ -115,7 +115,7 @@ class BoxTest &lt; ActiveSupport::TestCase
115 115 end
116 116 Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SomePlugin.new])
117 117  
118   - blocks = Box.new(:position => 2).acceptable_blocks
  118 + blocks = build(Box, :position => 2).acceptable_blocks
119 119 assert blocks.include?('plugin-block')
120 120 end
121 121  
... ...
test/unit/forum_helper_test.rb
... ... @@ -29,16 +29,16 @@ class ForumHelperTest &lt; ActiveSupport::TestCase
29 29 end
30 30  
31 31 should 'list posts with different classes' do
32   - forum.children << older_post = TextileArticle.create!(:name => 'First post', :profile => profile, :parent => forum, :published => false, :last_changed_by => profile)
  32 + forum.children << older_post = create(TextileArticle, :name => 'First post', :profile => profile, :parent => forum, :published => false, :last_changed_by => profile)
33 33 one_month_later = Time.now + 1.month
34 34 Time.stubs(:now).returns(one_month_later)
35   - forum.children << newer_post = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => forum, :published => true, :last_changed_by => profile)
  35 + forum.children << newer_post = create(TextileArticle, :name => 'Second post', :profile => profile, :parent => forum, :published => true, :last_changed_by => profile)
36 36 assert_match /forum-post position-1 first odd-post.*forum-post position-2 last not-published even-post/, list_forum_posts(forum.posts)
37 37 end
38 38  
39 39 should 'return post update if it has no comments' do
40 40 author = create_user('forum test author').person
41   - some_post = TextileArticle.create!(:name => 'First post', :profile => profile, :parent => forum, :published => true, :last_changed_by => author)
  41 + some_post = create(TextileArticle, :name => 'First post', :profile => profile, :parent => forum, :published => true, :last_changed_by => author)
42 42 assert some_post.comments.empty?
43 43 out = last_topic_update(some_post)
44 44 assert_match some_post.updated_at.to_s, out
... ... @@ -46,10 +46,10 @@ class ForumHelperTest &lt; ActiveSupport::TestCase
46 46 end
47 47  
48 48 should 'return last comment date if it has comments' do
49   - some_post = TextileArticle.create!(:name => 'First post', :profile => profile, :parent => forum, :published => true)
  49 + some_post = create(TextileArticle, :name => 'First post', :profile => profile, :parent => forum, :published => true)
50 50 a1, a2 = create_user('a1').person, create_user('a2').person
51   - some_post.comments << Comment.new(:title => 'test', :body => 'test', :author => a1, :created_at => Time.now - 1.day)
52   - some_post.comments << Comment.new(:title => 'test', :body => 'test', :author => a2, :created_at => Time.now)
  51 + some_post.comments << build(Comment, :title => 'test', :body => 'test', :author => a1, :created_at => Time.now - 1.day)
  52 + some_post.comments << build(Comment, :title => 'test', :body => 'test', :author => a2, :created_at => Time.now)
53 53 c = Comment.last
54 54 assert_equal 2, some_post.comments.count
55 55 out = last_topic_update(some_post)
... ... @@ -60,8 +60,8 @@ class ForumHelperTest &lt; ActiveSupport::TestCase
60 60 end
61 61  
62 62 should "return last comment author's name from unauthenticated user" do
63   - some_post = TextileArticle.create!(:name => 'First post', :profile => profile, :parent => forum, :published => true)
64   - some_post.comments << Comment.new(:name => 'John', :email => 'lenon@example.com', :title => 'test', :body => 'test')
  63 + some_post = create(TextileArticle, :name => 'First post', :profile => profile, :parent => forum, :published => true)
  64 + some_post.comments << build(Comment, :name => 'John', :email => 'lenon@example.com', :title => 'test', :body => 'test')
65 65 c = Comment.last
66 66 out = last_topic_update(some_post)
67 67 assert_match "#{c.created_at.to_s} by John", out
... ...
test/unit/production_cost_test.rb
... ... @@ -32,9 +32,9 @@ class ProductionCostTest &lt; ActiveSupport::TestCase
32 32 end
33 33  
34 34 should 'not have duplicated name on same environment' do
35   - cost = ProductionCost.create(:name => 'Taxes', :owner => Environment.default)
  35 + cost = create(ProductionCost, :name => 'Taxes', :owner => Environment.default)
36 36  
37   - invalid_cost = ProductionCost.new(:name => 'Taxes', :owner => Environment.default)
  37 + invalid_cost = build(ProductionCost, :name => 'Taxes', :owner => Environment.default)
38 38 invalid_cost.valid?
39 39  
40 40 assert invalid_cost.errors[:name.to_s].present?
... ... @@ -42,9 +42,9 @@ class ProductionCostTest &lt; ActiveSupport::TestCase
42 42  
43 43 should 'not have duplicated name on same enterprise' do
44 44 enterprise = fast_create(Enterprise)
45   - cost = ProductionCost.create(:name => 'Taxes', :owner => enterprise)
  45 + cost = create(ProductionCost, :name => 'Taxes', :owner => enterprise)
46 46  
47   - invalid_cost = ProductionCost.new(:name => 'Taxes', :owner => enterprise)
  47 + invalid_cost = build(ProductionCost, :name => 'Taxes', :owner => enterprise)
48 48 invalid_cost.valid?
49 49  
50 50 assert invalid_cost.errors[:name.to_s].present?
... ... @@ -53,8 +53,8 @@ class ProductionCostTest &lt; ActiveSupport::TestCase
53 53 should 'not allow same name on enterprise if already has on environment' do
54 54 enterprise = fast_create(Enterprise)
55 55  
56   - cost1 = ProductionCost.create(:name => 'Taxes', :owner => Environment.default)
57   - cost2 = ProductionCost.new(:name => 'Taxes', :owner => enterprise)
  56 + cost1 = create(ProductionCost, :name => 'Taxes', :owner => Environment.default)
  57 + cost2 = create(ProductionCost, :name => 'Taxes', :owner => enterprise)
58 58  
59 59 cost2.valid?
60 60  
... ... @@ -65,8 +65,8 @@ class ProductionCostTest &lt; ActiveSupport::TestCase
65 65 enterprise = fast_create(Enterprise)
66 66 enterprise2 = fast_create(Enterprise)
67 67  
68   - cost1 = ProductionCost.create(:name => 'Taxes', :owner => enterprise)
69   - cost2 = ProductionCost.new(:name => 'Taxes', :owner => enterprise2)
  68 + cost1 = create(ProductionCost, :name => 'Taxes', :owner => enterprise)
  69 + cost2 = build(ProductionCost, :name => 'Taxes', :owner => enterprise2)
70 70  
71 71 cost2.valid?
72 72  
... ... @@ -96,7 +96,7 @@ class ProductionCostTest &lt; ActiveSupport::TestCase
96 96  
97 97 should 'create a production cost on an enterprise' do
98 98 enterprise = fast_create(Enterprise)
99   - enterprise.production_costs.create(:name => 'Energy')
  99 + create(ProductionCost, :name => 'Energy', :owner => enterprise)
100 100 assert_equal ['Energy'], enterprise.production_costs.map(&:name)
101 101 end
102 102 end
... ...
test/unit/profile_test.rb
... ... @@ -592,7 +592,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
592 592 category2 = fast_create(Category, :parent_id => pcat.id)
593 593 profile = create(Profile, :region => region, :category_ids => [category.id])
594 594  
595   - profile.update_attributes!(:category_ids => [category2.id])
  595 + profile.update_attribute(:category_ids, [category2.id])
596 596  
597 597 assert_includes profile.categories(true), region
598 598 assert_includes profile.categories_including_virtual(true), pcat
... ... @@ -605,7 +605,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
605 605 category = fast_create(Category, :parent_id => pcat.id)
606 606 profile = create(Profile, :region => region, :category_ids => [category.id])
607 607  
608   - profile.update_attributes!(:region => region2)
  608 + profile.update_attribute(:region, region2)
609 609  
610 610 assert_includes profile.categories(true), category
611 611 assert_includes profile.categories_including_virtual(true), pcat
... ... @@ -747,7 +747,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
747 747 end
748 748  
749 749 should 'filter html from nickname' do
750   - p = Profile.create!(:identifier => 'testprofile', :name => 'test profile', :environment => Environment.default)
  750 + p = create(Profile, :identifier => 'testprofile', :name => 'test profile', :environment => Environment.default)
751 751 p.nickname = "<b>code</b>"
752 752 p.save!
753 753 assert_equal 'code', p.nickname
... ... @@ -841,7 +841,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
841 841 end
842 842  
843 843 should 'store theme' do
844   - p = Profile.new(:theme => 'my-shiny-theme')
  844 + p = build(Profile, :theme => 'my-shiny-theme')
845 845 assert_equal 'my-shiny-theme', p.theme
846 846 end
847 847  
... ... @@ -1144,7 +1144,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
1144 1144 env = fast_create(Environment)
1145 1145  
1146 1146 p1 = fast_create(Profile, :identifier => 'mytestprofile', :environment_id => env.id)
1147   - p2 = Profile.new(:identifier => 'mytestprofile', :environment => env)
  1147 + p2 = build(Profile, :identifier => 'mytestprofile', :environment => env)
1148 1148  
1149 1149 assert !p2.valid?
1150 1150 assert p2.errors.on(:identifier)
... ... @@ -1199,14 +1199,14 @@ class ProfileTest &lt; ActiveSupport::TestCase
1199 1199 should 'enable contact for person only if its features enabled in environment' do
1200 1200 env = Environment.default
1201 1201 env.disable('disable_contact_person')
1202   - person = Person.new(:name => 'Contacted', :environment => env)
  1202 + person = build(Person, :name => 'Contacted', :environment => env)
1203 1203 assert person.enable_contact?
1204 1204 end
1205 1205  
1206 1206 should 'enable contact for community only if its features enabled in environment' do
1207 1207 env = Environment.default
1208 1208 env.disable('disable_contact_person')
1209   - community = Community.new(:name => 'Contacted', :environment => env)
  1209 + community = build(Community, :name => 'Contacted', :environment => env)
1210 1210 assert community.enable_contact?
1211 1211 end
1212 1212  
... ... @@ -1322,7 +1322,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
1322 1322 end
1323 1323  
1324 1324 should 'profile be valid when image is empty' do
1325   - profile = Profile.new(:image_builder => {:uploaded_data => ""})
  1325 + profile = build(Profile, :image_builder => {:uploaded_data => ""})
1326 1326 profile.valid?
1327 1327 assert_nil profile.errors[:image]
1328 1328 end
... ... @@ -1346,7 +1346,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
1346 1346  
1347 1347 should 'not have a profile as a template if it is not defined as a template' do
1348 1348 template = fast_create(Profile)
1349   - profile = Profile.new(:template => template)
  1349 + profile = build(Profile, :template => template)
1350 1350 !profile.valid?
1351 1351 assert profile.errors[:template.to_s].present?
1352 1352  
... ... @@ -1752,22 +1752,22 @@ class ProfileTest &lt; ActiveSupport::TestCase
1752 1752  
1753 1753 should 'get organization roles' do
1754 1754 env = fast_create(Environment)
1755   - roles = %w(foo bar profile_foo profile_bar).map{ |r| Role.create!(:name => r, :key => r, :environment_id => env.id, :permissions => ["some"]) }
1756   - Role.create! :name => 'test', :key => 'profile_test', :environment_id => env.id + 1
  1755 + roles = %w(foo bar profile_foo profile_bar).map{ |r| create(Role, :name => r, :key => r, :environment_id => env.id, :permissions => ["some"]) }
  1756 + create Role, :name => 'test', :key => 'profile_test', :environment_id => env.id + 1
1757 1757 Profile::Roles.expects(:all_roles).returns(roles)
1758 1758 assert_equal roles[2..3], Profile::Roles.organization_member_roles(env.id)
1759 1759 end
1760 1760  
1761 1761 should 'get all roles' do
1762 1762 env = fast_create(Environment)
1763   - roles = %w(foo bar profile_foo profile_bar).map{ |r| Role.create!(:name => r, :environment_id => env.id, :permissions => ["some"]) }
1764   - Role.create! :name => 'test', :environment_id => env.id + 1
  1763 + roles = %w(foo bar profile_foo profile_bar).map{ |r| create(Role, :name => r, :environment_id => env.id, :permissions => ["some"]) }
  1764 + create Role, :name => 'test', :environment_id => env.id + 1
1765 1765 assert_equal roles, Profile::Roles.all_roles(env.id)
1766 1766 end
1767 1767  
1768 1768 should 'define method for role' do
1769 1769 env = fast_create(Environment)
1770   - r = Role.create! :name => 'Test Role', :environment_id => env.id
  1770 + r = create Role, :name => 'Test Role', :environment_id => env.id
1771 1771 assert_equal r, Profile::Roles.test_role(env.id)
1772 1772 assert_raise NoMethodError do
1773 1773 Profile::Roles.invalid_role(env.id)
... ...
test/unit/scrap_test.rb
... ... @@ -289,7 +289,7 @@ class ScrapTest &lt; ActiveSupport::TestCase
289 289 s, r = fast_create(Person), fast_create(Person)
290 290 root = fast_create(Scrap, :sender_id => s.id, :receiver_id => r.id)
291 291 assert_difference ActionTracker::Record, :count, 1 do
292   - reply = Scrap.create!(:sender => r, :receiver => s, :scrap_id => root.id, :content => 'sample')
  292 + reply = create(Scrap, :sender => r, :receiver => s, :scrap_id => root.id, :content => 'sample')
293 293 end
294 294 activity = ActionTracker::Record.last
295 295 assert_equal 'reply_scrap_on_self', activity.verb.to_s
... ...
test/unit/tags_block_test.rb
... ... @@ -35,8 +35,8 @@ class TagsBlockTest &lt; ActiveSupport::TestCase
35 35 @otheruser = create_user('othertestinguser').person
36 36 @otheruser.articles.build(:name => 'article A', :tag_list => 'other-tag').save!
37 37 @otheruser.articles.build(:name => 'article B', :tag_list => 'other-tag, second-tag').save!
38   - box = Box.create!(:owner => Environment.default)
39   - @block = TagsBlock.create!(:box => box)
  38 + box = create(Box, :owner => Environment.default)
  39 + @block = create(TagsBlock, :box => box)
40 40  
41 41 assert_match /\/tag\/first-tag" [^>]+"3 items"/, block.content
42 42 assert_match /\/tag\/second-tag" [^>]+"3 items"/, block.content
... ...