@@ -1,221 +1,221 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2010 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2010 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class UsersController < ApplicationController |
|
18 | class UsersController < ApplicationController | |
19 | layout 'admin' |
|
19 | layout 'admin' | |
20 |
|
20 | |||
21 | before_filter :require_admin, :except => :show |
|
21 | before_filter :require_admin, :except => :show | |
22 | accept_key_auth :index, :show, :create, :update |
|
22 | accept_key_auth :index, :show, :create, :update | |
23 |
|
23 | |||
24 | helper :sort |
|
24 | helper :sort | |
25 | include SortHelper |
|
25 | include SortHelper | |
26 | helper :custom_fields |
|
26 | helper :custom_fields | |
27 | include CustomFieldsHelper |
|
27 | include CustomFieldsHelper | |
28 |
|
28 | |||
29 | def index |
|
29 | def index | |
30 | sort_init 'login', 'asc' |
|
30 | sort_init 'login', 'asc' | |
31 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
31 | sort_update %w(login firstname lastname mail admin created_on last_login_on) | |
32 |
|
32 | |||
33 | case params[:format] |
|
33 | case params[:format] | |
34 | when 'xml', 'json' |
|
34 | when 'xml', 'json' | |
35 | @offset, @limit = api_offset_and_limit |
|
35 | @offset, @limit = api_offset_and_limit | |
36 | else |
|
36 | else | |
37 | @limit = per_page_option |
|
37 | @limit = per_page_option | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | @status = params[:status] ? params[:status].to_i : 1 |
|
40 | @status = params[:status] ? params[:status].to_i : 1 | |
41 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) |
|
41 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) | |
42 |
|
42 | |||
43 | unless params[:name].blank? |
|
43 | unless params[:name].blank? | |
44 | name = "%#{params[:name].strip.downcase}%" |
|
44 | name = "%#{params[:name].strip.downcase}%" | |
45 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] |
|
45 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | @user_count = User.count(:conditions => c.conditions) |
|
48 | @user_count = User.count(:conditions => c.conditions) | |
49 | @user_pages = Paginator.new self, @user_count, @limit, params['page'] |
|
49 | @user_pages = Paginator.new self, @user_count, @limit, params['page'] | |
50 | @offset ||= @user_pages.current.offset |
|
50 | @offset ||= @user_pages.current.offset | |
51 | @users = User.find :all, |
|
51 | @users = User.find :all, | |
52 | :order => sort_clause, |
|
52 | :order => sort_clause, | |
53 | :conditions => c.conditions, |
|
53 | :conditions => c.conditions, | |
54 | :limit => @limit, |
|
54 | :limit => @limit, | |
55 | :offset => @offset |
|
55 | :offset => @offset | |
56 |
|
56 | |||
57 | respond_to do |format| |
|
57 | respond_to do |format| | |
58 | format.html { render :layout => !request.xhr? } |
|
58 | format.html { render :layout => !request.xhr? } | |
59 | format.api |
|
59 | format.api | |
60 | end |
|
60 | end | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def show |
|
63 | def show | |
64 | @user = User.find(params[:id]) |
|
64 | @user = User.find(params[:id]) | |
65 |
|
65 | |||
66 | # show projects based on current user visibility |
|
66 | # show projects based on current user visibility | |
67 | @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) |
|
67 | @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) | |
68 |
|
68 | |||
69 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
69 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | |
70 | @events_by_day = events.group_by(&:event_date) |
|
70 | @events_by_day = events.group_by(&:event_date) | |
71 |
|
71 | |||
72 | unless User.current.admin? |
|
72 | unless User.current.admin? | |
73 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
73 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) | |
74 | render_404 |
|
74 | render_404 | |
75 | return |
|
75 | return | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | respond_to do |format| |
|
79 | respond_to do |format| | |
80 | format.html { render :layout => 'base' } |
|
80 | format.html { render :layout => 'base' } | |
81 | format.api |
|
81 | format.api | |
82 | end |
|
82 | end | |
83 | rescue ActiveRecord::RecordNotFound |
|
83 | rescue ActiveRecord::RecordNotFound | |
84 | render_404 |
|
84 | render_404 | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | def new |
|
87 | def new | |
88 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
88 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
89 | @auth_sources = AuthSource.find(:all) |
|
89 | @auth_sources = AuthSource.find(:all) | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
|
92 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } | |
93 | def create |
|
93 | def create | |
94 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
94 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
95 | @user.safe_attributes = params[:user] |
|
95 | @user.safe_attributes = params[:user] | |
96 | @user.admin = params[:user][:admin] || false |
|
96 | @user.admin = params[:user][:admin] || false | |
97 | @user.login = params[:user][:login] |
|
97 | @user.login = params[:user][:login] | |
98 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
|
98 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id | |
99 |
|
99 | |||
100 | # TODO: Similar to My#account |
|
100 | # TODO: Similar to My#account | |
101 | @user.pref.attributes = params[:pref] |
|
101 | @user.pref.attributes = params[:pref] | |
102 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
102 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
103 |
|
103 | |||
104 | if @user.save |
|
104 | if @user.save | |
105 | @user.pref.save |
|
105 | @user.pref.save | |
106 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
106 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) | |
107 |
|
107 | |||
108 | Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] |
|
108 | Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information] | |
109 |
|
109 | |||
110 | respond_to do |format| |
|
110 | respond_to do |format| | |
111 | format.html { |
|
111 | format.html { | |
112 | flash[:notice] = l(:notice_successful_create) |
|
112 | flash[:notice] = l(:notice_successful_create) | |
113 | redirect_to(params[:continue] ? |
|
113 | redirect_to(params[:continue] ? | |
114 | {:controller => 'users', :action => 'new'} : |
|
114 | {:controller => 'users', :action => 'new'} : | |
115 | {:controller => 'users', :action => 'edit', :id => @user} |
|
115 | {:controller => 'users', :action => 'edit', :id => @user} | |
116 | ) |
|
116 | ) | |
117 | } |
|
117 | } | |
118 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } |
|
118 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } | |
119 | end |
|
119 | end | |
120 | else |
|
120 | else | |
121 | @auth_sources = AuthSource.find(:all) |
|
121 | @auth_sources = AuthSource.find(:all) | |
122 | # Clear password input |
|
122 | # Clear password input | |
123 | @user.password = @user.password_confirmation = nil |
|
123 | @user.password = @user.password_confirmation = nil | |
124 |
|
124 | |||
125 | respond_to do |format| |
|
125 | respond_to do |format| | |
126 | format.html { render :action => 'new' } |
|
126 | format.html { render :action => 'new' } | |
127 | format.api { render_validation_errors(@user) } |
|
127 | format.api { render_validation_errors(@user) } | |
128 | end |
|
128 | end | |
129 | end |
|
129 | end | |
130 | end |
|
130 | end | |
131 |
|
131 | |||
132 | def edit |
|
132 | def edit | |
133 | @user = User.find(params[:id]) |
|
133 | @user = User.find(params[:id]) | |
134 |
|
134 | |||
135 | @auth_sources = AuthSource.find(:all) |
|
135 | @auth_sources = AuthSource.find(:all) | |
136 | @membership ||= Member.new |
|
136 | @membership ||= Member.new | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
139 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } | |
140 | def update |
|
140 | def update | |
141 | @user = User.find(params[:id]) |
|
141 | @user = User.find(params[:id]) | |
142 |
|
142 | |||
143 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
143 | @user.admin = params[:user][:admin] if params[:user][:admin] | |
144 | @user.login = params[:user][:login] if params[:user][:login] |
|
144 | @user.login = params[:user][:login] if params[:user][:login] | |
145 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
145 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) | |
146 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
146 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] | |
147 | end |
|
147 | end | |
148 | @user.safe_attributes = params[:user] |
|
148 | @user.safe_attributes = params[:user] | |
149 | # Was the account actived ? (do it before User#save clears the change) |
|
149 | # Was the account actived ? (do it before User#save clears the change) | |
150 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
150 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | |
151 | # TODO: Similar to My#account |
|
151 | # TODO: Similar to My#account | |
152 | @user.pref.attributes = params[:pref] |
|
152 | @user.pref.attributes = params[:pref] | |
153 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
153 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
154 |
|
154 | |||
155 | if @user.save |
|
155 | if @user.save | |
156 | @user.pref.save |
|
156 | @user.pref.save | |
157 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
157 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) | |
158 |
|
158 | |||
159 | if was_activated |
|
159 | if was_activated | |
160 | Mailer.deliver_account_activated(@user) |
|
160 | Mailer.deliver_account_activated(@user) | |
161 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? |
|
161 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? | |
162 | Mailer.deliver_account_information(@user, params[:user][:password]) |
|
162 | Mailer.deliver_account_information(@user, params[:user][:password]) | |
163 | end |
|
163 | end | |
164 |
|
164 | |||
165 | respond_to do |format| |
|
165 | respond_to do |format| | |
166 | format.html { |
|
166 | format.html { | |
167 | flash[:notice] = l(:notice_successful_update) |
|
167 | flash[:notice] = l(:notice_successful_update) | |
168 | redirect_to :back |
|
168 | redirect_to :back | |
169 | } |
|
169 | } | |
170 | format.api { head :ok } |
|
170 | format.api { head :ok } | |
171 | end |
|
171 | end | |
172 | else |
|
172 | else | |
173 | @auth_sources = AuthSource.find(:all) |
|
173 | @auth_sources = AuthSource.find(:all) | |
174 | @membership ||= Member.new |
|
174 | @membership ||= Member.new | |
175 | # Clear password input |
|
175 | # Clear password input | |
176 | @user.password = @user.password_confirmation = nil |
|
176 | @user.password = @user.password_confirmation = nil | |
177 |
|
177 | |||
178 | respond_to do |format| |
|
178 | respond_to do |format| | |
179 | format.html { render :action => :edit } |
|
179 | format.html { render :action => :edit } | |
180 | format.api { render_validation_errors(@user) } |
|
180 | format.api { render_validation_errors(@user) } | |
181 | end |
|
181 | end | |
182 | end |
|
182 | end | |
183 | rescue ::ActionController::RedirectBackError |
|
183 | rescue ::ActionController::RedirectBackError | |
184 | redirect_to :controller => 'users', :action => 'edit', :id => @user |
|
184 | redirect_to :controller => 'users', :action => 'edit', :id => @user | |
185 | end |
|
185 | end | |
186 |
|
186 | |||
187 | def edit_membership |
|
187 | def edit_membership | |
188 | @user = User.find(params[:id]) |
|
188 | @user = User.find(params[:id]) | |
189 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
189 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) | |
190 | @membership.save if request.post? |
|
190 | @membership.save if request.post? | |
191 | respond_to do |format| |
|
191 | respond_to do |format| | |
192 | if @membership.valid? |
|
192 | if @membership.valid? | |
193 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
193 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
194 | format.js { |
|
194 | format.js { | |
195 | render(:update) {|page| |
|
195 | render(:update) {|page| | |
196 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
|
196 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' | |
197 | page.visual_effect(:highlight, "member-#{@membership.id}") |
|
197 | page.visual_effect(:highlight, "member-#{@membership.id}") | |
198 | } |
|
198 | } | |
199 | } |
|
199 | } | |
200 | else |
|
200 | else | |
201 | format.js { |
|
201 | format.js { | |
202 | render(:update) {|page| |
|
202 | render(:update) {|page| | |
203 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
|
203 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) | |
204 | } |
|
204 | } | |
205 | } |
|
205 | } | |
206 | end |
|
206 | end | |
207 | end |
|
207 | end | |
208 | end |
|
208 | end | |
209 |
|
209 | |||
210 | def destroy_membership |
|
210 | def destroy_membership | |
211 | @user = User.find(params[:id]) |
|
211 | @user = User.find(params[:id]) | |
212 | @membership = Member.find(params[:membership_id]) |
|
212 | @membership = Member.find(params[:membership_id]) | |
213 | if request.post? && @membership.deletable? |
|
213 | if request.post? && @membership.deletable? | |
214 | @membership.destroy |
|
214 | @membership.destroy | |
215 | end |
|
215 | end | |
216 | respond_to do |format| |
|
216 | respond_to do |format| | |
217 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
217 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
218 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } |
|
218 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } | |
219 | end |
|
219 | end | |
220 | end |
|
220 | end | |
221 | end |
|
221 | end |
@@ -1,247 +1,281 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 | require 'users_controller' |
|
19 | require 'users_controller' | |
20 |
|
20 | |||
21 | # Re-raise errors caught by the controller. |
|
21 | # Re-raise errors caught by the controller. | |
22 | class UsersController; def rescue_action(e) raise e end; end |
|
22 | class UsersController; def rescue_action(e) raise e end; end | |
23 |
|
23 | |||
24 | class UsersControllerTest < ActionController::TestCase |
|
24 | class UsersControllerTest < ActionController::TestCase | |
25 | include Redmine::I18n |
|
25 | include Redmine::I18n | |
26 |
|
26 | |||
27 | fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values |
|
27 | fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values | |
28 |
|
28 | |||
29 | def setup |
|
29 | def setup | |
30 | @controller = UsersController.new |
|
30 | @controller = UsersController.new | |
31 | @request = ActionController::TestRequest.new |
|
31 | @request = ActionController::TestRequest.new | |
32 | @response = ActionController::TestResponse.new |
|
32 | @response = ActionController::TestResponse.new | |
33 | User.current = nil |
|
33 | User.current = nil | |
34 | @request.session[:user_id] = 1 # admin |
|
34 | @request.session[:user_id] = 1 # admin | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | def test_index |
|
37 | def test_index | |
38 | get :index |
|
38 | get :index | |
39 | assert_response :success |
|
39 | assert_response :success | |
40 | assert_template 'index' |
|
40 | assert_template 'index' | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def test_index |
|
43 | def test_index | |
44 | get :index |
|
44 | get :index | |
45 | assert_response :success |
|
45 | assert_response :success | |
46 | assert_template 'index' |
|
46 | assert_template 'index' | |
47 | assert_not_nil assigns(:users) |
|
47 | assert_not_nil assigns(:users) | |
48 | # active users only |
|
48 | # active users only | |
49 | assert_nil assigns(:users).detect {|u| !u.active?} |
|
49 | assert_nil assigns(:users).detect {|u| !u.active?} | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def test_index_with_name_filter |
|
52 | def test_index_with_name_filter | |
53 | get :index, :name => 'john' |
|
53 | get :index, :name => 'john' | |
54 | assert_response :success |
|
54 | assert_response :success | |
55 | assert_template 'index' |
|
55 | assert_template 'index' | |
56 | users = assigns(:users) |
|
56 | users = assigns(:users) | |
57 | assert_not_nil users |
|
57 | assert_not_nil users | |
58 | assert_equal 1, users.size |
|
58 | assert_equal 1, users.size | |
59 | assert_equal 'John', users.first.firstname |
|
59 | assert_equal 'John', users.first.firstname | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def test_show |
|
62 | def test_show | |
63 | @request.session[:user_id] = nil |
|
63 | @request.session[:user_id] = nil | |
64 | get :show, :id => 2 |
|
64 | get :show, :id => 2 | |
65 | assert_response :success |
|
65 | assert_response :success | |
66 | assert_template 'show' |
|
66 | assert_template 'show' | |
67 | assert_not_nil assigns(:user) |
|
67 | assert_not_nil assigns(:user) | |
68 |
|
68 | |||
69 | assert_tag 'li', :content => /Phone number/ |
|
69 | assert_tag 'li', :content => /Phone number/ | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def test_show_should_not_display_hidden_custom_fields |
|
72 | def test_show_should_not_display_hidden_custom_fields | |
73 | @request.session[:user_id] = nil |
|
73 | @request.session[:user_id] = nil | |
74 | UserCustomField.find_by_name('Phone number').update_attribute :visible, false |
|
74 | UserCustomField.find_by_name('Phone number').update_attribute :visible, false | |
75 | get :show, :id => 2 |
|
75 | get :show, :id => 2 | |
76 | assert_response :success |
|
76 | assert_response :success | |
77 | assert_template 'show' |
|
77 | assert_template 'show' | |
78 | assert_not_nil assigns(:user) |
|
78 | assert_not_nil assigns(:user) | |
79 |
|
79 | |||
80 | assert_no_tag 'li', :content => /Phone number/ |
|
80 | assert_no_tag 'li', :content => /Phone number/ | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | def test_show_should_not_fail_when_custom_values_are_nil |
|
83 | def test_show_should_not_fail_when_custom_values_are_nil | |
84 | user = User.find(2) |
|
84 | user = User.find(2) | |
85 |
|
85 | |||
86 | # Create a custom field to illustrate the issue |
|
86 | # Create a custom field to illustrate the issue | |
87 | custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text') |
|
87 | custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text') | |
88 | custom_value = user.custom_values.build(:custom_field => custom_field).save! |
|
88 | custom_value = user.custom_values.build(:custom_field => custom_field).save! | |
89 |
|
89 | |||
90 | get :show, :id => 2 |
|
90 | get :show, :id => 2 | |
91 | assert_response :success |
|
91 | assert_response :success | |
92 | end |
|
92 | end | |
93 |
|
93 | |||
94 | def test_show_inactive |
|
94 | def test_show_inactive | |
95 | @request.session[:user_id] = nil |
|
95 | @request.session[:user_id] = nil | |
96 | get :show, :id => 5 |
|
96 | get :show, :id => 5 | |
97 | assert_response 404 |
|
97 | assert_response 404 | |
98 | end |
|
98 | end | |
99 |
|
99 | |||
100 | def test_show_should_not_reveal_users_with_no_visible_activity_or_project |
|
100 | def test_show_should_not_reveal_users_with_no_visible_activity_or_project | |
101 | @request.session[:user_id] = nil |
|
101 | @request.session[:user_id] = nil | |
102 | get :show, :id => 9 |
|
102 | get :show, :id => 9 | |
103 | assert_response 404 |
|
103 | assert_response 404 | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | def test_show_inactive_by_admin |
|
106 | def test_show_inactive_by_admin | |
107 | @request.session[:user_id] = 1 |
|
107 | @request.session[:user_id] = 1 | |
108 | get :show, :id => 5 |
|
108 | get :show, :id => 5 | |
109 | assert_response 200 |
|
109 | assert_response 200 | |
110 | assert_not_nil assigns(:user) |
|
110 | assert_not_nil assigns(:user) | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
113 | def test_show_displays_memberships_based_on_project_visibility |
|
113 | def test_show_displays_memberships_based_on_project_visibility | |
114 | @request.session[:user_id] = 1 |
|
114 | @request.session[:user_id] = 1 | |
115 | get :show, :id => 2 |
|
115 | get :show, :id => 2 | |
116 | assert_response :success |
|
116 | assert_response :success | |
117 | memberships = assigns(:memberships) |
|
117 | memberships = assigns(:memberships) | |
118 | assert_not_nil memberships |
|
118 | assert_not_nil memberships | |
119 | project_ids = memberships.map(&:project_id) |
|
119 | project_ids = memberships.map(&:project_id) | |
120 | assert project_ids.include?(2) #private project admin can see |
|
120 | assert project_ids.include?(2) #private project admin can see | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | context "GET :new" do |
|
123 | context "GET :new" do | |
124 | setup do |
|
124 | setup do | |
125 | get :new |
|
125 | get :new | |
126 | end |
|
126 | end | |
127 |
|
127 | |||
128 | should_assign_to :user |
|
128 | should_assign_to :user | |
129 | should_respond_with :success |
|
129 | should_respond_with :success | |
130 | should_render_template :new |
|
130 | should_render_template :new | |
131 | end |
|
131 | end | |
132 |
|
132 | |||
133 | context "POST :create" do |
|
133 | context "POST :create" do | |
134 | context "when successful" do |
|
134 | context "when successful" do | |
135 | setup do |
|
135 | setup do | |
136 | post :create, :user => { |
|
136 | post :create, :user => { | |
137 | :firstname => 'John', |
|
137 | :firstname => 'John', | |
138 | :lastname => 'Doe', |
|
138 | :lastname => 'Doe', | |
139 | :login => 'jdoe', |
|
139 | :login => 'jdoe', | |
140 | :password => 'test', |
|
140 | :password => 'test', | |
141 | :password_confirmation => 'test', |
|
141 | :password_confirmation => 'test', | |
142 | :mail => 'jdoe@gmail.com', |
|
142 | :mail => 'jdoe@gmail.com', | |
143 | :mail_notification => 'none' |
|
143 | :mail_notification => 'none' | |
144 | } |
|
144 | } | |
145 | end |
|
145 | end | |
146 |
|
146 | |||
147 | should_assign_to :user |
|
147 | should_assign_to :user | |
148 | should_respond_with :redirect |
|
148 | should_respond_with :redirect | |
149 | should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}} |
|
149 | should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}} | |
150 |
|
150 | |||
151 | should 'set the users mail notification' do |
|
151 | should 'set the users mail notification' do | |
152 | user = User.last |
|
152 | user = User.last | |
153 | assert_equal 'none', user.mail_notification |
|
153 | assert_equal 'none', user.mail_notification | |
154 | end |
|
154 | end | |
155 |
|
155 | |||
156 | should 'set the password' do |
|
156 | should 'set the password' do | |
157 | user = User.first(:order => 'id DESC') |
|
157 | user = User.first(:order => 'id DESC') | |
158 | assert user.check_password?('test') |
|
158 | assert user.check_password?('test') | |
159 | end |
|
159 | end | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 | context "when unsuccessful" do |
|
162 | context "when unsuccessful" do | |
163 | setup do |
|
163 | setup do | |
164 | post :create, :user => {} |
|
164 | post :create, :user => {} | |
165 | end |
|
165 | end | |
166 |
|
166 | |||
167 | should_assign_to :user |
|
167 | should_assign_to :user | |
168 | should_respond_with :success |
|
168 | should_respond_with :success | |
169 | should_render_template :new |
|
169 | should_render_template :new | |
170 | end |
|
170 | end | |
|
171 | end | |||
|
172 | ||||
|
173 | def test_create | |||
|
174 | Setting.bcc_recipients = '1' | |||
171 |
|
175 | |||
|
176 | assert_difference 'User.count' do | |||
|
177 | assert_difference 'ActionMailer::Base.deliveries.size' do | |||
|
178 | post :create, | |||
|
179 | :user => { | |||
|
180 | :firstname => 'John', | |||
|
181 | :lastname => 'Doe', | |||
|
182 | :login => 'jdoe', | |||
|
183 | :password => 'secret', | |||
|
184 | :password_confirmation => 'secret', | |||
|
185 | :mail => 'jdoe@gmail.com', | |||
|
186 | :mail_notification => 'none' | |||
|
187 | }, | |||
|
188 | :send_information => '1' | |||
|
189 | end | |||
|
190 | end | |||
|
191 | ||||
|
192 | user = User.first(:order => 'id DESC') | |||
|
193 | assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id | |||
|
194 | ||||
|
195 | assert_equal 'John', user.firstname | |||
|
196 | assert_equal 'Doe', user.lastname | |||
|
197 | assert_equal 'jdoe', user.login | |||
|
198 | assert_equal 'jdoe@gmail.com', user.mail | |||
|
199 | assert_equal 'none', user.mail_notification | |||
|
200 | assert user.check_password?('secret') | |||
|
201 | ||||
|
202 | mail = ActionMailer::Base.deliveries.last | |||
|
203 | assert_not_nil mail | |||
|
204 | assert_equal [user.mail], mail.bcc | |||
|
205 | assert mail.body.include?('secret') | |||
172 | end |
|
206 | end | |
173 |
|
207 | |||
174 | def test_update |
|
208 | def test_update | |
175 | ActionMailer::Base.deliveries.clear |
|
209 | ActionMailer::Base.deliveries.clear | |
176 | put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'} |
|
210 | put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'} | |
177 |
|
211 | |||
178 | user = User.find(2) |
|
212 | user = User.find(2) | |
179 | assert_equal 'Changed', user.firstname |
|
213 | assert_equal 'Changed', user.firstname | |
180 | assert_equal 'only_assigned', user.mail_notification |
|
214 | assert_equal 'only_assigned', user.mail_notification | |
181 | assert_equal true, user.pref[:hide_mail] |
|
215 | assert_equal true, user.pref[:hide_mail] | |
182 | assert_equal 'desc', user.pref[:comments_sorting] |
|
216 | assert_equal 'desc', user.pref[:comments_sorting] | |
183 | assert ActionMailer::Base.deliveries.empty? |
|
217 | assert ActionMailer::Base.deliveries.empty? | |
184 | end |
|
218 | end | |
185 |
|
219 | |||
186 | def test_update_with_group_ids_should_assign_groups |
|
220 | def test_update_with_group_ids_should_assign_groups | |
187 | put :update, :id => 2, :user => {:group_ids => ['10']} |
|
221 | put :update, :id => 2, :user => {:group_ids => ['10']} | |
188 |
|
222 | |||
189 | user = User.find(2) |
|
223 | user = User.find(2) | |
190 | assert_equal [10], user.group_ids |
|
224 | assert_equal [10], user.group_ids | |
191 | end |
|
225 | end | |
192 |
|
226 | |||
193 | def test_update_with_activation_should_send_a_notification |
|
227 | def test_update_with_activation_should_send_a_notification | |
194 | u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') |
|
228 | u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') | |
195 | u.login = 'foo' |
|
229 | u.login = 'foo' | |
196 | u.status = User::STATUS_REGISTERED |
|
230 | u.status = User::STATUS_REGISTERED | |
197 | u.save! |
|
231 | u.save! | |
198 | ActionMailer::Base.deliveries.clear |
|
232 | ActionMailer::Base.deliveries.clear | |
199 | Setting.bcc_recipients = '1' |
|
233 | Setting.bcc_recipients = '1' | |
200 |
|
234 | |||
201 | put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE} |
|
235 | put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE} | |
202 | assert u.reload.active? |
|
236 | assert u.reload.active? | |
203 | mail = ActionMailer::Base.deliveries.last |
|
237 | mail = ActionMailer::Base.deliveries.last | |
204 | assert_not_nil mail |
|
238 | assert_not_nil mail | |
205 | assert_equal ['foo.bar@somenet.foo'], mail.bcc |
|
239 | assert_equal ['foo.bar@somenet.foo'], mail.bcc | |
206 | assert mail.body.include?(ll('fr', :notice_account_activated)) |
|
240 | assert mail.body.include?(ll('fr', :notice_account_activated)) | |
207 | end |
|
241 | end | |
208 |
|
242 | |||
209 | def test_update_with_password_change_should_send_a_notification |
|
243 | def test_update_with_password_change_should_send_a_notification | |
210 | ActionMailer::Base.deliveries.clear |
|
244 | ActionMailer::Base.deliveries.clear | |
211 | Setting.bcc_recipients = '1' |
|
245 | Setting.bcc_recipients = '1' | |
212 |
|
246 | |||
213 | put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1' |
|
247 | put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1' | |
214 | u = User.find(2) |
|
248 | u = User.find(2) | |
215 | assert u.check_password?('newpass') |
|
249 | assert u.check_password?('newpass') | |
216 |
|
250 | |||
217 | mail = ActionMailer::Base.deliveries.last |
|
251 | mail = ActionMailer::Base.deliveries.last | |
218 | assert_not_nil mail |
|
252 | assert_not_nil mail | |
219 | assert_equal [u.mail], mail.bcc |
|
253 | assert_equal [u.mail], mail.bcc | |
220 | assert mail.body.include?('newpass') |
|
254 | assert mail.body.include?('newpass') | |
221 | end |
|
255 | end | |
222 |
|
256 | |||
223 | test "put :update with a password change to an AuthSource user switching to Internal authentication" do |
|
257 | test "put :update with a password change to an AuthSource user switching to Internal authentication" do | |
224 | # Configure as auth source |
|
258 | # Configure as auth source | |
225 | u = User.find(2) |
|
259 | u = User.find(2) | |
226 | u.auth_source = AuthSource.find(1) |
|
260 | u.auth_source = AuthSource.find(1) | |
227 | u.save! |
|
261 | u.save! | |
228 |
|
262 | |||
229 | put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass' |
|
263 | put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass' | |
230 |
|
264 | |||
231 | assert_equal nil, u.reload.auth_source |
|
265 | assert_equal nil, u.reload.auth_source | |
232 | assert u.check_password?('newpass') |
|
266 | assert u.check_password?('newpass') | |
233 | end |
|
267 | end | |
234 |
|
268 | |||
235 | def test_edit_membership |
|
269 | def test_edit_membership | |
236 | post :edit_membership, :id => 2, :membership_id => 1, |
|
270 | post :edit_membership, :id => 2, :membership_id => 1, | |
237 | :membership => { :role_ids => [2]} |
|
271 | :membership => { :role_ids => [2]} | |
238 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
272 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' | |
239 | assert_equal [2], Member.find(1).role_ids |
|
273 | assert_equal [2], Member.find(1).role_ids | |
240 | end |
|
274 | end | |
241 |
|
275 | |||
242 | def test_destroy_membership |
|
276 | def test_destroy_membership | |
243 | post :destroy_membership, :id => 2, :membership_id => 1 |
|
277 | post :destroy_membership, :id => 2, :membership_id => 1 | |
244 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
278 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' | |
245 | assert_nil Member.find_by_id(1) |
|
279 | assert_nil Member.find_by_id(1) | |
246 | end |
|
280 | end | |
247 | end |
|
281 | end |
General Comments 0
You need to be logged in to leave comments.
Login now