Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require File.expand_path('../../test_helper', __FILE__) Chris@1494: Chris@1494: class MyControllerTest < ActionController::TestCase Chris@1494: fixtures :users, :user_preferences, :roles, :projects, :members, :member_roles, Chris@1494: :issues, :issue_statuses, :trackers, :enumerations, :custom_fields, :auth_sources Chris@1494: Chris@1494: def setup Chris@1494: @request.session[:user_id] = 2 Chris@1494: end Chris@1494: Chris@1494: def test_index Chris@1494: get :index Chris@1494: assert_response :success Chris@1494: assert_template 'page' Chris@1494: end Chris@1494: Chris@1494: def test_page Chris@1494: get :page Chris@1494: assert_response :success Chris@1494: assert_template 'page' Chris@1494: end Chris@1494: Chris@1494: def test_page_with_timelog_block Chris@1494: preferences = User.find(2).pref Chris@1494: preferences[:my_page_layout] = {'top' => ['timelog']} Chris@1494: preferences.save! Chris@1494: TimeEntry.create!(:user => User.find(2), :spent_on => Date.yesterday, :issue_id => 1, :hours => 2.5, :activity_id => 10) Chris@1494: Chris@1494: get :page Chris@1494: assert_response :success Chris@1494: assert_select 'tr.time-entry' do Chris@1494: assert_select 'td.subject a[href=/issues/1]' Chris@1494: assert_select 'td.hours', :text => '2.50' Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def test_page_with_all_blocks Chris@1494: blocks = MyController::BLOCKS.keys Chris@1494: preferences = User.find(2).pref Chris@1494: preferences[:my_page_layout] = {'top' => blocks} Chris@1494: preferences.save! Chris@1494: Chris@1494: get :page Chris@1494: assert_response :success Chris@1494: assert_select 'div.mypage-box', blocks.size Chris@1494: end Chris@1494: Chris@1494: def test_my_account_should_show_editable_custom_fields Chris@1494: get :account Chris@1494: assert_response :success Chris@1494: assert_template 'account' Chris@1494: assert_equal User.find(2), assigns(:user) Chris@1494: Chris@1494: assert_tag :input, :attributes => { :name => 'user[custom_field_values][4]'} Chris@1494: end Chris@1494: Chris@1494: def test_my_account_should_not_show_non_editable_custom_fields Chris@1494: UserCustomField.find(4).update_attribute :editable, false Chris@1494: Chris@1494: get :account Chris@1494: assert_response :success Chris@1494: assert_template 'account' Chris@1494: assert_equal User.find(2), assigns(:user) Chris@1494: Chris@1494: assert_no_tag :input, :attributes => { :name => 'user[custom_field_values][4]'} Chris@1494: end Chris@1494: Chris@1494: def test_update_account Chris@1494: post :account, Chris@1494: :user => { Chris@1494: :firstname => "Joe", Chris@1494: :login => "root", Chris@1494: :admin => 1, Chris@1494: :group_ids => ['10'], Chris@1494: :custom_field_values => {"4" => "0100562500"} Chris@1494: } Chris@1494: Chris@1494: assert_redirected_to '/my/account' Chris@1494: user = User.find(2) Chris@1494: assert_equal user, assigns(:user) Chris@1494: assert_equal "Joe", user.firstname Chris@1494: assert_equal "jsmith", user.login Chris@1494: assert_equal "0100562500", user.custom_value_for(4).value Chris@1494: # ignored Chris@1494: assert !user.admin? Chris@1494: assert user.groups.empty? Chris@1494: end Chris@1494: Chris@1494: def test_my_account_should_show_destroy_link Chris@1494: get :account Chris@1494: assert_select 'a[href=/my/account/destroy]' Chris@1494: end Chris@1494: Chris@1494: def test_get_destroy_should_display_the_destroy_confirmation Chris@1494: get :destroy Chris@1494: assert_response :success Chris@1494: assert_template 'destroy' Chris@1494: assert_select 'form[action=/my/account/destroy]' do Chris@1494: assert_select 'input[name=confirm]' Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def test_post_destroy_without_confirmation_should_not_destroy_account Chris@1494: assert_no_difference 'User.count' do Chris@1494: post :destroy Chris@1494: end Chris@1494: assert_response :success Chris@1494: assert_template 'destroy' Chris@1494: end Chris@1494: Chris@1494: def test_post_destroy_without_confirmation_should_destroy_account Chris@1494: assert_difference 'User.count', -1 do Chris@1494: post :destroy, :confirm => '1' Chris@1494: end Chris@1494: assert_redirected_to '/' Chris@1494: assert_match /deleted/i, flash[:notice] Chris@1494: end Chris@1494: Chris@1494: def test_post_destroy_with_unsubscribe_not_allowed_should_not_destroy_account Chris@1494: User.any_instance.stubs(:own_account_deletable?).returns(false) Chris@1494: Chris@1494: assert_no_difference 'User.count' do Chris@1494: post :destroy, :confirm => '1' Chris@1494: end Chris@1494: assert_redirected_to '/my/account' Chris@1494: end Chris@1494: Chris@1494: def test_change_password Chris@1494: get :password Chris@1494: assert_response :success Chris@1494: assert_template 'password' Chris@1494: Chris@1494: # non matching password confirmation Chris@1494: post :password, :password => 'jsmith', Chris@1494: :new_password => 'secret123', Chris@1494: :new_password_confirmation => 'secret1234' Chris@1494: assert_response :success Chris@1494: assert_template 'password' Chris@1494: assert_error_tag :content => /Password doesn't match confirmation/ Chris@1494: Chris@1494: # wrong password Chris@1494: post :password, :password => 'wrongpassword', Chris@1494: :new_password => 'secret123', Chris@1494: :new_password_confirmation => 'secret123' Chris@1494: assert_response :success Chris@1494: assert_template 'password' Chris@1494: assert_equal 'Wrong password', flash[:error] Chris@1494: Chris@1494: # good password Chris@1494: post :password, :password => 'jsmith', Chris@1494: :new_password => 'secret123', Chris@1494: :new_password_confirmation => 'secret123' Chris@1494: assert_redirected_to '/my/account' Chris@1494: assert User.try_to_login('jsmith', 'secret123') Chris@1494: end Chris@1494: Chris@1494: def test_change_password_should_redirect_if_user_cannot_change_its_password Chris@1494: User.find(2).update_attribute(:auth_source_id, 1) Chris@1494: Chris@1494: get :password Chris@1494: assert_not_nil flash[:error] Chris@1494: assert_redirected_to '/my/account' Chris@1494: end Chris@1494: Chris@1494: def test_page_layout Chris@1494: get :page_layout Chris@1494: assert_response :success Chris@1494: assert_template 'page_layout' Chris@1494: end Chris@1494: Chris@1494: def test_add_block Chris@1494: post :add_block, :block => 'issuesreportedbyme' Chris@1494: assert_redirected_to '/my/page_layout' Chris@1494: assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme') Chris@1494: end Chris@1494: Chris@1494: def test_add_invalid_block_should_redirect Chris@1494: post :add_block, :block => 'invalid' Chris@1494: assert_redirected_to '/my/page_layout' Chris@1494: end Chris@1494: Chris@1494: def test_remove_block Chris@1494: post :remove_block, :block => 'issuesassignedtome' Chris@1494: assert_redirected_to '/my/page_layout' Chris@1494: assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome') Chris@1494: end Chris@1494: Chris@1494: def test_order_blocks Chris@1494: xhr :post, :order_blocks, :group => 'left', 'blocks' => ['documents', 'calendar', 'latestnews'] Chris@1494: assert_response :success Chris@1494: assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left'] Chris@1494: end Chris@1494: Chris@1494: def test_reset_rss_key_with_existing_key Chris@1494: @previous_token_value = User.find(2).rss_key # Will generate one if it's missing Chris@1494: post :reset_rss_key Chris@1494: Chris@1494: assert_not_equal @previous_token_value, User.find(2).rss_key Chris@1494: assert User.find(2).rss_token Chris@1494: assert_match /reset/, flash[:notice] Chris@1494: assert_redirected_to '/my/account' Chris@1494: end Chris@1494: Chris@1494: def test_reset_rss_key_without_existing_key Chris@1494: assert_nil User.find(2).rss_token Chris@1494: post :reset_rss_key Chris@1494: Chris@1494: assert User.find(2).rss_token Chris@1494: assert_match /reset/, flash[:notice] Chris@1494: assert_redirected_to '/my/account' Chris@1494: end Chris@1494: Chris@1494: def test_reset_api_key_with_existing_key Chris@1494: @previous_token_value = User.find(2).api_key # Will generate one if it's missing Chris@1494: post :reset_api_key Chris@1494: Chris@1494: assert_not_equal @previous_token_value, User.find(2).api_key Chris@1494: assert User.find(2).api_token Chris@1494: assert_match /reset/, flash[:notice] Chris@1494: assert_redirected_to '/my/account' Chris@1494: end Chris@1494: Chris@1494: def test_reset_api_key_without_existing_key Chris@1494: assert_nil User.find(2).api_token Chris@1494: post :reset_api_key Chris@1494: Chris@1494: assert User.find(2).api_token Chris@1494: assert_match /reset/, flash[:notice] Chris@1494: assert_redirected_to '/my/account' Chris@1494: end Chris@1494: end