Code cleanup.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11438 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-02-18 17:29:31 +00:00
parent 504116bfca
commit 58c643b420
1 changed files with 243 additions and 250 deletions

View File

@ -27,22 +27,23 @@ class ProjectsControllerTest < ActionController::TestCase
Setting.default_language = 'en'
end
def test_index
def test_index_by_anonymous_should_not_show_private_projects
get :index
assert_response :success
assert_template 'index'
assert_not_nil assigns(:projects)
projects = assigns(:projects)
assert_not_nil projects
assert projects.all?(&:is_public?)
assert_tag :ul, :child => {:tag => 'li',
:descendant => {:tag => 'a', :content => 'eCookbook'},
:child => { :tag => 'ul',
:descendant => { :tag => 'a',
:content => 'Child of private child'
}
}
}
assert_no_tag :a, :content => /Private child of eCookbook/
assert_select 'ul' do
assert_select 'li' do
assert_select 'a', :text => 'eCookbook'
assert_select 'ul' do
assert_select 'a', :text => 'Child of private child'
end
end
end
assert_select 'a', :text => /Private child of eCookbook/, :count => 0
end
def test_index_atom
@ -53,90 +54,62 @@ class ProjectsControllerTest < ActionController::TestCase
assert_select 'feed>entry', :count => Project.count(:conditions => Project.visible_condition(User.current))
end
context "#index" do
context "by non-admin user with view_time_entries permission" do
setup do
test "#index by non-admin user with view_time_entries permission should show overall spent time link" do
@request.session[:user_id] = 3
end
should "show overall spent time link" do
get :index
assert_template 'index'
assert_tag :a, :attributes => {:href => '/time_entries'}
end
assert_select 'a[href=?]', '/time_entries'
end
context "by non-admin user without view_time_entries permission" do
setup do
test "#index by non-admin user without view_time_entries permission should not show overall spent time link" do
Role.find(2).remove_permission! :view_time_entries
Role.non_member.remove_permission! :view_time_entries
Role.anonymous.remove_permission! :view_time_entries
@request.session[:user_id] = 3
end
should "not show overall spent time link" do
get :index
assert_template 'index'
assert_no_tag :a, :attributes => {:href => '/time_entries'}
end
end
assert_select 'a[href=?]', '/time_entries', 0
end
context "#new" do
context "by admin user" do
setup do
test "#new by admin user should accept get" do
@request.session[:user_id] = 1
end
should "accept get" do
get :new
assert_response :success
assert_template 'new'
end
end
context "by non-admin user with add_project permission" do
setup do
test "#new by non-admin user with add_project permission should accept get" do
Role.non_member.add_permission! :add_project
@request.session[:user_id] = 9
end
should "accept get" do
get :new
assert_response :success
assert_template 'new'
assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}
end
assert_select 'select[name=?]', 'project[parent_id]', 0
end
context "by non-admin user with add_subprojects permission" do
setup do
test "#new by non-admin user with add_subprojects permission should accept get" do
Role.find(1).remove_permission! :add_project
Role.find(1).add_permission! :add_subprojects
@request.session[:user_id] = 2
end
should "accept get" do
get :new, :parent_id => 'ecookbook'
assert_response :success
assert_template 'new'
assert_select 'select[name=?]', 'project[parent_id]' do
# parent project selected
assert_tag :select, :attributes => {:name => 'project[parent_id]'},
:child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
assert_select 'option[value=1][selected=selected]'
# no empty value
assert_no_tag :select, :attributes => {:name => 'project[parent_id]'},
:child => {:tag => 'option', :attributes => {:value => ''}}
assert_select 'option[value=]', 0
end
end
end
context "POST :create" do
context "by admin user" do
setup do
test "#create by admin user should create a new project" do
@request.session[:user_id] = 1
end
should "create a new project" do
post :create,
:project => {
:name => "blog",
@ -165,7 +138,10 @@ class ProjectsControllerTest < ActionController::TestCase
assert project.issue_custom_fields.include?(IssueCustomField.find(9))
end
should "create a new subproject" do
test "#create by admin user should create a new subproject" do
@request.session[:user_id] = 1
assert_difference 'Project.count' do
post :create, :project => { :name => "blog",
:description => "weblog",
:identifier => "blog",
@ -174,27 +150,26 @@ class ProjectsControllerTest < ActionController::TestCase
:parent_id => 1
}
assert_redirected_to '/projects/blog/settings'
end
project = Project.find_by_name('blog')
assert_kind_of Project, project
assert_equal Project.find(1), project.parent
end
should "continue" do
test "#create by admin user should continue" do
@request.session[:user_id] = 1
assert_difference 'Project.count' do
post :create, :project => {:name => "blog", :identifier => "blog"}, :continue => 'Create and continue'
end
assert_redirected_to '/projects/new'
end
end
context "by non-admin user with add_project permission" do
setup do
test "#create by non-admin user with add_project permission should create a new project" do
Role.non_member.add_permission! :add_project
@request.session[:user_id] = 9
end
should "accept create a Project" do
post :create, :project => { :name => "blog",
:description => "weblog",
:identifier => "blog",
@ -218,7 +193,10 @@ class ProjectsControllerTest < ActionController::TestCase
assert_equal 1, project.members.size
end
should "fail with parent_id" do
test "#create by non-admin user with add_project permission should fail with parent_id" do
Role.non_member.add_permission! :add_project
@request.session[:user_id] = 9
assert_no_difference 'Project.count' do
post :create, :project => { :name => "blog",
:description => "weblog",
@ -233,16 +211,12 @@ class ProjectsControllerTest < ActionController::TestCase
assert_kind_of Project, project
assert_not_nil project.errors[:parent_id]
end
end
context "by non-admin user with add_subprojects permission" do
setup do
test "#create by non-admin user with add_subprojects permission should create a project with a parent_id" do
Role.find(1).remove_permission! :add_project
Role.find(1).add_permission! :add_subprojects
@request.session[:user_id] = 2
end
should "create a project with a parent_id" do
post :create, :project => { :name => "blog",
:description => "weblog",
:identifier => "blog",
@ -254,7 +228,11 @@ class ProjectsControllerTest < ActionController::TestCase
project = Project.find_by_name('blog')
end
should "fail without parent_id" do
test "#create by non-admin user with add_subprojects permission should fail without parent_id" do
Role.find(1).remove_permission! :add_project
Role.find(1).add_permission! :add_subprojects
@request.session[:user_id] = 2
assert_no_difference 'Project.count' do
post :create, :project => { :name => "blog",
:description => "weblog",
@ -269,7 +247,11 @@ class ProjectsControllerTest < ActionController::TestCase
assert_not_nil project.errors[:parent_id]
end
should "fail with unauthorized parent_id" do
test "#create by non-admin user with add_subprojects permission should fail with unauthorized parent_id" do
Role.find(1).remove_permission! :add_project
Role.find(1).add_permission! :add_subprojects
@request.session[:user_id] = 2
assert !User.find(2).member_of?(Project.find(6))
assert_no_difference 'Project.count' do
post :create, :project => { :name => "blog",
@ -285,8 +267,6 @@ class ProjectsControllerTest < ActionController::TestCase
assert_kind_of Project, project
assert_not_nil project.errors[:parent_id]
end
end
end
def test_create_subproject_with_inherit_members_should_inherit_members
Role.find_by_name('Manager').add_permission! :add_subprojects
@ -337,7 +317,7 @@ class ProjectsControllerTest < ActionController::TestCase
assert_not_nil assigns(:project)
assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
assert_tag 'li', :content => /Development status/
assert_select 'li', :text => /Development status/
end
def test_show_should_not_display_hidden_custom_fields
@ -347,7 +327,7 @@ class ProjectsControllerTest < ActionController::TestCase
assert_template 'show'
assert_not_nil assigns(:project)
assert_no_tag 'li', :content => /Development status/
assert_select 'li', :text => /Development status/, :count => 0
end
def test_show_should_not_fail_when_custom_values_are_nil
@ -367,22 +347,22 @@ class ProjectsControllerTest < ActionController::TestCase
get :show, :id => 'ecookbook'
assert_response 403
assert_nil assigns(:project)
assert_tag :tag => 'p', :content => /archived/
assert_select 'p', :text => /archived/
end
def test_private_subprojects_hidden
def test_show_should_not_show_private_subprojects_that_are_not_visible
get :show, :id => 'ecookbook'
assert_response :success
assert_template 'show'
assert_no_tag :tag => 'a', :content => /Private child/
assert_select 'a', :text => /Private child/, :count => 0
end
def test_private_subprojects_visible
def test_show_should_show_private_subprojects_that_are_visible
@request.session[:user_id] = 2 # manager who is a member of the private subproject
get :show, :id => 'ecookbook'
assert_response :success
assert_template 'show'
assert_tag :tag => 'a', :content => /Private child/
assert_select 'a', :text => /Private child/
end
def test_settings
@ -459,22 +439,37 @@ class ProjectsControllerTest < ActionController::TestCase
assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort
end
def test_destroy_without_confirmation
def test_destroy_leaf_project_without_confirmation_should_show_confirmation
@request.session[:user_id] = 1 # admin
assert_no_difference 'Project.count' do
delete :destroy, :id => 2
assert_response :success
assert_template 'destroy'
end
end
def test_destroy_without_confirmation_should_show_confirmation_with_subprojects
@request.session[:user_id] = 1 # admin
assert_no_difference 'Project.count' do
delete :destroy, :id => 1
assert_response :success
assert_template 'destroy'
assert_not_nil Project.find_by_id(1)
assert_tag :tag => 'strong',
:content => ['Private child of eCookbook',
end
assert_select 'strong',
:text => ['Private child of eCookbook',
'Child of private child, eCookbook Subproject 1',
'eCookbook Subproject 2'].join(', ')
end
def test_destroy
def test_destroy_with_confirmation_should_destroy_the_project_and_subprojects
@request.session[:user_id] = 1 # admin
assert_difference 'Project.count', -5 do
delete :destroy, :id => 1, :confirm => 1
assert_redirected_to '/admin/projects'
end
assert_nil Project.find_by_id(1)
end
@ -520,12 +515,11 @@ class ProjectsControllerTest < ActionController::TestCase
CustomField.delete_all
parent = nil
6.times do |i|
p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}")
p.set_parent!(parent)
p = Project.generate_with_parent!(parent)
get :show, :id => p
assert_tag :h1, :parent => { :attributes => {:id => 'header'}},
:children => { :count => [i, 3].min,
:only => { :tag => 'a' } }
assert_select '#header h1' do
assert_select 'a', :count => [i, 3].min
end
parent = p
end
@ -540,8 +534,7 @@ class ProjectsControllerTest < ActionController::TestCase
assert_equal Project.find(1).description, assigns(:project).description
assert_nil assigns(:project).id
assert_tag :tag => 'input',
:attributes => {:name => 'project[enabled_module_names][]', :value => 'issue_tracking'}
assert_select 'input[name=?][value=?]', 'project[enabled_module_names][]', 'issue_tracking', 1
end
def test_get_copy_with_invalid_source_should_respond_with_404