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