annotate test/functional/my_controller_test.rb @ 1459:cf78a7ade302 luisf

Merge from branch "bug_794"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 11 Nov 2013 18:25:50 +0000
parents 3e4c3460b6ca
children 622f24f53b42
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19 require 'my_controller'
Chris@0 20
Chris@0 21 # Re-raise errors caught by the controller.
Chris@0 22 class MyController; def rescue_action(e) raise e end; end
Chris@0 23
Chris@0 24 class MyControllerTest < ActionController::TestCase
Chris@1115 25 fixtures :users, :user_preferences, :roles, :projects, :members, :member_roles,
Chris@1115 26 :issues, :issue_statuses, :trackers, :enumerations, :custom_fields, :auth_sources
Chris@909 27
Chris@0 28 def setup
Chris@0 29 @controller = MyController.new
Chris@0 30 @request = ActionController::TestRequest.new
Chris@0 31 @request.session[:user_id] = 2
Chris@0 32 @response = ActionController::TestResponse.new
Chris@0 33 end
Chris@0 34
Chris@0 35 def test_index
Chris@0 36 get :index
Chris@0 37 assert_response :success
Chris@0 38 assert_template 'page'
Chris@0 39 end
Chris@909 40
Chris@0 41 def test_page
Chris@0 42 get :page
Chris@0 43 assert_response :success
Chris@0 44 assert_template 'page'
Chris@0 45 end
Chris@909 46
Chris@1115 47 def test_page_with_timelog_block
Chris@1115 48 preferences = User.find(2).pref
Chris@1115 49 preferences[:my_page_layout] = {'top' => ['timelog']}
Chris@1115 50 preferences.save!
Chris@1115 51 TimeEntry.create!(:user => User.find(2), :spent_on => Date.yesterday, :issue_id => 1, :hours => 2.5, :activity_id => 10)
Chris@1115 52
Chris@1115 53 get :page
Chris@1115 54 assert_response :success
Chris@1115 55 assert_select 'tr.time-entry' do
Chris@1115 56 assert_select 'td.subject a[href=/issues/1]'
Chris@1115 57 assert_select 'td.hours', :text => '2.50'
Chris@1115 58 end
Chris@1115 59 end
Chris@1115 60
Chris@0 61 def test_my_account_should_show_editable_custom_fields
Chris@0 62 get :account
Chris@0 63 assert_response :success
Chris@0 64 assert_template 'account'
Chris@0 65 assert_equal User.find(2), assigns(:user)
Chris@909 66
Chris@0 67 assert_tag :input, :attributes => { :name => 'user[custom_field_values][4]'}
Chris@0 68 end
Chris@909 69
Chris@0 70 def test_my_account_should_not_show_non_editable_custom_fields
Chris@0 71 UserCustomField.find(4).update_attribute :editable, false
Chris@909 72
Chris@0 73 get :account
Chris@0 74 assert_response :success
Chris@0 75 assert_template 'account'
Chris@0 76 assert_equal User.find(2), assigns(:user)
Chris@909 77
Chris@0 78 assert_no_tag :input, :attributes => { :name => 'user[custom_field_values][4]'}
Chris@0 79 end
Chris@0 80
Chris@0 81 def test_update_account
Chris@119 82 post :account,
Chris@119 83 :user => {
Chris@119 84 :firstname => "Joe",
Chris@119 85 :login => "root",
Chris@119 86 :admin => 1,
Chris@119 87 :group_ids => ['10'],
Chris@119 88 :custom_field_values => {"4" => "0100562500"}
Chris@119 89 }
Chris@909 90
chris@37 91 assert_redirected_to '/my/account'
Chris@0 92 user = User.find(2)
Chris@0 93 assert_equal user, assigns(:user)
Chris@0 94 assert_equal "Joe", user.firstname
Chris@0 95 assert_equal "jsmith", user.login
Chris@0 96 assert_equal "0100562500", user.custom_value_for(4).value
Chris@119 97 # ignored
Chris@0 98 assert !user.admin?
Chris@119 99 assert user.groups.empty?
Chris@0 100 end
Chris@909 101
Chris@1115 102 def test_my_account_should_show_destroy_link
Chris@1115 103 get :account
Chris@1115 104 assert_select 'a[href=/my/account/destroy]'
Chris@1115 105 end
Chris@1115 106
Chris@1115 107 def test_get_destroy_should_display_the_destroy_confirmation
Chris@1115 108 get :destroy
Chris@1115 109 assert_response :success
Chris@1115 110 assert_template 'destroy'
Chris@1115 111 assert_select 'form[action=/my/account/destroy]' do
Chris@1115 112 assert_select 'input[name=confirm]'
Chris@1115 113 end
Chris@1115 114 end
Chris@1115 115
Chris@1115 116 def test_post_destroy_without_confirmation_should_not_destroy_account
Chris@1115 117 assert_no_difference 'User.count' do
Chris@1115 118 post :destroy
Chris@1115 119 end
Chris@1115 120 assert_response :success
Chris@1115 121 assert_template 'destroy'
Chris@1115 122 end
Chris@1115 123
Chris@1115 124 def test_post_destroy_without_confirmation_should_destroy_account
Chris@1115 125 assert_difference 'User.count', -1 do
Chris@1115 126 post :destroy, :confirm => '1'
Chris@1115 127 end
Chris@1115 128 assert_redirected_to '/'
Chris@1115 129 assert_match /deleted/i, flash[:notice]
Chris@1115 130 end
Chris@1115 131
Chris@1115 132 def test_post_destroy_with_unsubscribe_not_allowed_should_not_destroy_account
Chris@1115 133 User.any_instance.stubs(:own_account_deletable?).returns(false)
Chris@1115 134
Chris@1115 135 assert_no_difference 'User.count' do
Chris@1115 136 post :destroy, :confirm => '1'
Chris@1115 137 end
Chris@1115 138 assert_redirected_to '/my/account'
Chris@1115 139 end
Chris@1115 140
Chris@0 141 def test_change_password
Chris@0 142 get :password
Chris@0 143 assert_response :success
Chris@0 144 assert_template 'password'
Chris@909 145
Chris@0 146 # non matching password confirmation
Chris@909 147 post :password, :password => 'jsmith',
Chris@1115 148 :new_password => 'secret123',
Chris@1115 149 :new_password_confirmation => 'secret1234'
Chris@0 150 assert_response :success
Chris@0 151 assert_template 'password'
Chris@1115 152 assert_error_tag :content => /Password doesn&#x27;t match confirmation/
Chris@909 153
Chris@0 154 # wrong password
Chris@909 155 post :password, :password => 'wrongpassword',
Chris@1115 156 :new_password => 'secret123',
Chris@1115 157 :new_password_confirmation => 'secret123'
Chris@0 158 assert_response :success
Chris@0 159 assert_template 'password'
Chris@0 160 assert_equal 'Wrong password', flash[:error]
Chris@909 161
Chris@0 162 # good password
Chris@0 163 post :password, :password => 'jsmith',
Chris@1115 164 :new_password => 'secret123',
Chris@1115 165 :new_password_confirmation => 'secret123'
chris@37 166 assert_redirected_to '/my/account'
Chris@1115 167 assert User.try_to_login('jsmith', 'secret123')
Chris@1115 168 end
Chris@1115 169
Chris@1115 170 def test_change_password_should_redirect_if_user_cannot_change_its_password
Chris@1115 171 User.find(2).update_attribute(:auth_source_id, 1)
Chris@1115 172
Chris@1115 173 get :password
Chris@1115 174 assert_not_nil flash[:error]
Chris@1115 175 assert_redirected_to '/my/account'
Chris@0 176 end
Chris@909 177
Chris@0 178 def test_page_layout
Chris@0 179 get :page_layout
Chris@0 180 assert_response :success
Chris@0 181 assert_template 'page_layout'
Chris@0 182 end
Chris@909 183
Chris@0 184 def test_add_block
Chris@1115 185 post :add_block, :block => 'issuesreportedbyme'
Chris@1115 186 assert_redirected_to '/my/page_layout'
Chris@0 187 assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme')
Chris@0 188 end
Chris@0 189
Chris@1294 190 def test_add_invalid_block_should_redirect
Chris@1294 191 post :add_block, :block => 'invalid'
Chris@1294 192 assert_redirected_to '/my/page_layout'
Chris@1294 193 end
Chris@1294 194
Chris@0 195 def test_remove_block
Chris@1115 196 post :remove_block, :block => 'issuesassignedtome'
Chris@1115 197 assert_redirected_to '/my/page_layout'
Chris@0 198 assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
Chris@0 199 end
Chris@0 200
Chris@0 201 def test_order_blocks
Chris@1115 202 xhr :post, :order_blocks, :group => 'left', 'blocks' => ['documents', 'calendar', 'latestnews']
Chris@0 203 assert_response :success
Chris@0 204 assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left']
Chris@0 205 end
Chris@0 206
Chris@1115 207 def test_reset_rss_key_with_existing_key
Chris@1115 208 @previous_token_value = User.find(2).rss_key # Will generate one if it's missing
Chris@1115 209 post :reset_rss_key
Chris@0 210
Chris@1115 211 assert_not_equal @previous_token_value, User.find(2).rss_key
Chris@1115 212 assert User.find(2).rss_token
Chris@1115 213 assert_match /reset/, flash[:notice]
Chris@1115 214 assert_redirected_to '/my/account'
Chris@0 215 end
Chris@0 216
Chris@1115 217 def test_reset_rss_key_without_existing_key
Chris@1115 218 assert_nil User.find(2).rss_token
Chris@1115 219 post :reset_rss_key
Chris@0 220
Chris@1115 221 assert User.find(2).rss_token
Chris@1115 222 assert_match /reset/, flash[:notice]
Chris@1115 223 assert_redirected_to '/my/account'
Chris@1115 224 end
Chris@0 225
Chris@1115 226 def test_reset_api_key_with_existing_key
Chris@1115 227 @previous_token_value = User.find(2).api_key # Will generate one if it's missing
Chris@1115 228 post :reset_api_key
Chris@0 229
Chris@1115 230 assert_not_equal @previous_token_value, User.find(2).api_key
Chris@1115 231 assert User.find(2).api_token
Chris@1115 232 assert_match /reset/, flash[:notice]
Chris@1115 233 assert_redirected_to '/my/account'
Chris@1115 234 end
Chris@909 235
Chris@1115 236 def test_reset_api_key_without_existing_key
Chris@1115 237 assert_nil User.find(2).api_token
Chris@1115 238 post :reset_api_key
Chris@0 239
Chris@1115 240 assert User.find(2).api_token
Chris@1115 241 assert_match /reset/, flash[:notice]
Chris@1115 242 assert_redirected_to '/my/account'
Chris@0 243 end
Chris@0 244 end