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