@@ -1,210 +1,208 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 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 UsersController < ApplicationController |
|
19 | 19 | layout 'admin' |
|
20 | 20 | |
|
21 | 21 | before_filter :require_admin, :except => :show |
|
22 | 22 | before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] |
|
23 | 23 | accept_api_auth :index, :show, :create, :update, :destroy |
|
24 | 24 | |
|
25 | 25 | helper :sort |
|
26 | 26 | include SortHelper |
|
27 | 27 | helper :custom_fields |
|
28 | 28 | include CustomFieldsHelper |
|
29 | 29 | |
|
30 | 30 | def index |
|
31 | 31 | sort_init 'login', 'asc' |
|
32 | 32 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
33 | 33 | |
|
34 | 34 | case params[:format] |
|
35 | 35 | when 'xml', 'json' |
|
36 | 36 | @offset, @limit = api_offset_and_limit |
|
37 | 37 | else |
|
38 | 38 | @limit = per_page_option |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | @status = params[:status] || 1 |
|
42 | 42 | |
|
43 | 43 | scope = User.logged.status(@status) |
|
44 | 44 | scope = scope.like(params[:name]) if params[:name].present? |
|
45 | 45 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? |
|
46 | 46 | |
|
47 | 47 | @user_count = scope.count |
|
48 | 48 | @user_pages = Paginator.new @user_count, @limit, params['page'] |
|
49 | 49 | @offset ||= @user_pages.offset |
|
50 | 50 | @users = scope.order(sort_clause).limit(@limit).offset(@offset).all |
|
51 | 51 | |
|
52 | 52 | respond_to do |format| |
|
53 | 53 | format.html { |
|
54 | 54 | @groups = Group.all.sort |
|
55 | 55 | render :layout => !request.xhr? |
|
56 | 56 | } |
|
57 | 57 | format.api |
|
58 | 58 | end |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def show |
|
62 | 62 | # show projects based on current user visibility |
|
63 | 63 | @memberships = @user.memberships.where(Project.visible_condition(User.current)).all |
|
64 | 64 | |
|
65 | 65 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
66 | 66 | @events_by_day = events.group_by(&:event_date) |
|
67 | 67 | |
|
68 | 68 | unless User.current.admin? |
|
69 | 69 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
70 | 70 | render_404 |
|
71 | 71 | return |
|
72 | 72 | end |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | respond_to do |format| |
|
76 | 76 | format.html { render :layout => 'base' } |
|
77 | 77 | format.api |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def new |
|
82 | 82 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
83 | 83 | @user.safe_attributes = params[:user] |
|
84 | 84 | @auth_sources = AuthSource.all |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def create |
|
88 | 88 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
89 | 89 | @user.safe_attributes = params[:user] |
|
90 | 90 | @user.admin = params[:user][:admin] || false |
|
91 | 91 | @user.login = params[:user][:login] |
|
92 | 92 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
|
93 | ||
|
94 | if @user.save | |
|
95 | 93 |
|
|
96 | @user.pref.save | |
|
97 | 94 | |
|
95 | if @user.save | |
|
98 | 96 | Mailer.account_information(@user, @user.password).deliver if params[:send_information] |
|
99 | 97 | |
|
100 | 98 | respond_to do |format| |
|
101 | 99 | format.html { |
|
102 | 100 | flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) |
|
103 | 101 | if params[:continue] |
|
104 | 102 | attrs = params[:user].slice(:generate_password) |
|
105 | 103 | redirect_to new_user_path(:user => attrs) |
|
106 | 104 | else |
|
107 | 105 | redirect_to edit_user_path(@user) |
|
108 | 106 | end |
|
109 | 107 | } |
|
110 | 108 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } |
|
111 | 109 | end |
|
112 | 110 | else |
|
113 | 111 | @auth_sources = AuthSource.all |
|
114 | 112 | # Clear password input |
|
115 | 113 | @user.password = @user.password_confirmation = nil |
|
116 | 114 | |
|
117 | 115 | respond_to do |format| |
|
118 | 116 | format.html { render :action => 'new' } |
|
119 | 117 | format.api { render_validation_errors(@user) } |
|
120 | 118 | end |
|
121 | 119 | end |
|
122 | 120 | end |
|
123 | 121 | |
|
124 | 122 | def edit |
|
125 | 123 | @auth_sources = AuthSource.all |
|
126 | 124 | @membership ||= Member.new |
|
127 | 125 | end |
|
128 | 126 | |
|
129 | 127 | def update |
|
130 | 128 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
131 | 129 | @user.login = params[:user][:login] if params[:user][:login] |
|
132 | 130 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
133 | 131 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
134 | 132 | end |
|
135 | 133 | @user.safe_attributes = params[:user] |
|
136 | 134 | # Was the account actived ? (do it before User#save clears the change) |
|
137 | 135 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
138 | 136 | # TODO: Similar to My#account |
|
139 | 137 | @user.pref.attributes = params[:pref] |
|
140 | 138 | |
|
141 | 139 | if @user.save |
|
142 | 140 | @user.pref.save |
|
143 | 141 | |
|
144 | 142 | if was_activated |
|
145 | 143 | Mailer.account_activated(@user).deliver |
|
146 | 144 | elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil? |
|
147 | 145 | Mailer.account_information(@user, @user.password).deliver |
|
148 | 146 | end |
|
149 | 147 | |
|
150 | 148 | respond_to do |format| |
|
151 | 149 | format.html { |
|
152 | 150 | flash[:notice] = l(:notice_successful_update) |
|
153 | 151 | redirect_to_referer_or edit_user_path(@user) |
|
154 | 152 | } |
|
155 | 153 | format.api { render_api_ok } |
|
156 | 154 | end |
|
157 | 155 | else |
|
158 | 156 | @auth_sources = AuthSource.all |
|
159 | 157 | @membership ||= Member.new |
|
160 | 158 | # Clear password input |
|
161 | 159 | @user.password = @user.password_confirmation = nil |
|
162 | 160 | |
|
163 | 161 | respond_to do |format| |
|
164 | 162 | format.html { render :action => :edit } |
|
165 | 163 | format.api { render_validation_errors(@user) } |
|
166 | 164 | end |
|
167 | 165 | end |
|
168 | 166 | end |
|
169 | 167 | |
|
170 | 168 | def destroy |
|
171 | 169 | @user.destroy |
|
172 | 170 | respond_to do |format| |
|
173 | 171 | format.html { redirect_back_or_default(users_path) } |
|
174 | 172 | format.api { render_api_ok } |
|
175 | 173 | end |
|
176 | 174 | end |
|
177 | 175 | |
|
178 | 176 | def edit_membership |
|
179 | 177 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
180 | 178 | @membership.save |
|
181 | 179 | respond_to do |format| |
|
182 | 180 | format.html { redirect_to edit_user_path(@user, :tab => 'memberships') } |
|
183 | 181 | format.js |
|
184 | 182 | end |
|
185 | 183 | end |
|
186 | 184 | |
|
187 | 185 | def destroy_membership |
|
188 | 186 | @membership = Member.find(params[:membership_id]) |
|
189 | 187 | if @membership.deletable? |
|
190 | 188 | @membership.destroy |
|
191 | 189 | end |
|
192 | 190 | respond_to do |format| |
|
193 | 191 | format.html { redirect_to edit_user_path(@user, :tab => 'memberships') } |
|
194 | 192 | format.js |
|
195 | 193 | end |
|
196 | 194 | end |
|
197 | 195 | |
|
198 | 196 | private |
|
199 | 197 | |
|
200 | 198 | def find_user |
|
201 | 199 | if params[:id] == 'current' |
|
202 | 200 | require_login || return |
|
203 | 201 | @user = User.current |
|
204 | 202 | else |
|
205 | 203 | @user = User.find(params[:id]) |
|
206 | 204 | end |
|
207 | 205 | rescue ActiveRecord::RecordNotFound |
|
208 | 206 | render_404 |
|
209 | 207 | end |
|
210 | 208 | end |
@@ -1,488 +1,507 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 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 | |
|
20 | 20 | class UsersControllerTest < ActionController::TestCase |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | |
|
23 | 23 | fixtures :users, :projects, :members, :member_roles, :roles, |
|
24 | 24 | :custom_fields, :custom_values, :groups_users, |
|
25 | 25 | :auth_sources |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | User.current = nil |
|
29 | 29 | @request.session[:user_id] = 1 # admin |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def test_index |
|
33 | 33 | get :index |
|
34 | 34 | assert_response :success |
|
35 | 35 | assert_template 'index' |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def test_index |
|
39 | 39 | get :index |
|
40 | 40 | assert_response :success |
|
41 | 41 | assert_template 'index' |
|
42 | 42 | assert_not_nil assigns(:users) |
|
43 | 43 | # active users only |
|
44 | 44 | assert_nil assigns(:users).detect {|u| !u.active?} |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def test_index_with_status_filter |
|
48 | 48 | get :index, :status => 3 |
|
49 | 49 | assert_response :success |
|
50 | 50 | assert_template 'index' |
|
51 | 51 | assert_not_nil assigns(:users) |
|
52 | 52 | assert_equal [3], assigns(:users).map(&:status).uniq |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def test_index_with_name_filter |
|
56 | 56 | get :index, :name => 'john' |
|
57 | 57 | assert_response :success |
|
58 | 58 | assert_template 'index' |
|
59 | 59 | users = assigns(:users) |
|
60 | 60 | assert_not_nil users |
|
61 | 61 | assert_equal 1, users.size |
|
62 | 62 | assert_equal 'John', users.first.firstname |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def test_index_with_group_filter |
|
66 | 66 | get :index, :group_id => '10' |
|
67 | 67 | assert_response :success |
|
68 | 68 | assert_template 'index' |
|
69 | 69 | users = assigns(:users) |
|
70 | 70 | assert users.any? |
|
71 | 71 | assert_equal([], (users - Group.find(10).users)) |
|
72 | 72 | assert_select 'select[name=group_id]' do |
|
73 | 73 | assert_select 'option[value=10][selected=selected]' |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def test_show |
|
78 | 78 | @request.session[:user_id] = nil |
|
79 | 79 | get :show, :id => 2 |
|
80 | 80 | assert_response :success |
|
81 | 81 | assert_template 'show' |
|
82 | 82 | assert_not_nil assigns(:user) |
|
83 | 83 | |
|
84 | 84 | assert_tag 'li', :content => /Phone number/ |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def test_show_should_not_display_hidden_custom_fields |
|
88 | 88 | @request.session[:user_id] = nil |
|
89 | 89 | UserCustomField.find_by_name('Phone number').update_attribute :visible, false |
|
90 | 90 | get :show, :id => 2 |
|
91 | 91 | assert_response :success |
|
92 | 92 | assert_template 'show' |
|
93 | 93 | assert_not_nil assigns(:user) |
|
94 | 94 | |
|
95 | 95 | assert_no_tag 'li', :content => /Phone number/ |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | def test_show_should_not_fail_when_custom_values_are_nil |
|
99 | 99 | user = User.find(2) |
|
100 | 100 | |
|
101 | 101 | # Create a custom field to illustrate the issue |
|
102 | 102 | custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text') |
|
103 | 103 | custom_value = user.custom_values.build(:custom_field => custom_field).save! |
|
104 | 104 | |
|
105 | 105 | get :show, :id => 2 |
|
106 | 106 | assert_response :success |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def test_show_inactive |
|
110 | 110 | @request.session[:user_id] = nil |
|
111 | 111 | get :show, :id => 5 |
|
112 | 112 | assert_response 404 |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def test_show_should_not_reveal_users_with_no_visible_activity_or_project |
|
116 | 116 | @request.session[:user_id] = nil |
|
117 | 117 | get :show, :id => 9 |
|
118 | 118 | assert_response 404 |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | def test_show_inactive_by_admin |
|
122 | 122 | @request.session[:user_id] = 1 |
|
123 | 123 | get :show, :id => 5 |
|
124 | 124 | assert_response 200 |
|
125 | 125 | assert_not_nil assigns(:user) |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | def test_show_displays_memberships_based_on_project_visibility |
|
129 | 129 | @request.session[:user_id] = 1 |
|
130 | 130 | get :show, :id => 2 |
|
131 | 131 | assert_response :success |
|
132 | 132 | memberships = assigns(:memberships) |
|
133 | 133 | assert_not_nil memberships |
|
134 | 134 | project_ids = memberships.map(&:project_id) |
|
135 | 135 | assert project_ids.include?(2) #private project admin can see |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def test_show_current_should_require_authentication |
|
139 | 139 | @request.session[:user_id] = nil |
|
140 | 140 | get :show, :id => 'current' |
|
141 | 141 | assert_response 302 |
|
142 | 142 | end |
|
143 | 143 | |
|
144 | 144 | def test_show_current |
|
145 | 145 | @request.session[:user_id] = 2 |
|
146 | 146 | get :show, :id => 'current' |
|
147 | 147 | assert_response :success |
|
148 | 148 | assert_template 'show' |
|
149 | 149 | assert_equal User.find(2), assigns(:user) |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def test_new |
|
153 | 153 | get :new |
|
154 | 154 | assert_response :success |
|
155 | 155 | assert_template :new |
|
156 | 156 | assert assigns(:user) |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | def test_create |
|
160 | 160 | Setting.bcc_recipients = '1' |
|
161 | 161 | |
|
162 | 162 | assert_difference 'User.count' do |
|
163 | 163 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
164 | 164 | post :create, |
|
165 | 165 | :user => { |
|
166 | 166 | :firstname => 'John', |
|
167 | 167 | :lastname => 'Doe', |
|
168 | 168 | :login => 'jdoe', |
|
169 | 169 | :password => 'secret123', |
|
170 | 170 | :password_confirmation => 'secret123', |
|
171 | 171 | :mail => 'jdoe@gmail.com', |
|
172 | 172 | :mail_notification => 'none' |
|
173 | 173 | }, |
|
174 | 174 | :send_information => '1' |
|
175 | 175 | end |
|
176 | 176 | end |
|
177 | 177 | |
|
178 | 178 | user = User.first(:order => 'id DESC') |
|
179 | 179 | assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id |
|
180 | 180 | |
|
181 | 181 | assert_equal 'John', user.firstname |
|
182 | 182 | assert_equal 'Doe', user.lastname |
|
183 | 183 | assert_equal 'jdoe', user.login |
|
184 | 184 | assert_equal 'jdoe@gmail.com', user.mail |
|
185 | 185 | assert_equal 'none', user.mail_notification |
|
186 | 186 | assert user.check_password?('secret123') |
|
187 | 187 | |
|
188 | 188 | mail = ActionMailer::Base.deliveries.last |
|
189 | 189 | assert_not_nil mail |
|
190 | 190 | assert_equal [user.mail], mail.bcc |
|
191 | 191 | assert_mail_body_match 'secret', mail |
|
192 | 192 | end |
|
193 | 193 | |
|
194 | 194 | def test_create_with_preferences |
|
195 | 195 | assert_difference 'User.count' do |
|
196 | 196 | post :create, |
|
197 | 197 | :user => { |
|
198 | 198 | :firstname => 'John', |
|
199 | 199 | :lastname => 'Doe', |
|
200 | 200 | :login => 'jdoe', |
|
201 | 201 | :password => 'secret123', |
|
202 | 202 | :password_confirmation => 'secret123', |
|
203 | 203 | :mail => 'jdoe@gmail.com', |
|
204 | 204 | :mail_notification => 'none' |
|
205 | 205 | }, |
|
206 | 206 | :pref => { |
|
207 | 207 | 'hide_mail' => '1', |
|
208 | 208 | 'time_zone' => 'Paris', |
|
209 | 209 | 'comments_sorting' => 'desc', |
|
210 | 210 | 'warn_on_leaving_unsaved' => '0' |
|
211 | 211 | } |
|
212 | 212 | end |
|
213 | 213 | user = User.first(:order => 'id DESC') |
|
214 | 214 | assert_equal 'jdoe', user.login |
|
215 | 215 | assert_equal true, user.pref.hide_mail |
|
216 | 216 | assert_equal 'Paris', user.pref.time_zone |
|
217 | 217 | assert_equal 'desc', user.pref[:comments_sorting] |
|
218 | 218 | assert_equal '0', user.pref[:warn_on_leaving_unsaved] |
|
219 | 219 | end |
|
220 | 220 | |
|
221 | 221 | def test_create_with_generate_password_should_email_the_password |
|
222 | 222 | assert_difference 'User.count' do |
|
223 | 223 | post :create, :user => { |
|
224 | 224 | :login => 'randompass', |
|
225 | 225 | :firstname => 'Random', |
|
226 | 226 | :lastname => 'Pass', |
|
227 | 227 | :mail => 'randompass@example.net', |
|
228 | 228 | :language => 'en', |
|
229 | 229 | :generate_password => '1', |
|
230 | 230 | :password => '', |
|
231 | 231 | :password_confirmation => '' |
|
232 | 232 | }, :send_information => 1 |
|
233 | 233 | end |
|
234 | 234 | user = User.order('id DESC').first |
|
235 | 235 | assert_equal 'randompass', user.login |
|
236 | 236 | |
|
237 | 237 | mail = ActionMailer::Base.deliveries.last |
|
238 | 238 | assert_not_nil mail |
|
239 | 239 | m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/) |
|
240 | 240 | assert m |
|
241 | 241 | password = m[1] |
|
242 | 242 | assert user.check_password?(password) |
|
243 | 243 | end |
|
244 | 244 | |
|
245 | 245 | def test_create_with_failure |
|
246 | 246 | assert_no_difference 'User.count' do |
|
247 | 247 | post :create, :user => {} |
|
248 | 248 | end |
|
249 | 249 | assert_response :success |
|
250 | 250 | assert_template 'new' |
|
251 | 251 | end |
|
252 | 252 | |
|
253 | def test_create_with_failure_sould_preserve_preference | |
|
254 | assert_no_difference 'User.count' do | |
|
255 | post :create, | |
|
256 | :user => {}, | |
|
257 | :pref => { | |
|
258 | 'no_self_notified' => '1', | |
|
259 | 'hide_mail' => '1', | |
|
260 | 'time_zone' => 'Paris', | |
|
261 | 'comments_sorting' => 'desc', | |
|
262 | 'warn_on_leaving_unsaved' => '0' | |
|
263 | } | |
|
264 | end | |
|
265 | assert_response :success | |
|
266 | assert_template 'new' | |
|
267 | ||
|
268 | assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/ | |
|
269 | assert_select 'input#pref_no_self_notified[value=1][checked=checked]' | |
|
270 | end | |
|
271 | ||
|
253 | 272 | def test_edit |
|
254 | 273 | get :edit, :id => 2 |
|
255 | 274 | assert_response :success |
|
256 | 275 | assert_template 'edit' |
|
257 | 276 | assert_equal User.find(2), assigns(:user) |
|
258 | 277 | end |
|
259 | 278 | |
|
260 | 279 | def test_update |
|
261 | 280 | ActionMailer::Base.deliveries.clear |
|
262 | 281 | put :update, :id => 2, |
|
263 | 282 | :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, |
|
264 | 283 | :pref => {:hide_mail => '1', :comments_sorting => 'desc'} |
|
265 | 284 | user = User.find(2) |
|
266 | 285 | assert_equal 'Changed', user.firstname |
|
267 | 286 | assert_equal 'only_assigned', user.mail_notification |
|
268 | 287 | assert_equal true, user.pref[:hide_mail] |
|
269 | 288 | assert_equal 'desc', user.pref[:comments_sorting] |
|
270 | 289 | assert ActionMailer::Base.deliveries.empty? |
|
271 | 290 | end |
|
272 | 291 | |
|
273 | 292 | def test_update_with_failure |
|
274 | 293 | assert_no_difference 'User.count' do |
|
275 | 294 | put :update, :id => 2, :user => {:firstname => ''} |
|
276 | 295 | end |
|
277 | 296 | assert_response :success |
|
278 | 297 | assert_template 'edit' |
|
279 | 298 | end |
|
280 | 299 | |
|
281 | 300 | def test_update_with_group_ids_should_assign_groups |
|
282 | 301 | put :update, :id => 2, :user => {:group_ids => ['10']} |
|
283 | 302 | user = User.find(2) |
|
284 | 303 | assert_equal [10], user.group_ids |
|
285 | 304 | end |
|
286 | 305 | |
|
287 | 306 | def test_update_with_activation_should_send_a_notification |
|
288 | 307 | u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') |
|
289 | 308 | u.login = 'foo' |
|
290 | 309 | u.status = User::STATUS_REGISTERED |
|
291 | 310 | u.save! |
|
292 | 311 | ActionMailer::Base.deliveries.clear |
|
293 | 312 | Setting.bcc_recipients = '1' |
|
294 | 313 | |
|
295 | 314 | put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE} |
|
296 | 315 | assert u.reload.active? |
|
297 | 316 | mail = ActionMailer::Base.deliveries.last |
|
298 | 317 | assert_not_nil mail |
|
299 | 318 | assert_equal ['foo.bar@somenet.foo'], mail.bcc |
|
300 | 319 | assert_mail_body_match ll('fr', :notice_account_activated), mail |
|
301 | 320 | end |
|
302 | 321 | |
|
303 | 322 | def test_update_with_password_change_should_send_a_notification |
|
304 | 323 | ActionMailer::Base.deliveries.clear |
|
305 | 324 | Setting.bcc_recipients = '1' |
|
306 | 325 | |
|
307 | 326 | put :update, :id => 2, :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}, :send_information => '1' |
|
308 | 327 | u = User.find(2) |
|
309 | 328 | assert u.check_password?('newpass123') |
|
310 | 329 | |
|
311 | 330 | mail = ActionMailer::Base.deliveries.last |
|
312 | 331 | assert_not_nil mail |
|
313 | 332 | assert_equal [u.mail], mail.bcc |
|
314 | 333 | assert_mail_body_match 'newpass123', mail |
|
315 | 334 | end |
|
316 | 335 | |
|
317 | 336 | def test_update_with_generate_password_should_email_the_password |
|
318 | 337 | ActionMailer::Base.deliveries.clear |
|
319 | 338 | Setting.bcc_recipients = '1' |
|
320 | 339 | |
|
321 | 340 | put :update, :id => 2, :user => { |
|
322 | 341 | :generate_password => '1', |
|
323 | 342 | :password => '', |
|
324 | 343 | :password_confirmation => '' |
|
325 | 344 | }, :send_information => '1' |
|
326 | 345 | |
|
327 | 346 | mail = ActionMailer::Base.deliveries.last |
|
328 | 347 | assert_not_nil mail |
|
329 | 348 | m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/) |
|
330 | 349 | assert m |
|
331 | 350 | password = m[1] |
|
332 | 351 | assert User.find(2).check_password?(password) |
|
333 | 352 | end |
|
334 | 353 | |
|
335 | 354 | def test_update_without_generate_password_should_not_change_password |
|
336 | 355 | put :update, :id => 2, :user => { |
|
337 | 356 | :firstname => 'changed', |
|
338 | 357 | :generate_password => '0', |
|
339 | 358 | :password => '', |
|
340 | 359 | :password_confirmation => '' |
|
341 | 360 | }, :send_information => '1' |
|
342 | 361 | |
|
343 | 362 | user = User.find(2) |
|
344 | 363 | assert_equal 'changed', user.firstname |
|
345 | 364 | assert user.check_password?('jsmith') |
|
346 | 365 | end |
|
347 | 366 | |
|
348 | 367 | def test_update_user_switchin_from_auth_source_to_password_authentication |
|
349 | 368 | # Configure as auth source |
|
350 | 369 | u = User.find(2) |
|
351 | 370 | u.auth_source = AuthSource.find(1) |
|
352 | 371 | u.save! |
|
353 | 372 | |
|
354 | 373 | put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'} |
|
355 | 374 | |
|
356 | 375 | assert_equal nil, u.reload.auth_source |
|
357 | 376 | assert u.check_password?('newpass123') |
|
358 | 377 | end |
|
359 | 378 | |
|
360 | 379 | def test_update_notified_project |
|
361 | 380 | get :edit, :id => 2 |
|
362 | 381 | assert_response :success |
|
363 | 382 | assert_template 'edit' |
|
364 | 383 | u = User.find(2) |
|
365 | 384 | assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort |
|
366 | 385 | assert_equal [1, 2, 5], u.notified_projects_ids.sort |
|
367 | 386 | assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1' |
|
368 | 387 | assert_equal 'all', u.mail_notification |
|
369 | 388 | put :update, :id => 2, |
|
370 | 389 | :user => { |
|
371 | 390 | :mail_notification => 'selected', |
|
372 | 391 | :notified_project_ids => [1, 2] |
|
373 | 392 | } |
|
374 | 393 | u = User.find(2) |
|
375 | 394 | assert_equal 'selected', u.mail_notification |
|
376 | 395 | assert_equal [1, 2], u.notified_projects_ids.sort |
|
377 | 396 | end |
|
378 | 397 | |
|
379 | 398 | def test_update_status_should_not_update_attributes |
|
380 | 399 | user = User.find(2) |
|
381 | 400 | user.pref[:no_self_notified] = '1' |
|
382 | 401 | user.pref.save |
|
383 | 402 | |
|
384 | 403 | put :update, :id => 2, :user => {:status => 3} |
|
385 | 404 | assert_response 302 |
|
386 | 405 | user = User.find(2) |
|
387 | 406 | assert_equal 3, user.status |
|
388 | 407 | assert_equal '1', user.pref[:no_self_notified] |
|
389 | 408 | end |
|
390 | 409 | |
|
391 | 410 | def test_destroy |
|
392 | 411 | assert_difference 'User.count', -1 do |
|
393 | 412 | delete :destroy, :id => 2 |
|
394 | 413 | end |
|
395 | 414 | assert_redirected_to '/users' |
|
396 | 415 | assert_nil User.find_by_id(2) |
|
397 | 416 | end |
|
398 | 417 | |
|
399 | 418 | def test_destroy_should_be_denied_for_non_admin_users |
|
400 | 419 | @request.session[:user_id] = 3 |
|
401 | 420 | |
|
402 | 421 | assert_no_difference 'User.count' do |
|
403 | 422 | get :destroy, :id => 2 |
|
404 | 423 | end |
|
405 | 424 | assert_response 403 |
|
406 | 425 | end |
|
407 | 426 | |
|
408 | 427 | def test_destroy_should_redirect_to_back_url_param |
|
409 | 428 | assert_difference 'User.count', -1 do |
|
410 | 429 | delete :destroy, :id => 2, :back_url => '/users?name=foo' |
|
411 | 430 | end |
|
412 | 431 | assert_redirected_to '/users?name=foo' |
|
413 | 432 | end |
|
414 | 433 | |
|
415 | 434 | def test_create_membership |
|
416 | 435 | assert_difference 'Member.count' do |
|
417 | 436 | post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]} |
|
418 | 437 | end |
|
419 | 438 | assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships' |
|
420 | 439 | member = Member.first(:order => 'id DESC') |
|
421 | 440 | assert_equal User.find(7), member.principal |
|
422 | 441 | assert_equal [2], member.role_ids |
|
423 | 442 | assert_equal 3, member.project_id |
|
424 | 443 | end |
|
425 | 444 | |
|
426 | 445 | def test_create_membership_js_format |
|
427 | 446 | assert_difference 'Member.count' do |
|
428 | 447 | post :edit_membership, :id => 7, :membership => {:project_id => 3, :role_ids => [2]}, :format => 'js' |
|
429 | 448 | assert_response :success |
|
430 | 449 | assert_template 'edit_membership' |
|
431 | 450 | assert_equal 'text/javascript', response.content_type |
|
432 | 451 | end |
|
433 | 452 | member = Member.first(:order => 'id DESC') |
|
434 | 453 | assert_equal User.find(7), member.principal |
|
435 | 454 | assert_equal [2], member.role_ids |
|
436 | 455 | assert_equal 3, member.project_id |
|
437 | 456 | assert_include 'tab-content-memberships', response.body |
|
438 | 457 | end |
|
439 | 458 | |
|
440 | 459 | def test_create_membership_js_format_with_failure |
|
441 | 460 | assert_no_difference 'Member.count' do |
|
442 | 461 | post :edit_membership, :id => 7, :membership => {:project_id => 3}, :format => 'js' |
|
443 | 462 | assert_response :success |
|
444 | 463 | assert_template 'edit_membership' |
|
445 | 464 | assert_equal 'text/javascript', response.content_type |
|
446 | 465 | end |
|
447 | 466 | assert_include 'alert', response.body, "Alert message not sent" |
|
448 | 467 | assert_include 'Role can\\\'t be empty', response.body, "Error message not sent" |
|
449 | 468 | end |
|
450 | 469 | |
|
451 | 470 | def test_update_membership |
|
452 | 471 | assert_no_difference 'Member.count' do |
|
453 | 472 | put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]} |
|
454 | 473 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
455 | 474 | end |
|
456 | 475 | assert_equal [2], Member.find(1).role_ids |
|
457 | 476 | end |
|
458 | 477 | |
|
459 | 478 | def test_update_membership_js_format |
|
460 | 479 | assert_no_difference 'Member.count' do |
|
461 | 480 | put :edit_membership, :id => 2, :membership_id => 1, :membership => {:role_ids => [2]}, :format => 'js' |
|
462 | 481 | assert_response :success |
|
463 | 482 | assert_template 'edit_membership' |
|
464 | 483 | assert_equal 'text/javascript', response.content_type |
|
465 | 484 | end |
|
466 | 485 | assert_equal [2], Member.find(1).role_ids |
|
467 | 486 | assert_include 'tab-content-memberships', response.body |
|
468 | 487 | end |
|
469 | 488 | |
|
470 | 489 | def test_destroy_membership |
|
471 | 490 | assert_difference 'Member.count', -1 do |
|
472 | 491 | delete :destroy_membership, :id => 2, :membership_id => 1 |
|
473 | 492 | end |
|
474 | 493 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
475 | 494 | assert_nil Member.find_by_id(1) |
|
476 | 495 | end |
|
477 | 496 | |
|
478 | 497 | def test_destroy_membership_js_format |
|
479 | 498 | assert_difference 'Member.count', -1 do |
|
480 | 499 | delete :destroy_membership, :id => 2, :membership_id => 1, :format => 'js' |
|
481 | 500 | assert_response :success |
|
482 | 501 | assert_template 'destroy_membership' |
|
483 | 502 | assert_equal 'text/javascript', response.content_type |
|
484 | 503 | end |
|
485 | 504 | assert_nil Member.find_by_id(1) |
|
486 | 505 | assert_include 'tab-content-memberships', response.body |
|
487 | 506 | end |
|
488 | 507 | end |
General Comments 0
You need to be logged in to leave comments.
Login now