From 7e00c87cbb6a87241fce4b75cba74c3bd9675882 Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Tue, 7 Jul 2015 18:16:52 -0300 Subject: [PATCH] rails4: fix integration tests --- lib/authenticated_test_helper.rb | 98 -------------------------------------------------------------------------------------------------- plugins/sniffer/test/integration/sniffer_map_test.rb | 2 +- test/action_tracker_test_helper.rb | 16 ---------------- test/factories.rb | 496 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- test/functional/account_controller_test.rb | 4 +--- test/integration/approve_reject_enterprise_test.rb | 9 --------- test/integration/assets_menu_test.rb | 8 ++++---- test/integration/assigning_validator_organizations_to_regions_test.rb | 4 ++-- test/integration/blocks_test.rb | 3 ++- test/integration/controller_naming_test.rb | 2 +- test/integration/editing_person_info_test.rb | 2 +- test/integration/enable_disable_features_test.rb | 3 ++- test/integration/enterprise_registration_test.rb | 6 +++--- test/integration/forgot_password_test.rb | 4 ++-- test/integration/http_caching_test.rb | 2 +- test/integration/login_to_the_application_test.rb | 3 ++- test/integration/manage_documents_test.rb | 8 ++++---- test/integration/manage_friendships_test.rb | 2 +- test/integration/online_doc_test.rb | 2 +- test/integration/performance_test.rb | 2 +- test/integration/routing_test.rb | 2 +- test/integration/search_popup_test.rb | 2 +- test/integration/signup_test.rb | 5 +++-- test/integration/user_registers_at_the_application_test.rb | 6 +++--- test/integration/varnish_conf_test.rb | 4 ++-- test/noosfero_doc_test.rb | 50 -------------------------------------------------- test/support/action_tracker_test_helper.rb | 16 ++++++++++++++++ test/support/authenticated_test_helper.rb | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/support/controller_test_case.rb | 16 ++++++++++++++++ test/support/factories.rb | 496 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/support/integration_test.rb | 23 +++++++++++++++++++++++ test/support/noosfero_doc_test.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/support/noosfero_test_helper.rb | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/support/should.rb | 27 +++++++++++++++++++++++++++ test/test_helper.rb | 138 ++++++++++-------------------------------------------------------------------------------------------------------------------------------- 35 files changed, 846 insertions(+), 834 deletions(-) delete mode 100644 lib/authenticated_test_helper.rb delete mode 100644 test/action_tracker_test_helper.rb delete mode 100644 test/factories.rb delete mode 100644 test/integration/approve_reject_enterprise_test.rb delete mode 100644 test/noosfero_doc_test.rb create mode 100644 test/support/action_tracker_test_helper.rb create mode 100644 test/support/authenticated_test_helper.rb create mode 100644 test/support/controller_test_case.rb create mode 100644 test/support/factories.rb create mode 100644 test/support/integration_test.rb create mode 100644 test/support/noosfero_doc_test.rb create mode 100644 test/support/noosfero_test_helper.rb create mode 100644 test/support/should.rb diff --git a/lib/authenticated_test_helper.rb b/lib/authenticated_test_helper.rb deleted file mode 100644 index a269b19..0000000 --- a/lib/authenticated_test_helper.rb +++ /dev/null @@ -1,98 +0,0 @@ -module AuthenticatedTestHelper - # Sets the current user in the session from the user fixtures. - def login_as(user) - @request.session[:user] = User.find_by_login(user.to_s).id - end - - def logout - @request.session.delete(:user) - end - - def content_type(type) - @request.env['Content-Type'] = type - end - - def accept(accept) - @request.env["HTTP_ACCEPT"] = accept - end - - def authorize_as(user) - if user - @request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{users(user).login}:test")}" - accept 'application/xml' - content_type 'application/xml' - else - @request.env["HTTP_AUTHORIZATION"] = nil - accept nil - content_type nil - end - end - - # Assert the block redirects to the login - # - # assert_requires_login(:bob) { |c| c.get :edit, :id => 1 } - # - def assert_requires_login(login = nil) - yield HttpLoginProxy.new(self, login) - end - - def assert_http_authentication_required(login = nil) - yield XmlLoginProxy.new(self, login) - end - - def reset!(*instance_vars) - instance_vars = [:controller, :request, :response] unless instance_vars.any? - instance_vars.collect! { |v| "@#{v}".to_sym } - instance_vars.each do |var| - instance_variable_set(var, instance_variable_get(var).class.new) - end - end -end - -class BaseLoginProxy - attr_reader :controller - attr_reader :options - def initialize(controller, login) - @controller = controller - @login = login - end - - private - def authenticated - raise NotImplementedError - end - - def check - raise NotImplementedError - end - - def method_missing(method, *args) - @controller.reset! - authenticate - @controller.send(method, *args) - check - end -end - -class HttpLoginProxy < BaseLoginProxy - protected - def authenticate - @controller.login_as @login if @login - end - - def check - @controller.assert_redirected_to :controller => 'account', :action => 'login' - end -end - -class XmlLoginProxy < BaseLoginProxy - protected - def authenticate - @controller.accept 'application/xml' - @controller.authorize_as @login if @login - end - - def check - @controller.assert_response 401 - end -end diff --git a/plugins/sniffer/test/integration/sniffer_map_test.rb b/plugins/sniffer/test/integration/sniffer_map_test.rb index 2e78e95..72dd1cf 100644 --- a/plugins/sniffer/test/integration/sniffer_map_test.rb +++ b/plugins/sniffer/test/integration/sniffer_map_test.rb @@ -1,6 +1,6 @@ require "#{File.dirname(__FILE__)}/../../../../test/test_helper" -class SnifferMapTest < ActionController::IntegrationTest +class SnifferMapTest < ActionDispatch::IntegrationTest fixtures :users, :profiles diff --git a/test/action_tracker_test_helper.rb b/test/action_tracker_test_helper.rb deleted file mode 100644 index bc6fea3..0000000 --- a/test/action_tracker_test_helper.rb +++ /dev/null @@ -1,16 +0,0 @@ -class UserStampSweeper < ActionController::Caching::Sweeper - private - def current_user - Person.first - end -end - -module ActionTracker - class Record - def back_in_time(time = 25.hours) - self.updated_at = Time.now.ago(time) - self.send :update_without_callbacks - self - end - end -end diff --git a/test/factories.rb b/test/factories.rb deleted file mode 100644 index 2970f1d..0000000 --- a/test/factories.rb +++ /dev/null @@ -1,496 +0,0 @@ -module Noosfero::Factory - - def fast_create(name, attrs = {}, options = {}) - defaults = defaults_for(name) - attrs[:slug] = attrs[:name].to_slug if attrs[:name].present? && attrs[:slug].blank? && defaults[:slug].present? - data = defaults_for(name.to_s.gsub('::','')).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 = klass.last(:order => "id") - if options[:category] - categories = options[:category] - unless categories.is_a?(Array) - categories = [categories] - end - categories.each do |category| - obj.add_category(category) - end - end - obj - end - - def create(name, attrs = {}) - target = 'create_' + name.to_s - if respond_to?(target) - send(target, attrs) - else - obj = build name - attrs.each{ |a, v| obj.send "#{a}=", v } - obj.save! - obj - end - end - - def build(name, attrs = {}) - defaults = defaults_for(name) - attrs[:slug] = attrs[:name].to_slug if attrs[:name].present? && attrs[:slug].blank? && defaults[:slug].present? - data = defaults_for(name).merge(attrs) - object = name.to_s.camelize.constantize.new - if object.respond_to?(:assign_attributes) - object.assign_attributes(data, :without_protection => true) - else - data.each { |attribute, value| object.send(attribute.to_s+'=', value) } - end - object - end - - def defaults_for(name) - send('defaults_for_' + name.to_s.underscore) - rescue - {} - end - - def self.num_seq - @num_seq ||= 0 - @num_seq += 1 - @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_trusted_sites', 'manage_environment_validators', 'manage_environment_users', 'manage_environment_organizations', 'manage_environment_templates', 'manage_environment_licenses', 'edit_appearance']) - create(RoleAssignment, :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 = nil, options = {}, person_options = {}) - name ||= 'user' + factory_num_seq.to_s - data = { - :login => name, - :email => name + '@noosfero.org', - :password => name.underscore, - :password_confirmation => name.underscore - }.merge(options) - user = build(User, data) - user.person_data = person_options - user.save! - 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 = nil, options = {}, person_options = {}) - name ||= 'user' + factory_num_seq.to_s - environment_id = options.delete(:environment_id) || (options.delete(:environment) || Environment.default).id - - password = options.delete(:password) - password_confirmation = options.delete(:password_confirmation) - raise build(Exception, "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, :name => 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, obj.class.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 = create(Role, :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 - Noosfero::Factory.num_seq - end - - ############################################### - # Environment - ############################################### - - def defaults_for_environment - seq = factory_num_seq - { - :name => "Environment %d" % seq, - :contact_email => "environment%d@example.com" % seq - } - end - - ############################################### - # Enterprise - ############################################### - - def defaults_for_enterprise - n = factory_num_seq.to_s - defaults_for_profile.merge({ :identifier => "enterprise-" + n, :name => 'Enterprise ' + n }) - end - - ############################################### - # Community - ############################################### - - def defaults_for_community - n = factory_num_seq.to_s - defaults_for_profile.merge({ :identifier => "community-" + n, :name => 'Community ' + n }) - end - - ############################################### - # Person - ############################################### - - def defaults_for_person - n = factory_num_seq.to_s - defaults_for_profile.merge({ :identifier => "person-" + n, :name => 'Person ' + n, :created_at => DateTime.now }) - 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 - - ############################################### - # Organization - ############################################### - - def defaults_for_organization - n = factory_num_seq.to_s - defaults_for_profile.merge({:identifier => 'organization-' + n, :name => 'Organization ' + n}) - end - - ############################################### - # Article (and friends) - ############################################### - - def defaults_for_article - name = 'My article ' + factory_num_seq.to_s - { :name => name, :slug => name.to_slug, :path => name.to_slug } - end - - alias :defaults_for_text_article :defaults_for_article - alias :defaults_for_textile_article :defaults_for_article - alias :defaults_for_tiny_mce_article :defaults_for_article - alias :defaults_for_rss_feed :defaults_for_article - alias :defaults_for_published_article :defaults_for_article - alias :defaults_for_folder :defaults_for_article - - ############################################### - # Event - ############################################### - - def defaults_for_event - num = factory_num_seq.to_s - { - :name => 'My event ' + num, - :slug => 'my-event-' + num, - :path => '/my-event-' + num, - :start_date => Date.today - } - end - - ############################################### - # UploadedFile - ############################################### - - def defaults_for_uploaded_file - name = 'My uploaded file ' + factory_num_seq.to_s - { :name => name, :abstract => name } - end - - ############################################### - # Blog - ############################################### - def defaults_for_blog - name = 'My blog ' + factory_num_seq.to_s - { :name => name, :slug => name.to_slug, :path => name.to_slug } - end - - def create_blog - profile = create(Profile, :identifier => 'testuser' + factory_num_seq.to_s, :name => 'Test user') - create(Blog, :name => 'blog', :profile => profile) - end - - ############################################### - # ExternalFeed - ############################################### - def defaults_for_external_feed - { :address => Rails.root.join('test', 'fixtures', 'files', 'feed.xml'), :blog_id => factory_num_seq } - end - - def create_external_feed(attrs = {}) - feed = build(:external_feed, attrs) - feed.blog = create_blog - feed.save! - feed - end - - ############################################### - # FeedReaderBlock - ############################################### - def defaults_for_feed_reader_block - { :address => Rails.root.join('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 - name = 'category' + factory_num_seq.to_s - { :environment_id => 1, :name => name, :slug => name.to_slug, :path => name.to_slug } - end - - alias :defaults_for_region :defaults_for_category - alias :defaults_for_product_category :defaults_for_category - - ############################################### - # Box - ############################################### - def defaults_for_box - { } - end - - ############################################### - # Block - ############################################### - def defaults_for_block - { } - end - - alias :defaults_for_blog_archives_block :defaults_for_block - alias :defaults_for_profile_list_block :defaults_for_block - - ############################################### - # Task - ############################################### - def defaults_for_task - { :code => "task_for_test_#{factory_num_seq.to_s}" } - end - - alias :defaults_for_add_friend :defaults_for_task - alias :defaults_for_add_member :defaults_for_task - alias :defaults_for_create_community :defaults_for_task - alias :defaults_for_email_activation :defaults_for_task - - ############################################### - # Product - ############################################### - - def defaults_for_product - { :name => 'Product ' + factory_num_seq.to_s } - end - - ############################################### - # Input - ############################################### - - def defaults_for_input - { } - end - - ############################################### - # Contact - ############################################### - - def defaults_for_contact - { :subject => 'hello there', :message => 'here I come to SPAM you' } - end - - ############################################### - # Qualifier - ############################################### - - def defaults_for_qualifier - { :name => 'Qualifier ' + factory_num_seq.to_s, :environment_id => 1 } - end - - ############################################### - # Certifier - ############################################### - - def defaults_for_certifier - defaults_for_qualifier.merge({ :name => 'Certifier ' + factory_num_seq.to_s }) - end - - ############################################### - # Scrap - ############################################### - - def defaults_for_scrap(params = {}) - { :content => 'some content ', :sender_id => 1, :receiver_id => 1, :created_at => DateTime.now }.merge(params) - end - - ############################################### - # ActionTrackerNotification - ############################################### - - def defaults_for_action_tracker_notification(params = {}) - { :action_tracker_id => 1, :profile_id => 1 }.merge(params) - end - - ############################################### - # ActionTracker - ############################################### - - def defaults_for_action_tracker_record(params = {}) - { :created_at => DateTime.now, :verb => 'add_member_in_community', :user_type => 'Profile', :user_id => 1 }.merge(params) - end - - ############################################### - # Friendship - ############################################### - - def defaults_for_friendship(params = {}) - { :created_at => DateTime.now, :person_id => 1, :friend_id => 2 }.merge(params) - end - - ############################################### - # RoleAssignment - ############################################### - - def defaults_for_role_assignment(params = {}) - { :role_id => 1, :accessor_id => 1, :accessor_type => 'Profile', :resource_id => 2, :resource_type => 'Profile' }.merge(params) - end - - ############################################### - # User - ############################################### - - def defaults_for_user(params = {}) - username = "user_#{rand(1000)}" - { :login => username, :email => username + '@noosfero.colivre', :crypted_password => 'test'}.merge(params) - end - - ############################################### - # Forum - ############################################### - - def defaults_for_forum(params = {}) - name = "forum_#{rand(1000)}" - { :profile_id => 1, :path => name.to_slug, :name => name, :slug => name.to_slug }.merge(params) - end - - ############################################### - # Gallery - ############################################### - - def defaults_for_gallery(params = {}) - name = "gallery_#{rand(1000)}" - { :profile_id => 1, :path => name.to_slug, :name => name, :slug => name.to_slug }.merge(params) - end - - def defaults_for_suggest_article - { :name => 'Sender', :email => 'sender@example.com', :article => {:name => 'Some title', :body => 'some body text', :abstract => 'some abstract text'}} - end - - def defaults_for_comment(params = {}) - name = "comment_#{rand(1000)}" - { :title => name, :body => "my own comment", :source_id => 1, :source_type => 'Article' }.merge(params) - end - - ############################################### - # Unit - ############################################### - - def defaults_for_unit - { :singular => 'Litre', :plural => 'Litres', :environment_id => 1 } - end - - ############################################### - # Production Cost - ############################################### - - def defaults_for_production_cost - { :name => 'Production cost ' + factory_num_seq.to_s } - end - - ############################################### - # National Region - ############################################### - - def defaults_for_national_region - { :name => 'National region ' + factory_num_seq.to_s } - end - - def defaults_for_license - name = "License #{rand(1000)}" - slug = name.to_slug - { :name => name, :url => "#{slug}.org", :slug => slug, :environment_id => 1} - end - -end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 59268e5..98ef59c 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -2,9 +2,7 @@ require_relative "../test_helper" require 'account_controller' class AccountControllerTest < ActionController::TestCase - # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead - # Then, you can remove it from this and the units test. - include AuthenticatedTestHelper + all_fixtures def teardown diff --git a/test/integration/approve_reject_enterprise_test.rb b/test/integration/approve_reject_enterprise_test.rb deleted file mode 100644 index 42cab92..0000000 --- a/test/integration/approve_reject_enterprise_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require_relative "../test_helper" - -class ApproveRejectEnterpriseTest < ActionController::IntegrationTest - all_fixtures - - def test_approving - true - end -end diff --git a/test/integration/assets_menu_test.rb b/test/integration/assets_menu_test.rb index fd13804..e705843 100644 --- a/test/integration/assets_menu_test.rb +++ b/test/integration/assets_menu_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class AssetsMenuTest < ActionController::IntegrationTest +class AssetsMenuTest < ActionDispatch::IntegrationTest def setup # HomeController.any_instance.stubs(:get_layout).returns('application') @@ -9,18 +9,18 @@ class AssetsMenuTest < ActionController::IntegrationTest parent = Category.create!(:name => "Parent Category", :environment => Environment.default, :display_color => '#888a85') @category = Category.create!(:name => "Category A", :environment => Environment.default, :parent => parent) end - + should 'link to uncategorized assets at site root' do get '/' assert_tag :tag => 'a', :attributes => { :href => '/search/contents' } end should 'link to assets inside category root' do - (1..SearchController::MULTIPLE_SEARCH_LIMIT+1).each do |i| + (1..SearchController::MULTIPLE_SEARCH_LIMIT+1).each do |i| enterprise = Enterprise.create! :identifier => "ent#{i}", :name => "enterprise#{i}" ProfileCategorization.add_category_to_profile(@category, enterprise) end - + get '/cat/parent-category/category-a' assert_tag :tag => 'a', :attributes => { :href => '/search/enterprises/parent-category/category-a' } end diff --git a/test/integration/assigning_validator_organizations_to_regions_test.rb b/test/integration/assigning_validator_organizations_to_regions_test.rb index f8b4fcf..8212281 100644 --- a/test/integration/assigning_validator_organizations_to_regions_test.rb +++ b/test/integration/assigning_validator_organizations_to_regions_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class AssigningValidatorOrganizationsToRegionsTest < ActionController::IntegrationTest +class AssigningValidatorOrganizationsToRegionsTest < ActionDispatch::IntegrationTest should 'be able to properly assign organizations as validators to regions' do env = Environment.default @@ -25,7 +25,7 @@ class AssigningValidatorOrganizationsToRegionsTest < ActionController::Integrati get "/admin/region_validators/region/#{region1.id}", :search => 'two' assert_response :success assert_tag :tag => 'form', :attributes => { :action => "/admin/region_validators/add/#{region1.id}" }, :descendant => { :tag => 'input', :attributes => { :type => 'hidden', :name => 'validator_id', :value => org2.id } } - + post "/admin/region_validators/add/#{region1.id}", :validator_id => org2.id assert_response :redirect diff --git a/test/integration/blocks_test.rb b/test/integration/blocks_test.rb index 04eb683..a04749c 100644 --- a/test/integration/blocks_test.rb +++ b/test/integration/blocks_test.rb @@ -1,6 +1,7 @@ require_relative "../test_helper" -class BlocksTest < ActionController::IntegrationTest +class BlocksTest < ActionDispatch::IntegrationTest + def blog_on_article_block_bootstrap profile = fast_create(Profile) blog = fast_create(Blog, :name => 'Blog', :profile_id => profile.id) diff --git a/test/integration/controller_naming_test.rb b/test/integration/controller_naming_test.rb index 16313bc..4689d0b 100644 --- a/test/integration/controller_naming_test.rb +++ b/test/integration/controller_naming_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class ControllerNamingTest < ActionController::IntegrationTest +class ControllerNamingTest < ActionDispatch::IntegrationTest should 'not have controllers with same name in different folders' do controllers = Dir.glob(Rails.root.join("app", "controllers", "**", "*_controller.rb")).map { |item| item.split(/\//).last } diff --git a/test/integration/editing_person_info_test.rb b/test/integration/editing_person_info_test.rb index 00b21a2..6f2b018 100644 --- a/test/integration/editing_person_info_test.rb +++ b/test/integration/editing_person_info_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class EditingPersonInfoTest < ActionController::IntegrationTest +class EditingPersonInfoTest < ActionDispatch::IntegrationTest fixtures :users, :profiles, :domains, :environments diff --git a/test/integration/enable_disable_features_test.rb b/test/integration/enable_disable_features_test.rb index 70975b9..1c7a036 100644 --- a/test/integration/enable_disable_features_test.rb +++ b/test/integration/enable_disable_features_test.rb @@ -1,6 +1,7 @@ require_relative "../test_helper" -class EnableDisableFeaturesTest < ActionController::IntegrationTest +class EnableDisableFeaturesTest < ActionDispatch::IntegrationTest + all_fixtures def test_enable_features diff --git a/test/integration/enterprise_registration_test.rb b/test/integration/enterprise_registration_test.rb index 92f2489..69d5f8a 100644 --- a/test/integration/enterprise_registration_test.rb +++ b/test/integration/enterprise_registration_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class EnterpriseRegistrationTest < ActionController::IntegrationTest +class EnterpriseRegistrationTest < ActionDispatch::IntegrationTest fixtures :users, :profiles, :environments @@ -40,7 +40,7 @@ class EnterpriseRegistrationTest < ActionController::IntegrationTest assert_difference 'CreateEnterprise.count' do post '/enterprise_registration', :create_enterprise => data.merge(:target_id => org.id) end - + assert_template 'confirmation' assert_tag :tag => 'a', :attributes => { :href => '/' } @@ -63,7 +63,7 @@ class EnterpriseRegistrationTest < ActionController::IntegrationTest post "/myprofile/myorg/enterprise_validation/approve/#{code}" assert_response :redirect - + follow_redirect! assert_equal "/myprofile/myorg/enterprise_validation/view_processed/#{code}", path assert_tag :span, :attributes => { :class => 'validation_approved' } diff --git a/test/integration/forgot_password_test.rb b/test/integration/forgot_password_test.rb index 133b33c..fc714e6 100644 --- a/test/integration/forgot_password_test.rb +++ b/test/integration/forgot_password_test.rb @@ -1,9 +1,9 @@ require_relative "../test_helper" -class ForgotPasswordTest < ActionController::IntegrationTest +class ForgotPasswordTest < ActionDispatch::IntegrationTest def setup - ActionController::Integration::Session.any_instance.stubs(:https?).returns(true) + ActionDispatch::Integration::Session.any_instance.stubs(:https?).returns(true) end def test_forgot_password_with_login diff --git a/test/integration/http_caching_test.rb b/test/integration/http_caching_test.rb index 3d2fa0d..65830fb 100644 --- a/test/integration/http_caching_test.rb +++ b/test/integration/http_caching_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class HttpCachingTest < ActionController::IntegrationTest +class HttpCachingTest < ActionDispatch::IntegrationTest def setup create_user('joao', password: 'test', password_confirmation: 'test').activate diff --git a/test/integration/login_to_the_application_test.rb b/test/integration/login_to_the_application_test.rb index 4ddb9fe..fbcc850 100644 --- a/test/integration/login_to_the_application_test.rb +++ b/test/integration/login_to_the_application_test.rb @@ -1,6 +1,7 @@ require_relative "../test_helper" -class LoginToTheApplicationTest < ActionController::IntegrationTest +class LoginToTheApplicationTest < ActionDispatch::IntegrationTest + fixtures :users, :environments, :profiles def test_unauthenticated_user_tries_to_access_his_control_panel diff --git a/test/integration/manage_documents_test.rb b/test/integration/manage_documents_test.rb index 00b0093..6530aaf 100644 --- a/test/integration/manage_documents_test.rb +++ b/test/integration/manage_documents_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class ManageDocumentsTest < ActionController::IntegrationTest +class ManageDocumentsTest < ActionDispatch::IntegrationTest all_fixtures @@ -78,18 +78,18 @@ class ManageDocumentsTest < ActionController::IntegrationTest assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}" } get '/myprofile/myuser' assert_response :success - + assert_tag :tag => 'a', :attributes => { :href => '/myprofile/myuser/cms' } get '/myprofile/myuser/cms' assert_response :success - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/myuser/cms/destroy/#{article.id}", 'data-confirm' => /Are you sure/ } + assert_tag tag: 'a', attributes: { href: "/myprofile/myuser/cms/destroy/#{article.id}" } post "/myprofile/myuser/cms/destroy/#{article.id}" assert_response :redirect follow_redirect! assert_equal "/myuser", path - + # the article was actually deleted assert_raise ActiveRecord::RecordNotFound do Article.find(article.id) diff --git a/test/integration/manage_friendships_test.rb b/test/integration/manage_friendships_test.rb index babac0b..23c18b8 100644 --- a/test/integration/manage_friendships_test.rb +++ b/test/integration/manage_friendships_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class ManageFriendshipsTest < ActionController::IntegrationTest +class ManageFriendshipsTest < ActionDispatch::IntegrationTest def setup FriendsController.any_instance.stubs(:get_layout).returns('application') diff --git a/test/integration/online_doc_test.rb b/test/integration/online_doc_test.rb index 8dc6e97..0333bae 100644 --- a/test/integration/online_doc_test.rb +++ b/test/integration/online_doc_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class OnlineDocTest < ActionController::IntegrationTest +class OnlineDocTest < ActionDispatch::IntegrationTest def test_404_section get '/doc/something-very-unlikely' diff --git a/test/integration/performance_test.rb b/test/integration/performance_test.rb index ef55305..7e9f910 100644 --- a/test/integration/performance_test.rb +++ b/test/integration/performance_test.rb @@ -1,7 +1,7 @@ require_relative "../test_helper" require 'benchmark' -class PerformanceTest < ActionController::IntegrationTest +class PerformanceTest < ActionDispatch::IntegrationTest all_fixtures diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index f32f3fa..eff3820 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class RoutingTest < ActionController::IntegrationTest +class RoutingTest < ActionDispatch::IntegrationTest def setup Domain.clear_cache diff --git a/test/integration/search_popup_test.rb b/test/integration/search_popup_test.rb index 0d6e3d3..61ac57d 100644 --- a/test/integration/search_popup_test.rb +++ b/test/integration/search_popup_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class SearchPopupTest < ActionController::IntegrationTest +class SearchPopupTest < ActionDispatch::IntegrationTest def setup HomeController.any_instance.stubs(:get_layout).returns('application') diff --git a/test/integration/signup_test.rb b/test/integration/signup_test.rb index aa9b268..7f065cd 100644 --- a/test/integration/signup_test.rb +++ b/test/integration/signup_test.rb @@ -1,10 +1,11 @@ require_relative "../test_helper" -class SignupTest < ActionController::IntegrationTest +class SignupTest < ActionDispatch::IntegrationTest + all_fixtures def setup - ActionController::Integration::Session.any_instance.stubs(:https?).returns(true) + ActionDispatch::Integration::Session.any_instance.stubs(:https?).returns(true) end def test_signup_form_submission_must_be_blocked_for_fast_bots diff --git a/test/integration/user_registers_at_the_application_test.rb b/test/integration/user_registers_at_the_application_test.rb index 87ddebb..eb308ec 100644 --- a/test/integration/user_registers_at_the_application_test.rb +++ b/test/integration/user_registers_at_the_application_test.rb @@ -1,6 +1,6 @@ require_relative "../test_helper" -class UserRegistersAtTheApplicationTest < ActionController::IntegrationTest +class UserRegistersAtTheApplicationTest < ActionDispatch::IntegrationTest fixtures :users, :environments, :profiles def test_successfull_registration @@ -11,7 +11,7 @@ class UserRegistersAtTheApplicationTest < ActionController::IntegrationTest get '/account/signup' assert_response :success - + post '/account/signup', :user => { :login => 'mylogin', :password => 'mypassword', :password_confirmation => 'mypassword', :email => 'mylogin@example.com' } assert_response :success @@ -32,7 +32,7 @@ class UserRegistersAtTheApplicationTest < ActionController::IntegrationTest get '/account/signup' assert_response :success - + post '/account/signup', :user => { :login => 'ze', :password => 'mypassword', :password_confirmation => 'mypassword', :email => 'mylogin@example.com' } assert_response :success assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation', :class => 'errorExplanation' } diff --git a/test/integration/varnish_conf_test.rb b/test/integration/varnish_conf_test.rb index 4bbc85a..529e1f9 100644 --- a/test/integration/varnish_conf_test.rb +++ b/test/integration/varnish_conf_test.rb @@ -1,6 +1,6 @@ -require 'test/unit' +require 'test_helper' -class VarnishConfTest < Test::Unit::TestCase +class VarnishConfTest < ActiveSupport::TestCase def test_not_use_return_in_varnish_noosfero assert !system('grep "return.*pass" etc/noosfero/varnish-noosfero.vcl') diff --git a/test/noosfero_doc_test.rb b/test/noosfero_doc_test.rb deleted file mode 100644 index b7235c0..0000000 --- a/test/noosfero_doc_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -# encoding: UTF-8 -require 'mocha' - -module Noosfero::DocTest - - unless defined?(ROOT) - ROOT = Rails.root.join("test", "tmp", "doc") - end - - def create_doc(section, topic, language, title, body = nil) - dir = File.join(ROOT, section) - FileUtils.mkdir_p(dir) - File.open("#{dir}/#{topic}.#{language}.xhtml", "w") do |f| - f.puts "

#{title}

" - f.puts body - end - end - - def setup_doc_test - FileUtils.mkdir_p(ROOT) - - # root - create_doc('', 'index', 'en', 'Noosfero online manual') - create_doc('', 'toc', 'en', '', '') - # cms - create_doc('cms', 'index', 'en', 'Content Management') - create_doc('cms', 'index', 'pt', 'Gerenciamento de conteúdo') - create_doc('cms', 'toc', 'en', '') - create_doc('cms', 'toc', 'pt', '') - create_doc('cms', 'adding-pictures', 'en', 'Adding pictures to gallery') - create_doc('cms', 'adding-pictures', 'pt', 'Adicionando fotos na galeria') - create_doc('cms', 'creating-a-blog', 'en', 'Creating a blog') - create_doc('cms', 'creating-a-blog', 'pt', 'Criando um blog') - # user - create_doc('user', 'index', 'en', 'User features') - create_doc('user', 'index', 'pt', 'Funcionalidades de Usuário') - create_doc('user', 'toc', 'en', '') - create_doc('user', 'toc', 'pt', '') - create_doc('user', 'accepting-friends', 'en', 'Accepting friends') - create_doc('user', 'accepting-friends', 'pt', 'Aceitando amigos') - create_doc('user', 'commenting-articles', 'en', 'Commenting articles', 'How to access') - create_doc('user', 'commenting-articles', 'pt', 'Comentando artigos') - - DocSection.stubs(:root_dir).returns(ROOT) - end - - def tear_down_doc_test - FileUtils.rm_rf(ROOT) - end -end diff --git a/test/support/action_tracker_test_helper.rb b/test/support/action_tracker_test_helper.rb new file mode 100644 index 0000000..bc6fea3 --- /dev/null +++ b/test/support/action_tracker_test_helper.rb @@ -0,0 +1,16 @@ +class UserStampSweeper < ActionController::Caching::Sweeper + private + def current_user + Person.first + end +end + +module ActionTracker + class Record + def back_in_time(time = 25.hours) + self.updated_at = Time.now.ago(time) + self.send :update_without_callbacks + self + end + end +end diff --git a/test/support/authenticated_test_helper.rb b/test/support/authenticated_test_helper.rb new file mode 100644 index 0000000..37ebad4 --- /dev/null +++ b/test/support/authenticated_test_helper.rb @@ -0,0 +1,99 @@ +module AuthenticatedTestHelper + + # Sets the current user in the session from the user fixtures. + def login_as(user) + @request.session[:user] = User.find_by_login(user.to_s).id + end + + def logout + @request.session.delete(:user) + end + + def content_type(type) + @request.env['Content-Type'] = type + end + + def accept(accept) + @request.env["HTTP_ACCEPT"] = accept + end + + def authorize_as(user) + if user + @request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{users(user).login}:test")}" + accept 'application/xml' + content_type 'application/xml' + else + @request.env["HTTP_AUTHORIZATION"] = nil + accept nil + content_type nil + end + end + + # Assert the block redirects to the login + # + # assert_requires_login(:bob) { |c| c.get :edit, :id => 1 } + # + def assert_requires_login(login = nil) + yield HttpLoginProxy.new(self, login) + end + + def assert_http_authentication_required(login = nil) + yield XmlLoginProxy.new(self, login) + end + + def reset!(*instance_vars) + instance_vars = [:controller, :request, :response] unless instance_vars.any? + instance_vars.collect! { |v| "@#{v}".to_sym } + instance_vars.each do |var| + instance_variable_set(var, instance_variable_get(var).class.new) + end + end +end + +class BaseLoginProxy + attr_reader :controller + attr_reader :options + def initialize(controller, login) + @controller = controller + @login = login + end + + private + def authenticated + raise NotImplementedError + end + + def check + raise NotImplementedError + end + + def method_missing(method, *args) + @controller.reset! + authenticate + @controller.send(method, *args) + check + end +end + +class HttpLoginProxy < BaseLoginProxy + protected + def authenticate + @controller.login_as @login if @login + end + + def check + @controller.assert_redirected_to :controller => 'account', :action => 'login' + end +end + +class XmlLoginProxy < BaseLoginProxy + protected + def authenticate + @controller.accept 'application/xml' + @controller.authorize_as @login if @login + end + + def check + @controller.assert_response 401 + end +end diff --git a/test/support/controller_test_case.rb b/test/support/controller_test_case.rb new file mode 100644 index 0000000..4007eb1 --- /dev/null +++ b/test/support/controller_test_case.rb @@ -0,0 +1,16 @@ + +class ActionController::TestCase + + class_attribute :default_params + self.default_params = {} + + def get path, parameters = nil, session = nil, flash = nil + super path, if parameters then self.default_params.merge parameters else self.default_params end, session, flash + end + + def post path, parameters = nil, session = nil, flash = nil + super path, if parameters then self.default_params.merge parameters else self.default_params end, session, flash + end + +end + diff --git a/test/support/factories.rb b/test/support/factories.rb new file mode 100644 index 0000000..2970f1d --- /dev/null +++ b/test/support/factories.rb @@ -0,0 +1,496 @@ +module Noosfero::Factory + + def fast_create(name, attrs = {}, options = {}) + defaults = defaults_for(name) + attrs[:slug] = attrs[:name].to_slug if attrs[:name].present? && attrs[:slug].blank? && defaults[:slug].present? + data = defaults_for(name.to_s.gsub('::','')).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 = klass.last(:order => "id") + if options[:category] + categories = options[:category] + unless categories.is_a?(Array) + categories = [categories] + end + categories.each do |category| + obj.add_category(category) + end + end + obj + end + + def create(name, attrs = {}) + target = 'create_' + name.to_s + if respond_to?(target) + send(target, attrs) + else + obj = build name + attrs.each{ |a, v| obj.send "#{a}=", v } + obj.save! + obj + end + end + + def build(name, attrs = {}) + defaults = defaults_for(name) + attrs[:slug] = attrs[:name].to_slug if attrs[:name].present? && attrs[:slug].blank? && defaults[:slug].present? + data = defaults_for(name).merge(attrs) + object = name.to_s.camelize.constantize.new + if object.respond_to?(:assign_attributes) + object.assign_attributes(data, :without_protection => true) + else + data.each { |attribute, value| object.send(attribute.to_s+'=', value) } + end + object + end + + def defaults_for(name) + send('defaults_for_' + name.to_s.underscore) + rescue + {} + end + + def self.num_seq + @num_seq ||= 0 + @num_seq += 1 + @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_trusted_sites', 'manage_environment_validators', 'manage_environment_users', 'manage_environment_organizations', 'manage_environment_templates', 'manage_environment_licenses', 'edit_appearance']) + create(RoleAssignment, :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 = nil, options = {}, person_options = {}) + name ||= 'user' + factory_num_seq.to_s + data = { + :login => name, + :email => name + '@noosfero.org', + :password => name.underscore, + :password_confirmation => name.underscore + }.merge(options) + user = build(User, data) + user.person_data = person_options + user.save! + 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 = nil, options = {}, person_options = {}) + name ||= 'user' + factory_num_seq.to_s + environment_id = options.delete(:environment_id) || (options.delete(:environment) || Environment.default).id + + password = options.delete(:password) + password_confirmation = options.delete(:password_confirmation) + raise build(Exception, "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, :name => 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, obj.class.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 = create(Role, :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 + Noosfero::Factory.num_seq + end + + ############################################### + # Environment + ############################################### + + def defaults_for_environment + seq = factory_num_seq + { + :name => "Environment %d" % seq, + :contact_email => "environment%d@example.com" % seq + } + end + + ############################################### + # Enterprise + ############################################### + + def defaults_for_enterprise + n = factory_num_seq.to_s + defaults_for_profile.merge({ :identifier => "enterprise-" + n, :name => 'Enterprise ' + n }) + end + + ############################################### + # Community + ############################################### + + def defaults_for_community + n = factory_num_seq.to_s + defaults_for_profile.merge({ :identifier => "community-" + n, :name => 'Community ' + n }) + end + + ############################################### + # Person + ############################################### + + def defaults_for_person + n = factory_num_seq.to_s + defaults_for_profile.merge({ :identifier => "person-" + n, :name => 'Person ' + n, :created_at => DateTime.now }) + 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 + + ############################################### + # Organization + ############################################### + + def defaults_for_organization + n = factory_num_seq.to_s + defaults_for_profile.merge({:identifier => 'organization-' + n, :name => 'Organization ' + n}) + end + + ############################################### + # Article (and friends) + ############################################### + + def defaults_for_article + name = 'My article ' + factory_num_seq.to_s + { :name => name, :slug => name.to_slug, :path => name.to_slug } + end + + alias :defaults_for_text_article :defaults_for_article + alias :defaults_for_textile_article :defaults_for_article + alias :defaults_for_tiny_mce_article :defaults_for_article + alias :defaults_for_rss_feed :defaults_for_article + alias :defaults_for_published_article :defaults_for_article + alias :defaults_for_folder :defaults_for_article + + ############################################### + # Event + ############################################### + + def defaults_for_event + num = factory_num_seq.to_s + { + :name => 'My event ' + num, + :slug => 'my-event-' + num, + :path => '/my-event-' + num, + :start_date => Date.today + } + end + + ############################################### + # UploadedFile + ############################################### + + def defaults_for_uploaded_file + name = 'My uploaded file ' + factory_num_seq.to_s + { :name => name, :abstract => name } + end + + ############################################### + # Blog + ############################################### + def defaults_for_blog + name = 'My blog ' + factory_num_seq.to_s + { :name => name, :slug => name.to_slug, :path => name.to_slug } + end + + def create_blog + profile = create(Profile, :identifier => 'testuser' + factory_num_seq.to_s, :name => 'Test user') + create(Blog, :name => 'blog', :profile => profile) + end + + ############################################### + # ExternalFeed + ############################################### + def defaults_for_external_feed + { :address => Rails.root.join('test', 'fixtures', 'files', 'feed.xml'), :blog_id => factory_num_seq } + end + + def create_external_feed(attrs = {}) + feed = build(:external_feed, attrs) + feed.blog = create_blog + feed.save! + feed + end + + ############################################### + # FeedReaderBlock + ############################################### + def defaults_for_feed_reader_block + { :address => Rails.root.join('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 + name = 'category' + factory_num_seq.to_s + { :environment_id => 1, :name => name, :slug => name.to_slug, :path => name.to_slug } + end + + alias :defaults_for_region :defaults_for_category + alias :defaults_for_product_category :defaults_for_category + + ############################################### + # Box + ############################################### + def defaults_for_box + { } + end + + ############################################### + # Block + ############################################### + def defaults_for_block + { } + end + + alias :defaults_for_blog_archives_block :defaults_for_block + alias :defaults_for_profile_list_block :defaults_for_block + + ############################################### + # Task + ############################################### + def defaults_for_task + { :code => "task_for_test_#{factory_num_seq.to_s}" } + end + + alias :defaults_for_add_friend :defaults_for_task + alias :defaults_for_add_member :defaults_for_task + alias :defaults_for_create_community :defaults_for_task + alias :defaults_for_email_activation :defaults_for_task + + ############################################### + # Product + ############################################### + + def defaults_for_product + { :name => 'Product ' + factory_num_seq.to_s } + end + + ############################################### + # Input + ############################################### + + def defaults_for_input + { } + end + + ############################################### + # Contact + ############################################### + + def defaults_for_contact + { :subject => 'hello there', :message => 'here I come to SPAM you' } + end + + ############################################### + # Qualifier + ############################################### + + def defaults_for_qualifier + { :name => 'Qualifier ' + factory_num_seq.to_s, :environment_id => 1 } + end + + ############################################### + # Certifier + ############################################### + + def defaults_for_certifier + defaults_for_qualifier.merge({ :name => 'Certifier ' + factory_num_seq.to_s }) + end + + ############################################### + # Scrap + ############################################### + + def defaults_for_scrap(params = {}) + { :content => 'some content ', :sender_id => 1, :receiver_id => 1, :created_at => DateTime.now }.merge(params) + end + + ############################################### + # ActionTrackerNotification + ############################################### + + def defaults_for_action_tracker_notification(params = {}) + { :action_tracker_id => 1, :profile_id => 1 }.merge(params) + end + + ############################################### + # ActionTracker + ############################################### + + def defaults_for_action_tracker_record(params = {}) + { :created_at => DateTime.now, :verb => 'add_member_in_community', :user_type => 'Profile', :user_id => 1 }.merge(params) + end + + ############################################### + # Friendship + ############################################### + + def defaults_for_friendship(params = {}) + { :created_at => DateTime.now, :person_id => 1, :friend_id => 2 }.merge(params) + end + + ############################################### + # RoleAssignment + ############################################### + + def defaults_for_role_assignment(params = {}) + { :role_id => 1, :accessor_id => 1, :accessor_type => 'Profile', :resource_id => 2, :resource_type => 'Profile' }.merge(params) + end + + ############################################### + # User + ############################################### + + def defaults_for_user(params = {}) + username = "user_#{rand(1000)}" + { :login => username, :email => username + '@noosfero.colivre', :crypted_password => 'test'}.merge(params) + end + + ############################################### + # Forum + ############################################### + + def defaults_for_forum(params = {}) + name = "forum_#{rand(1000)}" + { :profile_id => 1, :path => name.to_slug, :name => name, :slug => name.to_slug }.merge(params) + end + + ############################################### + # Gallery + ############################################### + + def defaults_for_gallery(params = {}) + name = "gallery_#{rand(1000)}" + { :profile_id => 1, :path => name.to_slug, :name => name, :slug => name.to_slug }.merge(params) + end + + def defaults_for_suggest_article + { :name => 'Sender', :email => 'sender@example.com', :article => {:name => 'Some title', :body => 'some body text', :abstract => 'some abstract text'}} + end + + def defaults_for_comment(params = {}) + name = "comment_#{rand(1000)}" + { :title => name, :body => "my own comment", :source_id => 1, :source_type => 'Article' }.merge(params) + end + + ############################################### + # Unit + ############################################### + + def defaults_for_unit + { :singular => 'Litre', :plural => 'Litres', :environment_id => 1 } + end + + ############################################### + # Production Cost + ############################################### + + def defaults_for_production_cost + { :name => 'Production cost ' + factory_num_seq.to_s } + end + + ############################################### + # National Region + ############################################### + + def defaults_for_national_region + { :name => 'National region ' + factory_num_seq.to_s } + end + + def defaults_for_license + name = "License #{rand(1000)}" + slug = name.to_slug + { :name => name, :url => "#{slug}.org", :slug => slug, :environment_id => 1} + end + +end diff --git a/test/support/integration_test.rb b/test/support/integration_test.rb new file mode 100644 index 0000000..2c3a82f --- /dev/null +++ b/test/support/integration_test.rb @@ -0,0 +1,23 @@ + +class ActionDispatch::IntegrationTest + + extend Test::Should + + def assert_can_login + assert_tag :tag => 'a', :attributes => { :id => 'link_login' } + end + + def assert_can_signup + assert_tag :tag => 'a', :attributes => { :href => '/account/signup'} + end + + def login(username, password) + ActionDispatch::Integration::Session.any_instance.stubs(:https?).returns(true) + + post '/account/login', :user => { :login => username, :password => password } + assert_response :redirect + follow_redirect! + assert_not_equal '/account/login', path + end + +end diff --git a/test/support/noosfero_doc_test.rb b/test/support/noosfero_doc_test.rb new file mode 100644 index 0000000..b7235c0 --- /dev/null +++ b/test/support/noosfero_doc_test.rb @@ -0,0 +1,50 @@ +# encoding: UTF-8 +require 'mocha' + +module Noosfero::DocTest + + unless defined?(ROOT) + ROOT = Rails.root.join("test", "tmp", "doc") + end + + def create_doc(section, topic, language, title, body = nil) + dir = File.join(ROOT, section) + FileUtils.mkdir_p(dir) + File.open("#{dir}/#{topic}.#{language}.xhtml", "w") do |f| + f.puts "

#{title}

" + f.puts body + end + end + + def setup_doc_test + FileUtils.mkdir_p(ROOT) + + # root + create_doc('', 'index', 'en', 'Noosfero online manual') + create_doc('', 'toc', 'en', '', '') + # cms + create_doc('cms', 'index', 'en', 'Content Management') + create_doc('cms', 'index', 'pt', 'Gerenciamento de conteúdo') + create_doc('cms', 'toc', 'en', '') + create_doc('cms', 'toc', 'pt', '') + create_doc('cms', 'adding-pictures', 'en', 'Adding pictures to gallery') + create_doc('cms', 'adding-pictures', 'pt', 'Adicionando fotos na galeria') + create_doc('cms', 'creating-a-blog', 'en', 'Creating a blog') + create_doc('cms', 'creating-a-blog', 'pt', 'Criando um blog') + # user + create_doc('user', 'index', 'en', 'User features') + create_doc('user', 'index', 'pt', 'Funcionalidades de Usuário') + create_doc('user', 'toc', 'en', '') + create_doc('user', 'toc', 'pt', '') + create_doc('user', 'accepting-friends', 'en', 'Accepting friends') + create_doc('user', 'accepting-friends', 'pt', 'Aceitando amigos') + create_doc('user', 'commenting-articles', 'en', 'Commenting articles', 'How to access') + create_doc('user', 'commenting-articles', 'pt', 'Comentando artigos') + + DocSection.stubs(:root_dir).returns(ROOT) + end + + def tear_down_doc_test + FileUtils.rm_rf(ROOT) + end +end diff --git a/test/support/noosfero_test_helper.rb b/test/support/noosfero_test_helper.rb new file mode 100644 index 0000000..d3c58d9 --- /dev/null +++ b/test/support/noosfero_test_helper.rb @@ -0,0 +1,70 @@ +module NoosferoTestHelper + + def link_to(content, url, options = {}) + "#{content}" + end + + def content_tag(tag, content, options = {}) + tag_attr = options.blank? ? '' : ' ' + options.collect{ |o| "#{o[0]}=\"#{o[1]}\"" }.join(' ') + "<#{tag}#{tag_attr}>#{content}" + end + + def submit_tag(content, options = {}) + content + end + + def remote_function(options = {}) + '' + end + + def tag(tag, args = {}) + attrs = args.map{|k,v| "#{k}='#{v}'"}.join(' ') + "<#{tag} #{attrs} />" + end + + def options_from_collection_for_select(collection, value_method, content_method) + "" + end + + def select_tag(id, collection, options = {}) + "" + end + + def options_for_select(collection, selected = nil) + collection.map{|item| ""}.join("\n") + end + + def params + {} + end + + def ui_icon(icon) + icon + end + + def will_paginate(arg1, arg2) + end + + def javascript_tag(any) + '' + end + def javascript_include_tag(any) + '' + end + def check_box_tag(name, value = 1, checked = false, options = {}) + name + end + def stylesheet_link_tag(arg) + arg + end + + def strip_tags(html) + html.gsub(/<[^>]+>/, '') + end + + def icon_for_article(article) + '' + end + +end + diff --git a/test/support/should.rb b/test/support/should.rb new file mode 100644 index 0000000..5e521be --- /dev/null +++ b/test/support/should.rb @@ -0,0 +1,27 @@ + +module Test + + module Should + + def should name, &block + @shoulds ||= [] + + destname = 'test_should_' + name.gsub(/[^a-zA-z0-9]+/, '_') + if @shoulds.include?(destname) + raise "there is already a test named \"#{destname}\"" + end + + @shoulds << destname + if block_given? + self.send(:define_method, destname, &block) + else + self.send(:define_method, destname) do + flunk 'pending: should ' + name + end + end + + end + + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 9d9e77a..b65ded8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,10 +9,14 @@ require 'mocha/mini_test' require "minitest/reporters" Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new, ENV, Minitest.backtrace_filter -require 'authenticated_test_helper' -require_relative 'factories' -require_relative 'noosfero_doc_test' -require_relative 'action_tracker_test_helper' +require_relative 'support/should' +require_relative 'support/factories' +require_relative 'support/integration_test' +require_relative 'support/controller_test_case' +require_relative 'support/authenticated_test_helper' +require_relative 'support/action_tracker_test_helper' +require_relative 'support/noosfero_doc_test' +require_relative 'support/noosfero_test_helper' FileUtils.rm_rf(Rails.root.join('index', 'test')) @@ -53,6 +57,8 @@ class ActiveSupport::TestCase include AuthenticatedTestHelper + extend Test::Should + fixtures :environments, :roles def self.all_fixtures @@ -61,25 +67,6 @@ class ActiveSupport::TestCase end end - def self.should(name, &block) - @shoulds ||= [] - - destname = 'test_should_' + name.gsub(/[^a-zA-z0-9]+/, '_') - if @shoulds.include?(destname) - raise "there is already a test named \"#{destname}\"" - end - - @shoulds << destname - if block_given? - self.send(:define_method, destname, &block) - else - self.send(:define_method, destname) do - flunk 'pending: should ' + name - end - end - - end - # deprecated on minitest def assert_block message=nil assert message || 'yield' do @@ -214,108 +201,3 @@ class ActiveSupport::TestCase end -module NoosferoTestHelper - def link_to(content, url, options = {}) - "#{content}" - end - - def content_tag(tag, content, options = {}) - tag_attr = options.blank? ? '' : ' ' + options.collect{ |o| "#{o[0]}=\"#{o[1]}\"" }.join(' ') - "<#{tag}#{tag_attr}>#{content}" - end - - def submit_tag(content, options = {}) - content - end - - def remote_function(options = {}) - '' - end - - def tag(tag, args = {}) - attrs = args.map{|k,v| "#{k}='#{v}'"}.join(' ') - "<#{tag} #{attrs} />" - end - - def options_from_collection_for_select(collection, value_method, content_method) - "" - end - - def select_tag(id, collection, options = {}) - "" - end - - def options_for_select(collection, selected = nil) - collection.map{|item| ""}.join("\n") - end - - def params - {} - end - - def ui_icon(icon) - icon - end - - def will_paginate(arg1, arg2) - end - - def javascript_tag(any) - '' - end - def javascript_include_tag(any) - '' - end - def check_box_tag(name, value = 1, checked = false, options = {}) - name - end - def stylesheet_link_tag(arg) - arg - end - - def strip_tags(html) - html.gsub(/<[^>]+>/, '') - end - - def icon_for_article(article) - '' - end - -end - -class ActionController::TestCase - - class_attribute :default_params - self.default_params = {} - - def get path, parameters = nil, session = nil, flash = nil - super path, if parameters then self.default_params.merge parameters else self.default_params end, session, flash - end - - def post path, parameters = nil, session = nil, flash = nil - super path, if parameters then self.default_params.merge parameters else self.default_params end, session, flash - end - -end - -class ActionController::IntegrationTest - def assert_can_login - assert_tag :tag => 'a', :attributes => { :id => 'link_login' } - end - - def assert_can_signup - assert_tag :tag => 'a', :attributes => { :href => '/account/signup'} - end - - def login(username, password) - ActionController::Integration::Session.any_instance.stubs(:https?).returns(true) - - post '/account/login', :user => { :login => username, :password => password } - assert_response :redirect - follow_redirect! - assert_not_equal '/account/login', path - end - -end - -Profile -- libgit2 0.21.2