From e5dfdd1575c7957e1d8fbe594f9062adc87eddf6 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 9 Oct 2013 17:17:34 +0000 Subject: [PATCH] rails3: fix community tests --- app/controllers/my_profile/memberships_controller.rb | 2 +- app/models/community.rb | 15 +++++++++++---- app/models/profile.rb | 11 ++++++++--- app/models/user.rb | 2 +- test/unit/community_test.rb | 51 +++++++++++++++++++++++++++++---------------------- 5 files changed, 50 insertions(+), 31 deletions(-) diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index a93480f..80e2913 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -10,7 +10,7 @@ class MembershipsController < MyProfileController @community = Community.new(params[:community]) @community.environment = environment if request.post? && @community.valid? - @community = Community.create_after_moderation(user, {:environment => environment}.merge(params[:community])) + @community = Community.create_after_moderation(user, params[:community].merge({:environment => environment})) redirect_to :action => 'index' return end diff --git a/app/models/community.rb b/app/models/community.rb index 55aa596..42507f6 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -18,12 +18,18 @@ class Community < Organization community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default') end + # Since we it's not a good idea to add the environment as accessible through + # mass-assignment, we set it manually here. Note that this requires that the + # places that call this method are safe from mass-assignment by setting the + # environment key themselves. def self.create_after_moderation(requestor, attributes = {}) + environment = attributes.delete(:environment) community = Community.new(attributes) + community.environment = environment if community.environment.enabled?('admin_must_approve_new_communities') - CreateCommunity.create(attributes.merge(:requestor => requestor)) + CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment)) else - community = Community.create(attributes) + community.save! community.add_admin(requestor) end community @@ -39,8 +45,9 @@ class Community < Organization super + FIELDS end - def validate - super + validate :presence_of_required_fieds + + def presence_of_required_fieds self.required_fields.each do |field| if self.send(field).blank? self.errors.add_on_blank(field) diff --git a/app/models/profile.rb b/app/models/profile.rb index e6dbddb..2d3efa5 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -343,9 +343,14 @@ class Profile < ActiveRecord::Base def copy_blocks_from(profile) self.boxes.destroy_all profile.boxes.each do |box| - self.boxes << Box.new(:position => box.position) + new_box = Box.new + new_box.position = box.position + self.boxes << new_box box.blocks.each do |block| - self.boxes[-1].blocks << block.class.new(:title => block[:title], :settings => block.settings, :position => block.position) + new_block = block.class.new(:title => block[:title]) + new_block.settings = block.settings + new_block.position = block.position + self.boxes[-1].blocks << new_block end end end @@ -373,7 +378,7 @@ class Profile < ActiveRecord::Base self.public_profile = template.public_profile # flush - self.save_without_validation! + self.save(:validate => false) end def apply_type_specific_template(template) diff --git a/app/models/user.rb b/app/models/user.rb index 8dd7962..d3adf66 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -310,6 +310,6 @@ class User < ActiveRecord::Base end def delay_activation_check - Delayed::Job.enqueue(UserActivationJob.new(self.id), 0, 72.hours.from_now) + Delayed::Job.enqueue(UserActivationJob.new(self.id), {:priority => 0, :run_at => 72.hours.from_now}) end end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index 46f4ae4..553c77d 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -13,19 +13,20 @@ class CommunityTest < ActiveSupport::TestCase end should 'convert name into identifier' do - c = Community.new(:environment => Environment.default, :name =>'My shiny new Community') + c = Community.new + c.name ='My shiny new Community' assert_equal 'My shiny new Community', c.name assert_equal 'my-shiny-new-community', c.identifier end should 'have a description attribute' do - c = Community.new(:environment => Environment.default) + c = build(Community, :environment => Environment.default) c.description = 'the description of the community' assert_equal 'the description of the community', c.description end should 'create default set of blocks' do - c = Community.create!(:environment => Environment.default, :name => 'my new community') + c = create(Community, :environment => Environment.default, :name => 'my new community') assert !c.boxes[0].blocks.empty?, 'person must have blocks in area 1' assert !c.boxes[1].blocks.empty?, 'person must have blocks in area 2' @@ -33,15 +34,16 @@ class CommunityTest < ActiveSupport::TestCase end should 'create a default set of articles' do - Community.any_instance.stubs(:default_set_of_articles).returns([Blog.new(:name => 'blog')]) - community = Community.create!(:environment => Environment.default, :name => 'my new community') + blog = build(Blog) + Community.any_instance.stubs(:default_set_of_articles).returns([blog]) + community = create(Community, :environment => Environment.default, :name => 'my new community') - assert_kind_of Blog, community.articles.find_by_path('blog') - assert_kind_of RssFeed, community.articles.find_by_path('blog/feed') + assert_kind_of Blog, community.articles.find_by_path(blog.path) + assert_kind_of RssFeed, community.articles.find_by_path(blog.feed.path) end should 'have contact_person' do - community = Community.new(:environment => Environment.default, :name => 'my new community') + community = build(Community, :environment => Environment.default, :name => 'my new community') assert_respond_to community, :contact_person end @@ -86,20 +88,20 @@ class CommunityTest < ActiveSupport::TestCase should 'have a community template' do template = fast_create(Community, :is_template => true) - p = Community.create!(:name => 'test_com', :identifier => 'test_com', :template => template) + p = create(Community, :name => 'test_com', :identifier => 'test_com', :template => template) assert_equal template, p.template end should 'have a default community template' do - env = Environment.create!(:name => 'test env') - p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env) + env = create(Environment, :name => 'test env') + p = create(Community, :name => 'test_com', :identifier => 'test_com', :environment => env) assert_kind_of Community, p.template end should 'return active_community_fields' do e = Environment.default e.expects(:active_community_fields).returns(['contact_phone', 'contact_email']).at_least_once - ent = Community.new(:environment => e) + ent = build(Community, :environment => e) assert_equal e.active_community_fields, ent.active_fields end @@ -107,7 +109,7 @@ class CommunityTest < ActiveSupport::TestCase should 'return required_community_fields' do e = Environment.default e.expects(:required_community_fields).returns(['contact_phone', 'contact_email']).at_least_once - community = Community.new(:environment => e) + community = build(Community, :environment => e) assert_equal e.required_community_fields, community.required_fields end @@ -115,7 +117,7 @@ class CommunityTest < ActiveSupport::TestCase should 'require fields if community needs' do e = Environment.default e.expects(:required_community_fields).returns(['contact_phone']).at_least_once - community = Community.new(:name => 'My community', :environment => e) + community = build(Community, :name => 'My community', :environment => e) assert ! community.valid? assert community.errors[:contact_phone.to_s].present? @@ -127,7 +129,7 @@ class CommunityTest < ActiveSupport::TestCase should 'return newest text articles as news' do c = fast_create(Community, :name => 'test_com') f = fast_create(Folder, :name => 'folder', :profile_id => c.id) - u = UploadedFile.create!(:profile => c, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + u = create(UploadedFile, :profile => c, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) older_t = fast_create(TinyMceArticle, :name => 'old news', :profile_id => c.id) t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id) t_in_f = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id, :parent_id => f.id) @@ -152,13 +154,13 @@ class CommunityTest < ActiveSupport::TestCase end should 'sanitize description' do - c = Community.create!(:name => 'test_com', :description => 'new community') + c = create(Community, :name => 'test_com', :description => 'new community') assert_sanitized c.description end should 'sanitize name' do - c = Community.create!(:name => 'test_com') + c = create(Community, :name => 'test_com') assert_sanitized c.name end @@ -166,6 +168,7 @@ class CommunityTest < ActiveSupport::TestCase should 'create a task when creating a community if feature is enabled' do env = Environment.default env.enable('admin_must_approve_new_communities') + person.stubs(:notification_emails).returns(['sample@example.org']) assert_difference CreateCommunity, :count do Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) @@ -181,11 +184,11 @@ class CommunityTest < ActiveSupport::TestCase env.disable('admin_must_approve_new_communities') assert_difference Community, :count do - Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) + Community.create_after_moderation(person, {:environment => env, :name => 'Example 1'}) end assert_no_difference CreateCommunity, :count do - Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) + Community.create_after_moderation(person, {:environment => env, :name => 'Example 2'}) end end @@ -216,6 +219,8 @@ class CommunityTest < ActiveSupport::TestCase community.add_member(fast_create(Person)) + community.stubs(:notification_emails).returns(['sample@example.org']) + assert_difference AddMember, :count do community.add_member(person) end @@ -290,7 +295,7 @@ class CommunityTest < ActiveSupport::TestCase article = create(TextileArticle, :profile_id => community.id) time = article.activity.updated_at Time.stubs(:now).returns(time + 1.day) - Comment.create!(:source_id => article.id, :title => 'some', :body => 'some', :author_id => p2.id) + create(Comment, :source_id => article.id, :title => 'some', :body => 'some', :author_id => p2.id) process_delayed_job_queue assert_equal time, article.activity.updated_at end @@ -348,7 +353,7 @@ class CommunityTest < ActiveSupport::TestCase person = fast_create(Person) community = fast_create(Community) - scrap = Scrap.create!(defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap')) + scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap')) activity = ActionTracker::Record.last assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) } @@ -359,7 +364,9 @@ class CommunityTest < ActiveSupport::TestCase community = fast_create(Community) UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once - article = create(TinyMceArticle, :profile => community, :name => 'An article about free software') + assert_difference ActionTracker::Record, :count, 1 do + article = create(TinyMceArticle, :profile => community, :name => 'An article about free software') + end assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) } end -- libgit2 0.21.2