##// END OF EJS Templates
Merged r11225 from trunk (#12838)....
Jean-Philippe Lang -
r11002:98eb2edd2550
parent child
Show More
@@ -1,197 +1,198
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class MyController < ApplicationController
19 19 before_filter :require_login
20 20
21 21 helper :issues
22 22 helper :users
23 23 helper :custom_fields
24 24
25 25 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
26 26 'issuesreportedbyme' => :label_reported_issues,
27 27 'issueswatched' => :label_watched_issues,
28 28 'news' => :label_news_latest,
29 29 'calendar' => :label_calendar,
30 30 'documents' => :label_document_plural,
31 31 'timelog' => :label_spent_time
32 32 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
33 33
34 34 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
35 35 'right' => ['issuesreportedbyme']
36 36 }.freeze
37 37
38 38 def index
39 39 page
40 40 render :action => 'page'
41 41 end
42 42
43 43 # Show user's page
44 44 def page
45 45 @user = User.current
46 46 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
47 47 end
48 48
49 49 # Edit user's account
50 50 def account
51 51 @user = User.current
52 52 @pref = @user.pref
53 53 if request.post?
54 54 @user.safe_attributes = params[:user]
55 55 @user.pref.attributes = params[:pref]
56 56 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
57 57 if @user.save
58 58 @user.pref.save
59 59 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
60 60 set_language_if_valid @user.language
61 61 flash[:notice] = l(:notice_account_updated)
62 62 redirect_to :action => 'account'
63 63 return
64 64 end
65 65 end
66 66 end
67 67
68 68 # Destroys user's account
69 69 def destroy
70 70 @user = User.current
71 71 unless @user.own_account_deletable?
72 72 redirect_to :action => 'account'
73 73 return
74 74 end
75 75
76 76 if request.post? && params[:confirm]
77 77 @user.destroy
78 78 if @user.destroyed?
79 79 logout_user
80 80 flash[:notice] = l(:notice_account_deleted)
81 81 end
82 82 redirect_to home_path
83 83 end
84 84 end
85 85
86 86 # Manage user's password
87 87 def password
88 88 @user = User.current
89 89 unless @user.change_password_allowed?
90 90 flash[:error] = l(:notice_can_t_change_password)
91 91 redirect_to :action => 'account'
92 92 return
93 93 end
94 94 if request.post?
95 95 if @user.check_password?(params[:password])
96 96 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
97 97 if @user.save
98 98 flash[:notice] = l(:notice_account_password_updated)
99 99 redirect_to :action => 'account'
100 100 end
101 101 else
102 102 flash[:error] = l(:notice_account_wrong_password)
103 103 end
104 104 end
105 105 end
106 106
107 107 # Create a new feeds key
108 108 def reset_rss_key
109 109 if request.post?
110 110 if User.current.rss_token
111 111 User.current.rss_token.destroy
112 112 User.current.reload
113 113 end
114 114 User.current.rss_key
115 115 flash[:notice] = l(:notice_feeds_access_key_reseted)
116 116 end
117 117 redirect_to :action => 'account'
118 118 end
119 119
120 120 # Create a new API key
121 121 def reset_api_key
122 122 if request.post?
123 123 if User.current.api_token
124 124 User.current.api_token.destroy
125 125 User.current.reload
126 126 end
127 127 User.current.api_key
128 128 flash[:notice] = l(:notice_api_access_key_reseted)
129 129 end
130 130 redirect_to :action => 'account'
131 131 end
132 132
133 133 # User's page layout configuration
134 134 def page_layout
135 135 @user = User.current
136 136 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
137 137 @block_options = []
138 138 BLOCKS.each do |k, v|
139 139 unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)}
140 140 @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
141 141 end
142 142 end
143 143 end
144 144
145 145 # Add a block to user's page
146 146 # The block is added on top of the page
147 147 # params[:block] : id of the block to add
148 148 def add_block
149 149 block = params[:block].to_s.underscore
150 (render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
151 @user = User.current
152 layout = @user.pref[:my_page_layout] || {}
153 # remove if already present in a group
154 %w(top left right).each {|f| (layout[f] ||= []).delete block }
155 # add it on top
156 layout['top'].unshift block
157 @user.pref[:my_page_layout] = layout
158 @user.pref.save
150 if block.present? && BLOCKS.key?(block)
151 @user = User.current
152 layout = @user.pref[:my_page_layout] || {}
153 # remove if already present in a group
154 %w(top left right).each {|f| (layout[f] ||= []).delete block }
155 # add it on top
156 layout['top'].unshift block
157 @user.pref[:my_page_layout] = layout
158 @user.pref.save
159 end
159 160 redirect_to :action => 'page_layout'
160 161 end
161 162
162 163 # Remove a block to user's page
163 164 # params[:block] : id of the block to remove
164 165 def remove_block
165 166 block = params[:block].to_s.underscore
166 167 @user = User.current
167 168 # remove block in all groups
168 169 layout = @user.pref[:my_page_layout] || {}
169 170 %w(top left right).each {|f| (layout[f] ||= []).delete block }
170 171 @user.pref[:my_page_layout] = layout
171 172 @user.pref.save
172 173 redirect_to :action => 'page_layout'
173 174 end
174 175
175 176 # Change blocks order on user's page
176 177 # params[:group] : group to order (top, left or right)
177 178 # params[:list-(top|left|right)] : array of block ids of the group
178 179 def order_blocks
179 180 group = params[:group]
180 181 @user = User.current
181 182 if group.is_a?(String)
182 183 group_items = (params["blocks"] || []).collect(&:underscore)
183 184 group_items.each {|s| s.sub!(/^block_/, '')}
184 185 if group_items and group_items.is_a? Array
185 186 layout = @user.pref[:my_page_layout] || {}
186 187 # remove group blocks if they are presents in other groups
187 188 %w(top left right).each {|f|
188 189 layout[f] = (layout[f] || []) - group_items
189 190 }
190 191 layout[group] = group_items
191 192 @user.pref[:my_page_layout] = layout
192 193 @user.pref.save
193 194 end
194 195 end
195 196 render :nothing => true
196 197 end
197 198 end
@@ -1,239 +1,244
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19 require 'my_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class MyController; def rescue_action(e) raise e end; end
23 23
24 24 class MyControllerTest < ActionController::TestCase
25 25 fixtures :users, :user_preferences, :roles, :projects, :members, :member_roles,
26 26 :issues, :issue_statuses, :trackers, :enumerations, :custom_fields, :auth_sources
27 27
28 28 def setup
29 29 @controller = MyController.new
30 30 @request = ActionController::TestRequest.new
31 31 @request.session[:user_id] = 2
32 32 @response = ActionController::TestResponse.new
33 33 end
34 34
35 35 def test_index
36 36 get :index
37 37 assert_response :success
38 38 assert_template 'page'
39 39 end
40 40
41 41 def test_page
42 42 get :page
43 43 assert_response :success
44 44 assert_template 'page'
45 45 end
46 46
47 47 def test_page_with_timelog_block
48 48 preferences = User.find(2).pref
49 49 preferences[:my_page_layout] = {'top' => ['timelog']}
50 50 preferences.save!
51 51 TimeEntry.create!(:user => User.find(2), :spent_on => Date.yesterday, :issue_id => 1, :hours => 2.5, :activity_id => 10)
52 52
53 53 get :page
54 54 assert_response :success
55 55 assert_select 'tr.time-entry' do
56 56 assert_select 'td.subject a[href=/issues/1]'
57 57 assert_select 'td.hours', :text => '2.50'
58 58 end
59 59 end
60 60
61 61 def test_my_account_should_show_editable_custom_fields
62 62 get :account
63 63 assert_response :success
64 64 assert_template 'account'
65 65 assert_equal User.find(2), assigns(:user)
66 66
67 67 assert_tag :input, :attributes => { :name => 'user[custom_field_values][4]'}
68 68 end
69 69
70 70 def test_my_account_should_not_show_non_editable_custom_fields
71 71 UserCustomField.find(4).update_attribute :editable, false
72 72
73 73 get :account
74 74 assert_response :success
75 75 assert_template 'account'
76 76 assert_equal User.find(2), assigns(:user)
77 77
78 78 assert_no_tag :input, :attributes => { :name => 'user[custom_field_values][4]'}
79 79 end
80 80
81 81 def test_update_account
82 82 post :account,
83 83 :user => {
84 84 :firstname => "Joe",
85 85 :login => "root",
86 86 :admin => 1,
87 87 :group_ids => ['10'],
88 88 :custom_field_values => {"4" => "0100562500"}
89 89 }
90 90
91 91 assert_redirected_to '/my/account'
92 92 user = User.find(2)
93 93 assert_equal user, assigns(:user)
94 94 assert_equal "Joe", user.firstname
95 95 assert_equal "jsmith", user.login
96 96 assert_equal "0100562500", user.custom_value_for(4).value
97 97 # ignored
98 98 assert !user.admin?
99 99 assert user.groups.empty?
100 100 end
101 101
102 102 def test_my_account_should_show_destroy_link
103 103 get :account
104 104 assert_select 'a[href=/my/account/destroy]'
105 105 end
106 106
107 107 def test_get_destroy_should_display_the_destroy_confirmation
108 108 get :destroy
109 109 assert_response :success
110 110 assert_template 'destroy'
111 111 assert_select 'form[action=/my/account/destroy]' do
112 112 assert_select 'input[name=confirm]'
113 113 end
114 114 end
115 115
116 116 def test_post_destroy_without_confirmation_should_not_destroy_account
117 117 assert_no_difference 'User.count' do
118 118 post :destroy
119 119 end
120 120 assert_response :success
121 121 assert_template 'destroy'
122 122 end
123 123
124 124 def test_post_destroy_without_confirmation_should_destroy_account
125 125 assert_difference 'User.count', -1 do
126 126 post :destroy, :confirm => '1'
127 127 end
128 128 assert_redirected_to '/'
129 129 assert_match /deleted/i, flash[:notice]
130 130 end
131 131
132 132 def test_post_destroy_with_unsubscribe_not_allowed_should_not_destroy_account
133 133 User.any_instance.stubs(:own_account_deletable?).returns(false)
134 134
135 135 assert_no_difference 'User.count' do
136 136 post :destroy, :confirm => '1'
137 137 end
138 138 assert_redirected_to '/my/account'
139 139 end
140 140
141 141 def test_change_password
142 142 get :password
143 143 assert_response :success
144 144 assert_template 'password'
145 145
146 146 # non matching password confirmation
147 147 post :password, :password => 'jsmith',
148 148 :new_password => 'secret123',
149 149 :new_password_confirmation => 'secret1234'
150 150 assert_response :success
151 151 assert_template 'password'
152 152 assert_error_tag :content => /Password doesn&#x27;t match confirmation/
153 153
154 154 # wrong password
155 155 post :password, :password => 'wrongpassword',
156 156 :new_password => 'secret123',
157 157 :new_password_confirmation => 'secret123'
158 158 assert_response :success
159 159 assert_template 'password'
160 160 assert_equal 'Wrong password', flash[:error]
161 161
162 162 # good password
163 163 post :password, :password => 'jsmith',
164 164 :new_password => 'secret123',
165 165 :new_password_confirmation => 'secret123'
166 166 assert_redirected_to '/my/account'
167 167 assert User.try_to_login('jsmith', 'secret123')
168 168 end
169 169
170 170 def test_change_password_should_redirect_if_user_cannot_change_its_password
171 171 User.find(2).update_attribute(:auth_source_id, 1)
172 172
173 173 get :password
174 174 assert_not_nil flash[:error]
175 175 assert_redirected_to '/my/account'
176 176 end
177 177
178 178 def test_page_layout
179 179 get :page_layout
180 180 assert_response :success
181 181 assert_template 'page_layout'
182 182 end
183 183
184 184 def test_add_block
185 185 post :add_block, :block => 'issuesreportedbyme'
186 186 assert_redirected_to '/my/page_layout'
187 187 assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme')
188 188 end
189 189
190 def test_add_invalid_block_should_redirect
191 post :add_block, :block => 'invalid'
192 assert_redirected_to '/my/page_layout'
193 end
194
190 195 def test_remove_block
191 196 post :remove_block, :block => 'issuesassignedtome'
192 197 assert_redirected_to '/my/page_layout'
193 198 assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
194 199 end
195 200
196 201 def test_order_blocks
197 202 xhr :post, :order_blocks, :group => 'left', 'blocks' => ['documents', 'calendar', 'latestnews']
198 203 assert_response :success
199 204 assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left']
200 205 end
201 206
202 207 def test_reset_rss_key_with_existing_key
203 208 @previous_token_value = User.find(2).rss_key # Will generate one if it's missing
204 209 post :reset_rss_key
205 210
206 211 assert_not_equal @previous_token_value, User.find(2).rss_key
207 212 assert User.find(2).rss_token
208 213 assert_match /reset/, flash[:notice]
209 214 assert_redirected_to '/my/account'
210 215 end
211 216
212 217 def test_reset_rss_key_without_existing_key
213 218 assert_nil User.find(2).rss_token
214 219 post :reset_rss_key
215 220
216 221 assert User.find(2).rss_token
217 222 assert_match /reset/, flash[:notice]
218 223 assert_redirected_to '/my/account'
219 224 end
220 225
221 226 def test_reset_api_key_with_existing_key
222 227 @previous_token_value = User.find(2).api_key # Will generate one if it's missing
223 228 post :reset_api_key
224 229
225 230 assert_not_equal @previous_token_value, User.find(2).api_key
226 231 assert User.find(2).api_token
227 232 assert_match /reset/, flash[:notice]
228 233 assert_redirected_to '/my/account'
229 234 end
230 235
231 236 def test_reset_api_key_without_existing_key
232 237 assert_nil User.find(2).api_token
233 238 post :reset_api_key
234 239
235 240 assert User.find(2).api_token
236 241 assert_match /reset/, flash[:notice]
237 242 assert_redirected_to '/my/account'
238 243 end
239 244 end
General Comments 0
You need to be logged in to leave comments. Login now