You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issues_test.rb 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../../test_helper', __FILE__)
  18. class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
  19. fixtures :projects,
  20. :users,
  21. :roles,
  22. :members,
  23. :member_roles,
  24. :issues,
  25. :issue_statuses,
  26. :issue_relations,
  27. :versions,
  28. :trackers,
  29. :projects_trackers,
  30. :issue_categories,
  31. :enabled_modules,
  32. :enumerations,
  33. :attachments,
  34. :workflows,
  35. :custom_fields,
  36. :custom_values,
  37. :custom_fields_projects,
  38. :custom_fields_trackers,
  39. :time_entries,
  40. :journals,
  41. :journal_details,
  42. :queries,
  43. :attachments
  44. test "GET /issues.xml should contain metadata" do
  45. get '/issues.xml'
  46. assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]',
  47. assigns(:issue_count).to_s
  48. end
  49. test "GET /issues.xml with nometa param should not contain metadata" do
  50. get '/issues.xml?nometa=1'
  51. assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
  52. end
  53. test "GET /issues.xml with nometa header should not contain metadata" do
  54. get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
  55. assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
  56. end
  57. test "GET /issues.xml with offset and limit" do
  58. get '/issues.xml?offset=2&limit=3'
  59. assert_equal 3, assigns(:limit)
  60. assert_equal 2, assigns(:offset)
  61. assert_select 'issues issue', 3
  62. end
  63. test "GET /issues.xml with relations" do
  64. get '/issues.xml?include=relations'
  65. assert_response :success
  66. assert_equal 'application/xml', @response.content_type
  67. assert_select 'issue id', :text => '3' do
  68. assert_select '~ relations relation', 1
  69. assert_select '~ relations relation[id="2"][issue_id="2"][issue_to_id="3"][relation_type=relates]'
  70. end
  71. assert_select 'issue id', :text => '1' do
  72. assert_select '~ relations'
  73. assert_select '~ relations relation', 0
  74. end
  75. end
  76. test "GET /issues.xml with invalid query params" do
  77. get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
  78. assert_response :unprocessable_entity
  79. assert_equal 'application/xml', @response.content_type
  80. assert_select 'errors error', :text => "Start date cannot be blank"
  81. end
  82. test "GET /issues.xml with custom field filter" do
  83. get '/issues.xml',
  84. {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
  85. expected_ids = Issue.visible.
  86. joins(:custom_values).
  87. where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
  88. assert expected_ids.any?
  89. assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
  90. ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
  91. end
  92. end
  93. test "GET /issues.xml with custom field filter (shorthand method)" do
  94. get '/issues.xml', {:cf_1 => 'MySQL'}
  95. expected_ids = Issue.visible.
  96. joins(:custom_values).
  97. where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
  98. assert expected_ids.any?
  99. assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
  100. ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
  101. end
  102. end
  103. def test_index_should_include_issue_attributes
  104. get '/issues.xml'
  105. assert_select 'issues>issue>is_private', :text => 'false'
  106. end
  107. def test_index_should_allow_timestamp_filtering
  108. Issue.delete_all
  109. Issue.generate!(:subject => '1').update_column(:updated_on, Time.parse("2014-01-02T10:25:00Z"))
  110. Issue.generate!(:subject => '2').update_column(:updated_on, Time.parse("2014-01-02T12:13:00Z"))
  111. get '/issues.xml',
  112. {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '<='},
  113. :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
  114. assert_select 'issues>issue', :count => 1
  115. assert_select 'issues>issue>subject', :text => '1'
  116. get '/issues.xml',
  117. {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
  118. :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
  119. assert_select 'issues>issue', :count => 1
  120. assert_select 'issues>issue>subject', :text => '2'
  121. get '/issues.xml',
  122. {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
  123. :v => {:updated_on => ['2014-01-02T08:00:00Z']}}
  124. assert_select 'issues>issue', :count => 2
  125. end
  126. test "GET /issues.xml with filter" do
  127. get '/issues.xml?status_id=5'
  128. expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
  129. assert expected_ids.any?
  130. assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
  131. ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
  132. end
  133. end
  134. test "GET /issues.json with filter" do
  135. get '/issues.json?status_id=5'
  136. json = ActiveSupport::JSON.decode(response.body)
  137. status_ids_used = json['issues'].collect {|j| j['status']['id'] }
  138. assert_equal 3, status_ids_used.length
  139. assert status_ids_used.all? {|id| id == 5 }
  140. end
  141. test "GET /issues/:id.xml with journals" do
  142. get '/issues/1.xml?include=journals'
  143. assert_select 'issue journals[type=array]' do
  144. assert_select 'journal[id="1"]' do
  145. assert_select 'details[type=array]' do
  146. assert_select 'detail[name=status_id]' do
  147. assert_select 'old_value', :text => '1'
  148. assert_select 'new_value', :text => '2'
  149. end
  150. end
  151. end
  152. end
  153. end
  154. test "GET /issues/:id.xml with journals should format timestamps in ISO 8601" do
  155. get '/issues/1.xml?include=journals'
  156. iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
  157. assert_select 'issue>created_on', :text => iso_date
  158. assert_select 'issue>updated_on', :text => iso_date
  159. assert_select 'issue journal>created_on', :text => iso_date
  160. end
  161. test "GET /issues/:id.xml with custom fields" do
  162. get '/issues/3.xml'
  163. assert_select 'issue custom_fields[type=array]' do
  164. assert_select 'custom_field[id="1"]' do
  165. assert_select 'value', :text => 'MySQL'
  166. end
  167. end
  168. assert_nothing_raised do
  169. Hash.from_xml(response.body).to_xml
  170. end
  171. end
  172. test "GET /issues/:id.xml with multi custom fields" do
  173. field = CustomField.find(1)
  174. field.update_attribute :multiple, true
  175. issue = Issue.find(3)
  176. issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
  177. issue.save!
  178. get '/issues/3.xml'
  179. assert_response :success
  180. assert_select 'issue custom_fields[type=array]' do
  181. assert_select 'custom_field[id="1"]' do
  182. assert_select 'value[type=array] value', 2
  183. end
  184. end
  185. xml = Hash.from_xml(response.body)
  186. custom_fields = xml['issue']['custom_fields']
  187. assert_kind_of Array, custom_fields
  188. field = custom_fields.detect {|f| f['id'] == '1'}
  189. assert_kind_of Hash, field
  190. assert_equal ['MySQL', 'Oracle'], field['value'].sort
  191. end
  192. test "GET /issues/:id.json with multi custom fields" do
  193. field = CustomField.find(1)
  194. field.update_attribute :multiple, true
  195. issue = Issue.find(3)
  196. issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
  197. issue.save!
  198. get '/issues/3.json'
  199. assert_response :success
  200. json = ActiveSupport::JSON.decode(response.body)
  201. custom_fields = json['issue']['custom_fields']
  202. assert_kind_of Array, custom_fields
  203. field = custom_fields.detect {|f| f['id'] == 1}
  204. assert_kind_of Hash, field
  205. assert_equal ['MySQL', 'Oracle'], field['value'].sort
  206. end
  207. test "GET /issues/:id.xml with empty value for multi custom field" do
  208. field = CustomField.find(1)
  209. field.update_attribute :multiple, true
  210. issue = Issue.find(3)
  211. issue.custom_field_values = {1 => ['']}
  212. issue.save!
  213. get '/issues/3.xml'
  214. assert_select 'issue custom_fields[type=array]' do
  215. assert_select 'custom_field[id="1"]' do
  216. assert_select 'value[type=array]:empty'
  217. end
  218. end
  219. xml = Hash.from_xml(response.body)
  220. custom_fields = xml['issue']['custom_fields']
  221. assert_kind_of Array, custom_fields
  222. field = custom_fields.detect {|f| f['id'] == '1'}
  223. assert_kind_of Hash, field
  224. assert_equal [], field['value']
  225. end
  226. test "GET /issues/:id.json with empty value for multi custom field" do
  227. field = CustomField.find(1)
  228. field.update_attribute :multiple, true
  229. issue = Issue.find(3)
  230. issue.custom_field_values = {1 => ['']}
  231. issue.save!
  232. get '/issues/3.json'
  233. assert_response :success
  234. json = ActiveSupport::JSON.decode(response.body)
  235. custom_fields = json['issue']['custom_fields']
  236. assert_kind_of Array, custom_fields
  237. field = custom_fields.detect {|f| f['id'] == 1}
  238. assert_kind_of Hash, field
  239. assert_equal [], field['value'].sort
  240. end
  241. test "GET /issues/:id.xml with attachments" do
  242. get '/issues/3.xml?include=attachments'
  243. assert_select 'issue attachments[type=array]' do
  244. assert_select 'attachment', 4
  245. assert_select 'attachment id', :text => '1' do
  246. assert_select '~ filename', :text => 'error281.txt'
  247. assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
  248. end
  249. end
  250. end
  251. test "GET /issues/:id.xml with subtasks" do
  252. issue = Issue.generate_with_descendants!(:project_id => 1)
  253. get "/issues/#{issue.id}.xml?include=children"
  254. assert_select 'issue id', :text => issue.id.to_s do
  255. assert_select '~ children[type=array] > issue', 2
  256. assert_select '~ children[type=array] > issue > children', 1
  257. end
  258. end
  259. test "GET /issues/:id.json with subtasks" do
  260. issue = Issue.generate_with_descendants!(:project_id => 1)
  261. get "/issues/#{issue.id}.json?include=children"
  262. json = ActiveSupport::JSON.decode(response.body)
  263. assert_equal 2, json['issue']['children'].size
  264. assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
  265. end
  266. def test_show_should_include_issue_attributes
  267. get '/issues/1.xml'
  268. assert_select 'issue>is_private', :text => 'false'
  269. end
  270. test "GET /issues/:id.xml?include=watchers should include watchers" do
  271. Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
  272. get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
  273. assert_response :ok
  274. assert_equal 'application/xml', response.content_type
  275. assert_select 'issue' do
  276. assert_select 'watchers', Issue.find(1).watchers.count
  277. assert_select 'watchers' do
  278. assert_select 'user[id="3"]'
  279. end
  280. end
  281. end
  282. test "GET /issues/:id.xml should not disclose associated changesets from projects the user has no access to" do
  283. project = Project.generate!(:is_public => false)
  284. repository = Repository::Subversion.create!(:project => project, :url => "svn://localhost")
  285. Issue.find(1).changesets << Changeset.generate!(:repository => repository)
  286. assert Issue.find(1).changesets.any?
  287. get '/issues/1.xml?include=changesets', {}, credentials('jsmith')
  288. # the user jsmith has no permission to view the associated changeset
  289. assert_select 'issue changesets[type=array]' do
  290. assert_select 'changeset', 0
  291. end
  292. end
  293. test "GET /issues/:id.xml should contains total_estimated_hours and total_spent_hours" do
  294. parent = Issue.find(3)
  295. child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
  296. TimeEntry.create!(:project => child.project, :issue => child, :user => child.author, :spent_on => child.author.today,
  297. :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id)
  298. get '/issues/3.xml'
  299. assert_equal 'application/xml', response.content_type
  300. assert_select 'issue' do
  301. assert_select 'estimated_hours', parent.estimated_hours.to_s
  302. assert_select 'total_estimated_hours', (parent.estimated_hours.to_f + 3.0).to_s
  303. assert_select 'spent_hours', parent.spent_hours.to_s
  304. assert_select 'total_spent_hours', (parent.spent_hours.to_f + 2.5).to_s
  305. end
  306. end
  307. test "GET /issues/:id.xml should contains total_estimated_hours, and should not contains spent_hours and total_spent_hours when permission does not exists" do
  308. parent = Issue.find(3)
  309. child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
  310. # remove permission!
  311. Role.anonymous.remove_permission! :view_time_entries
  312. #Role.all.each { |role| role.remove_permission! :view_time_entries }
  313. get '/issues/3.xml'
  314. assert_equal 'application/xml', response.content_type
  315. assert_select 'issue' do
  316. assert_select 'estimated_hours', parent.estimated_hours.to_s
  317. assert_select 'total_estimated_hours', (parent.estimated_hours.to_f + 3.0).to_s
  318. assert_select 'spent_hours', false
  319. assert_select 'total_spent_hours', false
  320. end
  321. end
  322. test "GET /issues/:id.json should contains total_estimated_hours and total_spent_hours" do
  323. parent = Issue.find(3)
  324. child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
  325. TimeEntry.create!(:project => child.project, :issue => child, :user => child.author, :spent_on => child.author.today,
  326. :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id)
  327. get '/issues/3.json'
  328. assert_equal 'application/json', response.content_type
  329. json = ActiveSupport::JSON.decode(response.body)
  330. assert_equal parent.estimated_hours, json['issue']['estimated_hours']
  331. assert_equal (parent.estimated_hours.to_f + 3.0), json['issue']['total_estimated_hours']
  332. assert_equal parent.spent_hours, json['issue']['spent_hours']
  333. assert_equal (parent.spent_hours.to_f + 2.5), json['issue']['total_spent_hours']
  334. end
  335. test "GET /issues/:id.json should contains total_estimated_hours, and should not contains spent_hours and total_spent_hours when permission does not exists" do
  336. parent = Issue.find(3)
  337. child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
  338. # remove permission!
  339. Role.anonymous.remove_permission! :view_time_entries
  340. #Role.all.each { |role| role.remove_permission! :view_time_entries }
  341. get '/issues/3.json'
  342. assert_equal 'application/json', response.content_type
  343. json = ActiveSupport::JSON.decode(response.body)
  344. assert_equal parent.estimated_hours, json['issue']['estimated_hours']
  345. assert_equal (parent.estimated_hours.to_f + 3.0), json['issue']['total_estimated_hours']
  346. assert_equal nil, json['issue']['spent_hours']
  347. assert_equal nil, json['issue']['total_spent_hours']
  348. end
  349. test "POST /issues.xml should create an issue with the attributes" do
  350. payload = <<-XML
  351. <?xml version="1.0" encoding="UTF-8" ?>
  352. <issue>
  353. <project_id>1</project_id>
  354. <tracker_id>2</tracker_id>
  355. <status_id>3</status_id>
  356. <subject>API test</subject>
  357. </issue>
  358. XML
  359. assert_difference('Issue.count') do
  360. post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
  361. end
  362. issue = Issue.order('id DESC').first
  363. assert_equal 1, issue.project_id
  364. assert_equal 2, issue.tracker_id
  365. assert_equal 3, issue.status_id
  366. assert_equal 'API test', issue.subject
  367. assert_response :created
  368. assert_equal 'application/xml', @response.content_type
  369. assert_select 'issue > id', :text => issue.id.to_s
  370. end
  371. test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
  372. assert_difference('Issue.count') do
  373. post '/issues.xml',
  374. {:issue => {:project_id => 1, :subject => 'Watchers',
  375. :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
  376. assert_response :created
  377. end
  378. issue = Issue.order('id desc').first
  379. assert_equal 2, issue.watchers.size
  380. assert_equal [1, 3], issue.watcher_user_ids.sort
  381. end
  382. test "POST /issues.xml with failure should return errors" do
  383. assert_no_difference('Issue.count') do
  384. post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
  385. end
  386. assert_select 'errors error', :text => "Subject cannot be blank"
  387. end
  388. test "POST /issues.json should create an issue with the attributes" do
  389. payload = <<-JSON
  390. {
  391. "issue": {
  392. "project_id": "1",
  393. "tracker_id": "2",
  394. "status_id": "3",
  395. "subject": "API test"
  396. }
  397. }
  398. JSON
  399. assert_difference('Issue.count') do
  400. post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
  401. end
  402. issue = Issue.order('id DESC').first
  403. assert_equal 1, issue.project_id
  404. assert_equal 2, issue.tracker_id
  405. assert_equal 3, issue.status_id
  406. assert_equal 'API test', issue.subject
  407. end
  408. test "POST /issues.json without tracker_id should accept custom fields" do
  409. field = IssueCustomField.generate!(
  410. :field_format => 'list',
  411. :multiple => true,
  412. :possible_values => ["V1", "V2", "V3"],
  413. :default_value => "V2",
  414. :is_for_all => true,
  415. :trackers => Tracker.all.to_a
  416. )
  417. payload = <<-JSON
  418. {
  419. "issue": {
  420. "project_id": "1",
  421. "subject": "Multivalued custom field",
  422. "custom_field_values":{"#{field.id}":["V1","V3"]}
  423. }
  424. }
  425. JSON
  426. assert_difference('Issue.count') do
  427. post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
  428. end
  429. assert_response :created
  430. issue = Issue.order('id DESC').first
  431. assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
  432. end
  433. test "POST /issues.json with omitted custom field should set default value" do
  434. field = IssueCustomField.generate!(:default_value => "Default")
  435. issue = new_record(Issue) do
  436. post '/issues.json',
  437. {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}}},
  438. credentials('jsmith')
  439. end
  440. assert_equal "Default", issue.custom_field_value(field)
  441. end
  442. test "POST /issues.json with custom field set to blank should not set default value" do
  443. field = IssueCustomField.generate!(:default_value => "Default")
  444. issue = new_record(Issue) do
  445. post '/issues.json',
  446. {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}}},
  447. credentials('jsmith')
  448. end
  449. assert_equal "", issue.custom_field_value(field)
  450. end
  451. test "POST /issues.json with failure should return errors" do
  452. assert_no_difference('Issue.count') do
  453. post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
  454. end
  455. json = ActiveSupport::JSON.decode(response.body)
  456. assert json['errors'].include?("Subject cannot be blank")
  457. end
  458. test "POST /issues.json with invalid project_id should respond with 422" do
  459. post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
  460. assert_response 422
  461. end
  462. test "PUT /issues/:id.xml" do
  463. assert_difference('Journal.count') do
  464. put '/issues/6.xml',
  465. {:issue => {:subject => 'API update', :notes => 'A new note'}},
  466. credentials('jsmith')
  467. end
  468. issue = Issue.find(6)
  469. assert_equal "API update", issue.subject
  470. journal = Journal.last
  471. assert_equal "A new note", journal.notes
  472. end
  473. test "PUT /issues/:id.xml with custom fields" do
  474. put '/issues/3.xml',
  475. {:issue => {:custom_fields => [
  476. {'id' => '1', 'value' => 'PostgreSQL' },
  477. {'id' => '2', 'value' => '150'}
  478. ]}},
  479. credentials('jsmith')
  480. issue = Issue.find(3)
  481. assert_equal '150', issue.custom_value_for(2).value
  482. assert_equal 'PostgreSQL', issue.custom_value_for(1).value
  483. end
  484. test "PUT /issues/:id.xml with multi custom fields" do
  485. field = CustomField.find(1)
  486. field.update_attribute :multiple, true
  487. put '/issues/3.xml',
  488. {:issue => {:custom_fields => [
  489. {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
  490. {'id' => '2', 'value' => '150'}
  491. ]}},
  492. credentials('jsmith')
  493. issue = Issue.find(3)
  494. assert_equal '150', issue.custom_value_for(2).value
  495. assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
  496. end
  497. test "PUT /issues/:id.xml with project change" do
  498. put '/issues/3.xml',
  499. {:issue => {:project_id => 2, :subject => 'Project changed'}},
  500. credentials('jsmith')
  501. issue = Issue.find(3)
  502. assert_equal 2, issue.project_id
  503. assert_equal 'Project changed', issue.subject
  504. end
  505. test "PUT /issues/:id.xml with notes only" do
  506. assert_difference('Journal.count') do
  507. put '/issues/6.xml',
  508. {:issue => {:notes => 'Notes only'}},
  509. credentials('jsmith')
  510. end
  511. journal = Journal.last
  512. assert_equal "Notes only", journal.notes
  513. end
  514. test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do
  515. field = IssueCustomField.generate!(:default_value => "Default")
  516. issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
  517. assert_equal "", issue.reload.custom_field_value(field)
  518. assert_difference('Journal.count') do
  519. put "/issues/#{issue.id}.json",
  520. {:issue => {:custom_field_values => {}, :notes => 'API'}},
  521. credentials('jsmith')
  522. end
  523. assert_equal "", issue.reload.custom_field_value(field)
  524. end
  525. test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do
  526. field = IssueCustomField.generate!(:default_value => "Default")
  527. issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
  528. assert_equal "", issue.reload.custom_field_value(field)
  529. assert_difference('Journal.count') do
  530. put "/issues/#{issue.id}.json",
  531. {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
  532. credentials('jsmith')
  533. end
  534. assert_equal "", issue.reload.custom_field_value(field)
  535. end
  536. test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do
  537. field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
  538. issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
  539. assert_difference('Journal.count') do
  540. put "/issues/#{issue.id}.json",
  541. {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}},
  542. credentials('jsmith')
  543. end
  544. assert_equal 2, issue.reload.tracker_id
  545. assert_equal "Default", issue.reload.custom_field_value(field)
  546. end
  547. test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
  548. field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
  549. issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
  550. assert_difference('Journal.count') do
  551. put "/issues/#{issue.id}.json",
  552. {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
  553. credentials('jsmith')
  554. end
  555. assert_equal 2, issue.reload.tracker_id
  556. assert_equal "", issue.reload.custom_field_value(field)
  557. end
  558. test "PUT /issues/:id.xml with failed update" do
  559. put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
  560. assert_response :unprocessable_entity
  561. assert_select 'errors error', :text => "Subject cannot be blank"
  562. end
  563. test "PUT /issues/:id.json" do
  564. assert_difference('Journal.count') do
  565. put '/issues/6.json',
  566. {:issue => {:subject => 'API update', :notes => 'A new note'}},
  567. credentials('jsmith')
  568. assert_response :ok
  569. assert_equal '', response.body
  570. end
  571. issue = Issue.find(6)
  572. assert_equal "API update", issue.subject
  573. journal = Journal.last
  574. assert_equal "A new note", journal.notes
  575. end
  576. test "PUT /issues/:id.json with failed update" do
  577. put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
  578. assert_response :unprocessable_entity
  579. json = ActiveSupport::JSON.decode(response.body)
  580. assert json['errors'].include?("Subject cannot be blank")
  581. end
  582. test "DELETE /issues/:id.xml" do
  583. assert_difference('Issue.count', -1) do
  584. delete '/issues/6.xml', {}, credentials('jsmith')
  585. assert_response :ok
  586. assert_equal '', response.body
  587. end
  588. assert_nil Issue.find_by_id(6)
  589. end
  590. test "DELETE /issues/:id.json" do
  591. assert_difference('Issue.count', -1) do
  592. delete '/issues/6.json', {}, credentials('jsmith')
  593. assert_response :ok
  594. assert_equal '', response.body
  595. end
  596. assert_nil Issue.find_by_id(6)
  597. end
  598. test "POST /issues/:id/watchers.xml should add watcher" do
  599. assert_difference 'Watcher.count' do
  600. post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
  601. assert_response :ok
  602. assert_equal '', response.body
  603. end
  604. watcher = Watcher.order('id desc').first
  605. assert_equal Issue.find(1), watcher.watchable
  606. assert_equal User.find(3), watcher.user
  607. end
  608. test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
  609. Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
  610. assert_difference 'Watcher.count', -1 do
  611. delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
  612. assert_response :ok
  613. assert_equal '', response.body
  614. end
  615. assert_equal false, Issue.find(1).watched_by?(User.find(3))
  616. end
  617. def test_create_issue_with_uploaded_file
  618. token = xml_upload('test_create_with_upload', credentials('jsmith'))
  619. attachment = Attachment.find_by_token(token)
  620. # create the issue with the upload's token
  621. assert_difference 'Issue.count' do
  622. post '/issues.xml',
  623. {:issue => {:project_id => 1, :subject => 'Uploaded file',
  624. :uploads => [{:token => token, :filename => 'test.txt',
  625. :content_type => 'text/plain'}]}},
  626. credentials('jsmith')
  627. assert_response :created
  628. end
  629. issue = Issue.order('id DESC').first
  630. assert_equal 1, issue.attachments.count
  631. assert_equal attachment, issue.attachments.first
  632. attachment.reload
  633. assert_equal 'test.txt', attachment.filename
  634. assert_equal 'text/plain', attachment.content_type
  635. assert_equal 'test_create_with_upload'.size, attachment.filesize
  636. assert_equal 2, attachment.author_id
  637. # get the issue with its attachments
  638. get "/issues/#{issue.id}.xml", :include => 'attachments'
  639. assert_response :success
  640. xml = Hash.from_xml(response.body)
  641. attachments = xml['issue']['attachments']
  642. assert_kind_of Array, attachments
  643. assert_equal 1, attachments.size
  644. url = attachments.first['content_url']
  645. assert_not_nil url
  646. # download the attachment
  647. get url
  648. assert_response :success
  649. assert_equal 'test_create_with_upload', response.body
  650. end
  651. def test_create_issue_with_multiple_uploaded_files_as_xml
  652. token1 = xml_upload('File content 1', credentials('jsmith'))
  653. token2 = xml_upload('File content 2', credentials('jsmith'))
  654. payload = <<-XML
  655. <?xml version="1.0" encoding="UTF-8" ?>
  656. <issue>
  657. <project_id>1</project_id>
  658. <tracker_id>1</tracker_id>
  659. <subject>Issue with multiple attachments</subject>
  660. <uploads type="array">
  661. <upload>
  662. <token>#{token1}</token>
  663. <filename>test1.txt</filename>
  664. </upload>
  665. <upload>
  666. <token>#{token2}</token>
  667. <filename>test1.txt</filename>
  668. </upload>
  669. </uploads>
  670. </issue>
  671. XML
  672. assert_difference 'Issue.count' do
  673. post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
  674. assert_response :created
  675. end
  676. issue = Issue.order('id DESC').first
  677. assert_equal 2, issue.attachments.count
  678. end
  679. def test_create_issue_with_multiple_uploaded_files_as_json
  680. token1 = json_upload('File content 1', credentials('jsmith'))
  681. token2 = json_upload('File content 2', credentials('jsmith'))
  682. payload = <<-JSON
  683. {
  684. "issue": {
  685. "project_id": "1",
  686. "tracker_id": "1",
  687. "subject": "Issue with multiple attachments",
  688. "uploads": [
  689. {"token": "#{token1}", "filename": "test1.txt"},
  690. {"token": "#{token2}", "filename": "test2.txt"}
  691. ]
  692. }
  693. }
  694. JSON
  695. assert_difference 'Issue.count' do
  696. post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
  697. assert_response :created
  698. end
  699. issue = Issue.order('id DESC').first
  700. assert_equal 2, issue.attachments.count
  701. end
  702. def test_update_issue_with_uploaded_file
  703. token = xml_upload('test_upload_with_upload', credentials('jsmith'))
  704. attachment = Attachment.find_by_token(token)
  705. # update the issue with the upload's token
  706. assert_difference 'Journal.count' do
  707. put '/issues/1.xml',
  708. {:issue => {:notes => 'Attachment added',
  709. :uploads => [{:token => token, :filename => 'test.txt',
  710. :content_type => 'text/plain'}]}},
  711. credentials('jsmith')
  712. assert_response :ok
  713. assert_equal '', @response.body
  714. end
  715. issue = Issue.find(1)
  716. assert_include attachment, issue.attachments
  717. end
  718. end