@@ -1,352 +1,359 | |||
|
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 'uri' |
|
19 | 19 | require 'cgi' |
|
20 | 20 | |
|
21 | 21 | class ApplicationController < ActionController::Base |
|
22 | 22 | include Redmine::I18n |
|
23 | 23 | |
|
24 | 24 | layout 'base' |
|
25 | 25 | exempt_from_layout 'builder' |
|
26 | 26 | |
|
27 | 27 | # Remove broken cookie after upgrade from 0.8.x (#4292) |
|
28 | 28 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 |
|
29 | 29 | # TODO: remove it when Rails is fixed |
|
30 | 30 | before_filter :delete_broken_cookies |
|
31 | 31 | def delete_broken_cookies |
|
32 | 32 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ |
|
33 | 33 | cookies.delete '_redmine_session' |
|
34 | 34 | redirect_to home_path |
|
35 | 35 | return false |
|
36 | 36 | end |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
40 | 40 | filter_parameter_logging :password |
|
41 | 41 | protect_from_forgery |
|
42 | 42 | |
|
43 | 43 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
44 | 44 | |
|
45 | 45 | include Redmine::Search::Controller |
|
46 | 46 | include Redmine::MenuManager::MenuController |
|
47 | 47 | helper Redmine::MenuManager::MenuHelper |
|
48 | 48 | |
|
49 | 49 | Redmine::Scm::Base.all.each do |scm| |
|
50 | 50 | require_dependency "repository/#{scm.underscore}" |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def user_setup |
|
54 | 54 | # Check the settings cache for each request |
|
55 | 55 | Setting.check_cache |
|
56 | 56 | # Find the current user |
|
57 | 57 | User.current = find_current_user |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | # Returns the current user or nil if no user is logged in |
|
61 | 61 | # and starts a session if needed |
|
62 | 62 | def find_current_user |
|
63 | 63 | if session[:user_id] |
|
64 | 64 | # existing session |
|
65 | 65 | (User.active.find(session[:user_id]) rescue nil) |
|
66 | 66 | elsif cookies[:autologin] && Setting.autologin? |
|
67 | 67 | # auto-login feature starts a new session |
|
68 | 68 | user = User.try_to_autologin(cookies[:autologin]) |
|
69 | 69 | session[:user_id] = user.id if user |
|
70 | 70 | user |
|
71 | 71 | elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) |
|
72 | 72 | # RSS key authentication does not start a session |
|
73 | 73 | User.find_by_rss_key(params[:key]) |
|
74 | 74 | elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) |
|
75 | 75 | if params[:key].present? && accept_key_auth_actions.include?(params[:action]) |
|
76 | 76 | # Use API key |
|
77 | 77 | User.find_by_api_key(params[:key]) |
|
78 | 78 | else |
|
79 | 79 | # HTTP Basic, either username/password or API key/random |
|
80 | 80 | authenticate_with_http_basic do |username, password| |
|
81 | 81 | User.try_to_login(username, password) || User.find_by_api_key(username) |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | end |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | # Sets the logged in user |
|
88 | 88 | def logged_user=(user) |
|
89 | 89 | reset_session |
|
90 | 90 | if user && user.is_a?(User) |
|
91 | 91 | User.current = user |
|
92 | 92 | session[:user_id] = user.id |
|
93 | 93 | else |
|
94 | 94 | User.current = User.anonymous |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | # check if login is globally required to access the application |
|
99 | 99 | def check_if_login_required |
|
100 | 100 | # no check needed if user is already logged in |
|
101 | 101 | return true if User.current.logged? |
|
102 | 102 | require_login if Setting.login_required? |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def set_localization |
|
106 | 106 | lang = nil |
|
107 | 107 | if User.current.logged? |
|
108 | 108 | lang = find_language(User.current.language) |
|
109 | 109 | end |
|
110 | 110 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
111 | 111 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
112 | 112 | if !accept_lang.blank? |
|
113 | 113 | accept_lang = accept_lang.downcase |
|
114 | 114 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
115 | 115 | end |
|
116 | 116 | end |
|
117 | 117 | lang ||= Setting.default_language |
|
118 | 118 | set_language_if_valid(lang) |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | def require_login |
|
122 | 122 | if !User.current.logged? |
|
123 | 123 | # Extract only the basic url parameters on non-GET requests |
|
124 | 124 | if request.get? |
|
125 | 125 | url = url_for(params) |
|
126 | 126 | else |
|
127 | 127 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
128 | 128 | end |
|
129 | 129 | respond_to do |format| |
|
130 | 130 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
131 | 131 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
132 | 132 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
133 | 133 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
134 | 134 | end |
|
135 | 135 | return false |
|
136 | 136 | end |
|
137 | 137 | true |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | def require_admin |
|
141 | 141 | return unless require_login |
|
142 | 142 | if !User.current.admin? |
|
143 | 143 | render_403 |
|
144 | 144 | return false |
|
145 | 145 | end |
|
146 | 146 | true |
|
147 | 147 | end |
|
148 | 148 | |
|
149 | 149 | def deny_access |
|
150 | 150 | User.current.logged? ? render_403 : require_login |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | # Authorize the user for the requested action |
|
154 | 154 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
155 | 155 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global) |
|
156 | 156 | allowed ? true : deny_access |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | # Authorize the user for the requested action outside a project |
|
160 | 160 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
161 | 161 | authorize(ctrl, action, global) |
|
162 | 162 | end |
|
163 | 163 | |
|
164 | 164 | # Find project of id params[:id] |
|
165 | 165 | def find_project |
|
166 | 166 | @project = Project.find(params[:id]) |
|
167 | 167 | rescue ActiveRecord::RecordNotFound |
|
168 | 168 | render_404 |
|
169 | 169 | end |
|
170 | 170 | |
|
171 | 171 | # Find a project based on params[:project_id] |
|
172 | 172 | # TODO: some subclasses override this, see about merging their logic |
|
173 | 173 | def find_optional_project |
|
174 | 174 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
175 | 175 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
176 | 176 | allowed ? true : deny_access |
|
177 | 177 | rescue ActiveRecord::RecordNotFound |
|
178 | 178 | render_404 |
|
179 | 179 | end |
|
180 | 180 | |
|
181 | 181 | # Finds and sets @project based on @object.project |
|
182 | 182 | def find_project_from_association |
|
183 | 183 | render_404 unless @object.present? |
|
184 | 184 | |
|
185 | 185 | @project = @object.project |
|
186 | 186 | rescue ActiveRecord::RecordNotFound |
|
187 | 187 | render_404 |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | def find_model_object |
|
191 | 191 | model = self.class.read_inheritable_attribute('model_object') |
|
192 | 192 | if model |
|
193 | 193 | @object = model.find(params[:id]) |
|
194 | 194 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
195 | 195 | end |
|
196 | 196 | rescue ActiveRecord::RecordNotFound |
|
197 | 197 | render_404 |
|
198 | 198 | end |
|
199 | 199 | |
|
200 | 200 | def self.model_object(model) |
|
201 | 201 | write_inheritable_attribute('model_object', model) |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | # make sure that the user is a member of the project (or admin) if project is private |
|
205 | 205 | # used as a before_filter for actions that do not require any particular permission on the project |
|
206 | 206 | def check_project_privacy |
|
207 | 207 | if @project && @project.active? |
|
208 | 208 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
209 | 209 | true |
|
210 | 210 | else |
|
211 | 211 | User.current.logged? ? render_403 : require_login |
|
212 | 212 | end |
|
213 | 213 | else |
|
214 | 214 | @project = nil |
|
215 | 215 | render_404 |
|
216 | 216 | false |
|
217 | 217 | end |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | def redirect_back_or_default(default) |
|
221 | 221 | back_url = CGI.unescape(params[:back_url].to_s) |
|
222 | 222 | if !back_url.blank? |
|
223 | 223 | begin |
|
224 | 224 | uri = URI.parse(back_url) |
|
225 | 225 | # do not redirect user to another host or to the login or register page |
|
226 | 226 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
227 | 227 | redirect_to(back_url) |
|
228 | 228 | return |
|
229 | 229 | end |
|
230 | 230 | rescue URI::InvalidURIError |
|
231 | 231 | # redirect to default |
|
232 | 232 | end |
|
233 | 233 | end |
|
234 | 234 | redirect_to default |
|
235 | 235 | end |
|
236 | 236 | |
|
237 | 237 | def render_403 |
|
238 | 238 | @project = nil |
|
239 | 239 | respond_to do |format| |
|
240 | 240 | format.html { render :template => "common/403", :layout => (request.xhr? ? false : 'base'), :status => 403 } |
|
241 | 241 | format.atom { head 403 } |
|
242 | 242 | format.xml { head 403 } |
|
243 | 243 | format.json { head 403 } |
|
244 | 244 | end |
|
245 | 245 | return false |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | def render_404 |
|
249 | 249 | respond_to do |format| |
|
250 | 250 | format.html { render :template => "common/404", :layout => !request.xhr?, :status => 404 } |
|
251 | 251 | format.atom { head 404 } |
|
252 | 252 | format.xml { head 404 } |
|
253 | 253 | format.json { head 404 } |
|
254 | 254 | end |
|
255 | 255 | return false |
|
256 | 256 | end |
|
257 | 257 | |
|
258 | 258 | def render_error(msg) |
|
259 | 259 | respond_to do |format| |
|
260 | 260 | format.html { |
|
261 | 261 | flash.now[:error] = msg |
|
262 | 262 | render :text => '', :layout => !request.xhr?, :status => 500 |
|
263 | 263 | } |
|
264 | 264 | format.atom { head 500 } |
|
265 | 265 | format.xml { head 500 } |
|
266 | 266 | format.json { head 500 } |
|
267 | 267 | end |
|
268 | 268 | end |
|
269 | 269 | |
|
270 | 270 | def invalid_authenticity_token |
|
271 | 271 | if api_request? |
|
272 | 272 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
273 | 273 | end |
|
274 | 274 | render_error "Invalid form authenticity token." |
|
275 | 275 | end |
|
276 | 276 | |
|
277 | 277 | def render_feed(items, options={}) |
|
278 | 278 | @items = items || [] |
|
279 | 279 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
280 | 280 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
281 | 281 | @title = options[:title] || Setting.app_title |
|
282 | 282 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' |
|
283 | 283 | end |
|
284 | 284 | |
|
285 | 285 | def self.accept_key_auth(*actions) |
|
286 | 286 | actions = actions.flatten.map(&:to_s) |
|
287 | 287 | write_inheritable_attribute('accept_key_auth_actions', actions) |
|
288 | 288 | end |
|
289 | 289 | |
|
290 | 290 | def accept_key_auth_actions |
|
291 | 291 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] |
|
292 | 292 | end |
|
293 | 293 | |
|
294 | 294 | # Returns the number of objects that should be displayed |
|
295 | 295 | # on the paginated list |
|
296 | 296 | def per_page_option |
|
297 | 297 | per_page = nil |
|
298 | 298 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
299 | 299 | per_page = params[:per_page].to_s.to_i |
|
300 | 300 | session[:per_page] = per_page |
|
301 | 301 | elsif session[:per_page] |
|
302 | 302 | per_page = session[:per_page] |
|
303 | 303 | else |
|
304 | 304 | per_page = Setting.per_page_options_array.first || 25 |
|
305 | 305 | end |
|
306 | 306 | per_page |
|
307 | 307 | end |
|
308 | 308 | |
|
309 | 309 | # qvalues http header parser |
|
310 | 310 | # code taken from webrick |
|
311 | 311 | def parse_qvalues(value) |
|
312 | 312 | tmp = [] |
|
313 | 313 | if value |
|
314 | 314 | parts = value.split(/,\s*/) |
|
315 | 315 | parts.each {|part| |
|
316 | 316 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
317 | 317 | val = m[1] |
|
318 | 318 | q = (m[2] or 1).to_f |
|
319 | 319 | tmp.push([val, q]) |
|
320 | 320 | end |
|
321 | 321 | } |
|
322 | 322 | tmp = tmp.sort_by{|val, q| -q} |
|
323 | 323 | tmp.collect!{|val, q| val} |
|
324 | 324 | end |
|
325 | 325 | return tmp |
|
326 | 326 | rescue |
|
327 | 327 | nil |
|
328 | 328 | end |
|
329 | 329 | |
|
330 | 330 | # Returns a string that can be used as filename value in Content-Disposition header |
|
331 | 331 | def filename_for_content_disposition(name) |
|
332 | 332 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
333 | 333 | end |
|
334 | 334 | |
|
335 | 335 | def api_request? |
|
336 | 336 | %w(xml json).include? params[:format] |
|
337 | 337 | end |
|
338 | 338 | |
|
339 | 339 | # Renders a warning flash if obj has unsaved attachments |
|
340 | 340 | def render_attachment_warning_if_needed(obj) |
|
341 | 341 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
342 | 342 | end |
|
343 | 343 | |
|
344 | 344 | # Rescues an invalid query statement. Just in case... |
|
345 | 345 | def query_statement_invalid(exception) |
|
346 | 346 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
347 | 347 | session.delete(:query) |
|
348 | 348 | sort_clear if respond_to?(:sort_clear) |
|
349 | 349 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
350 | 350 | end |
|
351 | 351 | |
|
352 | # Converts the errors on an ActiveRecord object into a common JSON format | |
|
353 | def object_errors_to_json(object) | |
|
354 | object.errors.collect do |attribute, error| | |
|
355 | { attribute => error } | |
|
356 | end.to_json | |
|
357 | end | |
|
358 | ||
|
352 | 359 | end |
@@ -1,481 +1,488 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2008 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 IssuesController < ApplicationController |
|
19 | 19 | menu_item :new_issue, :only => [:new, :create] |
|
20 | 20 | default_search_scope :issues |
|
21 | 21 | |
|
22 | 22 | before_filter :find_issue, :only => [:show, :edit, :update, :reply] |
|
23 | 23 | before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] |
|
24 | 24 | before_filter :find_project, :only => [:new, :create, :update_form, :preview, :auto_complete] |
|
25 | 25 | before_filter :authorize, :except => [:index, :changes, :preview, :context_menu] |
|
26 | 26 | before_filter :find_optional_project, :only => [:index, :changes] |
|
27 | 27 | before_filter :check_for_default_issue_status, :only => [:new, :create] |
|
28 | 28 | before_filter :build_new_issue_from_params, :only => [:new, :create] |
|
29 | 29 | accept_key_auth :index, :show, :changes |
|
30 | 30 | |
|
31 | 31 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
32 | 32 | |
|
33 | 33 | helper :journals |
|
34 | 34 | helper :projects |
|
35 | 35 | include ProjectsHelper |
|
36 | 36 | helper :custom_fields |
|
37 | 37 | include CustomFieldsHelper |
|
38 | 38 | helper :issue_relations |
|
39 | 39 | include IssueRelationsHelper |
|
40 | 40 | helper :watchers |
|
41 | 41 | include WatchersHelper |
|
42 | 42 | helper :attachments |
|
43 | 43 | include AttachmentsHelper |
|
44 | 44 | helper :queries |
|
45 | 45 | include QueriesHelper |
|
46 | 46 | helper :sort |
|
47 | 47 | include SortHelper |
|
48 | 48 | include IssuesHelper |
|
49 | 49 | helper :timelog |
|
50 | 50 | include Redmine::Export::PDF |
|
51 | 51 | |
|
52 | 52 | verify :method => [:post, :delete], |
|
53 | 53 | :only => :destroy, |
|
54 | 54 | :render => { :nothing => true, :status => :method_not_allowed } |
|
55 | 55 | |
|
56 | 56 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
|
57 | 57 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
58 | 58 | |
|
59 | 59 | def index |
|
60 | 60 | retrieve_query |
|
61 | 61 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
62 | 62 | sort_update(@query.sortable_columns) |
|
63 | 63 | |
|
64 | 64 | if @query.valid? |
|
65 | 65 | limit = case params[:format] |
|
66 | 66 | when 'csv', 'pdf' |
|
67 | 67 | Setting.issues_export_limit.to_i |
|
68 | 68 | when 'atom' |
|
69 | 69 | Setting.feeds_limit.to_i |
|
70 | 70 | else |
|
71 | 71 | per_page_option |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | @issue_count = @query.issue_count |
|
75 | 75 | @issue_pages = Paginator.new self, @issue_count, limit, params['page'] |
|
76 | 76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
77 | 77 | :order => sort_clause, |
|
78 | 78 | :offset => @issue_pages.current.offset, |
|
79 | 79 | :limit => limit) |
|
80 | 80 | @issue_count_by_group = @query.issue_count_by_group |
|
81 | 81 | |
|
82 | 82 | respond_to do |format| |
|
83 | 83 | format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } |
|
84 | 84 | format.xml { render :layout => false } |
|
85 | format.json { render :text => @issues.to_json, :layout => false } | |
|
85 | 86 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
86 | 87 | format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
87 | 88 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } |
|
88 | 89 | end |
|
89 | 90 | else |
|
90 | 91 | # Send html if the query is not valid |
|
91 | 92 | render(:template => 'issues/index.rhtml', :layout => !request.xhr?) |
|
92 | 93 | end |
|
93 | 94 | rescue ActiveRecord::RecordNotFound |
|
94 | 95 | render_404 |
|
95 | 96 | end |
|
96 | 97 | |
|
97 | 98 | def changes |
|
98 | 99 | retrieve_query |
|
99 | 100 | sort_init 'id', 'desc' |
|
100 | 101 | sort_update(@query.sortable_columns) |
|
101 | 102 | |
|
102 | 103 | if @query.valid? |
|
103 | 104 | @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", |
|
104 | 105 | :limit => 25) |
|
105 | 106 | end |
|
106 | 107 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
|
107 | 108 | render :layout => false, :content_type => 'application/atom+xml' |
|
108 | 109 | rescue ActiveRecord::RecordNotFound |
|
109 | 110 | render_404 |
|
110 | 111 | end |
|
111 | 112 | |
|
112 | 113 | def show |
|
113 | 114 | @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") |
|
114 | 115 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
115 | 116 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
116 | 117 | @changesets = @issue.changesets.visible.all |
|
117 | 118 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
118 | 119 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
119 | 120 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
120 | 121 | @priorities = IssuePriority.all |
|
121 | 122 | @time_entry = TimeEntry.new |
|
122 | 123 | respond_to do |format| |
|
123 | 124 | format.html { render :template => 'issues/show.rhtml' } |
|
124 | 125 | format.xml { render :layout => false } |
|
126 | format.json { render :text => @issue.to_json, :layout => false } | |
|
125 | 127 | format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } |
|
126 | 128 | format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } |
|
127 | 129 | end |
|
128 | 130 | end |
|
129 | 131 | |
|
130 | 132 | # Add a new issue |
|
131 | 133 | # The new issue will be created from an existing one if copy_from parameter is given |
|
132 | 134 | def new |
|
133 | 135 | render :action => 'new', :layout => !request.xhr? |
|
134 | 136 | end |
|
135 | 137 | |
|
136 | 138 | def create |
|
137 | 139 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
138 | 140 | if @issue.save |
|
139 | 141 | attachments = Attachment.attach_files(@issue, params[:attachments]) |
|
140 | 142 | render_attachment_warning_if_needed(@issue) |
|
141 | 143 | flash[:notice] = l(:notice_successful_create) |
|
142 | 144 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
143 | 145 | respond_to do |format| |
|
144 | 146 | format.html { |
|
145 | 147 | redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : |
|
146 | 148 | { :action => 'show', :id => @issue }) |
|
147 | 149 | } |
|
148 | 150 | format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } |
|
151 | format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false } | |
|
149 | 152 | end |
|
150 | 153 | return |
|
151 | 154 | else |
|
152 | 155 | respond_to do |format| |
|
153 | 156 | format.html { render :action => 'new' } |
|
154 | 157 | format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } |
|
158 | format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } | |
|
155 | 159 | end |
|
156 | 160 | end |
|
157 | 161 | end |
|
158 | 162 | |
|
159 | 163 | # Attributes that can be updated on workflow transition (without :edit permission) |
|
160 | 164 | # TODO: make it configurable (at least per role) |
|
161 | 165 | UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) |
|
162 | 166 | |
|
163 | 167 | def edit |
|
164 | 168 | update_issue_from_params |
|
165 | 169 | |
|
166 | 170 | @journal = @issue.current_journal |
|
167 | 171 | |
|
168 | 172 | respond_to do |format| |
|
169 | 173 | format.html { } |
|
170 | 174 | format.xml { } |
|
171 | 175 | end |
|
172 | 176 | end |
|
173 | 177 | |
|
174 | 178 | def update |
|
175 | 179 | update_issue_from_params |
|
176 | 180 | |
|
177 | 181 | if @issue.save_issue_with_child_records(params, @time_entry) |
|
178 | 182 | render_attachment_warning_if_needed(@issue) |
|
179 | 183 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
180 | 184 | |
|
181 | 185 | respond_to do |format| |
|
182 | 186 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } |
|
183 | 187 | format.xml { head :ok } |
|
188 | format.json { head :ok } | |
|
184 | 189 | end |
|
185 | 190 | else |
|
186 | 191 | render_attachment_warning_if_needed(@issue) |
|
187 | 192 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
188 | 193 | @journal = @issue.current_journal |
|
189 | 194 | |
|
190 | 195 | respond_to do |format| |
|
191 | 196 | format.html { render :action => 'edit' } |
|
192 | 197 | format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } |
|
198 | format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } | |
|
193 | 199 | end |
|
194 | 200 | end |
|
195 | 201 | end |
|
196 | 202 | |
|
197 | 203 | def reply |
|
198 | 204 | journal = Journal.find(params[:journal_id]) if params[:journal_id] |
|
199 | 205 | if journal |
|
200 | 206 | user = journal.user |
|
201 | 207 | text = journal.notes |
|
202 | 208 | else |
|
203 | 209 | user = @issue.author |
|
204 | 210 | text = @issue.description |
|
205 | 211 | end |
|
206 | 212 | # Replaces pre blocks with [...] |
|
207 | 213 | text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]') |
|
208 | 214 | content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " |
|
209 | 215 | content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" |
|
210 | 216 | |
|
211 | 217 | render(:update) { |page| |
|
212 | 218 | page.<< "$('notes').value = \"#{escape_javascript content}\";" |
|
213 | 219 | page.show 'update' |
|
214 | 220 | page << "Form.Element.focus('notes');" |
|
215 | 221 | page << "Element.scrollTo('update');" |
|
216 | 222 | page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;" |
|
217 | 223 | } |
|
218 | 224 | end |
|
219 | 225 | |
|
220 | 226 | # Bulk edit a set of issues |
|
221 | 227 | def bulk_edit |
|
222 | 228 | @issues.sort! |
|
223 | 229 | if request.post? |
|
224 | 230 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
225 | 231 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
226 | 232 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] |
|
227 | 233 | |
|
228 | 234 | unsaved_issue_ids = [] |
|
229 | 235 | @issues.each do |issue| |
|
230 | 236 | issue.reload |
|
231 | 237 | journal = issue.init_journal(User.current, params[:notes]) |
|
232 | 238 | issue.safe_attributes = attributes |
|
233 | 239 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
234 | 240 | unless issue.save |
|
235 | 241 | # Keep unsaved issue ids to display them in flash error |
|
236 | 242 | unsaved_issue_ids << issue.id |
|
237 | 243 | end |
|
238 | 244 | end |
|
239 | 245 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
240 | 246 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) |
|
241 | 247 | return |
|
242 | 248 | end |
|
243 | 249 | @available_statuses = Workflow.available_statuses(@project) |
|
244 | 250 | @custom_fields = @project.all_issue_custom_fields |
|
245 | 251 | end |
|
246 | 252 | |
|
247 | 253 | def move |
|
248 | 254 | @issues.sort! |
|
249 | 255 | @copy = params[:copy_options] && params[:copy_options][:copy] |
|
250 | 256 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
251 | 257 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] |
|
252 | 258 | @target_project ||= @project |
|
253 | 259 | @trackers = @target_project.trackers |
|
254 | 260 | @available_statuses = Workflow.available_statuses(@project) |
|
255 | 261 | if request.post? |
|
256 | 262 | new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) |
|
257 | 263 | unsaved_issue_ids = [] |
|
258 | 264 | moved_issues = [] |
|
259 | 265 | @issues.each do |issue| |
|
260 | 266 | issue.reload |
|
261 | 267 | changed_attributes = {} |
|
262 | 268 | [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute| |
|
263 | 269 | unless params[valid_attribute].blank? |
|
264 | 270 | changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute]) |
|
265 | 271 | end |
|
266 | 272 | end |
|
267 | 273 | issue.init_journal(User.current) |
|
268 | 274 | call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) |
|
269 | 275 | if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes}) |
|
270 | 276 | moved_issues << r |
|
271 | 277 | else |
|
272 | 278 | unsaved_issue_ids << issue.id |
|
273 | 279 | end |
|
274 | 280 | end |
|
275 | 281 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
276 | 282 | |
|
277 | 283 | if params[:follow] |
|
278 | 284 | if @issues.size == 1 && moved_issues.size == 1 |
|
279 | 285 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first |
|
280 | 286 | else |
|
281 | 287 | redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) |
|
282 | 288 | end |
|
283 | 289 | else |
|
284 | 290 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
285 | 291 | end |
|
286 | 292 | return |
|
287 | 293 | end |
|
288 | 294 | render :layout => false if request.xhr? |
|
289 | 295 | end |
|
290 | 296 | |
|
291 | 297 | def destroy |
|
292 | 298 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
293 | 299 | if @hours > 0 |
|
294 | 300 | case params[:todo] |
|
295 | 301 | when 'destroy' |
|
296 | 302 | # nothing to do |
|
297 | 303 | when 'nullify' |
|
298 | 304 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
299 | 305 | when 'reassign' |
|
300 | 306 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
301 | 307 | if reassign_to.nil? |
|
302 | 308 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
303 | 309 | return |
|
304 | 310 | else |
|
305 | 311 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
306 | 312 | end |
|
307 | 313 | else |
|
308 | unless params[:format] == 'xml' | |
|
314 | unless params[:format] == 'xml' || params[:format] == 'json' | |
|
309 | 315 | # display the destroy form if it's a user request |
|
310 | 316 | return |
|
311 | 317 | end |
|
312 | 318 | end |
|
313 | 319 | end |
|
314 | 320 | @issues.each(&:destroy) |
|
315 | 321 | respond_to do |format| |
|
316 | 322 | format.html { redirect_to :action => 'index', :project_id => @project } |
|
317 | 323 | format.xml { head :ok } |
|
324 | format.json { head :ok } | |
|
318 | 325 | end |
|
319 | 326 | end |
|
320 | 327 | |
|
321 | 328 | def context_menu |
|
322 | 329 | @issues = Issue.find_all_by_id(params[:ids], :include => :project) |
|
323 | 330 | if (@issues.size == 1) |
|
324 | 331 | @issue = @issues.first |
|
325 | 332 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
326 | 333 | end |
|
327 | 334 | projects = @issues.collect(&:project).compact.uniq |
|
328 | 335 | @project = projects.first if projects.size == 1 |
|
329 | 336 | |
|
330 | 337 | @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), |
|
331 | 338 | :log_time => (@project && User.current.allowed_to?(:log_time, @project)), |
|
332 | 339 | :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))), |
|
333 | 340 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), |
|
334 | 341 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), |
|
335 | 342 | :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) |
|
336 | 343 | } |
|
337 | 344 | if @project |
|
338 | 345 | @assignables = @project.assignable_users |
|
339 | 346 | @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to) |
|
340 | 347 | @trackers = @project.trackers |
|
341 | 348 | end |
|
342 | 349 | |
|
343 | 350 | @priorities = IssuePriority.all.reverse |
|
344 | 351 | @statuses = IssueStatus.find(:all, :order => 'position') |
|
345 | 352 | @back = params[:back_url] || request.env['HTTP_REFERER'] |
|
346 | 353 | |
|
347 | 354 | render :layout => false |
|
348 | 355 | end |
|
349 | 356 | |
|
350 | 357 | def update_form |
|
351 | 358 | if params[:id].blank? |
|
352 | 359 | @issue = Issue.new |
|
353 | 360 | @issue.project = @project |
|
354 | 361 | else |
|
355 | 362 | @issue = @project.issues.visible.find(params[:id]) |
|
356 | 363 | end |
|
357 | 364 | @issue.attributes = params[:issue] |
|
358 | 365 | @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq |
|
359 | 366 | @priorities = IssuePriority.all |
|
360 | 367 | |
|
361 | 368 | render :partial => 'attributes' |
|
362 | 369 | end |
|
363 | 370 | |
|
364 | 371 | def preview |
|
365 | 372 | @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? |
|
366 | 373 | if @issue |
|
367 | 374 | @attachements = @issue.attachments |
|
368 | 375 | @description = params[:issue] && params[:issue][:description] |
|
369 | 376 | if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n") |
|
370 | 377 | @description = nil |
|
371 | 378 | end |
|
372 | 379 | @notes = params[:notes] |
|
373 | 380 | else |
|
374 | 381 | @description = (params[:issue] ? params[:issue][:description] : nil) |
|
375 | 382 | end |
|
376 | 383 | render :layout => false |
|
377 | 384 | end |
|
378 | 385 | |
|
379 | 386 | def auto_complete |
|
380 | 387 | @issues = [] |
|
381 | 388 | q = params[:q].to_s |
|
382 | 389 | if q.match(/^\d+$/) |
|
383 | 390 | @issues << @project.issues.visible.find_by_id(q.to_i) |
|
384 | 391 | end |
|
385 | 392 | unless q.blank? |
|
386 | 393 | @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10) |
|
387 | 394 | end |
|
388 | 395 | render :layout => false |
|
389 | 396 | end |
|
390 | 397 | |
|
391 | 398 | private |
|
392 | 399 | def find_issue |
|
393 | 400 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
394 | 401 | @project = @issue.project |
|
395 | 402 | rescue ActiveRecord::RecordNotFound |
|
396 | 403 | render_404 |
|
397 | 404 | end |
|
398 | 405 | |
|
399 | 406 | # Filter for bulk operations |
|
400 | 407 | def find_issues |
|
401 | 408 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
402 | 409 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
403 | 410 | projects = @issues.collect(&:project).compact.uniq |
|
404 | 411 | if projects.size == 1 |
|
405 | 412 | @project = projects.first |
|
406 | 413 | else |
|
407 | 414 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
408 | 415 | render_error 'Can not bulk edit/move/destroy issues from different projects' |
|
409 | 416 | return false |
|
410 | 417 | end |
|
411 | 418 | rescue ActiveRecord::RecordNotFound |
|
412 | 419 | render_404 |
|
413 | 420 | end |
|
414 | 421 | |
|
415 | 422 | def find_project |
|
416 | 423 | project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] |
|
417 | 424 | @project = Project.find(project_id) |
|
418 | 425 | rescue ActiveRecord::RecordNotFound |
|
419 | 426 | render_404 |
|
420 | 427 | end |
|
421 | 428 | |
|
422 | 429 | # Used by #edit and #update to set some common instance variables |
|
423 | 430 | # from the params |
|
424 | 431 | # TODO: Refactor, not everything in here is needed by #edit |
|
425 | 432 | def update_issue_from_params |
|
426 | 433 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
427 | 434 | @priorities = IssuePriority.all |
|
428 | 435 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
429 | 436 | @time_entry = TimeEntry.new |
|
430 | 437 | |
|
431 | 438 | @notes = params[:notes] |
|
432 | 439 | @issue.init_journal(User.current, @notes) |
|
433 | 440 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
434 | 441 | if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] |
|
435 | 442 | attrs = params[:issue].dup |
|
436 | 443 | attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed |
|
437 | 444 | attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} |
|
438 | 445 | @issue.safe_attributes = attrs |
|
439 | 446 | end |
|
440 | 447 | |
|
441 | 448 | end |
|
442 | 449 | |
|
443 | 450 | # TODO: Refactor, lots of extra code in here |
|
444 | 451 | def build_new_issue_from_params |
|
445 | 452 | @issue = Issue.new |
|
446 | 453 | @issue.copy_from(params[:copy_from]) if params[:copy_from] |
|
447 | 454 | @issue.project = @project |
|
448 | 455 | # Tracker must be set before custom field values |
|
449 | 456 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
450 | 457 | if @issue.tracker.nil? |
|
451 | 458 | render_error l(:error_no_tracker_in_project) |
|
452 | 459 | return false |
|
453 | 460 | end |
|
454 | 461 | if params[:issue].is_a?(Hash) |
|
455 | 462 | @issue.safe_attributes = params[:issue] |
|
456 | 463 | @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project) |
|
457 | 464 | end |
|
458 | 465 | @issue.author = User.current |
|
459 | 466 | @issue.start_date ||= Date.today |
|
460 | 467 | @priorities = IssuePriority.all |
|
461 | 468 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
462 | 469 | end |
|
463 | 470 | |
|
464 | 471 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
|
465 | 472 | if unsaved_issue_ids.empty? |
|
466 | 473 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
|
467 | 474 | else |
|
468 | 475 | flash[:error] = l(:notice_failed_to_save_issues, |
|
469 | 476 | :count => unsaved_issue_ids.size, |
|
470 | 477 | :total => issues.size, |
|
471 | 478 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
472 | 479 | end |
|
473 | 480 | end |
|
474 | 481 | |
|
475 | 482 | def check_for_default_issue_status |
|
476 | 483 | if IssueStatus.default.nil? |
|
477 | 484 | render_error l(:error_no_default_issue_status) |
|
478 | 485 | return false |
|
479 | 486 | end |
|
480 | 487 | end |
|
481 | 488 | end |
@@ -1,187 +1,339 | |||
|
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 | |
|
20 | 20 | class IssuesApiTest < ActionController::IntegrationTest |
|
21 | 21 | fixtures :projects, |
|
22 | 22 | :users, |
|
23 | 23 | :roles, |
|
24 | 24 | :members, |
|
25 | 25 | :member_roles, |
|
26 | 26 | :issues, |
|
27 | 27 | :issue_statuses, |
|
28 | 28 | :versions, |
|
29 | 29 | :trackers, |
|
30 | 30 | :projects_trackers, |
|
31 | 31 | :issue_categories, |
|
32 | 32 | :enabled_modules, |
|
33 | 33 | :enumerations, |
|
34 | 34 | :attachments, |
|
35 | 35 | :workflows, |
|
36 | 36 | :custom_fields, |
|
37 | 37 | :custom_values, |
|
38 | 38 | :custom_fields_projects, |
|
39 | 39 | :custom_fields_trackers, |
|
40 | 40 | :time_entries, |
|
41 | 41 | :journals, |
|
42 | 42 | :journal_details, |
|
43 | 43 | :queries |
|
44 | 44 | |
|
45 | 45 | def setup |
|
46 | 46 | Setting.rest_api_enabled = '1' |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | context "/index.xml" do |
|
50 | 50 | setup do |
|
51 | 51 | get '/issues.xml' |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | should_respond_with :success |
|
55 | 55 | should_respond_with_content_type 'application/xml' |
|
56 | 56 | end |
|
57 | ||
|
57 | ||
|
58 | context "/index.json" do | |
|
59 | setup do | |
|
60 | get '/issues.json' | |
|
61 | end | |
|
62 | ||
|
63 | should_respond_with :success | |
|
64 | should_respond_with_content_type 'application/json' | |
|
65 | ||
|
66 | should 'return a valid JSON string' do | |
|
67 | assert ActiveSupport::JSON.decode(response.body) | |
|
68 | end | |
|
69 | end | |
|
70 | ||
|
58 | 71 | context "/index.xml with filter" do |
|
59 | 72 | setup do |
|
60 | 73 | get '/issues.xml?status_id=5' |
|
61 | 74 | end |
|
62 | 75 | |
|
63 | 76 | should_respond_with :success |
|
64 | 77 | should_respond_with_content_type 'application/xml' |
|
65 | 78 | should "show only issues with the status_id" do |
|
66 | 79 | assert_tag :tag => 'issues', |
|
67 | 80 | :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), |
|
68 | 81 | :only => { :tag => 'issue' } } |
|
69 | 82 | end |
|
70 | 83 | end |
|
71 | 84 | |
|
85 | context "/index.json with filter" do | |
|
86 | setup do | |
|
87 | get '/issues.json?status_id=5' | |
|
88 | end | |
|
89 | ||
|
90 | should_respond_with :success | |
|
91 | should_respond_with_content_type 'application/json' | |
|
92 | ||
|
93 | should 'return a valid JSON string' do | |
|
94 | assert ActiveSupport::JSON.decode(response.body) | |
|
95 | end | |
|
96 | ||
|
97 | should "show only issues with the status_id" do | |
|
98 | json = ActiveSupport::JSON.decode(response.body) | |
|
99 | status_ids_used = json.collect {|j| j['status_id'] } | |
|
100 | assert_equal 3, status_ids_used.length | |
|
101 | assert status_ids_used.all? {|id| id == 5 } | |
|
102 | end | |
|
103 | ||
|
104 | end | |
|
105 | ||
|
72 | 106 | context "/issues/1.xml" do |
|
73 | 107 | setup do |
|
74 | 108 | get '/issues/1.xml' |
|
75 | 109 | end |
|
76 | 110 | |
|
77 | 111 | should_respond_with :success |
|
78 | 112 | should_respond_with_content_type 'application/xml' |
|
79 | 113 | end |
|
80 | 114 | |
|
115 | context "/issues/1.json" do | |
|
116 | setup do | |
|
117 | get '/issues/1.json' | |
|
118 | end | |
|
119 | ||
|
120 | should_respond_with :success | |
|
121 | should_respond_with_content_type 'application/json' | |
|
122 | ||
|
123 | should 'return a valid JSON string' do | |
|
124 | assert ActiveSupport::JSON.decode(response.body) | |
|
125 | end | |
|
126 | end | |
|
127 | ||
|
81 | 128 | context "POST /issues.xml" do |
|
82 | 129 | setup do |
|
83 | 130 | @issue_count = Issue.count |
|
84 | 131 | @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3} |
|
85 | 132 | post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith') |
|
86 | 133 | end |
|
87 | 134 | |
|
88 | 135 | should_respond_with :created |
|
89 | 136 | should_respond_with_content_type 'application/xml' |
|
90 | 137 | |
|
91 | 138 | should "create an issue with the attributes" do |
|
92 | 139 | assert_equal Issue.count, @issue_count + 1 |
|
93 | 140 | |
|
94 | 141 | issue = Issue.first(:order => 'id DESC') |
|
95 | 142 | @attributes.each do |attribute, value| |
|
96 | 143 | assert_equal value, issue.send(attribute) |
|
97 | 144 | end |
|
98 | 145 | end |
|
99 | 146 | end |
|
100 | 147 | |
|
101 | 148 | context "POST /issues.xml with failure" do |
|
102 | 149 | setup do |
|
103 | 150 | @attributes = {:project_id => 1} |
|
104 | 151 | post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith') |
|
105 | 152 | end |
|
106 | 153 | |
|
107 | 154 | should_respond_with :unprocessable_entity |
|
108 | 155 | should_respond_with_content_type 'application/xml' |
|
109 | 156 | |
|
110 | 157 | should "have an errors tag" do |
|
111 | 158 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} |
|
112 | 159 | end |
|
113 | 160 | end |
|
114 | ||
|
161 | ||
|
162 | context "POST /issues.json" do | |
|
163 | setup do | |
|
164 | @issue_count = Issue.count | |
|
165 | @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3} | |
|
166 | post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith') | |
|
167 | end | |
|
168 | ||
|
169 | should_respond_with :created | |
|
170 | should_respond_with_content_type 'application/json' | |
|
171 | ||
|
172 | should "create an issue with the attributes" do | |
|
173 | assert_equal Issue.count, @issue_count + 1 | |
|
174 | ||
|
175 | issue = Issue.first(:order => 'id DESC') | |
|
176 | @attributes.each do |attribute, value| | |
|
177 | assert_equal value, issue.send(attribute) | |
|
178 | end | |
|
179 | end | |
|
180 | end | |
|
181 | ||
|
182 | context "POST /issues.json with failure" do | |
|
183 | setup do | |
|
184 | @attributes = {:project_id => 1} | |
|
185 | post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith') | |
|
186 | end | |
|
187 | ||
|
188 | should_respond_with :unprocessable_entity | |
|
189 | should_respond_with_content_type 'application/json' | |
|
190 | ||
|
191 | should "have an errors element" do | |
|
192 | json = ActiveSupport::JSON.decode(response.body) | |
|
193 | assert_equal "can't be blank", json.first['subject'] | |
|
194 | end | |
|
195 | end | |
|
196 | ||
|
115 | 197 | context "PUT /issues/1.xml" do |
|
116 | 198 | setup do |
|
117 | 199 | @issue_count = Issue.count |
|
118 | 200 | @journal_count = Journal.count |
|
119 | 201 | @attributes = {:subject => 'API update'} |
|
120 | 202 | |
|
121 | 203 | put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith') |
|
122 | 204 | end |
|
123 | 205 | |
|
124 | 206 | should_respond_with :ok |
|
125 | 207 | should_respond_with_content_type 'application/xml' |
|
126 | 208 | |
|
127 | 209 | should "not create a new issue" do |
|
128 | 210 | assert_equal Issue.count, @issue_count |
|
129 | 211 | end |
|
130 | 212 | |
|
131 | 213 | should "create a new journal" do |
|
132 | 214 | assert_equal Journal.count, @journal_count + 1 |
|
133 | 215 | end |
|
134 | 216 | |
|
135 | 217 | should "update the issue" do |
|
136 | 218 | issue = Issue.find(1) |
|
137 | 219 | @attributes.each do |attribute, value| |
|
138 | 220 | assert_equal value, issue.send(attribute) |
|
139 | 221 | end |
|
140 | 222 | end |
|
141 | 223 | |
|
142 | 224 | end |
|
143 | 225 | |
|
144 | 226 | context "PUT /issues/1.xml with failed update" do |
|
145 | 227 | setup do |
|
146 | 228 | @attributes = {:subject => ''} |
|
147 | 229 | @issue_count = Issue.count |
|
148 | 230 | @journal_count = Journal.count |
|
149 | 231 | |
|
150 | 232 | put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith') |
|
151 | 233 | end |
|
152 | 234 | |
|
153 | 235 | should_respond_with :unprocessable_entity |
|
154 | 236 | should_respond_with_content_type 'application/xml' |
|
155 | 237 | |
|
156 | 238 | should "not create a new issue" do |
|
157 | 239 | assert_equal Issue.count, @issue_count |
|
158 | 240 | end |
|
159 | 241 | |
|
160 | 242 | should "not create a new journal" do |
|
161 | 243 | assert_equal Journal.count, @journal_count |
|
162 | 244 | end |
|
163 | 245 | |
|
164 | 246 | should "have an errors tag" do |
|
165 | 247 | assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} |
|
166 | 248 | end |
|
167 | 249 | end |
|
168 | 250 | |
|
251 | context "PUT /issues/1.json" do | |
|
252 | setup do | |
|
253 | @issue_count = Issue.count | |
|
254 | @journal_count = Journal.count | |
|
255 | @attributes = {:subject => 'API update'} | |
|
256 | ||
|
257 | put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith') | |
|
258 | end | |
|
259 | ||
|
260 | should_respond_with :ok | |
|
261 | should_respond_with_content_type 'application/json' | |
|
262 | ||
|
263 | should "not create a new issue" do | |
|
264 | assert_equal Issue.count, @issue_count | |
|
265 | end | |
|
266 | ||
|
267 | should "create a new journal" do | |
|
268 | assert_equal Journal.count, @journal_count + 1 | |
|
269 | end | |
|
270 | ||
|
271 | should "update the issue" do | |
|
272 | issue = Issue.find(1) | |
|
273 | @attributes.each do |attribute, value| | |
|
274 | assert_equal value, issue.send(attribute) | |
|
275 | end | |
|
276 | end | |
|
277 | ||
|
278 | end | |
|
279 | ||
|
280 | context "PUT /issues/1.json with failed update" do | |
|
281 | setup do | |
|
282 | @attributes = {:subject => ''} | |
|
283 | @issue_count = Issue.count | |
|
284 | @journal_count = Journal.count | |
|
285 | ||
|
286 | put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith') | |
|
287 | end | |
|
288 | ||
|
289 | should_respond_with :unprocessable_entity | |
|
290 | should_respond_with_content_type 'application/json' | |
|
291 | ||
|
292 | should "not create a new issue" do | |
|
293 | assert_equal Issue.count, @issue_count | |
|
294 | end | |
|
295 | ||
|
296 | should "not create a new journal" do | |
|
297 | assert_equal Journal.count, @journal_count | |
|
298 | end | |
|
299 | ||
|
300 | should "have an errors attribute" do | |
|
301 | json = ActiveSupport::JSON.decode(response.body) | |
|
302 | assert_equal "can't be blank", json.first['subject'] | |
|
303 | end | |
|
304 | end | |
|
305 | ||
|
169 | 306 | context "DELETE /issues/1.xml" do |
|
170 | 307 | setup do |
|
171 | 308 | @issue_count = Issue.count |
|
172 | 309 | delete '/issues/1.xml', {}, :authorization => credentials('jsmith') |
|
173 | 310 | end |
|
174 | 311 | |
|
175 | 312 | should_respond_with :ok |
|
176 | 313 | should_respond_with_content_type 'application/xml' |
|
177 | 314 | |
|
178 | 315 | should "delete the issue" do |
|
179 | 316 | assert_equal Issue.count, @issue_count -1 |
|
180 | 317 | assert_nil Issue.find_by_id(1) |
|
181 | 318 | end |
|
182 | 319 | end |
|
183 | ||
|
320 | ||
|
321 | context "DELETE /issues/1.json" do | |
|
322 | setup do | |
|
323 | @issue_count = Issue.count | |
|
324 | delete '/issues/1.json', {}, :authorization => credentials('jsmith') | |
|
325 | end | |
|
326 | ||
|
327 | should_respond_with :ok | |
|
328 | should_respond_with_content_type 'application/json' | |
|
329 | ||
|
330 | should "delete the issue" do | |
|
331 | assert_equal Issue.count, @issue_count -1 | |
|
332 | assert_nil Issue.find_by_id(1) | |
|
333 | end | |
|
334 | end | |
|
335 | ||
|
184 | 336 | def credentials(user, password=nil) |
|
185 | 337 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) |
|
186 | 338 | end |
|
187 | 339 | end |
General Comments 0
You need to be logged in to leave comments.
Login now