diff --git a/lib/tasks/test_smells.rake b/lib/tasks/test_smells.rake new file mode 100644 index 0000000..4eefbe8 --- /dev/null +++ b/lib/tasks/test_smells.rake @@ -0,0 +1,18 @@ +def find_test_smells(id, title, pattern) + full_id = 'test:smells:' + id + task full_id do + system(" + ( + echo '########################################################' + echo '# #{title}' + echo '########################################################' + echo + ack-grep --group --color '#{pattern}' test/unit/ test/functional/ test/integration/ + ) | less -R + ") + end + task 'test:smells' => full_id +end + +find_test_smells 'dbhits', "Full database hits (they are probably unecessary)", '\.create' +find_test_smells 'constants', 'Probably unecessary contants for creating objects', "create_user.*password" diff --git a/test/factories.rb b/test/factories.rb index 99c598f..d7d5a1e 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -1,14 +1,17 @@ module Noosfero::Factory - - def fast_create(name, attrs = {}) - obj = build(name, attrs) - obj.attributes.keys.each do |attr| - if !obj.column_for_attribute(attr).null && obj.send(attr).nil? - obj.send("#{attr}=", factory_num_seq) - end + + def fast_create(name, attrs = {}, options = {}) + data = defaults_for(name).merge(attrs) + klass = name.to_s.camelize.constantize + if klass.superclass != ActiveRecord::Base + data[:type] = klass.to_s + end + if options[:timestamps] + fast_insert_with_timestamps(klass, data) + else + fast_insert(klass, data) end - obj.save_without_validation! - obj + return klass.last(:order => "id") end def create(name, attrs = {}) @@ -23,13 +26,12 @@ module Noosfero::Factory end def build(name, attrs = {}) - data = - if respond_to?('defaults_for_' + name.to_s) - send('defaults_for_'+ name.to_s).merge(attrs) - else - attrs - end - eval(name.to_s.camelize).new(data) + data = defaults_for(name).merge(attrs) + name.to_s.camelize.constantize.new(data) + end + + def defaults_for(name) + send('defaults_for_' + name.to_s.underscore) || {} end def self.num_seq @@ -38,6 +40,105 @@ module Noosfero::Factory @num_seq end + ###### old stuff to be rearranged + def create_admin_user(env) + admin_user = User.find_by_login('adminuser') || create_user('adminuser', :email => 'adminuser@noosfero.org', :password => 'adminuser', :password_confirmation => 'adminuser', :environment => env) + admin_role = Role.find_by_name('admin_role') || Role.create!(:name => 'admin_role', :permissions => ['view_environment_admin_panel','edit_environment_features', 'edit_environment_design', 'manage_environment_categories', 'manage_environment_roles', 'manage_environment_validators']) + RoleAssignment.create!(:accessor => admin_user.person, :role => admin_role, :resource => env) unless admin_user.person.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([admin_role, admin_user, env]) + admin_user.login + end + + def create_environment(domainname) + environment = fast_create(Environment) + fast_create(Domain, :name => domainname, :owner_type => 'Environment', :owner_id => environment.id) + environment + end + + # Old version of create user. Use it if you need to create an user for + # testing that passes through the actual user creation process. + # + # Be aware that this is slow, though. + def create_user_full(name, options = {}, person_options = {}) + data = { + :login => name, + :email => name + '@noosfero.org', + :password => name.underscore, + :password_confirmation => name.underscore + }.merge(options) + user = User.new(data) + user.save! + user.person.update_attributes!(person_data.merge(person_options)) + user + end + + def person_data + {} + end + + # This method knows way too much about the model. But since creating an + # actual user is really expensive, for tests we need a fast alternative. + def create_user(name, options = {}, person_options = {}) + environment_id = options.delete(:environment_id) || (options.delete(:environment) || Environment.default).id + + password = options.delete(:password) + password_confirmation = options.delete(:password_confirmation) + raise Exception.new("Passwords don't match") if (password && password_confirmation && password != password_confirmation) + crypted_password = (password || name).crypt('xy') + + data = { + :login => name, + :email => name + '@noosfero.org', + :crypted_password => crypted_password, + :password_type => 'crypt', + :salt => 'xy', + :environment_id => environment_id, + }.merge(options) + user = fast_insert_with_timestamps(User, data) + person = fast_insert_with_timestamps(Person, { :type => 'Person', :identifier => name, :user_id => user.id, :environment_id => environment_id }.merge(person_options)) + homepage = fast_insert_with_timestamps(TextileArticle, { :type => 'TextileArticle', :name => 'homepage', :slug => 'homepage', :path => 'homepage', :profile_id => person.id }) + fast_update(person, {:home_page_id => homepage.id}) + box = fast_insert(Box, { :owner_type => "Profile", :owner_id => person.id, :position => 1}) + block = fast_insert(Block, { :box_id => box.id, :type => 'MainBlock', :position => 0}) + user + end + + def fast_insert(klass, data) + names = data.keys + values = names.map {|k| ActiveRecord::Base.send(:sanitize_sql_array, ['?', data[k]]) } + sql = 'insert into %s(%s) values (%s)' % [klass.table_name, names.join(','), values.join(',')] + klass.connection.execute(sql) + klass.last(:order => 'id') + end + + def fast_insert_with_timestamps(klass, data) + now = Time.now + fast_insert(klass, { :created_at => now, :updated_at => now}.merge(data)) + end + + def fast_update(obj, data) + obj.class.connection.execute('update %s set %s where id = %d' % [obj.class.table_name, ActiveRecord::Base.send(:sanitize_sql_for_assignment, data), obj.id]) + end + + def give_permission(user, permission, target) + user = Person.find_by_identifier(user) if user.kind_of?(String) + target ||= user + i = 0 + while Role.find_by_name('test_role' + i.to_s) + i+=1 + end + + role = Role.create!(:name => 'test_role' + i.to_s, :permissions => [permission]) + assert user.add_role(role, target) + assert user.has_permission?(permission, target) + user + end + + def create_user_with_permission(name, permission, target= nil) + user = create_user(name).person + give_permission(user, permission, target) + end + + protected def factory_num_seq @@ -45,6 +146,49 @@ module Noosfero::Factory end ############################################### + # Environment + ############################################### + + def defaults_for_environment + { :name => 'Environment ' + factory_num_seq.to_s } + end + + ############################################### + # Enterprise + ############################################### + + def defaults_for_enterprise + n = factory_num_seq.to_s + defaults_for_profile.merge({ :identifier => "enterprise-" + n, :name => 'Enterprise ' + n }) + end + + ############################################### + # Enterprise + ############################################### + + def defaults_for_community + n = factory_num_seq.to_s + defaults_for_profile.merge({ :identifier => "community-" + n, :name => 'Community ' + n }) + end + + ############################################### + # Profile + ############################################### + + def defaults_for_profile + n = factory_num_seq.to_s + { :public_profile => true, :identifier => 'profile-' + n, :name => 'Profile ' + n, :environment_id => 1 } + end + + ############################################### + # Article + ############################################### + + def defaults_for_article + { :name => 'My article ' + factory_num_seq.to_s } + end + + ############################################### # Blog ############################################### def create_blog @@ -56,7 +200,7 @@ module Noosfero::Factory # ExternalFeed ############################################### def defaults_for_external_feed - { :address => RAILS_ROOT + '/test/fixtures/files/feed.xml' } + { :address => RAILS_ROOT + '/test/fixtures/files/feed.xml', :blog_id => factory_num_seq } end def create_external_feed(attrs = {}) @@ -73,4 +217,22 @@ module Noosfero::Factory { :address => RAILS_ROOT + '/test/fixtures/files/feed.xml' } end + ############################################### + # Domain + ############################################### + def defaults_for_domain + { :name => 'example' + factory_num_seq.to_s + '.com' } + end + + ############################################### + # Category + ############################################### + def defaults_for_category + { :environment_id => Environment.default.id, :name => 'category' + factory_num_seq.to_s } + end + + def defaults_for_region + defaults_for_category + end + end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 5edee98..46f155b 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -405,7 +405,7 @@ class ApplicationControllerTest < Test::Unit::TestCase should 'not display invisible blocks' do @controller.expects(:uses_design_blocks?).returns(true) - p = create_user('test_user').person + p = create_user_full('test_user').person @controller.expects(:profile).at_least_once.returns(p) b = p.blocks[1] b.expects(:visible).returns(false) diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 9265ad4..fef6906 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -139,20 +139,6 @@ class ProfileControllerTest < Test::Unit::TestCase assert_tag :tag => 'a', :content => /Friends/, :attributes => { :href => /profile\/#{person.identifier}\/friends$/ } end - should 'not show homepage and feed automatically created on recent content' do - person = create_user('person_1').person - get :index, :profile => person.identifier - assert_tag :tag => 'div', :content => 'Recent content', :attributes => { :class => 'block recent-documents-block' }, :child => { :tag => 'ul', :content => '' } - end - - should 'show homepage on recent content after update' do - person = create_user('person_1').person - person.home_page.name = 'Changed name' - assert person.home_page.save! - get :index, :profile => person.identifier - assert_tag :tag => 'div', :content => 'Recent content', :attributes => { :class => 'block recent-documents-block' }, :child => { :tag => 'ul', :content => /#{person.home_page.name}/ } - end - should 'display tag for profile' do @profile.articles.create!(:name => 'testarticle', :tag_list => 'tag1') @@ -243,14 +229,14 @@ class ProfileControllerTest < Test::Unit::TestCase should 'display add friend button' do login_as(@profile.identifier) - friend = create_user('friendtestuser').person + friend = create_user_full('friendtestuser').person get :index, :profile => friend.identifier assert_tag :tag => 'a', :content => 'Add friend' end should 'not display add friend button if user already request friendship' do login_as(@profile.identifier) - friend = create_user('friendtestuser').person + friend = create_user_full('friendtestuser').person AddFriend.create!(:person => @profile, :friend => friend) get :index, :profile => friend.identifier assert_no_tag :tag => 'a', :content => 'Add friend' @@ -258,7 +244,7 @@ class ProfileControllerTest < Test::Unit::TestCase should 'not display add friend button if user already friend' do login_as(@profile.identifier) - friend = create_user('friendtestuser').person + friend = create_user_full('friendtestuser').person @profile.add_friend(friend) @profile.friends.reload assert @profile.is_a_friend?(friend) @@ -369,7 +355,7 @@ class ProfileControllerTest < Test::Unit::TestCase end should 'display contact button only if friends' do - friend = create_user('friend_user').person + friend = create_user_full('friend_user').person @profile.add_friend(friend) env = Environment.default env.disable('disable_contact_person') @@ -380,14 +366,14 @@ class ProfileControllerTest < Test::Unit::TestCase end should 'not display contact button if no friends' do - nofriend = create_user('no_friend').person + nofriend = create_user_full('no_friend').person login_as(@profile.identifier) get :index, :profile => nofriend.identifier assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{nofriend.identifier}/new" } end should 'display contact button only if friends and its enable in environment' do - friend = create_user('friend_user').person + friend = create_user_full('friend_user').person env = Environment.default env.disable('disable_contact_person') env.save! @@ -398,7 +384,7 @@ class ProfileControllerTest < Test::Unit::TestCase end should 'not display contact button if friends and its disable in environment' do - friend = create_user('friend_user').person + friend = create_user_full('friend_user').person env = Environment.default env.enable('disable_contact_person') env.save! @@ -409,7 +395,6 @@ class ProfileControllerTest < Test::Unit::TestCase end should 'display contact button for community if its enable in environment' do - friend = create_user('friend_user').person env = Environment.default community = Community.create!(:name => 'my test community', :environment => env) env.disable('disable_contact_community') diff --git a/test/functional/profile_members_controller_test.rb b/test/functional/profile_members_controller_test.rb index 4fc849b..4b07b05 100644 --- a/test/functional/profile_members_controller_test.rb +++ b/test/functional/profile_members_controller_test.rb @@ -220,7 +220,7 @@ class ProfileMembersControllerTest < Test::Unit::TestCase should 'find users' do ent = Enterprise.create!(:name => 'Test Ent', :identifier => 'test_ent') - user = create_user('test_user').person + user = create_user_full('test_user').person u = create_user_with_permission('ent_user', 'manage_memberships', ent) login_as :ent_user @@ -240,8 +240,8 @@ class ProfileMembersControllerTest < Test::Unit::TestCase end should 'return users with as a prefix' do - daniel = create_user('daniel').person - daniela = create_user('daniela').person + daniel = create_user_full('daniel').person + daniela = create_user_full('daniela').person ent = Enterprise.create!(:name => 'Test Ent', :identifier => 'test_ent') p = create_user_with_permission('test_user', 'manage_memberships', ent) diff --git a/test/test_helper.rb b/test/test_helper.rb index f1902f5..6e64237 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -68,56 +68,6 @@ class Test::Unit::TestCase end - def create_admin_user(env) - admin_user = User.find_by_login('adminuser') || create_user('adminuser', :email => 'adminuser@noosfero.org', :password => 'adminuser', :password_confirmation => 'adminuser', :environment => env) - admin_role = Role.find_by_name('admin_role') || Role.create!(:name => 'admin_role', :permissions => ['view_environment_admin_panel','edit_environment_features', 'edit_environment_design', 'manage_environment_categories', 'manage_environment_roles', 'manage_environment_validators']) - RoleAssignment.create!(:accessor => admin_user.person, :role => admin_role, :resource => env) unless admin_user.person.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([admin_role, admin_user, env]) - admin_user.login - end - - def create_environment(domainname) - env = Environment.create!(:name => domainname) - env.domains << Domain.new(:name => domainname) - env.save! - env - end - - def create_user(name, options = {}, person_options = {}) - data = { - :login => name, - :email => name + '@noosfero.org', - :password => name.underscore, - :password_confirmation => name.underscore - }.merge(options) - user = User.new(data) - user.save! - user.person.update_attributes!(person_data.merge(person_options)) - user - end - - def person_data - {} - end - - def give_permission(user, permission, target) - user = Person.find_by_identifier(user) if user.kind_of?(String) - target ||= user - i = 0 - while Role.find_by_name('test_role' + i.to_s) - i+=1 - end - - role = Role.create!(:name => 'test_role' + i.to_s, :permissions => [permission]) - assert user.add_role(role, target) - assert user.has_permission?(permission, target) - user - end - - def create_user_with_permission(name, permission, target= nil) - user = create_user(name).person - give_permission(user, permission, target) - end - alias :ok :assert_block def assert_equivalent(enum1, enum2) @@ -247,7 +197,6 @@ class ActionController::IntegrationTest assert_tag :tag => 'a', :attributes => { :href => '/account/logout' } end - def login(username, password) ActionController::Integration::Session.any_instance.stubs(:https?).returns(true) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index d0fa383..f052bb6 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -735,15 +735,17 @@ class ArticleTest < Test::Unit::TestCase end should 'not get tagged with tag from other environment' do - a1 = Article.create!(:name => 'Published at', :profile => profile, :tag_list => 'bli') - e = Environment.create!(:name => 'other env') - p = create_user('other_user', :environment => e).person - a2 = Article.create!(:name => 'Published at', :profile => p, :tag_list => 'bli') - t = a2.tags[0] - as = e.articles.find_tagged_with(t) - - assert_includes as, a2 - assert_not_includes as, a1 + article_from_this_environment = create(Article, :profile => profile, :tag_list => 'bli') + + other_environment = fast_create(Environment) + user_from_other_environment = create_user('other_user', :environment => other_environment).person + article_from_other_enviroment = create(Article, :profile => user_from_other_environment, :tag_list => 'bli') + + tag = article_from_other_enviroment.tags.first + tagged_articles_in_other_environment = other_environment.articles.find_tagged_with(tag) + + assert_includes tagged_articles_in_other_environment, article_from_other_enviroment + assert_not_includes tagged_articles_in_other_environment, article_from_this_environment end should 'ignore category with zero as id' do diff --git a/test/unit/boxes_helper_test.rb b/test/unit/boxes_helper_test.rb index 4746de6..e585093 100644 --- a/test/unit/boxes_helper_test.rb +++ b/test/unit/boxes_helper_test.rb @@ -23,8 +23,15 @@ class BoxesHelperTest < Test::Unit::TestCase assert_tag_in_string display_boxes(holder, 'main content'), :tag => "div", :attributes => { :id => 'profile-footer' }, :content => 'my custom footer' end - should 'display invisible block for editing' do + def create_user_with_blocks p = create_user('test_user').person + LinkListBlock.create!(:box => p.boxes.first) + p + end + + should 'display invisible block for editing' do + p = create_user_with_blocks + b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0] b.visible = false; b.save! box = b.box @@ -37,7 +44,8 @@ class BoxesHelperTest < Test::Unit::TestCase end should 'not display invisible block' do - p = create_user('test_user').person + p = create_user_with_blocks + b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0] b.visible = false; b.save! box = b.box diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 5e45fc7..1e599e8 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -28,7 +28,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase task.valid? assert task.errors.invalid?(:requestor_id) - task.requestor = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + task.requestor = create_user('testuser').person task.valid? assert !task.errors.invalid?(:requestor_id) end @@ -98,7 +98,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator - person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + person = create_user('testuser').person task = CreateEnterprise.create!({ :name => 'My new enterprise', @@ -132,7 +132,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator - person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + person = create_user('testuser').person task = CreateEnterprise.create!({ :name => 'My new enterprise', @@ -175,7 +175,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator - person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + person = create_user('testuser').person task = CreateEnterprise.new({ :name => 'My new enterprise', :identifier => 'mynewenterprise', diff --git a/test/unit/environment_statistics_block_test.rb b/test/unit/environment_statistics_block_test.rb index ccf9644..82134e5 100644 --- a/test/unit/environment_statistics_block_test.rb +++ b/test/unit/environment_statistics_block_test.rb @@ -20,36 +20,35 @@ class EnvironmentStatisticsBlockTest < Test::Unit::TestCase end should 'generate statistics' do - env = Environment.create!(:name => "My test environment") + env = create(Environment) user1 = create_user('testuser1', :environment_id => env.id) user2 = create_user('testuser2', :environment_id => env.id) - env.enterprises.build(:identifier => 'mytestenterprise', :name => 'My test enterprise').save! - - env.communities.build(:identifier => 'mytestcommunity', :name => 'mytestcommunity').save! + fast_create(Enterprise, :environment_id => env.id) + fast_create(Community, :environment_id => env.id) block = EnvironmentStatisticsBlock.new env.boxes.first.blocks << block content = block.content - assert_match /One enterprise/, content - assert_match /2 users/, content - assert_match /One community/, content + assert_match(/One enterprise/, content) + assert_match(/2 users/, content) + assert_match(/One community/, content) end should 'generate statistics but not for private profiles' do - env = Environment.create!(:name => "My test environment") + env = create(Environment) user1 = create_user('testuser1', :environment_id => env.id) user2 = create_user('testuser2', :environment_id => env.id) user3 = create_user('testuser3', :environment_id => env.id) p = user3.person; p.public_profile = false; p.save! - env.enterprises.build(:identifier => 'mytestenterprise', :name => 'My test enterprise').save! - env.enterprises.build(:identifier => 'mytestenterprise2', :name => 'My test enterprise 2', :public_profile => false).save! + fast_create(Enterprise, :environment_id => env.id) + fast_create(Enterprise, :environment_id => env.id, :public_profile => false) - env.communities.build(:identifier => 'mytestcommunity', :name => 'mytestcommunity').save! - env.communities.build(:identifier => 'mytestcommunity2', :name => 'mytestcommunity 2', :public_profile => false).save! + fast_create(Community, :environment_id => env.id) + fast_create(Community, :environment_id => env.id, :public_profile => false) block = EnvironmentStatisticsBlock.new env.boxes.first.blocks << block diff --git a/test/unit/external_feed_test.rb b/test/unit/external_feed_test.rb index 2c9880d..b4fa25a 100644 --- a/test/unit/external_feed_test.rb +++ b/test/unit/external_feed_test.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' class ExternalFeedTest < ActiveSupport::TestCase should 'require blog' do - e = build(:external_feed, :blog => nil) + e = ExternalFeed.new e.valid? assert e.errors[:blog_id] e.blog = create_blog diff --git a/test/unit/feed_handler_test.rb b/test/unit/feed_handler_test.rb index 49a9e70..964f7e6 100644 --- a/test/unit/feed_handler_test.rb +++ b/test/unit/feed_handler_test.rb @@ -8,7 +8,7 @@ class FeedHandlerTest < Test::Unit::TestCase end attr_reader :handler def container - @container ||= fast_create(:feed_reader_block) + @container ||= create(:feed_reader_block) end should 'fetch feed content' do @@ -90,7 +90,7 @@ class FeedHandlerTest < Test::Unit::TestCase [:external_feed, :feed_reader_block].each do |container_class| should "reset the errors count after a successfull run (#{container_class})" do - container = fast_create(container_class, :update_errors => 1, :address => RAILS_ROOT + '/test/fixtures/files/feed.xml') + container = build(container_class, :update_errors => 1, :address => RAILS_ROOT + '/test/fixtures/files/feed.xml') handler.expects(:actually_process_container).with(container) handler.process(container) assert_equal 0, container.update_errors @@ -99,7 +99,7 @@ class FeedHandlerTest < Test::Unit::TestCase should "set error message and disable in case of errors (#{container_class})" do FeedHandler.stubs(:max_errors).returns(4) - container = fast_create(container_class) + container = create(container_class) handler.stubs(:actually_process_container).with(container).raises(Exception.new("crash")) # in the first 4 errors, we are ok diff --git a/test/unit/feed_reader_block_test.rb b/test/unit/feed_reader_block_test.rb index aace9d5..f554ccc 100644 --- a/test/unit/feed_reader_block_test.rb +++ b/test/unit/feed_reader_block_test.rb @@ -5,7 +5,7 @@ class FeedReaderBlockTest < ActiveSupport::TestCase include DatesHelper def setup - @feed = fast_create(:feed_reader_block) + @feed = create(:feed_reader_block) end attr_reader :feed diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 62a6b18..91e030b 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -163,14 +163,14 @@ class PersonTest < Test::Unit::TestCase end should 'get a default home page and a RSS feed' do - person = create_user('mytestuser').person + person = create_user_full('mytestuser').person assert_kind_of Article, person.home_page assert_kind_of RssFeed, person.articles.find_by_path('feed') end should 'create default set of blocks' do - p = create_user('testingblocks').person + p = create_user_full('testingblocks').person assert p.boxes[0].blocks.map(&:class).include?(MainBlock), 'person must have a MainBlock upon creation' diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index c48536d..bf99987 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -39,19 +39,19 @@ class ProfileTest < Test::Unit::TestCase end should 'be assigned to default environment if no environment is informed' do - assert_equal Environment.default, Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile').environment + assert_equal Environment.default, create(Profile).environment end should 'not override environment informed before creation' do - env = Environment.create!(:name => 'My test environment') - p = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment_id => env.id) + env = fast_create(Environment) + p = create(Profile, :environment_id => env.id) assert_equal env, p.environment end should 'be able to set environment after instantiation and before creating' do - env = Environment.create!(:name => 'My test environment') - p = Profile.new(:identifier => 'mytestprofile', :name => 'My test profile') + env = fast_create(Environment) + p = create(Profile) p.environment = env p.save! @@ -73,7 +73,7 @@ class ProfileTest < Test::Unit::TestCase end should 'provide access to home page' do - profile = Profile.create!(:identifier => 'newprofile', :name => 'New Profile') + profile = create(Profile) assert_kind_of Article, profile.home_page end @@ -87,7 +87,7 @@ class ProfileTest < Test::Unit::TestCase end def test_can_have_affiliated_people - pr = Profile.create(:name => 'composite_profile', :identifier => 'composite') + pr = fast_create(Profile) pe = create_user('aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person member_role = Role.new(:name => 'new_member_role') @@ -98,17 +98,17 @@ class ProfileTest < Test::Unit::TestCase end def test_find_by_contents - p = Profile.create(:name => 'wanted', :identifier => 'wanted') + p = create(Profile, :name => 'wanted') assert Profile.find_by_contents('wanted').include?(p) assert ! Profile.find_by_contents('not_wanted').include?(p) end should 'remove pages when removing profile' do - profile = Profile.create!(:name => 'testing profile', :identifier => 'testingprofile') - first = profile.articles.build(:name => 'first'); first.save! - second = profile.articles.build(:name => 'second'); second.save! - third = profile.articles.build(:name => 'third'); third.save! + profile = fast_create(Profile) + first = fast_create(Article, :profile_id => profile.id) + second = fast_create(Article, :profile_id => profile.id) + third = fast_create(Article, :profile_id => profile.id) total = Article.count mine = profile.articles.count @@ -133,19 +133,19 @@ class ProfileTest < Test::Unit::TestCase end should 'provide recent documents' do - profile = Profile.create!(:name => 'testing profile', :identifier => 'testingprofile') + profile = fast_create(Profile) profile.articles.destroy_all - first = profile.articles.build(:name => 'first'); first.save! - second = profile.articles.build(:name => 'second'); second.save! - third = profile.articles.build(:name => 'third'); third.save! + first = fast_create(Article, { :profile_id => profile.id }, :timestamps => true) + second = fast_create(Article, { :profile_id => profile.id }, :timestamps => true) + third = fast_create(Article, { :profile_id => profile.id }, :timestamps => true) assert_equal [third, second], profile.recent_documents(2) assert_equal [third, second, first], profile.recent_documents end should 'affiliate and provide a list of the affiliated users' do - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting') + profile = fast_create(Profile) person = create_user('test_user').person role = Role.create!(:name => 'just_another_test_role') assert profile.affiliate(person, role) @@ -153,8 +153,8 @@ class ProfileTest < Test::Unit::TestCase end should 'authorize users that have permission on the environment' do - env = Environment.create!(:name => 'test_env') - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + env = fast_create(Environment) + profile = fast_create(Profile, :environment_id => env.id) person = create_user('test_user').person role = Role.create!(:name => 'just_another_test_role', :permissions => ['edit_profile']) assert env.affiliate(person, role) @@ -162,29 +162,24 @@ class ProfileTest < Test::Unit::TestCase end should 'have articles' do - env = Environment.create!(:name => 'test_env') - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + profile = build(Profile) assert_raise ActiveRecord::AssociationTypeMismatch do profile.articles << 1 end assert_nothing_raised do - profile.articles << Article.new(:name => 'testing article') + profile.articles << build(Article) end end should 'list top-level articles' do - env = Environment.create!(:name => 'test_env') - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + profile = fast_create(Profile) - p1 = profile.articles.build(:name => 'parent1') - p1.save! - p2 = profile.articles.build(:name => 'parent2') - p2.save! + p1 = create(Article, :profile_id => profile.id) + p2 = create(Article, :profile_id => profile.id) - child = profile.articles.build(:name => 'parent2', :parent_id => p1.id) - child.save! + child = create(Article, :profile_id => profile.id, :parent_id => p1.id) top = profile.top_level_articles assert top.include?(p1) @@ -193,8 +188,7 @@ class ProfileTest < Test::Unit::TestCase end should 'be able to optionally reload the list of top level articles' do - env = Environment.create!(:name => 'test_env') - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + profile = fast_create(Profile) list = profile.top_level_articles same_list = profile.top_level_articles @@ -205,8 +199,8 @@ class ProfileTest < Test::Unit::TestCase end should 'be able to find profiles by their names with ferret' do - small = Profile.create!(:name => 'A small profile for testing ', :identifier => 'smallprofilefortesting') - big = Profile.create!(:name => 'A big profile for testing', :identifier => 'bigprofilefortesting') + small = create(Profile, :name => 'A small profile for testing') + big = create(Profile, :name => 'A big profile for testing') assert Profile.find_by_contents('small').include?(small) assert Profile.find_by_contents('big').include?(big) @@ -217,13 +211,12 @@ class ProfileTest < Test::Unit::TestCase end should 'provide a shortcut for picking a profile by its identifier' do - profile = Profile.create!(:name => 'bla', :identifier => 'testprofile') + profile = fast_create(Profile, :identifier => 'testprofile') assert_equal profile, Profile['testprofile'] end should 'have boxes upon creation' do - profile = Profile.create!(:name => 'test profile', :identifier => 'testprofile') - + profile = create(Profile) assert profile.boxes.size > 0 end @@ -247,66 +240,71 @@ class ProfileTest < Test::Unit::TestCase end should 'provide url to itself' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + environment = create_environment('mycolivre.net') + profile = build(Profile, :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) assert_equal({ :host => 'mycolivre.net', :profile => 'testprofile', :controller => 'content_viewer', :action => 'view_page', :page => []}, profile.url) end should 'provide URL to admin area' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + environment = create_environment('mycolivre.net') + profile = build(Profile, :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + assert_equal({ :profile => 'testprofile', :controller => 'profile_editor', :action => 'index'}, profile.admin_url) end should 'provide URL to public profile' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + environment = create_environment('mycolivre.net') + profile = build(Profile, :identifier => 'testprofile', :environment_id => environment.id) + assert_equal({ :host => 'mycolivre.net', :profile => 'testprofile', :controller => 'profile', :action => 'index' }, profile.public_profile_url) end should "use own domain name instead of environment's for home page url" do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + profile = build(Profile, :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) profile.domains << Domain.new(:name => 'micojones.net') + assert_equal({:host => 'micojones.net', :profile => nil, :controller => 'content_viewer', :action => 'view_page', :page => []}, profile.url) end should 'help developers by adding a suitable port to url' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + profile = build(Profile) Noosfero.expects(:url_options).returns({ :port => 9999 }) - ok('Profile#url_options must include port option when running in development mode') { profile.url[:port] == 9999 } + assert profile.url[:port] == 9999, 'Profile#url_options must include port option when running in development mode' end should 'help developers by adding a suitable port to url options for own domain urls' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - profile.domains << Domain.new(:name => 'micojones.net') + environment = create_environment('mycolivre.net') + profile = build(Profile, :environment_id => environment.id) + profile.domains << build(Domain) Noosfero.expects(:url_options).returns({ :port => 9999 }) - ok('Profile#url must include port options when running in developers mode') { profile.url[:port] == 9999 } + assert profile.url[:port] == 9999, 'Profile#url must include port options when running in developers mode' end should 'list article tags for profile' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') - profile.articles.build(:name => 'first', :tag_list => 'first-tag').save! - profile.articles.build(:name => 'second', :tag_list => 'first-tag, second-tag').save! - profile.articles.build(:name => 'third', :tag_list => 'first-tag, second-tag, third-tag').save! + profile = fast_create(Profile) + create(Article, :profile => profile, :tag_list => 'first-tag') + create(Article, :profile => profile, :tag_list => 'first-tag, second-tag') + create(Article, :profile => profile, :tag_list => 'first-tag, second-tag, third-tag') assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, profile.article_tags) - end should 'list tags for profile' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :tag_list => 'first-tag, second-tag') + profile = create(Profile, :tag_list => 'first-tag, second-tag') assert_equal(['first-tag', 'second-tag'], profile.tags.map(&:name)) end should 'find content tagged with given tag' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') - first = profile.articles.build(:name => 'first', :tag_list => 'first-tag'); first.save! - second = profile.articles.build(:name => 'second', :tag_list => 'first-tag, second-tag'); second.save! - third = profile.articles.build(:name => 'third', :tag_list => 'first-tag, second-tag, third-tag'); third.save! - profile.reload + profile = fast_create(Profile) + first = create(Article, :profile => profile, :tag_list => 'first-tag') + second = create(Article, :profile => profile, :tag_list => 'first-tag, second-tag') + third = create(Article, :profile => profile, :tag_list => 'first-tag, second-tag, third-tag') assert_equivalent [ first, second, third], profile.find_tagged_with('first-tag') assert_equivalent [ second, third ], profile.find_tagged_with('second-tag') @@ -337,14 +335,14 @@ class ProfileTest < Test::Unit::TestCase end should 'create a homepage and a feed on creation' do - profile = Organization.create!(:name => 'my test profile', :identifier => 'mytestprofile') + profile = create(Profile) assert_kind_of Article, profile.home_page assert_kind_of RssFeed, profile.articles.find_by_path('feed') end should 'not allow to add members' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) p = create_user('mytestuser').person assert_raise RuntimeError do c.add_member(p) @@ -352,7 +350,7 @@ class ProfileTest < Test::Unit::TestCase end should 'allow to add administrators' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) p = create_user('mytestuser').person c.add_admin(p) @@ -361,7 +359,7 @@ class ProfileTest < Test::Unit::TestCase end should 'not allow to add moderators' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) p = create_user('mytestuser').person assert_raise RuntimeError do c.add_moderator(p) @@ -369,7 +367,7 @@ class ProfileTest < Test::Unit::TestCase end should 'have tasks' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) t1 = c.tasks.build t1.save! @@ -380,7 +378,7 @@ class ProfileTest < Test::Unit::TestCase end should 'have pending tasks' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) t1 = c.tasks.build; t1.save! t2 = c.tasks.build; t2.save!; t2.finish t3 = c.tasks.build; t3.save! @@ -389,7 +387,7 @@ class ProfileTest < Test::Unit::TestCase end should 'have finished tasks' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) t1 = c.tasks.build; t1.save! t2 = c.tasks.build; t2.save!; t2.finish t3 = c.tasks.build; t3.save!; t3.finish @@ -398,12 +396,12 @@ class ProfileTest < Test::Unit::TestCase end should 'responds to categories' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) assert_respond_to c, :categories end should 'have categories' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) cat = Environment.default.categories.build(:name => 'a category'); cat.save! c.add_category cat c.save! @@ -431,13 +429,13 @@ class ProfileTest < Test::Unit::TestCase end should 'advertise false to homepage and feed on creation' do - profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + profile = create(Profile) assert !profile.home_page.advertise? assert !profile.articles.find_by_path('feed').advertise? end should 'advertise true to homepage after update' do - profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + profile = create(Profile) assert !profile.home_page.advertise? profile.home_page.name = 'Changed name' assert profile.home_page.save! @@ -445,7 +443,7 @@ class ProfileTest < Test::Unit::TestCase end should 'advertise true to feed after update' do - profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + profile = create(Profile) assert !profile.articles.find_by_path('feed').advertise? profile.articles.find_by_path('feed').name = 'Changed name' assert profile.articles.find_by_path('feed').save! @@ -453,15 +451,13 @@ class ProfileTest < Test::Unit::TestCase end should 'have latitude and longitude' do - e = Enterprise.create!(:name => 'test1', :identifier => 'test1') - e.lat, e.lng = 45, 45 ; e.save! + e = fast_create(Enterprise, :lat => 45, :lng => 45) assert_includes Enterprise.find_within(2, :origin => [45, 45]), e end should 'have latitude and longitude and find' do - e = Enterprise.create!(:name => 'test1', :identifier => 'test1') - e.lat, e.lng = 45, 45 ; e.save! + e = fast_create(Enterprise, :lat => 45, :lng => 45) assert_includes Enterprise.find(:all, :within => 2, :origin => [45, 45]), e end @@ -477,8 +473,8 @@ class ProfileTest < Test::Unit::TestCase end should 'be able to find the public profiles but not private ones' do - p1 = Profile.create!(:name => 'test1', :identifier => 'test1', :public_profile => true) - p2 = Profile.create!(:name => 'test2', :identifier => 'test2', :public_profile => false) + p1 = create(Profile, :public_profile => true) + p2 = create(Profile, :public_profile => false) result = Profile.find(:all, :conditions => {:public_profile => true}) assert_includes result, p1 @@ -548,24 +544,24 @@ class ProfileTest < Test::Unit::TestCase should 'index profile identifier for searching' do Profile.destroy_all - p = Profile.create!(:identifier => 'lalala', :name => 'Interesting Profile') + p = create(Profile, :identifier => 'lalala') assert_includes Profile.find_by_contents('lalala'), p end should 'index profile name for searching' do - p = Profile.create!(:identifier => 'testprofile', :name => 'Interesting Profile') + p = create(Profile, :name => 'Interesting Profile') assert_includes Profile.find_by_contents('interesting'), p end should 'enabled by default on creation' do - profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + profile = fast_create(Profile) assert profile.enabled? end should 'categorize in the entire category hierarchy' do - c1 = Category.create!(:environment => Environment.default, :name => 'c1') - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2') - c3 = c2.children.create!(:environment => Environment.default, :name => 'c3') + c1 = fast_create(Category) + c2 = fast_create(Category, :parent_id => c1.id) + c3 = fast_create(Category, :parent_id => c2.id) profile = create_user('testuser').person profile.add_category(c3) @@ -580,11 +576,11 @@ class ProfileTest < Test::Unit::TestCase end should 'redefine the entire category set at once' do - c1 = Category.create!(:environment => Environment.default, :name => 'c1') - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2') - c3 = c2.children.create!(:environment => Environment.default, :name => 'c3') - c4 = c1.children.create!(:environment => Environment.default, :name => 'c4') - profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c1 = fast_create(Category) + c2 = fast_create(Category, :parent_id => c1.id) + c3 = fast_create(Category, :parent_id => c2.id) + c4 = fast_create(Category, :parent_id => c1.id) + profile = fast_create(Profile) profile.add_category(c4) @@ -594,33 +590,33 @@ class ProfileTest < Test::Unit::TestCase end should 'be able to create an profile already with categories' do - c1 = Category.create!(:environment => Environment.default, :name => 'c1') - c2 = Category.create!(:environment => Environment.default, :name => 'c2') + c1 = create(Category) + c2 = create(Category) - profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile', :category_ids => [c1.id, c2.id]) + profile = create(Profile, :category_ids => [c1.id, c2.id]) assert_equivalent [c1, c2], profile.categories(true) end should 'be associated with a region' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + region = fast_create(Region) + profile = fast_create(Profile, :region_id => region.id) assert_equal region, profile.region end should 'categorized automatically in its region' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + region = fast_create(Region) + profile = create(Profile, :region => region) assert_equal [region], profile.categories(true) end should 'change categorization when changing region' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + region = fast_create(Region) + region2 = fast_create(Region) - region2 = Region.create!(:name => "Feira de Santana", :environment => Environment.default) + profile = fast_create(Profile, :region_id => region.id) profile.region = region2 profile.save! @@ -629,8 +625,8 @@ class ProfileTest < Test::Unit::TestCase end should 'remove categorization when removing region' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + region = fast_create(Region) + profile = fast_create(Profile, :region_id => region.id) profile.region = nil profile.save! @@ -639,8 +635,8 @@ class ProfileTest < Test::Unit::TestCase end should 'not remove region, only dissasociate from it' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + region = fast_create(Region) + profile = fast_create(Profile, :region_id => region.id) profile.region = nil profile.save! @@ -651,19 +647,18 @@ class ProfileTest < Test::Unit::TestCase end should 'be able to create with categories and region at the same time' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - category = Category.create!(:name => 'test category', :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id]) + region = fast_create(Region) + category = fast_create(Category) + profile = create(Profile, :region => region, :category_ids => [category.id]) assert_equivalent [region, category], profile.categories(true) end should 'be able to update categories and not get regions removed' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - category = Category.create!(:name => 'test category', :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id]) - - category2 = Category.create!(:name => 'test category 2', :environment => Environment.default) + region = fast_create(Region) + category = fast_create(Category) + category2 = fast_create(Category) + profile = create(Profile, :region => region, :category_ids => [category.id]) profile.update_attributes!(:category_ids => [category2.id]) @@ -671,11 +666,10 @@ class ProfileTest < Test::Unit::TestCase end should 'be able to update region and not get categories removed' do - region = Region.create!(:name => "Salvador", :environment => Environment.default) - category = Category.create!(:name => 'test category', :environment => Environment.default) - profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id]) - - region2 = Region.create!(:name => "Aracaju", :environment => Environment.default) + region = fast_create(Region) + region2 = fast_create(Region) + category = fast_create(Category) + profile = create(Profile, :region => region, :category_ids => [category.id]) profile.update_attributes!(:region => region2) @@ -691,16 +685,16 @@ class ProfileTest < Test::Unit::TestCase end should 'query region for location' do - region = Region.new(:name => 'Some ackwrad region name') - p = Profile.new(:region => region) - assert_equal 'Some ackwrad region name', p.location + region = build(Region, :name => 'Some ackward region name') + p = build(Profile, :region => region) + assert_equal 'Some ackward region name', p.location end should 'query region hierarchy for location up to 2 levels' do - country = Region.new(:name => "Brazil") - state = Region.new(:name => "Bahia", :parent => country) - city = Region.new(:name => "Salvador", :parent => state) - p = Profile.new(:region => city) + country = build(Region, :name => "Brazil") + state = build(Region, :name => "Bahia", :parent => country) + city = build(Region, :name => "Salvador", :parent => state) + p = build(Profile, :region => city) assert_equal 'Salvador - Bahia', p.location end @@ -735,21 +729,21 @@ class ProfileTest < Test::Unit::TestCase end should 'default home page is a TinyMceArticle' do - profile = Profile.create!(:identifier => 'newprofile', :name => 'New Profile') + profile = create(Profile) assert_kind_of TinyMceArticle, profile.home_page end should 'not add a category twice to profile' do - c1 = Category.create!(:environment => Environment.default, :name => 'c1') - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2') - c3 = c1.children.create!(:environment => Environment.default, :name => 'c3') - profile = create_user('testuser').person + c1 = fast_create(Category) + c2 = fast_create(Category, :parent_id => c1.id) + c3 = fast_create(Category, :parent_id => c1.id) + profile = fast_create(Profile) profile.category_ids = [c2,c3,c3].map(&:id) assert_equal [c2, c3], profile.categories(true) end should 'not return nil members when a member is removed from system' do - p = Community.create!(:name => 'test community', :identifier => 'test_comm') + p = fast_create(Community) member = create_user('test_user').person p.add_member(member) @@ -880,16 +874,16 @@ class ProfileTest < Test::Unit::TestCase end should 'respond to public? as public_profile' do - p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1') - p2 = Profile.create!(:name => 'test profile 2', :identifier => 'test_profile2', :public_profile => false) + p1 = fast_create(Profile) + p2 = fast_create(Profile, :public_profile => false) assert p1.public? assert !p2.public? end should 'create a initial private folder when a public profile is created' do - p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1') - p2 = Profile.create!(:name => 'test profile 2', :identifier => 'test_profile2', :public_profile => false) + p1 = create(Profile) + p2 = create(Profile, :public_profile => false) assert p1.articles.find(:first, :conditions => {:public_article => false}) assert !p2.articles.find(:first, :conditions => {:public_article => false}) @@ -897,7 +891,7 @@ class ProfileTest < Test::Unit::TestCase should 'remove member with many roles' do person = create_user('test_user').person - community = Community.create!(:name => 'Boca do Siri', :identifier => 'boca_do_siri') + community = fast_create(Community) community.affiliate(person, Profile::Roles.all_roles(community.environment.id)) community.remove_member(person) @@ -908,12 +902,12 @@ class ProfileTest < Test::Unit::TestCase should 'copy set of articles from a template' do template = create_user('test_template').person template.articles.destroy_all - a1 = template.articles.create(:name => 'some xyz article') - a2 = template.articles.create(:name => 'some child article', :parent => a1) + a1 = fast_create(Article, :profile_id => template.id, :name => 'some xyz article') + a2 = fast_create(Article, :profile_id => template.id, :name => 'some child article', :parent_id => a1.id) Profile.any_instance.stubs(:template).returns(template) - p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile') + p = create(Profile) assert_equal 1, p.top_level_articles.size top_art = p.top_level_articles[0] @@ -926,14 +920,13 @@ class ProfileTest < Test::Unit::TestCase should 'copy homepage from template' do template = create_user('test_template').person template.articles.destroy_all - a1 = template.articles.create(:name => 'some xyz article') + a1 = fast_create(Article, :profile_id => template.id, :name => 'some xyz article') template.home_page = a1 template.save! Profile.any_instance.stubs(:template).returns(template) - p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile') - p.reload + p = create(Profile) assert_not_nil p.home_page assert_equal 'some xyz article', p.home_page.name @@ -942,12 +935,11 @@ class ProfileTest < Test::Unit::TestCase should 'not advertise the articles copied from templates' do template = create_user('test_template').person template.articles.destroy_all - a = template.articles.create(:name => 'some xyz article') + a = fast_create(Article, :profile_id => template.id, :name => 'some xyz article') Profile.any_instance.stubs(:template).returns(template) - p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile') - p.reload + p = create(Profile) a_copy = p.articles[0] @@ -955,7 +947,7 @@ class ProfileTest < Test::Unit::TestCase end should 'copy set of boxes from profile template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.boxes.destroy_all template.boxes << Box.new template.boxes[0].blocks << Block.new @@ -963,18 +955,18 @@ class ProfileTest < Test::Unit::TestCase Profile.any_instance.stubs(:template).returns(template) - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + p = create(Profile) assert_equal 1, p.boxes.size assert_equal 1, p.boxes[0].blocks.size end should 'copy layout template when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.layout_template = 'leftbar' template.save! - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + p = create(Profile) p.apply_template(template) @@ -982,7 +974,7 @@ class ProfileTest < Test::Unit::TestCase end should 'copy blocks when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.boxes.destroy_all template.boxes << Box.new template.boxes[0].blocks << Block.new @@ -997,7 +989,7 @@ class ProfileTest < Test::Unit::TestCase end should 'copy articles when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.articles.create(:name => 'template article') template.save! @@ -1009,14 +1001,14 @@ class ProfileTest < Test::Unit::TestCase end should 'rename existing articles when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.boxes.destroy_all template.boxes << Box.new template.boxes[0].blocks << Block.new template.articles.create(:name => 'some article') template.save! - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + p = create(Profile) p.articles.create(:name => 'some article') p.apply_template(template) @@ -1026,11 +1018,11 @@ class ProfileTest < Test::Unit::TestCase end should 'copy header when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template[:custom_header] = '{name}' template.save! - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + p = create(Profile, :name => 'test prof') p.apply_template(template) @@ -1040,11 +1032,9 @@ class ProfileTest < Test::Unit::TestCase end should 'copy footer when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template', :address => 'Template address') - template[:custom_footer] = '{address}' - template.save! + template = create(Profile, :address => 'Template address', :custom_footer => '{address}') - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof', :address => 'Profile address') + p = create(Profile, :address => 'Profile address') p.apply_template(template) assert_equal '{address}', p[:custom_footer] @@ -1053,10 +1043,9 @@ class ProfileTest < Test::Unit::TestCase end should 'ignore failing validation when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template', :address => 'Template address', :layout_template => 'leftbar', :custom_footer => 'my custom footer', :custom_header => 'my custom header') - template.save! + template = create(Profile, :layout_template => 'leftbar', :custom_footer => 'my custom footer', :custom_header => 'my custom header') - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof', :address => 'Profile address') + p = create(Profile) def p.validate self.errors.add('identifier', 'is invalid') end @@ -1070,26 +1059,26 @@ class ProfileTest < Test::Unit::TestCase end should 'copy homepage when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template', :address => 'Template address') - template.articles.destroy_all - a1 = template.articles.create(:name => 'some xyz article') + template = fast_create(Profile) + a1 = fast_create(Article, :profile_id => template.id, :name => 'some xyz article') template.home_page = a1 template.save! - p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile') + p = fast_create(Profile) p.apply_template(template) assert_not_nil p.home_page - assert_equal 'some xyz article', Profile['test_profile'].home_page.name + assert_equal 'some xyz article', p.home_page.name end should 'not copy blocks default_title when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.boxes.destroy_all template.boxes << Box.new b = Block.new() template.boxes[0].blocks << b - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + + p = create(Profile) assert b[:title].blank? p.copy_blocks_from(template) @@ -1098,12 +1087,13 @@ class ProfileTest < Test::Unit::TestCase end should 'copy blocks title when applying template' do - template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template = fast_create(Profile) template.boxes.destroy_all template.boxes << Box.new b = Block.new(:title => 'default title') template.boxes[0].blocks << b - p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + + p = create(Profile) assert !b[:title].blank? p.copy_blocks_from(template) @@ -1116,7 +1106,7 @@ class ProfileTest < Test::Unit::TestCase Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR) begin - p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1') + p1 = fast_create(Profile) t = Theme.new('test_theme'); t.owner = p1; t.save assert_equal [t], p1.themes @@ -1129,7 +1119,7 @@ class ProfileTest < Test::Unit::TestCase Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR) begin - p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1') + p1 = fast_create(Profile) t = Theme.new('test_theme'); t.owner = p1; t.save assert_equal t, p1.find_theme('test_theme') @@ -1139,12 +1129,12 @@ class ProfileTest < Test::Unit::TestCase end should 'have a layout template' do - p = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => Environment.default) + p = Profile.new assert_equal 'default', p.layout_template end should 'get boxes limit from template' do - p = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => Environment.default) + p = create(Profile) layout = mock layout.expects(:number_of_boxes).returns(6) @@ -1165,11 +1155,11 @@ class ProfileTest < Test::Unit::TestCase end should 'not be possible to have different profiles with the same identifier in the same environment' do - env = Environment.create!(:name => 'My test environment') + env = fast_create(Environment) - p1 = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => env) + p1 = fast_create(Profile, :identifier => 'mytestprofile', :environment_id => env.id) - p2 = Profile.new(:identifier => 'mytestprofile', :name => 'My test profile', :environment => env) + p2 = Profile.new(:identifier => 'mytestprofile', :environment => env) assert !p2.valid? assert p2.errors.on(:identifier) @@ -1177,32 +1167,32 @@ class ProfileTest < Test::Unit::TestCase end should 'be possible to have different profiles with the same identifier in different environments' do - p1 = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile') + p1 = fast_create(Profile, :identifier => 'mytestprofile') - env = Environment.create!(:name => 'My test environment') - p2 = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => env) + env = fast_create(Environment) + p2 = create(Profile, :identifier => 'mytestprofile', :environment => env) assert_not_equal p1.environment, p2.environment end should 'has blog' do - p = create_user('testuser').person + p = fast_create(Profile) p.articles << Blog.new(:profile => p, :name => 'blog_feed_test') assert p.has_blog? end should 'not has blog' do - p = create_user('testuser').person + p = fast_create(Profile) assert !p.has_blog? end should 'get nil when no blog' do - p = create_user('testuser').person + p = fast_create(Profile) assert_nil p.blog end should 'list admins' do - c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + c = fast_create(Profile) p = create_user('mytestuser').person c.add_admin(p) @@ -1255,7 +1245,7 @@ class ProfileTest < Test::Unit::TestCase should 'find task from all environment if is admin' do env = Environment.default - another = Environment.create!(:name => 'another_env') + another = fast_create(Environment) person = Person['ze'] task1 = Task.create!(:requestor => person, :target => env) task2 = Task.create!(:requestor => person, :target => another) @@ -1269,15 +1259,15 @@ class ProfileTest < Test::Unit::TestCase end should 'find task by id on all environments' do - env = Environment.create!(:name => 'other_env') - another = Environment.create!(:name => 'another_env') - person = Person['ze'] + other = fast_create(Environment) + another = fast_create(Environment) + person = Person['ze'] - task1 = Task.create!(:requestor => person, :target => env) + task1 = Task.create!(:requestor => person, :target => other) task2 = Task.create!(:requestor => person, :target => another) - person.stubs(:is_admin?).with(env).returns(true) - Environment.find(:all).select{|i| i.name != 'other_env'}.each do |env| + person.stubs(:is_admin?).with(other).returns(true) + Environment.find(:all).select{|i| i != other }.each do |env| person.stubs(:is_admin?).with(env).returns(false) end @@ -1294,18 +1284,18 @@ class ProfileTest < Test::Unit::TestCase end should 'use its first domain hostname name if available' do - profile = create_user('testuser').person + profile = fast_create(Profile) profile.domains << Domain.new(:name => 'myowndomain.net') assert_equal 'myowndomain.net', profile.default_hostname end should 'have a preferred domain name' do - person = create_user('testuser').person - domain = Domain.create!(:name => 'myowndomain.net', :owner => person) - person.preferred_domain = domain - person.save! + profile = fast_create(Profile) + domain = create(Domain, :owner => profile) + profile.preferred_domain = domain + profile.save! - assert_equal domain, Person.find(person.id).preferred_domain(true) + assert_equal domain, Profile.find(profile.id).preferred_domain(true) end should 'use preferred domain for hostname' do @@ -1316,16 +1306,16 @@ class ProfileTest < Test::Unit::TestCase end should 'provide a list of possible preferred domain names' do - profile = create_user('testuser').person - domain1 = Domain.create!(:name => 'envdomain.net', :owner => profile.environment) - domain2 = Domain.create!(:name => 'profiledomain.net', :owner => profile) + profile = fast_create(Profile) + domain1 = create(Domain, :owner => profile.environment) + domain2 = create(Domain, :owner => profile) assert_includes profile.possible_domains, domain1 assert_includes profile.possible_domains, domain2 end should 'list folder articles' do - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting') + profile = fast_create(Profile) Article.destroy_all p1 = Folder.create!(:name => 'parent1', :profile => profile) p2 = Blog.create!(:name => 'parent2', :profile => profile) @@ -1340,13 +1330,13 @@ class ProfileTest < Test::Unit::TestCase end should 'validates profile image when save' do - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) + profile = build(Profile, :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) profile.image.expects(:valid?).returns(false).at_least_once assert !profile.valid? end should 'profile is invalid when image not valid' do - profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) + profile = build(Profile, :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) profile.image.expects(:valid?).returns(false).at_least_once profile.image.errors.add(:size, "fake error") assert !profile.valid? @@ -1365,36 +1355,36 @@ class ProfileTest < Test::Unit::TestCase end should 'copy header and footer after create a person' do - template = create_user('template').person + template = fast_create(Profile) template.custom_footer = "footer customized" template.custom_header = "header customized" Environment.any_instance.stubs(:person_template).returns(template) - person = create_user('mytestuser').person + person = create_user_full('mytestuser').person assert_equal "footer customized", person.custom_footer assert_equal "header customized", person.custom_header end should 'provide URL to leave' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + profile = build(Profile, :identifier => 'testprofile') assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'leave'}, profile.leave_url) end should 'provide URL to join' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + profile = build(Profile, :identifier => 'testprofile') assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'join'}, profile.join_url) end - should 'ignore categgory with id zero' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - c = Category.create!(:name => 'test cat', :environment => profile.environment) + should 'ignore category with id zero' do + profile = fast_create(Profile) + c = fast_create(Category) profile.category_ids = ['0', c.id, nil] assert_equal [c], profile.categories end should 'get first blog when has multiple blogs' do - p = create_user('testuser').person + p = fast_create(Profile) p.blogs << Blog.new(:profile => p, :name => 'Blog one') p.blogs << Blog.new(:profile => p, :name => 'Blog two') p.blogs << Blog.new(:profile => p, :name => 'Blog three') @@ -1403,7 +1393,7 @@ class ProfileTest < Test::Unit::TestCase end should 'list all events' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') + profile = fast_create(Profile) event1 = Event.new(:name => 'Ze Birthday', :start_date => Date.today) event2 = Event.new(:name => 'Mane Birthday', :start_date => Date.today >> 1) profile.events << [event1, event2] @@ -1412,7 +1402,7 @@ class ProfileTest < Test::Unit::TestCase end should 'list events by day' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') + profile = fast_create(Profile) today = Date.today yesterday_event = Event.new(:name => 'Joao Birthday', :start_date => today - 1.day) @@ -1425,7 +1415,7 @@ class ProfileTest < Test::Unit::TestCase end should 'list events in a range' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') + profile = fast_create(Profile) today = Date.today event_in_range = Event.new(:name => 'Noosfero Conference', :start_date => today - 2.day, :end_date => today + 2.day) @@ -1439,7 +1429,7 @@ class ProfileTest < Test::Unit::TestCase end should 'not list events out of range' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') + profile = fast_create(Profile) today = Date.today event_in_range1 = Event.new(:name => 'Foswiki Conference', :start_date => today - 2.day, :end_date => today + 2.day) @@ -1454,7 +1444,7 @@ class ProfileTest < Test::Unit::TestCase end should 'sort events by name' do - profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile') + profile = fast_create(Profile) event1 = Event.new(:name => 'Noosfero Hackaton', :start_date => Date.today) event2 = Event.new(:name => 'Debian Day', :start_date => Date.today) event3 = Event.new(:name => 'Fisl 10', :start_date => Date.today) @@ -1463,15 +1453,14 @@ class ProfileTest < Test::Unit::TestCase end should 'be available if identifier doesnt exist on environment' do - p = create_user('identifier-test').person - - env = Environment.create(:name => 'Environment test') + p = fast_create(Profile, :identifier => 'identifier-test') + env = fast_create(Environment) assert_equal true, Profile.is_available?('identifier-test', env) end should 'not be available if identifier exists on environment' do p = create_user('identifier-test').person - + p = fast_create(Profile, :identifier => 'identifier-test') assert_equal false, Profile.is_available?('identifier-test', Environment.default) end -- libgit2 0.21.2