##// END OF EJS Templates
Backported r4397 from trunk....
Jean-Philippe Lang -
r4287:2a7a95d7465e
parent child
Show More
@@ -1,271 +1,271
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 ProjectsController < ApplicationController
18 class ProjectsController < ApplicationController
19 menu_item :overview
19 menu_item :overview
20 menu_item :roadmap, :only => :roadmap
20 menu_item :roadmap, :only => :roadmap
21 menu_item :settings, :only => :settings
21 menu_item :settings, :only => :settings
22
22
23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
25 before_filter :authorize_global, :only => [:new, :create]
25 before_filter :authorize_global, :only => [:new, :create]
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27 accept_key_auth :index
27 accept_key_auth :index
28
28
29 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
29 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
30 if controller.request.post?
30 if controller.request.post?
31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
32 end
32 end
33 end
33 end
34
34
35 # TODO: convert to PUT only
35 # TODO: convert to PUT only
36 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
36 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
37
37
38 helper :sort
38 helper :sort
39 include SortHelper
39 include SortHelper
40 helper :custom_fields
40 helper :custom_fields
41 include CustomFieldsHelper
41 include CustomFieldsHelper
42 helper :issues
42 helper :issues
43 helper :queries
43 helper :queries
44 include QueriesHelper
44 include QueriesHelper
45 helper :repositories
45 helper :repositories
46 include RepositoriesHelper
46 include RepositoriesHelper
47 include ProjectsHelper
47 include ProjectsHelper
48
48
49 # Lists visible projects
49 # Lists visible projects
50 def index
50 def index
51 respond_to do |format|
51 respond_to do |format|
52 format.html {
52 format.html {
53 @projects = Project.visible.find(:all, :order => 'lft')
53 @projects = Project.visible.find(:all, :order => 'lft')
54 }
54 }
55 format.xml {
55 format.xml {
56 @projects = Project.visible.find(:all, :order => 'lft')
56 @projects = Project.visible.find(:all, :order => 'lft')
57 }
57 }
58 format.atom {
58 format.atom {
59 projects = Project.visible.find(:all, :order => 'created_on DESC',
59 projects = Project.visible.find(:all, :order => 'created_on DESC',
60 :limit => Setting.feeds_limit.to_i)
60 :limit => Setting.feeds_limit.to_i)
61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
62 }
62 }
63 end
63 end
64 end
64 end
65
65
66 def new
66 def new
67 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
67 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
68 @trackers = Tracker.all
68 @trackers = Tracker.all
69 @project = Project.new(params[:project])
69 @project = Project.new(params[:project])
70
70
71 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
71 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
72 @project.trackers = Tracker.all
72 @project.trackers = Tracker.all
73 @project.is_public = Setting.default_projects_public?
73 @project.is_public = Setting.default_projects_public?
74 @project.enabled_module_names = Setting.default_projects_modules
74 @project.enabled_module_names = Setting.default_projects_modules
75 end
75 end
76
76
77 def create
77 def create
78 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
78 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
79 @trackers = Tracker.all
79 @trackers = Tracker.all
80 @project = Project.new(params[:project])
80 @project = Project.new(params[:project])
81
81
82 @project.enabled_module_names = params[:enabled_modules]
82 @project.enabled_module_names = params[:enabled_modules]
83 if validate_parent_id && @project.save
83 if validate_parent_id && @project.save
84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
85 # Add current user as a project member if he is not admin
85 # Add current user as a project member if he is not admin
86 unless User.current.admin?
86 unless User.current.admin?
87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
88 m = Member.new(:user => User.current, :roles => [r])
88 m = Member.new(:user => User.current, :roles => [r])
89 @project.members << m
89 @project.members << m
90 end
90 end
91 respond_to do |format|
91 respond_to do |format|
92 format.html {
92 format.html {
93 flash[:notice] = l(:notice_successful_create)
93 flash[:notice] = l(:notice_successful_create)
94 redirect_to :controller => 'projects', :action => 'settings', :id => @project
94 redirect_to :controller => 'projects', :action => 'settings', :id => @project
95 }
95 }
96 format.xml { head :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
96 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
97 end
97 end
98 else
98 else
99 respond_to do |format|
99 respond_to do |format|
100 format.html { render :action => 'new' }
100 format.html { render :action => 'new' }
101 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
101 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
102 end
102 end
103 end
103 end
104
104
105 end
105 end
106
106
107 def copy
107 def copy
108 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
108 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
109 @trackers = Tracker.all
109 @trackers = Tracker.all
110 @root_projects = Project.find(:all,
110 @root_projects = Project.find(:all,
111 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
111 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
112 :order => 'name')
112 :order => 'name')
113 @source_project = Project.find(params[:id])
113 @source_project = Project.find(params[:id])
114 if request.get?
114 if request.get?
115 @project = Project.copy_from(@source_project)
115 @project = Project.copy_from(@source_project)
116 if @project
116 if @project
117 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
117 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
118 else
118 else
119 redirect_to :controller => 'admin', :action => 'projects'
119 redirect_to :controller => 'admin', :action => 'projects'
120 end
120 end
121 else
121 else
122 Mailer.with_deliveries(params[:notifications] == '1') do
122 Mailer.with_deliveries(params[:notifications] == '1') do
123 @project = Project.new(params[:project])
123 @project = Project.new(params[:project])
124 @project.enabled_module_names = params[:enabled_modules]
124 @project.enabled_module_names = params[:enabled_modules]
125 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
125 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
126 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
126 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
127 flash[:notice] = l(:notice_successful_create)
127 flash[:notice] = l(:notice_successful_create)
128 redirect_to :controller => 'projects', :action => 'settings'
128 redirect_to :controller => 'projects', :action => 'settings'
129 elsif !@project.new_record?
129 elsif !@project.new_record?
130 # Project was created
130 # Project was created
131 # But some objects were not copied due to validation failures
131 # But some objects were not copied due to validation failures
132 # (eg. issues from disabled trackers)
132 # (eg. issues from disabled trackers)
133 # TODO: inform about that
133 # TODO: inform about that
134 redirect_to :controller => 'projects', :action => 'settings'
134 redirect_to :controller => 'projects', :action => 'settings'
135 end
135 end
136 end
136 end
137 end
137 end
138 rescue ActiveRecord::RecordNotFound
138 rescue ActiveRecord::RecordNotFound
139 redirect_to :controller => 'admin', :action => 'projects'
139 redirect_to :controller => 'admin', :action => 'projects'
140 end
140 end
141
141
142 # Show @project
142 # Show @project
143 def show
143 def show
144 if params[:jump]
144 if params[:jump]
145 # try to redirect to the requested menu item
145 # try to redirect to the requested menu item
146 redirect_to_project_menu_item(@project, params[:jump]) && return
146 redirect_to_project_menu_item(@project, params[:jump]) && return
147 end
147 end
148
148
149 @users_by_role = @project.users_by_role
149 @users_by_role = @project.users_by_role
150 @subprojects = @project.children.visible
150 @subprojects = @project.children.visible
151 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
151 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
152 @trackers = @project.rolled_up_trackers
152 @trackers = @project.rolled_up_trackers
153
153
154 cond = @project.project_condition(Setting.display_subprojects_issues?)
154 cond = @project.project_condition(Setting.display_subprojects_issues?)
155
155
156 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
156 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
157 :include => [:project, :status, :tracker],
157 :include => [:project, :status, :tracker],
158 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
158 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
159 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
159 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
160 :include => [:project, :status, :tracker],
160 :include => [:project, :status, :tracker],
161 :conditions => cond)
161 :conditions => cond)
162
162
163 TimeEntry.visible_by(User.current) do
163 TimeEntry.visible_by(User.current) do
164 @total_hours = TimeEntry.sum(:hours,
164 @total_hours = TimeEntry.sum(:hours,
165 :include => :project,
165 :include => :project,
166 :conditions => cond).to_f
166 :conditions => cond).to_f
167 end
167 end
168 @key = User.current.rss_key
168 @key = User.current.rss_key
169
169
170 respond_to do |format|
170 respond_to do |format|
171 format.html
171 format.html
172 format.xml
172 format.xml
173 end
173 end
174 end
174 end
175
175
176 def settings
176 def settings
177 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
177 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
178 @issue_category ||= IssueCategory.new
178 @issue_category ||= IssueCategory.new
179 @member ||= @project.members.new
179 @member ||= @project.members.new
180 @trackers = Tracker.all
180 @trackers = Tracker.all
181 @repository ||= @project.repository
181 @repository ||= @project.repository
182 @wiki ||= @project.wiki
182 @wiki ||= @project.wiki
183 end
183 end
184
184
185 def edit
185 def edit
186 end
186 end
187
187
188 def update
188 def update
189 @project.attributes = params[:project]
189 @project.attributes = params[:project]
190 if validate_parent_id && @project.save
190 if validate_parent_id && @project.save
191 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
191 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
192 respond_to do |format|
192 respond_to do |format|
193 format.html {
193 format.html {
194 flash[:notice] = l(:notice_successful_update)
194 flash[:notice] = l(:notice_successful_update)
195 redirect_to :action => 'settings', :id => @project
195 redirect_to :action => 'settings', :id => @project
196 }
196 }
197 format.xml { head :ok }
197 format.xml { head :ok }
198 end
198 end
199 else
199 else
200 respond_to do |format|
200 respond_to do |format|
201 format.html {
201 format.html {
202 settings
202 settings
203 render :action => 'settings'
203 render :action => 'settings'
204 }
204 }
205 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
205 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
206 end
206 end
207 end
207 end
208 end
208 end
209
209
210 def modules
210 def modules
211 @project.enabled_module_names = params[:enabled_modules]
211 @project.enabled_module_names = params[:enabled_modules]
212 flash[:notice] = l(:notice_successful_update)
212 flash[:notice] = l(:notice_successful_update)
213 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
213 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
214 end
214 end
215
215
216 def archive
216 def archive
217 if request.post?
217 if request.post?
218 unless @project.archive
218 unless @project.archive
219 flash[:error] = l(:error_can_not_archive_project)
219 flash[:error] = l(:error_can_not_archive_project)
220 end
220 end
221 end
221 end
222 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
222 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
223 end
223 end
224
224
225 def unarchive
225 def unarchive
226 @project.unarchive if request.post? && !@project.active?
226 @project.unarchive if request.post? && !@project.active?
227 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
227 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
228 end
228 end
229
229
230 # Delete @project
230 # Delete @project
231 def destroy
231 def destroy
232 @project_to_destroy = @project
232 @project_to_destroy = @project
233 if request.get?
233 if request.get?
234 # display confirmation view
234 # display confirmation view
235 else
235 else
236 if params[:format] == 'xml' || params[:confirm]
236 if params[:format] == 'xml' || params[:confirm]
237 @project_to_destroy.destroy
237 @project_to_destroy.destroy
238 respond_to do |format|
238 respond_to do |format|
239 format.html { redirect_to :controller => 'admin', :action => 'projects' }
239 format.html { redirect_to :controller => 'admin', :action => 'projects' }
240 format.xml { head :ok }
240 format.xml { head :ok }
241 end
241 end
242 end
242 end
243 end
243 end
244 # hide project in layout
244 # hide project in layout
245 @project = nil
245 @project = nil
246 end
246 end
247
247
248 private
248 private
249 def find_optional_project
249 def find_optional_project
250 return true unless params[:id]
250 return true unless params[:id]
251 @project = Project.find(params[:id])
251 @project = Project.find(params[:id])
252 authorize
252 authorize
253 rescue ActiveRecord::RecordNotFound
253 rescue ActiveRecord::RecordNotFound
254 render_404
254 render_404
255 end
255 end
256
256
257 # Validates parent_id param according to user's permissions
257 # Validates parent_id param according to user's permissions
258 # TODO: move it to Project model in a validation that depends on User.current
258 # TODO: move it to Project model in a validation that depends on User.current
259 def validate_parent_id
259 def validate_parent_id
260 return true if User.current.admin?
260 return true if User.current.admin?
261 parent_id = params[:project] && params[:project][:parent_id]
261 parent_id = params[:project] && params[:project][:parent_id]
262 if parent_id || @project.new_record?
262 if parent_id || @project.new_record?
263 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
263 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
264 unless @project.allowed_parents.include?(parent)
264 unless @project.allowed_parents.include?(parent)
265 @project.errors.add :parent_id, :invalid
265 @project.errors.add :parent_id, :invalid
266 return false
266 return false
267 end
267 end
268 end
268 end
269 true
269 true
270 end
270 end
271 end
271 end
@@ -1,99 +1,102
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
19
20 class ProjectsApiTest < ActionController::IntegrationTest
20 class ProjectsApiTest < ActionController::IntegrationTest
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 :attachments, :custom_fields, :custom_values, :time_entries
23 :attachments, :custom_fields, :custom_values, :time_entries
24
24
25 def setup
25 def setup
26 Setting.rest_api_enabled = '1'
26 Setting.rest_api_enabled = '1'
27 end
27 end
28
28
29 def test_index
29 def test_index
30 get '/projects.xml'
30 get '/projects.xml'
31 assert_response :success
31 assert_response :success
32 assert_equal 'application/xml', @response.content_type
32 assert_equal 'application/xml', @response.content_type
33 end
33 end
34
34
35 def test_show
35 def test_show
36 get '/projects/1.xml'
36 get '/projects/1.xml'
37 assert_response :success
37 assert_response :success
38 assert_equal 'application/xml', @response.content_type
38 assert_equal 'application/xml', @response.content_type
39 end
39 end
40
40
41 def test_create
41 def test_create
42 attributes = {:name => 'API test', :identifier => 'api-test'}
42 attributes = {:name => 'API test', :identifier => 'api-test'}
43 assert_difference 'Project.count' do
43 assert_difference 'Project.count' do
44 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
44 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
45 end
45 end
46 assert_response :created
46
47 assert_equal 'application/xml', @response.content_type
48 project = Project.first(:order => 'id DESC')
47 project = Project.first(:order => 'id DESC')
49 attributes.each do |attribute, value|
48 attributes.each do |attribute, value|
50 assert_equal value, project.send(attribute)
49 assert_equal value, project.send(attribute)
51 end
50 end
51
52 assert_response :created
53 assert_equal 'application/xml', @response.content_type
54 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
52 end
55 end
53
56
54 def test_create_failure
57 def test_create_failure
55 attributes = {:name => 'API test'}
58 attributes = {:name => 'API test'}
56 assert_no_difference 'Project.count' do
59 assert_no_difference 'Project.count' do
57 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
60 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
58 end
61 end
59 assert_response :unprocessable_entity
62 assert_response :unprocessable_entity
60 assert_equal 'application/xml', @response.content_type
63 assert_equal 'application/xml', @response.content_type
61 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
64 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
62 end
65 end
63
66
64 def test_update
67 def test_update
65 attributes = {:name => 'API update'}
68 attributes = {:name => 'API update'}
66 assert_no_difference 'Project.count' do
69 assert_no_difference 'Project.count' do
67 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
70 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
68 end
71 end
69 assert_response :ok
72 assert_response :ok
70 assert_equal 'application/xml', @response.content_type
73 assert_equal 'application/xml', @response.content_type
71 project = Project.find(1)
74 project = Project.find(1)
72 attributes.each do |attribute, value|
75 attributes.each do |attribute, value|
73 assert_equal value, project.send(attribute)
76 assert_equal value, project.send(attribute)
74 end
77 end
75 end
78 end
76
79
77 def test_update_failure
80 def test_update_failure
78 attributes = {:name => ''}
81 attributes = {:name => ''}
79 assert_no_difference 'Project.count' do
82 assert_no_difference 'Project.count' do
80 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
83 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
81 end
84 end
82 assert_response :unprocessable_entity
85 assert_response :unprocessable_entity
83 assert_equal 'application/xml', @response.content_type
86 assert_equal 'application/xml', @response.content_type
84 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
87 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
85 end
88 end
86
89
87 def test_destroy
90 def test_destroy
88 assert_difference 'Project.count', -1 do
91 assert_difference 'Project.count', -1 do
89 delete '/projects/2.xml', {}, :authorization => credentials('admin')
92 delete '/projects/2.xml', {}, :authorization => credentials('admin')
90 end
93 end
91 assert_response :ok
94 assert_response :ok
92 assert_equal 'application/xml', @response.content_type
95 assert_equal 'application/xml', @response.content_type
93 assert_nil Project.find_by_id(2)
96 assert_nil Project.find_by_id(2)
94 end
97 end
95
98
96 def credentials(user, password=nil)
99 def credentials(user, password=nil)
97 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
100 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
98 end
101 end
99 end
102 end
General Comments 0
You need to be logged in to leave comments. Login now