@@ -1,214 +1,214 | |||
|
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 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 self, @user_count, @limit, params['page'] |
|
49 | 49 | @offset ||= @user_pages.current.offset |
|
50 | 50 | @users = scope.find :all, |
|
51 | 51 | :order => sort_clause, |
|
52 | 52 | :limit => @limit, |
|
53 | 53 | :offset => @offset |
|
54 | 54 | |
|
55 | 55 | respond_to do |format| |
|
56 | 56 | format.html { |
|
57 | 57 | @groups = Group.all.sort |
|
58 | 58 | render :layout => !request.xhr? |
|
59 | 59 | } |
|
60 | 60 | format.api |
|
61 | 61 | end |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def show |
|
65 | 65 | # show projects based on current user visibility |
|
66 | 66 | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) |
|
67 | 67 | |
|
68 | 68 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
69 | 69 | @events_by_day = events.group_by(&:event_date) |
|
70 | 70 | |
|
71 | 71 | unless User.current.admin? |
|
72 | 72 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
73 | 73 | render_404 |
|
74 | 74 | return |
|
75 | 75 | end |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | respond_to do |format| |
|
79 | 79 | format.html { render :layout => 'base' } |
|
80 | 80 | format.api |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def new |
|
85 | 85 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
86 | 86 | @auth_sources = AuthSource.find(:all) |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | def create |
|
90 | 90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
91 | 91 | @user.safe_attributes = params[:user] |
|
92 | 92 | @user.admin = params[:user][:admin] || false |
|
93 | 93 | @user.login = params[:user][:login] |
|
94 | 94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
|
95 | 95 | |
|
96 | 96 | if @user.save |
|
97 | 97 | @user.pref.attributes = params[:pref] |
|
98 | 98 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
99 | 99 | @user.pref.save |
|
100 | 100 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
101 | 101 | |
|
102 | 102 | Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information] |
|
103 | 103 | |
|
104 | 104 | respond_to do |format| |
|
105 | 105 | format.html { |
|
106 | 106 | flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) |
|
107 | 107 | redirect_to(params[:continue] ? |
|
108 | 108 | {:controller => 'users', :action => 'new'} : |
|
109 | 109 | {:controller => 'users', :action => 'edit', :id => @user} |
|
110 | 110 | ) |
|
111 | 111 | } |
|
112 | 112 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } |
|
113 | 113 | end |
|
114 | 114 | else |
|
115 | 115 | @auth_sources = AuthSource.find(:all) |
|
116 | 116 | # Clear password input |
|
117 | 117 | @user.password = @user.password_confirmation = nil |
|
118 | 118 | |
|
119 | 119 | respond_to do |format| |
|
120 | 120 | format.html { render :action => 'new' } |
|
121 | 121 | format.api { render_validation_errors(@user) } |
|
122 | 122 | end |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def edit |
|
127 | 127 | @auth_sources = AuthSource.find(:all) |
|
128 | 128 | @membership ||= Member.new |
|
129 | 129 | end |
|
130 | 130 | |
|
131 | 131 | def update |
|
132 | 132 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
133 | 133 | @user.login = params[:user][:login] if params[:user][:login] |
|
134 | 134 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
135 | 135 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
136 | 136 | end |
|
137 | 137 | @user.safe_attributes = params[:user] |
|
138 | 138 | # Was the account actived ? (do it before User#save clears the change) |
|
139 | 139 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
140 | 140 | # TODO: Similar to My#account |
|
141 | 141 | @user.pref.attributes = params[:pref] |
|
142 | 142 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
143 | 143 | |
|
144 | 144 | if @user.save |
|
145 | 145 | @user.pref.save |
|
146 | 146 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
147 | 147 | |
|
148 | 148 | if was_activated |
|
149 | 149 | Mailer.account_activated(@user).deliver |
|
150 | 150 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? |
|
151 | 151 | Mailer.account_information(@user, params[:user][:password]).deliver |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | respond_to do |format| |
|
155 | 155 | format.html { |
|
156 | 156 | flash[:notice] = l(:notice_successful_update) |
|
157 | 157 | redirect_to_referer_or edit_user_path(@user) |
|
158 | 158 | } |
|
159 | 159 | format.api { render_api_ok } |
|
160 | 160 | end |
|
161 | 161 | else |
|
162 | 162 | @auth_sources = AuthSource.find(:all) |
|
163 | 163 | @membership ||= Member.new |
|
164 | 164 | # Clear password input |
|
165 | 165 | @user.password = @user.password_confirmation = nil |
|
166 | 166 | |
|
167 | 167 | respond_to do |format| |
|
168 | 168 | format.html { render :action => :edit } |
|
169 | 169 | format.api { render_validation_errors(@user) } |
|
170 | 170 | end |
|
171 | 171 | end |
|
172 | 172 | end |
|
173 | 173 | |
|
174 | 174 | def destroy |
|
175 | 175 | @user.destroy |
|
176 | 176 | respond_to do |format| |
|
177 |
format.html { redirect_ |
|
|
177 | format.html { redirect_back_or_default(users_url) } | |
|
178 | 178 | format.api { render_api_ok } |
|
179 | 179 | end |
|
180 | 180 | end |
|
181 | 181 | |
|
182 | 182 | def edit_membership |
|
183 | 183 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
184 | 184 | @membership.save |
|
185 | 185 | respond_to do |format| |
|
186 | 186 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
187 | 187 | format.js |
|
188 | 188 | end |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | def destroy_membership |
|
192 | 192 | @membership = Member.find(params[:membership_id]) |
|
193 | 193 | if @membership.deletable? |
|
194 | 194 | @membership.destroy |
|
195 | 195 | end |
|
196 | 196 | respond_to do |format| |
|
197 | 197 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
198 | 198 | format.js |
|
199 | 199 | end |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | private |
|
203 | 203 | |
|
204 | 204 | def find_user |
|
205 | 205 | if params[:id] == 'current' |
|
206 | 206 | require_login || return |
|
207 | 207 | @user = User.current |
|
208 | 208 | else |
|
209 | 209 | @user = User.find(params[:id]) |
|
210 | 210 | end |
|
211 | 211 | rescue ActiveRecord::RecordNotFound |
|
212 | 212 | render_404 |
|
213 | 213 | end |
|
214 | 214 | end |
@@ -1,58 +1,58 | |||
|
1 | 1 | <div class="contextual"> |
|
2 | 2 | <%= link_to l(:label_user_new), new_user_path, :class => 'icon icon-add' %> |
|
3 | 3 | </div> |
|
4 | 4 | |
|
5 | 5 | <h2><%=l(:label_user_plural)%></h2> |
|
6 | 6 | |
|
7 | 7 | <%= form_tag({}, :method => :get) do %> |
|
8 | 8 | <fieldset><legend><%= l(:label_filter_plural) %></legend> |
|
9 | 9 | <label for='status'><%= l(:field_status) %>:</label> |
|
10 | 10 | <%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;" %> |
|
11 | 11 | |
|
12 | 12 | <% if @groups.present? %> |
|
13 | 13 | <label for='group_id'><%= l(:label_group) %>:</label> |
|
14 | 14 | <%= select_tag 'group_id', content_tag('option') + options_from_collection_for_select(@groups, :id, :name, params[:group_id].to_i), :onchange => "this.form.submit(); return false;" %> |
|
15 | 15 | <% end %> |
|
16 | 16 | |
|
17 | 17 | <label for='name'><%= l(:label_user) %>:</label> |
|
18 | 18 | <%= text_field_tag 'name', params[:name], :size => 30 %> |
|
19 | 19 | <%= submit_tag l(:button_apply), :class => "small", :name => nil %> |
|
20 | 20 | <%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %> |
|
21 | 21 | </fieldset> |
|
22 | 22 | <% end %> |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | <div class="autoscroll"> |
|
26 | 26 | <table class="list"> |
|
27 | 27 | <thead><tr> |
|
28 | 28 | <%= sort_header_tag('login', :caption => l(:field_login)) %> |
|
29 | 29 | <%= sort_header_tag('firstname', :caption => l(:field_firstname)) %> |
|
30 | 30 | <%= sort_header_tag('lastname', :caption => l(:field_lastname)) %> |
|
31 | 31 | <%= sort_header_tag('mail', :caption => l(:field_mail)) %> |
|
32 | 32 | <%= sort_header_tag('admin', :caption => l(:field_admin), :default_order => 'desc') %> |
|
33 | 33 | <%= sort_header_tag('created_on', :caption => l(:field_created_on), :default_order => 'desc') %> |
|
34 | 34 | <%= sort_header_tag('last_login_on', :caption => l(:field_last_login_on), :default_order => 'desc') %> |
|
35 | 35 | <th></th> |
|
36 | 36 | </tr></thead> |
|
37 | 37 | <tbody> |
|
38 | 38 | <% for user in @users -%> |
|
39 | 39 | <tr class="user <%= cycle("odd", "even") %> <%= %w(anon active registered locked)[user.status] %>"> |
|
40 | 40 | <td class="username"><%= avatar(user, :size => "14") %><%= link_to h(user.login), edit_user_path(user) %></td> |
|
41 | 41 | <td class="firstname"><%= h(user.firstname) %></td> |
|
42 | 42 | <td class="lastname"><%= h(user.lastname) %></td> |
|
43 | 43 | <td class="email"><%= mail_to(h(user.mail)) %></td> |
|
44 | 44 | <td align="center"><%= checked_image user.admin? %></td> |
|
45 | 45 | <td class="created_on" align="center"><%= format_time(user.created_on) %></td> |
|
46 | 46 | <td class="last_login_on" align="center"><%= format_time(user.last_login_on) unless user.last_login_on.nil? %></td> |
|
47 | 47 | <td class="buttons"> |
|
48 | 48 | <%= change_status_link(user) %> |
|
49 | <%= delete_link user_path(user) unless User.current == user %> | |
|
49 | <%= delete_link user_path(user, :back_url => users_path(params)) unless User.current == user %> | |
|
50 | 50 | </td> |
|
51 | 51 | </tr> |
|
52 | 52 | <% end -%> |
|
53 | 53 | </tbody> |
|
54 | 54 | </table> |
|
55 | 55 | </div> |
|
56 | 56 | <p class="pagination"><%= pagination_links_full @user_pages, @user_count %></p> |
|
57 | 57 | |
|
58 | 58 | <% html_title(l(:label_user_plural)) -%> |
@@ -1,425 +1,432 | |||
|
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 'users_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class UsersController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class UsersControllerTest < ActionController::TestCase |
|
25 | 25 | include Redmine::I18n |
|
26 | 26 | |
|
27 | 27 | fixtures :users, :projects, :members, :member_roles, :roles, |
|
28 | 28 | :custom_fields, :custom_values, :groups_users, |
|
29 | 29 | :auth_sources |
|
30 | 30 | |
|
31 | 31 | def setup |
|
32 | 32 | @controller = UsersController.new |
|
33 | 33 | @request = ActionController::TestRequest.new |
|
34 | 34 | @response = ActionController::TestResponse.new |
|
35 | 35 | User.current = nil |
|
36 | 36 | @request.session[:user_id] = 1 # admin |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def test_index |
|
40 | 40 | get :index |
|
41 | 41 | assert_response :success |
|
42 | 42 | assert_template 'index' |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def test_index |
|
46 | 46 | get :index |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_template 'index' |
|
49 | 49 | assert_not_nil assigns(:users) |
|
50 | 50 | # active users only |
|
51 | 51 | assert_nil assigns(:users).detect {|u| !u.active?} |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def test_index_with_status_filter |
|
55 | 55 | get :index, :status => 3 |
|
56 | 56 | assert_response :success |
|
57 | 57 | assert_template 'index' |
|
58 | 58 | assert_not_nil assigns(:users) |
|
59 | 59 | assert_equal [3], assigns(:users).map(&:status).uniq |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def test_index_with_name_filter |
|
63 | 63 | get :index, :name => 'john' |
|
64 | 64 | assert_response :success |
|
65 | 65 | assert_template 'index' |
|
66 | 66 | users = assigns(:users) |
|
67 | 67 | assert_not_nil users |
|
68 | 68 | assert_equal 1, users.size |
|
69 | 69 | assert_equal 'John', users.first.firstname |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def test_index_with_group_filter |
|
73 | 73 | get :index, :group_id => '10' |
|
74 | 74 | assert_response :success |
|
75 | 75 | assert_template 'index' |
|
76 | 76 | users = assigns(:users) |
|
77 | 77 | assert users.any? |
|
78 | 78 | assert_equal([], (users - Group.find(10).users)) |
|
79 | 79 | assert_select 'select[name=group_id]' do |
|
80 | 80 | assert_select 'option[value=10][selected=selected]' |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def test_show |
|
85 | 85 | @request.session[:user_id] = nil |
|
86 | 86 | get :show, :id => 2 |
|
87 | 87 | assert_response :success |
|
88 | 88 | assert_template 'show' |
|
89 | 89 | assert_not_nil assigns(:user) |
|
90 | 90 | |
|
91 | 91 | assert_tag 'li', :content => /Phone number/ |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def test_show_should_not_display_hidden_custom_fields |
|
95 | 95 | @request.session[:user_id] = nil |
|
96 | 96 | UserCustomField.find_by_name('Phone number').update_attribute :visible, false |
|
97 | 97 | get :show, :id => 2 |
|
98 | 98 | assert_response :success |
|
99 | 99 | assert_template 'show' |
|
100 | 100 | assert_not_nil assigns(:user) |
|
101 | 101 | |
|
102 | 102 | assert_no_tag 'li', :content => /Phone number/ |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def test_show_should_not_fail_when_custom_values_are_nil |
|
106 | 106 | user = User.find(2) |
|
107 | 107 | |
|
108 | 108 | # Create a custom field to illustrate the issue |
|
109 | 109 | custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text') |
|
110 | 110 | custom_value = user.custom_values.build(:custom_field => custom_field).save! |
|
111 | 111 | |
|
112 | 112 | get :show, :id => 2 |
|
113 | 113 | assert_response :success |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | def test_show_inactive |
|
117 | 117 | @request.session[:user_id] = nil |
|
118 | 118 | get :show, :id => 5 |
|
119 | 119 | assert_response 404 |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def test_show_should_not_reveal_users_with_no_visible_activity_or_project |
|
123 | 123 | @request.session[:user_id] = nil |
|
124 | 124 | get :show, :id => 9 |
|
125 | 125 | assert_response 404 |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | def test_show_inactive_by_admin |
|
129 | 129 | @request.session[:user_id] = 1 |
|
130 | 130 | get :show, :id => 5 |
|
131 | 131 | assert_response 200 |
|
132 | 132 | assert_not_nil assigns(:user) |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def test_show_displays_memberships_based_on_project_visibility |
|
136 | 136 | @request.session[:user_id] = 1 |
|
137 | 137 | get :show, :id => 2 |
|
138 | 138 | assert_response :success |
|
139 | 139 | memberships = assigns(:memberships) |
|
140 | 140 | assert_not_nil memberships |
|
141 | 141 | project_ids = memberships.map(&:project_id) |
|
142 | 142 | assert project_ids.include?(2) #private project admin can see |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | def test_show_current_should_require_authentication |
|
146 | 146 | @request.session[:user_id] = nil |
|
147 | 147 | get :show, :id => 'current' |
|
148 | 148 | assert_response 302 |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | def test_show_current |
|
152 | 152 | @request.session[:user_id] = 2 |
|
153 | 153 | get :show, :id => 'current' |
|
154 | 154 | assert_response :success |
|
155 | 155 | assert_template 'show' |
|
156 | 156 | assert_equal User.find(2), assigns(:user) |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | def test_new |
|
160 | 160 | get :new |
|
161 | 161 | assert_response :success |
|
162 | 162 | assert_template :new |
|
163 | 163 | assert assigns(:user) |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | def test_create |
|
167 | 167 | Setting.bcc_recipients = '1' |
|
168 | 168 | |
|
169 | 169 | assert_difference 'User.count' do |
|
170 | 170 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
171 | 171 | post :create, |
|
172 | 172 | :user => { |
|
173 | 173 | :firstname => 'John', |
|
174 | 174 | :lastname => 'Doe', |
|
175 | 175 | :login => 'jdoe', |
|
176 | 176 | :password => 'secret', |
|
177 | 177 | :password_confirmation => 'secret', |
|
178 | 178 | :mail => 'jdoe@gmail.com', |
|
179 | 179 | :mail_notification => 'none' |
|
180 | 180 | }, |
|
181 | 181 | :send_information => '1' |
|
182 | 182 | end |
|
183 | 183 | end |
|
184 | 184 | |
|
185 | 185 | user = User.first(:order => 'id DESC') |
|
186 | 186 | assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id |
|
187 | 187 | |
|
188 | 188 | assert_equal 'John', user.firstname |
|
189 | 189 | assert_equal 'Doe', user.lastname |
|
190 | 190 | assert_equal 'jdoe', user.login |
|
191 | 191 | assert_equal 'jdoe@gmail.com', user.mail |
|
192 | 192 | assert_equal 'none', user.mail_notification |
|
193 | 193 | assert user.check_password?('secret') |
|
194 | 194 | |
|
195 | 195 | mail = ActionMailer::Base.deliveries.last |
|
196 | 196 | assert_not_nil mail |
|
197 | 197 | assert_equal [user.mail], mail.bcc |
|
198 | 198 | assert_mail_body_match 'secret', mail |
|
199 | 199 | end |
|
200 | 200 | |
|
201 | 201 | def test_create_with_preferences |
|
202 | 202 | assert_difference 'User.count' do |
|
203 | 203 | post :create, |
|
204 | 204 | :user => { |
|
205 | 205 | :firstname => 'John', |
|
206 | 206 | :lastname => 'Doe', |
|
207 | 207 | :login => 'jdoe', |
|
208 | 208 | :password => 'secret', |
|
209 | 209 | :password_confirmation => 'secret', |
|
210 | 210 | :mail => 'jdoe@gmail.com', |
|
211 | 211 | :mail_notification => 'none' |
|
212 | 212 | }, |
|
213 | 213 | :pref => { |
|
214 | 214 | 'hide_mail' => '1', |
|
215 | 215 | 'time_zone' => 'Paris', |
|
216 | 216 | 'comments_sorting' => 'desc', |
|
217 | 217 | 'warn_on_leaving_unsaved' => '0' |
|
218 | 218 | } |
|
219 | 219 | end |
|
220 | 220 | user = User.first(:order => 'id DESC') |
|
221 | 221 | assert_equal 'jdoe', user.login |
|
222 | 222 | assert_equal true, user.pref.hide_mail |
|
223 | 223 | assert_equal 'Paris', user.pref.time_zone |
|
224 | 224 | assert_equal 'desc', user.pref[:comments_sorting] |
|
225 | 225 | assert_equal '0', user.pref[:warn_on_leaving_unsaved] |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | def test_create_with_failure |
|
229 | 229 | assert_no_difference 'User.count' do |
|
230 | 230 | post :create, :user => {} |
|
231 | 231 | end |
|
232 | 232 | assert_response :success |
|
233 | 233 | assert_template 'new' |
|
234 | 234 | end |
|
235 | 235 | |
|
236 | 236 | def test_edit |
|
237 | 237 | get :edit, :id => 2 |
|
238 | 238 | assert_response :success |
|
239 | 239 | assert_template 'edit' |
|
240 | 240 | assert_equal User.find(2), assigns(:user) |
|
241 | 241 | end |
|
242 | 242 | |
|
243 | 243 | def test_update |
|
244 | 244 | ActionMailer::Base.deliveries.clear |
|
245 | 245 | put :update, :id => 2, |
|
246 | 246 | :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, |
|
247 | 247 | :pref => {:hide_mail => '1', :comments_sorting => 'desc'} |
|
248 | 248 | user = User.find(2) |
|
249 | 249 | assert_equal 'Changed', user.firstname |
|
250 | 250 | assert_equal 'only_assigned', user.mail_notification |
|
251 | 251 | assert_equal true, user.pref[:hide_mail] |
|
252 | 252 | assert_equal 'desc', user.pref[:comments_sorting] |
|
253 | 253 | assert ActionMailer::Base.deliveries.empty? |
|
254 | 254 | end |
|
255 | 255 | |
|
256 | 256 | def test_update_with_failure |
|
257 | 257 | assert_no_difference 'User.count' do |
|
258 | 258 | put :update, :id => 2, :user => {:firstname => ''} |
|
259 | 259 | end |
|
260 | 260 | assert_response :success |
|
261 | 261 | assert_template 'edit' |
|
262 | 262 | end |
|
263 | 263 | |
|
264 | 264 | def test_update_with_group_ids_should_assign_groups |
|
265 | 265 | put :update, :id => 2, :user => {:group_ids => ['10']} |
|
266 | 266 | user = User.find(2) |
|
267 | 267 | assert_equal [10], user.group_ids |
|
268 | 268 | end |
|
269 | 269 | |
|
270 | 270 | def test_update_with_activation_should_send_a_notification |
|
271 | 271 | u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr') |
|
272 | 272 | u.login = 'foo' |
|
273 | 273 | u.status = User::STATUS_REGISTERED |
|
274 | 274 | u.save! |
|
275 | 275 | ActionMailer::Base.deliveries.clear |
|
276 | 276 | Setting.bcc_recipients = '1' |
|
277 | 277 | |
|
278 | 278 | put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE} |
|
279 | 279 | assert u.reload.active? |
|
280 | 280 | mail = ActionMailer::Base.deliveries.last |
|
281 | 281 | assert_not_nil mail |
|
282 | 282 | assert_equal ['foo.bar@somenet.foo'], mail.bcc |
|
283 | 283 | assert_mail_body_match ll('fr', :notice_account_activated), mail |
|
284 | 284 | end |
|
285 | 285 | |
|
286 | 286 | def test_update_with_password_change_should_send_a_notification |
|
287 | 287 | ActionMailer::Base.deliveries.clear |
|
288 | 288 | Setting.bcc_recipients = '1' |
|
289 | 289 | |
|
290 | 290 | put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1' |
|
291 | 291 | u = User.find(2) |
|
292 | 292 | assert u.check_password?('newpass') |
|
293 | 293 | |
|
294 | 294 | mail = ActionMailer::Base.deliveries.last |
|
295 | 295 | assert_not_nil mail |
|
296 | 296 | assert_equal [u.mail], mail.bcc |
|
297 | 297 | assert_mail_body_match 'newpass', mail |
|
298 | 298 | end |
|
299 | 299 | |
|
300 | 300 | def test_update_user_switchin_from_auth_source_to_password_authentication |
|
301 | 301 | # Configure as auth source |
|
302 | 302 | u = User.find(2) |
|
303 | 303 | u.auth_source = AuthSource.find(1) |
|
304 | 304 | u.save! |
|
305 | 305 | |
|
306 | 306 | put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass', :password_confirmation => 'newpass'} |
|
307 | 307 | |
|
308 | 308 | assert_equal nil, u.reload.auth_source |
|
309 | 309 | assert u.check_password?('newpass') |
|
310 | 310 | end |
|
311 | 311 | |
|
312 | 312 | def test_update_notified_project |
|
313 | 313 | get :edit, :id => 2 |
|
314 | 314 | assert_response :success |
|
315 | 315 | assert_template 'edit' |
|
316 | 316 | u = User.find(2) |
|
317 | 317 | assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort |
|
318 | 318 | assert_equal [1, 2, 5], u.notified_projects_ids.sort |
|
319 | 319 | assert_tag :tag => 'input', |
|
320 | 320 | :attributes => { |
|
321 | 321 | :id => 'notified_project_ids_', |
|
322 | 322 | :value => 1, |
|
323 | 323 | } |
|
324 | 324 | assert_equal 'all', u.mail_notification |
|
325 | 325 | put :update, :id => 2, |
|
326 | 326 | :user => { |
|
327 | 327 | :mail_notification => 'selected', |
|
328 | 328 | }, |
|
329 | 329 | :notified_project_ids => [1, 2] |
|
330 | 330 | u = User.find(2) |
|
331 | 331 | assert_equal 'selected', u.mail_notification |
|
332 | 332 | assert_equal [1, 2], u.notified_projects_ids.sort |
|
333 | 333 | end |
|
334 | 334 | |
|
335 | 335 | def test_destroy |
|
336 | 336 | assert_difference 'User.count', -1 do |
|
337 | 337 | delete :destroy, :id => 2 |
|
338 | 338 | end |
|
339 | 339 | assert_redirected_to '/users' |
|
340 | 340 | assert_nil User.find_by_id(2) |
|
341 | 341 | end |
|
342 | 342 | |
|
343 | 343 | def test_destroy_should_be_denied_for_non_admin_users |
|
344 | 344 | @request.session[:user_id] = 3 |
|
345 | 345 | |
|
346 | 346 | assert_no_difference 'User.count' do |
|
347 | 347 | get :destroy, :id => 2 |
|
348 | 348 | end |
|
349 | 349 | assert_response 403 |
|
350 | 350 | end |
|
351 | 351 | |
|
352 | def test_destroy_should_redirect_to_back_url_param | |
|
353 | assert_difference 'User.count', -1 do | |
|
354 | delete :destroy, :id => 2, :back_url => '/users?name=foo' | |
|
355 | end | |
|
356 | assert_redirected_to '/users?name=foo' | |
|
357 | end | |
|
358 | ||
|
352 | 359 | def test_create_membership |
|
353 | 360 | assert_difference 'Member.count' do |
|
354 | 361 | post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]} |
|
355 | 362 | end |
|
356 | 363 | assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships' |
|
357 | 364 | member = Member.first(:order => 'id DESC') |
|
358 | 365 | assert_equal User.find(7), member.principal |
|
359 | 366 | assert_equal [2], member.role_ids |
|
360 | 367 | assert_equal 3, member.project_id |
|
361 | 368 | end |
|
362 | 369 | |
|
363 | 370 | def test_create_membership_js_format |
|
364 | 371 | assert_difference 'Member.count' do |
|
365 | 372 | post :edit_membership, :id => 7, :membership => {:project_id => 3, :role_ids => [2]}, :format => 'js' |
|
366 | 373 | assert_response :success |
|
367 | 374 | assert_template 'edit_membership' |
|
368 | 375 | assert_equal 'text/javascript', response.content_type |
|
369 | 376 | end |
|
370 | 377 | member = Member.first(:order => 'id DESC') |
|
371 | 378 | assert_equal User.find(7), member.principal |
|
372 | 379 | assert_equal [2], member.role_ids |
|
373 | 380 | assert_equal 3, member.project_id |
|
374 | 381 | assert_include 'tab-content-memberships', response.body |
|
375 | 382 | end |
|
376 | 383 | |
|
377 | 384 | def test_create_membership_js_format_with_failure |
|
378 | 385 | assert_no_difference 'Member.count' do |
|
379 | 386 | post :edit_membership, :id => 7, :membership => {:project_id => 3}, :format => 'js' |
|
380 | 387 | assert_response :success |
|
381 | 388 | assert_template 'edit_membership' |
|
382 | 389 | assert_equal 'text/javascript', response.content_type |
|
383 | 390 | end |
|
384 | 391 | assert_include 'alert', response.body, "Alert message not sent" |
|
385 | 392 | assert_include 'Role can\\\'t be empty', response.body, "Error message not sent" |
|
386 | 393 | end |
|
387 | 394 | |
|
388 | 395 | def test_update_membership |
|
389 | 396 | assert_no_difference 'Member.count' do |
|
390 | 397 | put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]} |
|
391 | 398 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
392 | 399 | end |
|
393 | 400 | assert_equal [2], Member.find(1).role_ids |
|
394 | 401 | end |
|
395 | 402 | |
|
396 | 403 | def test_update_membership_js_format |
|
397 | 404 | assert_no_difference 'Member.count' do |
|
398 | 405 | put :edit_membership, :id => 2, :membership_id => 1, :membership => {:role_ids => [2]}, :format => 'js' |
|
399 | 406 | assert_response :success |
|
400 | 407 | assert_template 'edit_membership' |
|
401 | 408 | assert_equal 'text/javascript', response.content_type |
|
402 | 409 | end |
|
403 | 410 | assert_equal [2], Member.find(1).role_ids |
|
404 | 411 | assert_include 'tab-content-memberships', response.body |
|
405 | 412 | end |
|
406 | 413 | |
|
407 | 414 | def test_destroy_membership |
|
408 | 415 | assert_difference 'Member.count', -1 do |
|
409 | 416 | delete :destroy_membership, :id => 2, :membership_id => 1 |
|
410 | 417 | end |
|
411 | 418 | assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
|
412 | 419 | assert_nil Member.find_by_id(1) |
|
413 | 420 | end |
|
414 | 421 | |
|
415 | 422 | def test_destroy_membership_js_format |
|
416 | 423 | assert_difference 'Member.count', -1 do |
|
417 | 424 | delete :destroy_membership, :id => 2, :membership_id => 1, :format => 'js' |
|
418 | 425 | assert_response :success |
|
419 | 426 | assert_template 'destroy_membership' |
|
420 | 427 | assert_equal 'text/javascript', response.content_type |
|
421 | 428 | end |
|
422 | 429 | assert_nil Member.find_by_id(1) |
|
423 | 430 | assert_include 'tab-content-memberships', response.body |
|
424 | 431 | end |
|
425 | 432 | end |
General Comments 0
You need to be logged in to leave comments.
Login now