##// 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 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 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 MyController < ApplicationController
18 class MyController < ApplicationController
19 before_filter :require_login
19 before_filter :require_login
20
20
21 helper :issues
21 helper :issues
22 helper :custom_fields
22 helper :custom_fields
23
23
24 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
24 BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues,
25 'issuesreportedbyme' => :label_reported_issues,
25 'issuesreportedbyme' => :label_reported_issues,
26 'issueswatched' => :label_watched_issues,
26 'issueswatched' => :label_watched_issues,
27 'news' => :label_news_latest,
27 'news' => :label_news_latest,
28 'calendar' => :label_calendar,
28 'calendar' => :label_calendar,
29 'documents' => :label_document_plural,
29 'documents' => :label_document_plural,
30 'timelog' => :label_spent_time
30 'timelog' => :label_spent_time
31 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
31 }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze
32
32
33 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
33 DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'],
34 'right' => ['issuesreportedbyme']
34 'right' => ['issuesreportedbyme']
35 }.freeze
35 }.freeze
36
36
37 verify :xhr => true,
37 verify :xhr => true,
38 :only => [:add_block, :remove_block, :order_blocks]
38 :only => [:add_block, :remove_block, :order_blocks]
39
39
40 def index
40 def index
41 page
41 page
42 render :action => 'page'
42 render :action => 'page'
43 end
43 end
44
44
45 # Show user's page
45 # Show user's page
46 def page
46 def page
47 @user = User.current
47 @user = User.current
48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
48 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT
49 end
49 end
50
50
51 # Edit user's account
51 # Edit user's account
52 def account
52 def account
53 @user = User.current
53 @user = User.current
54 @pref = @user.pref
54 @pref = @user.pref
55 if request.post?
55 if request.post?
56 @user.safe_attributes = params[:user]
56 @user.safe_attributes = params[:user]
57 @user.mail_notification = params[:notification_option] || 'only_my_events'
58 @user.pref.attributes = params[:pref]
57 @user.pref.attributes = params[:pref]
59 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
58 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
60 if @user.save
59 if @user.save
61 @user.pref.save
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 set_language_if_valid @user.language
62 set_language_if_valid @user.language
64 flash[:notice] = l(:notice_account_updated)
63 flash[:notice] = l(:notice_account_updated)
65 redirect_to :action => 'account'
64 redirect_to :action => 'account'
66 return
65 return
67 end
66 end
68 end
67 end
69 @notification_options = @user.valid_notification_options
68 @notification_options = @user.valid_notification_options
70 @notification_option = @user.mail_notification #? ? 'all' : (@user.notified_projects_ids.empty? ? 'none' : 'selected')
71 end
69 end
72
70
73 # Manage user's password
71 # Manage user's password
74 def password
72 def password
75 @user = User.current
73 @user = User.current
76 unless @user.change_password_allowed?
74 unless @user.change_password_allowed?
77 flash[:error] = l(:notice_can_t_change_password)
75 flash[:error] = l(:notice_can_t_change_password)
78 redirect_to :action => 'account'
76 redirect_to :action => 'account'
79 return
77 return
80 end
78 end
81 if request.post?
79 if request.post?
82 if @user.check_password?(params[:password])
80 if @user.check_password?(params[:password])
83 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
81 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
84 if @user.save
82 if @user.save
85 flash[:notice] = l(:notice_account_password_updated)
83 flash[:notice] = l(:notice_account_password_updated)
86 redirect_to :action => 'account'
84 redirect_to :action => 'account'
87 end
85 end
88 else
86 else
89 flash[:error] = l(:notice_account_wrong_password)
87 flash[:error] = l(:notice_account_wrong_password)
90 end
88 end
91 end
89 end
92 end
90 end
93
91
94 # Create a new feeds key
92 # Create a new feeds key
95 def reset_rss_key
93 def reset_rss_key
96 if request.post?
94 if request.post?
97 if User.current.rss_token
95 if User.current.rss_token
98 User.current.rss_token.destroy
96 User.current.rss_token.destroy
99 User.current.reload
97 User.current.reload
100 end
98 end
101 User.current.rss_key
99 User.current.rss_key
102 flash[:notice] = l(:notice_feeds_access_key_reseted)
100 flash[:notice] = l(:notice_feeds_access_key_reseted)
103 end
101 end
104 redirect_to :action => 'account'
102 redirect_to :action => 'account'
105 end
103 end
106
104
107 # Create a new API key
105 # Create a new API key
108 def reset_api_key
106 def reset_api_key
109 if request.post?
107 if request.post?
110 if User.current.api_token
108 if User.current.api_token
111 User.current.api_token.destroy
109 User.current.api_token.destroy
112 User.current.reload
110 User.current.reload
113 end
111 end
114 User.current.api_key
112 User.current.api_key
115 flash[:notice] = l(:notice_api_access_key_reseted)
113 flash[:notice] = l(:notice_api_access_key_reseted)
116 end
114 end
117 redirect_to :action => 'account'
115 redirect_to :action => 'account'
118 end
116 end
119
117
120 # User's page layout configuration
118 # User's page layout configuration
121 def page_layout
119 def page_layout
122 @user = User.current
120 @user = User.current
123 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
121 @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
124 @block_options = []
122 @block_options = []
125 BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]}
123 BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]}
126 end
124 end
127
125
128 # Add a block to user's page
126 # Add a block to user's page
129 # The block is added on top of the page
127 # The block is added on top of the page
130 # params[:block] : id of the block to add
128 # params[:block] : id of the block to add
131 def add_block
129 def add_block
132 block = params[:block].to_s.underscore
130 block = params[:block].to_s.underscore
133 (render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
131 (render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
134 @user = User.current
132 @user = User.current
135 layout = @user.pref[:my_page_layout] || {}
133 layout = @user.pref[:my_page_layout] || {}
136 # remove if already present in a group
134 # remove if already present in a group
137 %w(top left right).each {|f| (layout[f] ||= []).delete block }
135 %w(top left right).each {|f| (layout[f] ||= []).delete block }
138 # add it on top
136 # add it on top
139 layout['top'].unshift block
137 layout['top'].unshift block
140 @user.pref[:my_page_layout] = layout
138 @user.pref[:my_page_layout] = layout
141 @user.pref.save
139 @user.pref.save
142 render :partial => "block", :locals => {:user => @user, :block_name => block}
140 render :partial => "block", :locals => {:user => @user, :block_name => block}
143 end
141 end
144
142
145 # Remove a block to user's page
143 # Remove a block to user's page
146 # params[:block] : id of the block to remove
144 # params[:block] : id of the block to remove
147 def remove_block
145 def remove_block
148 block = params[:block].to_s.underscore
146 block = params[:block].to_s.underscore
149 @user = User.current
147 @user = User.current
150 # remove block in all groups
148 # remove block in all groups
151 layout = @user.pref[:my_page_layout] || {}
149 layout = @user.pref[:my_page_layout] || {}
152 %w(top left right).each {|f| (layout[f] ||= []).delete block }
150 %w(top left right).each {|f| (layout[f] ||= []).delete block }
153 @user.pref[:my_page_layout] = layout
151 @user.pref[:my_page_layout] = layout
154 @user.pref.save
152 @user.pref.save
155 render :nothing => true
153 render :nothing => true
156 end
154 end
157
155
158 # Change blocks order on user's page
156 # Change blocks order on user's page
159 # params[:group] : group to order (top, left or right)
157 # params[:group] : group to order (top, left or right)
160 # params[:list-(top|left|right)] : array of block ids of the group
158 # params[:list-(top|left|right)] : array of block ids of the group
161 def order_blocks
159 def order_blocks
162 group = params[:group]
160 group = params[:group]
163 @user = User.current
161 @user = User.current
164 if group.is_a?(String)
162 if group.is_a?(String)
165 group_items = (params["list-#{group}"] || []).collect(&:underscore)
163 group_items = (params["list-#{group}"] || []).collect(&:underscore)
166 if group_items and group_items.is_a? Array
164 if group_items and group_items.is_a? Array
167 layout = @user.pref[:my_page_layout] || {}
165 layout = @user.pref[:my_page_layout] || {}
168 # remove group blocks if they are presents in other groups
166 # remove group blocks if they are presents in other groups
169 %w(top left right).each {|f|
167 %w(top left right).each {|f|
170 layout[f] = (layout[f] || []) - group_items
168 layout[f] = (layout[f] || []) - group_items
171 }
169 }
172 layout[group] = group_items
170 layout[group] = group_items
173 @user.pref[:my_page_layout] = layout
171 @user.pref[:my_page_layout] = layout
174 @user.pref.save
172 @user.pref.save
175 end
173 end
176 end
174 end
177 render :nothing => true
175 render :nothing => true
178 end
176 end
179 end
177 end
@@ -1,235 +1,228
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
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 @auth_sources = AuthSource.find(:all)
91 @auth_sources = AuthSource.find(:all)
93 end
92 end
94
93
95 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
94 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
96 def create
95 def create
97 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
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 @user.safe_attributes = params[:user]
99 @user.safe_attributes = params[:user]
102 @user.admin = params[:user][:admin] || false
100 @user.admin = params[:user][:admin] || false
103 @user.login = params[:user][:login]
101 @user.login = params[:user][:login]
104 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
102 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
105
103
106 # TODO: Similar to My#account
104 # TODO: Similar to My#account
107 @user.mail_notification = params[:notification_option] || 'only_my_events'
108 @user.pref.attributes = params[:pref]
105 @user.pref.attributes = params[:pref]
109 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
106 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
110
107
111 if @user.save
108 if @user.save
112 @user.pref.save
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 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
112 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
116
113
117 respond_to do |format|
114 respond_to do |format|
118 format.html {
115 format.html {
119 flash[:notice] = l(:notice_successful_create)
116 flash[:notice] = l(:notice_successful_create)
120 redirect_to(params[:continue] ?
117 redirect_to(params[:continue] ?
121 {:controller => 'users', :action => 'new'} :
118 {:controller => 'users', :action => 'new'} :
122 {:controller => 'users', :action => 'edit', :id => @user}
119 {:controller => 'users', :action => 'edit', :id => @user}
123 )
120 )
124 }
121 }
125 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
122 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
126 end
123 end
127 else
124 else
128 @auth_sources = AuthSource.find(:all)
125 @auth_sources = AuthSource.find(:all)
129 @notification_option = @user.mail_notification
130 # Clear password input
126 # Clear password input
131 @user.password = @user.password_confirmation = nil
127 @user.password = @user.password_confirmation = nil
132
128
133 respond_to do |format|
129 respond_to do |format|
134 format.html { render :action => 'new' }
130 format.html { render :action => 'new' }
135 format.api { render_validation_errors(@user) }
131 format.api { render_validation_errors(@user) }
136 end
132 end
137 end
133 end
138 end
134 end
139
135
140 def edit
136 def edit
141 @user = User.find(params[:id])
137 @user = User.find(params[:id])
142 @notification_options = @user.valid_notification_options
138 @notification_options = @user.valid_notification_options
143 @notification_option = @user.mail_notification
144
139
145 @auth_sources = AuthSource.find(:all)
140 @auth_sources = AuthSource.find(:all)
146 @membership ||= Member.new
141 @membership ||= Member.new
147 end
142 end
148
143
149 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
144 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
150 def update
145 def update
151 @user = User.find(params[:id])
146 @user = User.find(params[:id])
152 @notification_options = @user.valid_notification_options
147 @notification_options = @user.valid_notification_options
153 @notification_option = @user.mail_notification
154
148
155 @user.admin = params[:user][:admin] if params[:user][:admin]
149 @user.admin = params[:user][:admin] if params[:user][:admin]
156 @user.login = params[:user][:login] if params[:user][:login]
150 @user.login = params[:user][:login] if params[:user][:login]
157 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
151 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
158 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
152 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
159 end
153 end
160 @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
154 @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
161 @user.safe_attributes = params[:user]
155 @user.safe_attributes = params[:user]
162 # Was the account actived ? (do it before User#save clears the change)
156 # Was the account actived ? (do it before User#save clears the change)
163 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
157 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
164 # TODO: Similar to My#account
158 # TODO: Similar to My#account
165 @user.mail_notification = params[:notification_option] || 'only_my_events'
166 @user.pref.attributes = params[:pref]
159 @user.pref.attributes = params[:pref]
167 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
160 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
168
161
169 if @user.save
162 if @user.save
170 @user.pref.save
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 if was_activated
166 if was_activated
174 Mailer.deliver_account_activated(@user)
167 Mailer.deliver_account_activated(@user)
175 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
168 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
176 Mailer.deliver_account_information(@user, params[:user][:password])
169 Mailer.deliver_account_information(@user, params[:user][:password])
177 end
170 end
178
171
179 respond_to do |format|
172 respond_to do |format|
180 format.html {
173 format.html {
181 flash[:notice] = l(:notice_successful_update)
174 flash[:notice] = l(:notice_successful_update)
182 redirect_to :back
175 redirect_to :back
183 }
176 }
184 format.api { head :ok }
177 format.api { head :ok }
185 end
178 end
186 else
179 else
187 @auth_sources = AuthSource.find(:all)
180 @auth_sources = AuthSource.find(:all)
188 @membership ||= Member.new
181 @membership ||= Member.new
189 # Clear password input
182 # Clear password input
190 @user.password = @user.password_confirmation = nil
183 @user.password = @user.password_confirmation = nil
191
184
192 respond_to do |format|
185 respond_to do |format|
193 format.html { render :action => :edit }
186 format.html { render :action => :edit }
194 format.api { render_validation_errors(@user) }
187 format.api { render_validation_errors(@user) }
195 end
188 end
196 end
189 end
197 rescue ::ActionController::RedirectBackError
190 rescue ::ActionController::RedirectBackError
198 redirect_to :controller => 'users', :action => 'edit', :id => @user
191 redirect_to :controller => 'users', :action => 'edit', :id => @user
199 end
192 end
200
193
201 def edit_membership
194 def edit_membership
202 @user = User.find(params[:id])
195 @user = User.find(params[:id])
203 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
196 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
204 @membership.save if request.post?
197 @membership.save if request.post?
205 respond_to do |format|
198 respond_to do |format|
206 if @membership.valid?
199 if @membership.valid?
207 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
200 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
208 format.js {
201 format.js {
209 render(:update) {|page|
202 render(:update) {|page|
210 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
203 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
211 page.visual_effect(:highlight, "member-#{@membership.id}")
204 page.visual_effect(:highlight, "member-#{@membership.id}")
212 }
205 }
213 }
206 }
214 else
207 else
215 format.js {
208 format.js {
216 render(:update) {|page|
209 render(:update) {|page|
217 page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
210 page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
218 }
211 }
219 }
212 }
220 end
213 end
221 end
214 end
222 end
215 end
223
216
224 def destroy_membership
217 def destroy_membership
225 @user = User.find(params[:id])
218 @user = User.find(params[:id])
226 @membership = Member.find(params[:membership_id])
219 @membership = Member.find(params[:membership_id])
227 if request.post? && @membership.deletable?
220 if request.post? && @membership.deletable?
228 @membership.destroy
221 @membership.destroy
229 end
222 end
230 respond_to do |format|
223 respond_to do |format|
231 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
224 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
232 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
225 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
233 end
226 end
234 end
227 end
235 end
228 end
@@ -1,12 +1,12
1 <p>
1 <p>
2 <%= select_tag 'notification_option', options_for_select(@notification_options.collect {|o| [l(o.last), o.first]}, @notification_option),
2 <%= select_tag 'user[mail_notification]', options_for_select(@notification_options.collect {|o| [l(o.last), o.first]}, @user.mail_notification),
3 :onchange => 'if ($("notification_option").value == "selected") {Element.show("notified-projects")} else {Element.hide("notified-projects")}' %>
3 :onchange => 'if (this.value == "selected") {Element.show("notified-projects")} else {Element.hide("notified-projects")}' %>
4 </p>
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 <p><% @user.projects.each do |project| %>
6 <p><% @user.projects.each do |project| %>
7 <label><%= check_box_tag 'notified_project_ids[]', project.id, @user.notified_projects_ids.include?(project.id) %> <%=h project.name %></label><br />
7 <label><%= check_box_tag 'notified_project_ids[]', project.id, @user.notified_projects_ids.include?(project.id) %> <%=h project.name %></label><br />
8 <% end %></p>
8 <% end %></p>
9 <p><em><%= l(:text_user_mail_option) %></em></p>
9 <p><em><%= l(:text_user_mail_option) %></em></p>
10 <% end %>
10 <% end %>
11 <p><label><%= l(:label_user_mail_no_self_notified) %></label><%= check_box_tag 'no_self_notified', 1, @user.pref[:no_self_notified] %></p>
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 # 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 :mail_notification => 'none'
144 :notification_option => 'none'
144 }
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
155
156 should 'set the password' do
156 should 'set the password' do
157 user = User.first(:order => 'id DESC')
157 user = User.first(:order => 'id DESC')
158 assert user.check_password?('test')
158 assert user.check_password?('test')
159 end
159 end
160 end
160 end
161
161
162 context "when unsuccessful" do
162 context "when unsuccessful" do
163 setup do
163 setup do
164 post :create, :user => {}
164 post :create, :user => {}
165 end
165 end
166
166
167 should_assign_to :user
167 should_assign_to :user
168 should_respond_with :success
168 should_respond_with :success
169 should_render_template :new
169 should_render_template :new
170 end
170 end
171
171
172 end
172 end
173
173
174 def test_update
174 def test_update
175 ActionMailer::Base.deliveries.clear
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 user = User.find(2)
178 user = User.find(2)
179 assert_equal 'Changed', user.firstname
179 assert_equal 'Changed', user.firstname
180 assert_equal 'all', user.mail_notification
180 assert_equal 'only_assigned', user.mail_notification
181 assert_equal true, user.pref[:hide_mail]
181 assert_equal true, user.pref[:hide_mail]
182 assert_equal 'desc', user.pref[:comments_sorting]
182 assert_equal 'desc', user.pref[:comments_sorting]
183 assert ActionMailer::Base.deliveries.empty?
183 assert ActionMailer::Base.deliveries.empty?
184 end
184 end
185
185
186 def test_update_with_activation_should_send_a_notification
186 def test_update_with_activation_should_send_a_notification
187 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')
188 u.login = 'foo'
188 u.login = 'foo'
189 u.status = User::STATUS_REGISTERED
189 u.status = User::STATUS_REGISTERED
190 u.save!
190 u.save!
191 ActionMailer::Base.deliveries.clear
191 ActionMailer::Base.deliveries.clear
192 Setting.bcc_recipients = '1'
192 Setting.bcc_recipients = '1'
193
193
194 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
194 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
195 assert u.reload.active?
195 assert u.reload.active?
196 mail = ActionMailer::Base.deliveries.last
196 mail = ActionMailer::Base.deliveries.last
197 assert_not_nil mail
197 assert_not_nil mail
198 assert_equal ['foo.bar@somenet.foo'], mail.bcc
198 assert_equal ['foo.bar@somenet.foo'], mail.bcc
199 assert mail.body.include?(ll('fr', :notice_account_activated))
199 assert mail.body.include?(ll('fr', :notice_account_activated))
200 end
200 end
201
201
202 def test_update_with_password_change_should_send_a_notification
202 def test_update_with_password_change_should_send_a_notification
203 ActionMailer::Base.deliveries.clear
203 ActionMailer::Base.deliveries.clear
204 Setting.bcc_recipients = '1'
204 Setting.bcc_recipients = '1'
205
205
206 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
206 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
207 u = User.find(2)
207 u = User.find(2)
208 assert u.check_password?('newpass')
208 assert u.check_password?('newpass')
209
209
210 mail = ActionMailer::Base.deliveries.last
210 mail = ActionMailer::Base.deliveries.last
211 assert_not_nil mail
211 assert_not_nil mail
212 assert_equal [u.mail], mail.bcc
212 assert_equal [u.mail], mail.bcc
213 assert mail.body.include?('newpass')
213 assert mail.body.include?('newpass')
214 end
214 end
215
215
216 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
217 # Configure as auth source
217 # Configure as auth source
218 u = User.find(2)
218 u = User.find(2)
219 u.auth_source = AuthSource.find(1)
219 u.auth_source = AuthSource.find(1)
220 u.save!
220 u.save!
221
221
222 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'
223
223
224 assert_equal nil, u.reload.auth_source
224 assert_equal nil, u.reload.auth_source
225 assert u.check_password?('newpass')
225 assert u.check_password?('newpass')
226 end
226 end
227
227
228 def test_edit_membership
228 def test_edit_membership
229 post :edit_membership, :id => 2, :membership_id => 1,
229 post :edit_membership, :id => 2, :membership_id => 1,
230 :membership => { :role_ids => [2]}
230 :membership => { :role_ids => [2]}
231 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
231 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
232 assert_equal [2], Member.find(1).role_ids
232 assert_equal [2], Member.find(1).role_ids
233 end
233 end
234
234
235 def test_destroy_membership
235 def test_destroy_membership
236 post :destroy_membership, :id => 2, :membership_id => 1
236 post :destroy_membership, :id => 2, :membership_id => 1
237 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
237 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
238 assert_nil Member.find_by_id(1)
238 assert_nil Member.find_by_id(1)
239 end
239 end
240 end
240 end
@@ -1,257 +1,258
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', :password => 'secret'}}
57 @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}}
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', :password => 'secret'}},
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_equal 'only_assigned', user.mail_notification
76 assert !user.admin?
77 assert !user.admin?
77 assert user.check_password?('secret')
78 assert user.check_password?('secret')
78
79
79 assert_response :created
80 assert_response :created
80 assert_equal 'application/xml', @response.content_type
81 assert_equal 'application/xml', @response.content_type
81 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
82 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
82 end
83 end
83 end
84 end
84
85
85 context ".json" do
86 context ".json" do
86 should_allow_api_authentication(:post,
87 should_allow_api_authentication(:post,
87 '/users.json',
88 '/users.json',
88 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}},
89 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}},
89 {:success_code => :created})
90 {:success_code => :created})
90
91
91 should "create a user with the attributes" do
92 should "create a user with the attributes" do
92 assert_difference('User.count') do
93 assert_difference('User.count') do
93 post '/users.json', @parameters, :authorization => credentials('admin')
94 post '/users.json', @parameters, :authorization => credentials('admin')
94 end
95 end
95
96
96 user = User.first(:order => 'id DESC')
97 user = User.first(:order => 'id DESC')
97 assert_equal 'foo', user.login
98 assert_equal 'foo', user.login
98 assert_equal 'Firstname', user.firstname
99 assert_equal 'Firstname', user.firstname
99 assert_equal 'Lastname', user.lastname
100 assert_equal 'Lastname', user.lastname
100 assert_equal 'foo@example.net', user.mail
101 assert_equal 'foo@example.net', user.mail
101 assert !user.admin?
102 assert !user.admin?
102
103
103 assert_response :created
104 assert_response :created
104 assert_equal 'application/json', @response.content_type
105 assert_equal 'application/json', @response.content_type
105 json = ActiveSupport::JSON.decode(response.body)
106 json = ActiveSupport::JSON.decode(response.body)
106 assert_kind_of Hash, json
107 assert_kind_of Hash, json
107 assert_kind_of Hash, json['user']
108 assert_kind_of Hash, json['user']
108 assert_equal user.id, json['user']['id']
109 assert_equal user.id, json['user']['id']
109 end
110 end
110 end
111 end
111 end
112 end
112
113
113 context "with invalid parameters" do
114 context "with invalid parameters" do
114 setup do
115 setup do
115 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
116 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
116 end
117 end
117
118
118 context ".xml" do
119 context ".xml" do
119 should "return errors" do
120 should "return errors" do
120 assert_no_difference('User.count') do
121 assert_no_difference('User.count') do
121 post '/users.xml', @parameters, :authorization => credentials('admin')
122 post '/users.xml', @parameters, :authorization => credentials('admin')
122 end
123 end
123
124
124 assert_response :unprocessable_entity
125 assert_response :unprocessable_entity
125 assert_equal 'application/xml', @response.content_type
126 assert_equal 'application/xml', @response.content_type
126 assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"}
127 assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"}
127 end
128 end
128 end
129 end
129
130
130 context ".json" do
131 context ".json" do
131 should "return errors" do
132 should "return errors" do
132 assert_no_difference('User.count') do
133 assert_no_difference('User.count') do
133 post '/users.json', @parameters, :authorization => credentials('admin')
134 post '/users.json', @parameters, :authorization => credentials('admin')
134 end
135 end
135
136
136 assert_response :unprocessable_entity
137 assert_response :unprocessable_entity
137 assert_equal 'application/json', @response.content_type
138 assert_equal 'application/json', @response.content_type
138 json = ActiveSupport::JSON.decode(response.body)
139 json = ActiveSupport::JSON.decode(response.body)
139 assert_kind_of Hash, json
140 assert_kind_of Hash, json
140 assert json.has_key?('errors')
141 assert json.has_key?('errors')
141 assert_kind_of Array, json['errors']
142 assert_kind_of Array, json['errors']
142 end
143 end
143 end
144 end
144 end
145 end
145 end
146 end
146
147
147 context "PUT /users/2" do
148 context "PUT /users/2" do
148 context "with valid parameters" do
149 context "with valid parameters" do
149 setup do
150 setup do
150 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}
151 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}
151 end
152 end
152
153
153 context ".xml" do
154 context ".xml" do
154 should_allow_api_authentication(:put,
155 should_allow_api_authentication(:put,
155 '/users/2.xml',
156 '/users/2.xml',
156 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
157 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
157 {:success_code => :ok})
158 {:success_code => :ok})
158
159
159 should "update user with the attributes" do
160 should "update user with the attributes" do
160 assert_no_difference('User.count') do
161 assert_no_difference('User.count') do
161 put '/users/2.xml', @parameters, :authorization => credentials('admin')
162 put '/users/2.xml', @parameters, :authorization => credentials('admin')
162 end
163 end
163
164
164 user = User.find(2)
165 user = User.find(2)
165 assert_equal 'jsmith', user.login
166 assert_equal 'jsmith', user.login
166 assert_equal 'John', user.firstname
167 assert_equal 'John', user.firstname
167 assert_equal 'Renamed', user.lastname
168 assert_equal 'Renamed', user.lastname
168 assert_equal 'jsmith@somenet.foo', user.mail
169 assert_equal 'jsmith@somenet.foo', user.mail
169 assert !user.admin?
170 assert !user.admin?
170
171
171 assert_response :ok
172 assert_response :ok
172 end
173 end
173 end
174 end
174
175
175 context ".json" do
176 context ".json" do
176 should_allow_api_authentication(:put,
177 should_allow_api_authentication(:put,
177 '/users/2.json',
178 '/users/2.json',
178 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
179 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
179 {:success_code => :ok})
180 {:success_code => :ok})
180
181
181 should "update user with the attributes" do
182 should "update user with the attributes" do
182 assert_no_difference('User.count') do
183 assert_no_difference('User.count') do
183 put '/users/2.json', @parameters, :authorization => credentials('admin')
184 put '/users/2.json', @parameters, :authorization => credentials('admin')
184 end
185 end
185
186
186 user = User.find(2)
187 user = User.find(2)
187 assert_equal 'jsmith', user.login
188 assert_equal 'jsmith', user.login
188 assert_equal 'John', user.firstname
189 assert_equal 'John', user.firstname
189 assert_equal 'Renamed', user.lastname
190 assert_equal 'Renamed', user.lastname
190 assert_equal 'jsmith@somenet.foo', user.mail
191 assert_equal 'jsmith@somenet.foo', user.mail
191 assert !user.admin?
192 assert !user.admin?
192
193
193 assert_response :ok
194 assert_response :ok
194 end
195 end
195 end
196 end
196 end
197 end
197
198
198 context "with invalid parameters" do
199 context "with invalid parameters" do
199 setup do
200 setup do
200 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}}
201 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}}
201 end
202 end
202
203
203 context ".xml" do
204 context ".xml" do
204 should "return errors" do
205 should "return errors" do
205 assert_no_difference('User.count') do
206 assert_no_difference('User.count') do
206 put '/users/2.xml', @parameters, :authorization => credentials('admin')
207 put '/users/2.xml', @parameters, :authorization => credentials('admin')
207 end
208 end
208
209
209 assert_response :unprocessable_entity
210 assert_response :unprocessable_entity
210 assert_equal 'application/xml', @response.content_type
211 assert_equal 'application/xml', @response.content_type
211 assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"}
212 assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"}
212 end
213 end
213 end
214 end
214
215
215 context ".json" do
216 context ".json" do
216 should "return errors" do
217 should "return errors" do
217 assert_no_difference('User.count') do
218 assert_no_difference('User.count') do
218 put '/users/2.json', @parameters, :authorization => credentials('admin')
219 put '/users/2.json', @parameters, :authorization => credentials('admin')
219 end
220 end
220
221
221 assert_response :unprocessable_entity
222 assert_response :unprocessable_entity
222 assert_equal 'application/json', @response.content_type
223 assert_equal 'application/json', @response.content_type
223 json = ActiveSupport::JSON.decode(response.body)
224 json = ActiveSupport::JSON.decode(response.body)
224 assert_kind_of Hash, json
225 assert_kind_of Hash, json
225 assert json.has_key?('errors')
226 assert json.has_key?('errors')
226 assert_kind_of Array, json['errors']
227 assert_kind_of Array, json['errors']
227 end
228 end
228 end
229 end
229 end
230 end
230
231
231 context "DELETE /users/2" do
232 context "DELETE /users/2" do
232 context ".xml" do
233 context ".xml" do
233 should "not be allowed" do
234 should "not be allowed" do
234 assert_no_difference('User.count') do
235 assert_no_difference('User.count') do
235 delete '/users/2.xml'
236 delete '/users/2.xml'
236 end
237 end
237
238
238 assert_response :method_not_allowed
239 assert_response :method_not_allowed
239 end
240 end
240 end
241 end
241
242
242 context ".json" do
243 context ".json" do
243 should "not be allowed" do
244 should "not be allowed" do
244 assert_no_difference('User.count') do
245 assert_no_difference('User.count') do
245 delete '/users/2.json'
246 delete '/users/2.json'
246 end
247 end
247
248
248 assert_response :method_not_allowed
249 assert_response :method_not_allowed
249 end
250 end
250 end
251 end
251 end
252 end
252 end
253 end
253
254
254 def credentials(user, password=nil)
255 def credentials(user, password=nil)
255 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
256 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
256 end
257 end
257 end
258 end
General Comments 0
You need to be logged in to leave comments. Login now