##// END OF EJS Templates
Make sure the RSS token is getting destroyed and created....
Eric Davis -
r3096:e1013c44a3b7
parent child
Show More
@@ -1,166 +1,170
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 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 :custom_fields
23 23
24 24 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
25 25 'issuesreportedbyme' => :label_reported_issues,
26 26 'issueswatched' => :label_watched_issues,
27 27 'news' => :label_news_latest,
28 28 'calendar' => :label_calendar,
29 29 'documents' => :label_document_plural,
30 30 'timelog' => :label_spent_time
31 31 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
32 32
33 33 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
34 34 'right' => ['issuesreportedbyme']
35 35 }.freeze
36 36
37 37 verify :xhr => true,
38 38 :only => [:add_block, :remove_block, :order_blocks]
39 39
40 40 def index
41 41 page
42 42 render :action => 'page'
43 43 end
44 44
45 45 # Show user's page
46 46 def page
47 47 @user = User.current
48 48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
49 49 end
50 50
51 51 # Edit user's account
52 52 def account
53 53 @user = User.current
54 54 @pref = @user.pref
55 55 if request.post?
56 56 @user.attributes = params[:user]
57 57 @user.mail_notification = (params[:notification_option] == 'all')
58 58 @user.pref.attributes = params[:pref]
59 59 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
60 60 if @user.save
61 61 @user.pref.save
62 62 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
63 63 set_language_if_valid @user.language
64 64 flash[:notice] = l(:notice_account_updated)
65 65 redirect_to :action => 'account'
66 66 return
67 67 end
68 68 end
69 69 @notification_options = [[l(:label_user_mail_option_all), 'all'],
70 70 [l(:label_user_mail_option_none), 'none']]
71 71 # Only users that belong to more than 1 project can select projects for which they are notified
72 72 # Note that @user.membership.size would fail since AR ignores :include association option when doing a count
73 73 @notification_options.insert 1, [l(:label_user_mail_option_selected), 'selected'] if @user.memberships.length > 1
74 74 @notification_option = @user.mail_notification? ? 'all' : (@user.notified_projects_ids.empty? ? 'none' : 'selected')
75 75 end
76 76
77 77 # Manage user's password
78 78 def password
79 79 @user = User.current
80 80 if @user.auth_source_id
81 81 flash[:error] = l(:notice_can_t_change_password)
82 82 redirect_to :action => 'account'
83 83 return
84 84 end
85 85 if request.post?
86 86 if @user.check_password?(params[:password])
87 87 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
88 88 if @user.save
89 89 flash[:notice] = l(:notice_account_password_updated)
90 90 redirect_to :action => 'account'
91 91 end
92 92 else
93 93 flash[:error] = l(:notice_account_wrong_password)
94 94 end
95 95 end
96 96 end
97 97
98 98 # Create a new feeds key
99 99 def reset_rss_key
100 if request.post? && User.current.rss_token
101 User.current.rss_token.destroy
100 if request.post?
101 if User.current.rss_token
102 User.current.rss_token.destroy
103 User.current.reload
104 end
105 User.current.rss_key
102 106 flash[:notice] = l(:notice_feeds_access_key_reseted)
103 107 end
104 108 redirect_to :action => 'account'
105 109 end
106 110
107 111 # User's page layout configuration
108 112 def page_layout
109 113 @user = User.current
110 114 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
111 115 @block_options = []
112 116 BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]}
113 117 end
114 118
115 119 # Add a block to user's page
116 120 # The block is added on top of the page
117 121 # params[:block] : id of the block to add
118 122 def add_block
119 123 block = params[:block].to_s.underscore
120 124 (render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
121 125 @user = User.current
122 126 layout = @user.pref[:my_page_layout] || {}
123 127 # remove if already present in a group
124 128 %w(top left right).each {|f| (layout[f] ||= []).delete block }
125 129 # add it on top
126 130 layout['top'].unshift block
127 131 @user.pref[:my_page_layout] = layout
128 132 @user.pref.save
129 133 render :partial => "block", :locals => {:user => @user, :block_name => block}
130 134 end
131 135
132 136 # Remove a block to user's page
133 137 # params[:block] : id of the block to remove
134 138 def remove_block
135 139 block = params[:block].to_s.underscore
136 140 @user = User.current
137 141 # remove block in all groups
138 142 layout = @user.pref[:my_page_layout] || {}
139 143 %w(top left right).each {|f| (layout[f] ||= []).delete block }
140 144 @user.pref[:my_page_layout] = layout
141 145 @user.pref.save
142 146 render :nothing => true
143 147 end
144 148
145 149 # Change blocks order on user's page
146 150 # params[:group] : group to order (top, left or right)
147 151 # params[:list-(top|left|right)] : array of block ids of the group
148 152 def order_blocks
149 153 group = params[:group]
150 154 @user = User.current
151 155 if group.is_a?(String)
152 156 group_items = (params["list-#{group}"] || []).collect(&:underscore)
153 157 if group_items and group_items.is_a? Array
154 158 layout = @user.pref[:my_page_layout] || {}
155 159 # remove group blocks if they are presents in other groups
156 160 %w(top left right).each {|f|
157 161 layout[f] = (layout[f] || []) - group_items
158 162 }
159 163 layout[group] = group_items
160 164 @user.pref[:my_page_layout] = layout
161 165 @user.pref.save
162 166 end
163 167 end
164 168 render :nothing => true
165 169 end
166 170 end
@@ -1,132 +1,166
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
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, :issues, :issue_statuses, :trackers, :enumerations, :custom_fields
26 26
27 27 def setup
28 28 @controller = MyController.new
29 29 @request = ActionController::TestRequest.new
30 30 @request.session[:user_id] = 2
31 31 @response = ActionController::TestResponse.new
32 32 end
33 33
34 34 def test_index
35 35 get :index
36 36 assert_response :success
37 37 assert_template 'page'
38 38 end
39 39
40 40 def test_page
41 41 get :page
42 42 assert_response :success
43 43 assert_template 'page'
44 44 end
45 45
46 46 def test_my_account_should_show_editable_custom_fields
47 47 get :account
48 48 assert_response :success
49 49 assert_template 'account'
50 50 assert_equal User.find(2), assigns(:user)
51 51
52 52 assert_tag :input, :attributes => { :name => 'user[custom_field_values][4]'}
53 53 end
54 54
55 55 def test_my_account_should_not_show_non_editable_custom_fields
56 56 UserCustomField.find(4).update_attribute :editable, false
57 57
58 58 get :account
59 59 assert_response :success
60 60 assert_template 'account'
61 61 assert_equal User.find(2), assigns(:user)
62 62
63 63 assert_no_tag :input, :attributes => { :name => 'user[custom_field_values][4]'}
64 64 end
65 65
66 66 def test_update_account
67 67 post :account, :user => {:firstname => "Joe",
68 68 :login => "root",
69 69 :admin => 1,
70 70 :custom_field_values => {"4" => "0100562500"}}
71 71 assert_redirected_to 'my/account'
72 72 user = User.find(2)
73 73 assert_equal user, assigns(:user)
74 74 assert_equal "Joe", user.firstname
75 75 assert_equal "jsmith", user.login
76 76 assert_equal "0100562500", user.custom_value_for(4).value
77 77 assert !user.admin?
78 78 end
79 79
80 80 def test_change_password
81 81 get :password
82 82 assert_response :success
83 83 assert_template 'password'
84 84
85 85 # non matching password confirmation
86 86 post :password, :password => 'jsmith',
87 87 :new_password => 'hello',
88 88 :new_password_confirmation => 'hello2'
89 89 assert_response :success
90 90 assert_template 'password'
91 91 assert_tag :tag => "div", :attributes => { :class => "errorExplanation" }
92 92
93 93 # wrong password
94 94 post :password, :password => 'wrongpassword',
95 95 :new_password => 'hello',
96 96 :new_password_confirmation => 'hello'
97 97 assert_response :success
98 98 assert_template 'password'
99 99 assert_equal 'Wrong password', flash[:error]
100 100
101 101 # good password
102 102 post :password, :password => 'jsmith',
103 103 :new_password => 'hello',
104 104 :new_password_confirmation => 'hello'
105 105 assert_redirected_to 'my/account'
106 106 assert User.try_to_login('jsmith', 'hello')
107 107 end
108 108
109 109 def test_page_layout
110 110 get :page_layout
111 111 assert_response :success
112 112 assert_template 'page_layout'
113 113 end
114 114
115 115 def test_add_block
116 116 xhr :post, :add_block, :block => 'issuesreportedbyme'
117 117 assert_response :success
118 118 assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme')
119 119 end
120 120
121 121 def test_remove_block
122 122 xhr :post, :remove_block, :block => 'issuesassignedtome'
123 123 assert_response :success
124 124 assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
125 125 end
126 126
127 127 def test_order_blocks
128 128 xhr :post, :order_blocks, :group => 'left', 'list-left' => ['documents', 'calendar', 'latestnews']
129 129 assert_response :success
130 130 assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left']
131 131 end
132
133 context "POST to reset_rss_key" do
134 context "with an existing rss_token" do
135 setup do
136 @previous_token_value = User.find(2).rss_key # Will generate one if it's missing
137 post :reset_rss_key
138 end
139
140 should "destroy the existing token" do
141 assert_not_equal @previous_token_value, User.find(2).rss_key
142 end
143
144 should "create a new token" do
145 assert User.find(2).rss_token
146 end
147
148 should_set_the_flash_to /reset/
149 should_redirect_to('my account') {'/my/account' }
150 end
151
152 context "with no rss_token" do
153 setup do
154 assert_nil User.find(2).rss_token
155 post :reset_rss_key
156 end
157
158 should "create a new token" do
159 assert User.find(2).rss_token
160 end
161
162 should_set_the_flash_to /reset/
163 should_redirect_to('my account') {'/my/account' }
164 end
165 end
132 166 end
General Comments 0
You need to be logged in to leave comments. Login now