Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require File.expand_path('../../test_helper', __FILE__) Chris@909: Chris@909: class GroupsControllerTest < ActionController::TestCase Chris@909: fixtures :projects, :users, :members, :member_roles, :groups_users Chris@909: Chris@909: def setup Chris@909: @request.session[:user_id] = 1 Chris@909: end Chris@909: Chris@909: def test_index Chris@909: get :index Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: end Chris@909: Chris@909: def test_show Chris@909: get :show, :id => 10 Chris@909: assert_response :success Chris@909: assert_template 'show' Chris@909: end Chris@909: Chris@909: def test_new Chris@909: get :new Chris@909: assert_response :success Chris@909: assert_template 'new' Chris@909: end Chris@909: Chris@909: def test_create Chris@909: assert_difference 'Group.count' do Chris@909: post :create, :group => {:lastname => 'New group'} Chris@909: end Chris@909: assert_redirected_to '/groups' Chris@909: group = Group.first(:order => 'id DESC') Chris@909: assert_equal 'New group', group.name Chris@909: assert_equal [], group.users Chris@909: end Chris@909: Chris@909: def test_create_and_continue Chris@909: assert_difference 'Group.count' do Chris@909: post :create, :group => {:lastname => 'New group'}, :continue => 'Create and continue' Chris@909: end Chris@909: assert_redirected_to '/groups/new' Chris@909: group = Group.first(:order => 'id DESC') Chris@909: assert_equal 'New group', group.name Chris@909: end Chris@909: Chris@909: def test_create_with_failure Chris@909: assert_no_difference 'Group.count' do Chris@909: post :create, :group => {:lastname => ''} Chris@909: end Chris@909: assert_response :success Chris@909: assert_template 'new' Chris@909: end Chris@909: Chris@909: def test_edit Chris@909: get :edit, :id => 10 Chris@909: assert_response :success Chris@909: assert_template 'edit' Chris@909: assert_tag 'div', :attributes => {:id => 'tab-content-users'} Chris@909: assert_tag 'div', :attributes => {:id => 'tab-content-memberships'} Chris@909: end Chris@909: Chris@909: def test_update Chris@909: new_name = 'New name' Chris@909: put :update, :id => 10, :group => {:lastname => new_name} Chris@909: assert_redirected_to '/groups' Chris@909: group = Group.find(10) Chris@909: assert_equal new_name, group.name Chris@909: end Chris@909: Chris@909: def test_update_with_failure Chris@909: put :update, :id => 10, :group => {:lastname => ''} Chris@909: assert_response :success Chris@909: assert_template 'edit' Chris@909: end Chris@909: Chris@909: def test_destroy Chris@909: assert_difference 'Group.count', -1 do Chris@909: post :destroy, :id => 10 Chris@909: end Chris@909: assert_redirected_to '/groups' Chris@909: end Chris@909: Chris@909: def test_add_users Chris@909: assert_difference 'Group.find(10).users.count', 2 do Chris@909: post :add_users, :id => 10, :user_ids => ['2', '3'] Chris@909: end Chris@909: end Chris@909: Chris@909: def test_xhr_add_users Chris@909: assert_difference 'Group.find(10).users.count', 2 do Chris@909: xhr :post, :add_users, :id => 10, :user_ids => ['2', '3'] Chris@909: end Chris@909: assert_select_rjs :replace_html, 'tab-content-users' Chris@909: end Chris@909: Chris@909: def test_remove_user Chris@909: assert_difference 'Group.find(10).users.count', -1 do Chris@909: delete :remove_user, :id => 10, :user_id => '8' Chris@909: end Chris@909: end Chris@909: Chris@909: def test_xhr_remove_user Chris@909: assert_difference 'Group.find(10).users.count', -1 do Chris@909: xhr :delete, :remove_user, :id => 10, :user_id => '8' Chris@909: end Chris@909: assert_select_rjs :replace_html, 'tab-content-users' Chris@909: end Chris@909: Chris@909: def test_new_membership Chris@909: assert_difference 'Group.find(10).members.count' do Chris@909: post :edit_membership, :id => 10, :membership => { :project_id => 2, :role_ids => ['1', '2']} Chris@909: end Chris@909: end Chris@909: Chris@909: def test_xhr_new_membership Chris@909: assert_difference 'Group.find(10).members.count' do Chris@909: xhr :post, :edit_membership, :id => 10, :membership => { :project_id => 2, :role_ids => ['1', '2']} Chris@909: end Chris@909: assert_select_rjs :replace_html, 'tab-content-memberships' Chris@909: end Chris@909: Chris@909: def test_xhr_new_membership_with_failure Chris@909: assert_no_difference 'Group.find(10).members.count' do Chris@909: xhr :post, :edit_membership, :id => 10, :membership => { :project_id => 999, :role_ids => ['1', '2']} Chris@909: end Chris@909: assert @response.body.match(/alert/i), "Alert message not sent" Chris@909: end Chris@909: Chris@909: def test_edit_membership Chris@909: assert_no_difference 'Group.find(10).members.count' do Chris@909: post :edit_membership, :id => 10, :membership_id => 6, :membership => { :role_ids => ['1', '3']} Chris@909: end Chris@909: end Chris@909: Chris@909: def test_destroy_membership Chris@909: assert_difference 'Group.find(10).members.count', -1 do Chris@909: post :destroy_membership, :id => 10, :membership_id => 6 Chris@909: end Chris@909: end Chris@909: Chris@909: def test_xhr_destroy_membership Chris@909: assert_difference 'Group.find(10).members.count', -1 do Chris@909: xhr :post, :destroy_membership, :id => 10, :membership_id => 6 Chris@909: end Chris@909: assert_select_rjs :replace_html, 'tab-content-memberships' Chris@909: end Chris@909: Chris@909: def test_autocomplete_for_user Chris@909: get :autocomplete_for_user, :id => 10, :q => 'mis' Chris@909: assert_response :success Chris@909: users = assigns(:users) Chris@909: assert_not_nil users Chris@909: assert users.any? Chris@909: assert !users.include?(Group.find(10).users.first) Chris@909: end Chris@909: end