Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require File.expand_path('../../test_helper', __FILE__) Chris@1296: require 'projects_controller' Chris@1296: Chris@1296: # Re-raise errors caught by the controller. Chris@1296: class ProjectsController; def rescue_action(e) raise e end; end Chris@1296: Chris@1296: class ProjectsControllerTest < ActionController::TestCase Chris@1296: fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, Chris@1296: :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, Chris@1296: :attachments, :custom_fields, :custom_values, :time_entries Chris@1296: Chris@1296: def setup Chris@1296: @controller = ProjectsController.new Chris@1296: @request = ActionController::TestRequest.new Chris@1296: @response = ActionController::TestResponse.new Chris@1296: @request.session[:user_id] = nil Chris@1296: Setting.default_language = 'en' Chris@1296: end Chris@1296: Chris@1296: def test_index Chris@1296: get :index Chris@1296: assert_response :success Chris@1296: assert_template 'index' Chris@1296: assert_not_nil assigns(:projects) Chris@1296: Chris@1296: assert_tag :ul, :child => {:tag => 'li', Chris@1296: :descendant => {:tag => 'a', :content => 'eCookbook'}, Chris@1296: :child => { :tag => 'ul', Chris@1296: :descendant => { :tag => 'a', Chris@1296: :content => 'Child of private child' Chris@1296: } Chris@1296: } Chris@1296: } Chris@1296: Chris@1296: assert_no_tag :a, :content => /Private child of eCookbook/ Chris@1296: end Chris@1296: Chris@1296: def test_index_atom Chris@1296: get :index, :format => 'atom' Chris@1296: assert_response :success Chris@1296: assert_template 'common/feed' Chris@1296: assert_select 'feed>title', :text => 'Redmine: Latest projects' Chris@1296: assert_select 'feed>entry', :count => Project.count(:conditions => Project.visible_condition(User.current)) Chris@1296: end Chris@1296: Chris@1296: context "#index" do Chris@1296: context "by non-admin user with view_time_entries permission" do Chris@1296: setup do Chris@1296: @request.session[:user_id] = 3 Chris@1296: end Chris@1296: should "show overall spent time link" do Chris@1296: get :index Chris@1296: assert_template 'index' Chris@1296: assert_tag :a, :attributes => {:href => '/time_entries'} Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: context "by non-admin user without view_time_entries permission" do Chris@1296: setup do Chris@1296: Role.find(2).remove_permission! :view_time_entries Chris@1296: Role.non_member.remove_permission! :view_time_entries Chris@1296: Role.anonymous.remove_permission! :view_time_entries Chris@1296: @request.session[:user_id] = 3 Chris@1296: end Chris@1296: should "not show overall spent time link" do Chris@1296: get :index Chris@1296: assert_template 'index' Chris@1296: assert_no_tag :a, :attributes => {:href => '/time_entries'} Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: context "#new" do Chris@1296: context "by admin user" do Chris@1296: setup do Chris@1296: @request.session[:user_id] = 1 Chris@1296: end Chris@1296: Chris@1296: should "accept get" do Chris@1296: get :new Chris@1296: assert_response :success Chris@1296: assert_template 'new' Chris@1296: end Chris@1296: Chris@1296: end Chris@1296: Chris@1296: context "by non-admin user with add_project permission" do Chris@1296: setup do Chris@1296: Role.non_member.add_permission! :add_project Chris@1296: @request.session[:user_id] = 9 Chris@1296: end Chris@1296: Chris@1296: should "accept get" do Chris@1296: get :new Chris@1296: assert_response :success Chris@1296: assert_template 'new' Chris@1296: assert_no_tag :select, :attributes => {:name => 'project[parent_id]'} Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: context "by non-admin user with add_subprojects permission" do Chris@1296: setup do Chris@1296: Role.find(1).remove_permission! :add_project Chris@1296: Role.find(1).add_permission! :add_subprojects Chris@1296: @request.session[:user_id] = 2 Chris@1296: end Chris@1296: Chris@1296: should "accept get" do Chris@1296: get :new, :parent_id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'new' Chris@1296: # parent project selected Chris@1296: assert_tag :select, :attributes => {:name => 'project[parent_id]'}, Chris@1296: :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}} Chris@1296: # no empty value Chris@1296: assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}, Chris@1296: :child => {:tag => 'option', :attributes => {:value => ''}} Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: end Chris@1296: Chris@1296: context "POST :create" do Chris@1296: context "by admin user" do Chris@1296: setup do Chris@1296: @request.session[:user_id] = 1 Chris@1296: end Chris@1296: Chris@1296: should "create a new project" do Chris@1296: post :create, Chris@1296: :project => { Chris@1296: :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :homepage => 'http://weblog', Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' }, Chris@1296: :tracker_ids => ['1', '3'], Chris@1296: # an issue custom field that is not for all project Chris@1296: :issue_custom_field_ids => ['9'], Chris@1296: :enabled_module_names => ['issue_tracking', 'news', 'repository'] Chris@1296: } Chris@1296: assert_redirected_to '/projects/blog/settings' Chris@1296: Chris@1296: project = Project.find_by_name('blog') Chris@1296: assert_kind_of Project, project Chris@1296: assert project.active? Chris@1296: assert_equal 'weblog', project.description Chris@1296: assert_equal 'http://weblog', project.homepage Chris@1296: assert_equal true, project.is_public? Chris@1296: assert_nil project.parent Chris@1296: assert_equal 'Beta', project.custom_value_for(3).value Chris@1296: assert_equal [1, 3], project.trackers.map(&:id).sort Chris@1296: assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort Chris@1296: assert project.issue_custom_fields.include?(IssueCustomField.find(9)) Chris@1296: end Chris@1296: Chris@1296: should "create a new subproject" do Chris@1296: post :create, :project => { :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' }, Chris@1296: :parent_id => 1 Chris@1296: } Chris@1296: assert_redirected_to '/projects/blog/settings' Chris@1296: Chris@1296: project = Project.find_by_name('blog') Chris@1296: assert_kind_of Project, project Chris@1296: assert_equal Project.find(1), project.parent Chris@1296: end Chris@1296: Chris@1296: should "continue" do Chris@1296: assert_difference 'Project.count' do Chris@1296: post :create, :project => {:name => "blog", :identifier => "blog"}, :continue => 'Create and continue' Chris@1296: end Chris@1296: assert_redirected_to '/projects/new?' Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: context "by non-admin user with add_project permission" do Chris@1296: setup do Chris@1296: Role.non_member.add_permission! :add_project Chris@1296: @request.session[:user_id] = 9 Chris@1296: end Chris@1296: Chris@1296: should "accept create a Project" do Chris@1296: post :create, :project => { :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' }, Chris@1296: :tracker_ids => ['1', '3'], Chris@1296: :enabled_module_names => ['issue_tracking', 'news', 'repository'] Chris@1296: } Chris@1296: Chris@1296: assert_redirected_to '/projects/blog/settings' Chris@1296: Chris@1296: project = Project.find_by_name('blog') Chris@1296: assert_kind_of Project, project Chris@1296: assert_equal 'weblog', project.description Chris@1296: assert_equal true, project.is_public? Chris@1296: assert_equal [1, 3], project.trackers.map(&:id).sort Chris@1296: assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort Chris@1296: Chris@1296: # User should be added as a project member Chris@1296: assert User.find(9).member_of?(project) Chris@1296: assert_equal 1, project.members.size Chris@1296: end Chris@1296: Chris@1296: should "fail with parent_id" do Chris@1296: assert_no_difference 'Project.count' do Chris@1296: post :create, :project => { :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' }, Chris@1296: :parent_id => 1 Chris@1296: } Chris@1296: end Chris@1296: assert_response :success Chris@1296: project = assigns(:project) Chris@1296: assert_kind_of Project, project Chris@1296: assert_not_nil project.errors[:parent_id] Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: context "by non-admin user with add_subprojects permission" do Chris@1296: setup do Chris@1296: Role.find(1).remove_permission! :add_project Chris@1296: Role.find(1).add_permission! :add_subprojects Chris@1296: @request.session[:user_id] = 2 Chris@1296: end Chris@1296: Chris@1296: should "create a project with a parent_id" do Chris@1296: post :create, :project => { :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' }, Chris@1296: :parent_id => 1 Chris@1296: } Chris@1296: assert_redirected_to '/projects/blog/settings' Chris@1296: project = Project.find_by_name('blog') Chris@1296: end Chris@1296: Chris@1296: should "fail without parent_id" do Chris@1296: assert_no_difference 'Project.count' do Chris@1296: post :create, :project => { :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' } Chris@1296: } Chris@1296: end Chris@1296: assert_response :success Chris@1296: project = assigns(:project) Chris@1296: assert_kind_of Project, project Chris@1296: assert_not_nil project.errors[:parent_id] Chris@1296: end Chris@1296: Chris@1296: should "fail with unauthorized parent_id" do Chris@1296: assert !User.find(2).member_of?(Project.find(6)) Chris@1296: assert_no_difference 'Project.count' do Chris@1296: post :create, :project => { :name => "blog", Chris@1296: :description => "weblog", Chris@1296: :identifier => "blog", Chris@1296: :is_public => 1, Chris@1296: :custom_field_values => { '3' => 'Beta' }, Chris@1296: :parent_id => 6 Chris@1296: } Chris@1296: end Chris@1296: assert_response :success Chris@1296: project = assigns(:project) Chris@1296: assert_kind_of Project, project Chris@1296: assert_not_nil project.errors[:parent_id] Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def test_create_should_preserve_modules_on_validation_failure Chris@1296: with_settings :default_projects_modules => ['issue_tracking', 'repository'] do Chris@1296: @request.session[:user_id] = 1 Chris@1296: assert_no_difference 'Project.count' do Chris@1296: post :create, :project => { Chris@1296: :name => "blog", Chris@1296: :identifier => "", Chris@1296: :enabled_module_names => %w(issue_tracking news) Chris@1296: } Chris@1296: end Chris@1296: assert_response :success Chris@1296: project = assigns(:project) Chris@1296: assert_equal %w(issue_tracking news), project.enabled_module_names.sort Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def test_show_by_id Chris@1296: get :show, :id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: assert_not_nil assigns(:project) Chris@1296: end Chris@1296: Chris@1296: def test_show_by_identifier Chris@1296: get :show, :id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: assert_not_nil assigns(:project) Chris@1296: assert_equal Project.find_by_identifier('ecookbook'), assigns(:project) Chris@1296: Chris@1296: assert_tag 'li', :content => /Development status/ Chris@1296: end Chris@1296: Chris@1296: def test_show_should_not_display_hidden_custom_fields Chris@1296: ProjectCustomField.find_by_name('Development status').update_attribute :visible, false Chris@1296: get :show, :id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: assert_not_nil assigns(:project) Chris@1296: Chris@1296: assert_no_tag 'li', :content => /Development status/ Chris@1296: end Chris@1296: Chris@1296: def test_show_should_not_fail_when_custom_values_are_nil Chris@1296: project = Project.find_by_identifier('ecookbook') Chris@1296: project.custom_values.first.update_attribute(:value, nil) Chris@1296: get :show, :id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: assert_not_nil assigns(:project) Chris@1296: assert_equal Project.find_by_identifier('ecookbook'), assigns(:project) Chris@1296: end Chris@1296: Chris@1296: def show_archived_project_should_be_denied Chris@1296: project = Project.find_by_identifier('ecookbook') Chris@1296: project.archive! Chris@1296: Chris@1296: get :show, :id => 'ecookbook' Chris@1296: assert_response 403 Chris@1296: assert_nil assigns(:project) Chris@1296: assert_tag :tag => 'p', :content => /archived/ Chris@1296: end Chris@1296: Chris@1296: def test_private_subprojects_hidden Chris@1296: get :show, :id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: assert_no_tag :tag => 'a', :content => /Private child/ Chris@1296: end Chris@1296: Chris@1296: def test_private_subprojects_visible Chris@1296: @request.session[:user_id] = 2 # manager who is a member of the private subproject Chris@1296: get :show, :id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: assert_tag :tag => 'a', :content => /Private child/ Chris@1296: end Chris@1296: Chris@1296: def test_settings Chris@1296: @request.session[:user_id] = 2 # manager Chris@1296: get :settings, :id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'settings' Chris@1296: end Chris@1296: Chris@1296: def test_settings_should_be_denied_for_member_on_closed_project Chris@1296: Project.find(1).close Chris@1296: @request.session[:user_id] = 2 # manager Chris@1296: Chris@1296: get :settings, :id => 1 Chris@1296: assert_response 403 Chris@1296: end Chris@1296: Chris@1296: def test_settings_should_be_denied_for_anonymous_on_closed_project Chris@1296: Project.find(1).close Chris@1296: Chris@1296: get :settings, :id => 1 Chris@1296: assert_response 302 Chris@1296: end Chris@1296: Chris@1296: def test_update Chris@1296: @request.session[:user_id] = 2 # manager Chris@1296: post :update, :id => 1, :project => {:name => 'Test changed name', Chris@1296: :issue_custom_field_ids => ['']} Chris@1296: assert_redirected_to '/projects/ecookbook/settings' Chris@1296: project = Project.find(1) Chris@1296: assert_equal 'Test changed name', project.name Chris@1296: end Chris@1296: Chris@1296: def test_update_with_failure Chris@1296: @request.session[:user_id] = 2 # manager Chris@1296: post :update, :id => 1, :project => {:name => ''} Chris@1296: assert_response :success Chris@1296: assert_template 'settings' Chris@1296: assert_error_tag :content => /name can't be blank/i Chris@1296: end Chris@1296: Chris@1296: def test_update_should_be_denied_for_member_on_closed_project Chris@1296: Project.find(1).close Chris@1296: @request.session[:user_id] = 2 # manager Chris@1296: Chris@1296: post :update, :id => 1, :project => {:name => 'Closed'} Chris@1296: assert_response 403 Chris@1296: assert_equal 'eCookbook', Project.find(1).name Chris@1296: end Chris@1296: Chris@1296: def test_update_should_be_denied_for_anonymous_on_closed_project Chris@1296: Project.find(1).close Chris@1296: Chris@1296: post :update, :id => 1, :project => {:name => 'Closed'} Chris@1296: assert_response 302 Chris@1296: assert_equal 'eCookbook', Project.find(1).name Chris@1296: end Chris@1296: Chris@1296: def test_modules Chris@1296: @request.session[:user_id] = 2 Chris@1296: Project.find(1).enabled_module_names = ['issue_tracking', 'news'] Chris@1296: Chris@1296: post :modules, :id => 1, :enabled_module_names => ['issue_tracking', 'repository', 'documents'] Chris@1296: assert_redirected_to '/projects/ecookbook/settings/modules' Chris@1296: assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort Chris@1296: end Chris@1296: Chris@1296: def test_destroy_without_confirmation Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: delete :destroy, :id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'destroy' Chris@1296: assert_not_nil Project.find_by_id(1) Chris@1296: assert_tag :tag => 'strong', Chris@1296: :content => ['Private child of eCookbook', Chris@1296: 'Child of private child, eCookbook Subproject 1', Chris@1296: 'eCookbook Subproject 2'].join(', ') Chris@1296: end Chris@1296: Chris@1296: def test_destroy Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: delete :destroy, :id => 1, :confirm => 1 Chris@1296: assert_redirected_to '/admin/projects' Chris@1296: assert_nil Project.find_by_id(1) Chris@1296: end Chris@1296: Chris@1296: def test_archive Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: post :archive, :id => 1 Chris@1296: assert_redirected_to '/admin/projects' Chris@1296: assert !Project.find(1).active? Chris@1296: end Chris@1296: Chris@1296: def test_archive_with_failure Chris@1296: @request.session[:user_id] = 1 Chris@1296: Project.any_instance.stubs(:archive).returns(false) Chris@1296: post :archive, :id => 1 Chris@1296: assert_redirected_to '/admin/projects' Chris@1296: assert_match /project cannot be archived/i, flash[:error] Chris@1296: end Chris@1296: Chris@1296: def test_unarchive Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: Project.find(1).archive Chris@1296: post :unarchive, :id => 1 Chris@1296: assert_redirected_to '/admin/projects' Chris@1296: assert Project.find(1).active? Chris@1296: end Chris@1296: Chris@1296: def test_close Chris@1296: @request.session[:user_id] = 2 Chris@1296: post :close, :id => 1 Chris@1296: assert_redirected_to '/projects/ecookbook' Chris@1296: assert_equal Project::STATUS_CLOSED, Project.find(1).status Chris@1296: end Chris@1296: Chris@1296: def test_reopen Chris@1296: Project.find(1).close Chris@1296: @request.session[:user_id] = 2 Chris@1296: post :reopen, :id => 1 Chris@1296: assert_redirected_to '/projects/ecookbook' Chris@1296: assert Project.find(1).active? Chris@1296: end Chris@1296: Chris@1296: def test_project_breadcrumbs_should_be_limited_to_3_ancestors Chris@1296: CustomField.delete_all Chris@1296: parent = nil Chris@1296: 6.times do |i| Chris@1296: p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}") Chris@1296: p.set_parent!(parent) Chris@1296: get :show, :id => p Chris@1296: assert_tag :h1, :parent => { :attributes => {:id => 'header'}}, Chris@1296: :children => { :count => [i, 3].min, Chris@1296: :only => { :tag => 'a' } } Chris@1296: Chris@1296: parent = p Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def test_get_copy Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: get :copy, :id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'copy' Chris@1296: assert assigns(:project) Chris@1296: assert_equal Project.find(1).description, assigns(:project).description Chris@1296: assert_nil assigns(:project).id Chris@1296: Chris@1296: assert_tag :tag => 'input', Chris@1296: :attributes => {:name => 'project[enabled_module_names][]', :value => 'issue_tracking'} Chris@1296: end Chris@1296: Chris@1296: def test_get_copy_with_invalid_source_should_respond_with_404 Chris@1296: @request.session[:user_id] = 1 Chris@1296: get :copy, :id => 99 Chris@1296: assert_response 404 Chris@1296: end Chris@1296: Chris@1296: def test_post_copy_should_copy_requested_items Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: CustomField.delete_all Chris@1296: Chris@1296: assert_difference 'Project.count' do Chris@1296: post :copy, :id => 1, Chris@1296: :project => { Chris@1296: :name => 'Copy', Chris@1296: :identifier => 'unique-copy', Chris@1296: :tracker_ids => ['1', '2', '3', ''], Chris@1296: :enabled_module_names => %w(issue_tracking time_tracking) Chris@1296: }, Chris@1296: :only => %w(issues versions) Chris@1296: end Chris@1296: project = Project.find('unique-copy') Chris@1296: source = Project.find(1) Chris@1296: assert_equal %w(issue_tracking time_tracking), project.enabled_module_names.sort Chris@1296: Chris@1296: assert_equal source.versions.count, project.versions.count, "All versions were not copied" Chris@1296: assert_equal source.issues.count, project.issues.count, "All issues were not copied" Chris@1296: assert_equal 0, project.members.count Chris@1296: end Chris@1296: Chris@1296: def test_post_copy_should_redirect_to_settings_when_successful Chris@1296: @request.session[:user_id] = 1 # admin Chris@1296: post :copy, :id => 1, :project => {:name => 'Copy', :identifier => 'unique-copy'} Chris@1296: assert_response :redirect Chris@1296: assert_redirected_to :controller => 'projects', :action => 'settings', :id => 'unique-copy' Chris@1296: end Chris@1296: Chris@1296: def test_jump_should_redirect_to_active_tab Chris@1296: get :show, :id => 1, :jump => 'issues' Chris@1296: assert_redirected_to '/projects/ecookbook/issues' Chris@1296: end Chris@1296: Chris@1296: def test_jump_should_not_redirect_to_inactive_tab Chris@1296: get :show, :id => 3, :jump => 'documents' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: end Chris@1296: Chris@1296: def test_jump_should_not_redirect_to_unknown_tab Chris@1296: get :show, :id => 3, :jump => 'foobar' Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: end Chris@1296: end