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