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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2010 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.dirname(__FILE__)}/../../test_helper"
  18. class ApiTest::IssuesTest < ActionController::IntegrationTest
  19. fixtures :projects,
  20. :users,
  21. :roles,
  22. :members,
  23. :member_roles,
  24. :issues,
  25. :issue_statuses,
  26. :versions,
  27. :trackers,
  28. :projects_trackers,
  29. :issue_categories,
  30. :enabled_modules,
  31. :enumerations,
  32. :attachments,
  33. :workflows,
  34. :custom_fields,
  35. :custom_values,
  36. :custom_fields_projects,
  37. :custom_fields_trackers,
  38. :time_entries,
  39. :journals,
  40. :journal_details,
  41. :queries
  42. def setup
  43. Setting.rest_api_enabled = '1'
  44. end
  45. context "/index.xml" do
  46. setup do
  47. get '/issues.xml'
  48. end
  49. should_respond_with :success
  50. should_respond_with_content_type 'application/xml'
  51. end
  52. context "/index.json" do
  53. setup do
  54. get '/issues.json'
  55. end
  56. should_respond_with :success
  57. should_respond_with_content_type 'application/json'
  58. should 'return a valid JSON string' do
  59. assert ActiveSupport::JSON.decode(response.body)
  60. end
  61. end
  62. context "/index.xml with filter" do
  63. setup do
  64. get '/issues.xml?status_id=5'
  65. end
  66. should_respond_with :success
  67. should_respond_with_content_type 'application/xml'
  68. should "show only issues with the status_id" do
  69. assert_tag :tag => 'issues',
  70. :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}),
  71. :only => { :tag => 'issue' } }
  72. end
  73. end
  74. context "/index.json with filter" do
  75. setup do
  76. get '/issues.json?status_id=5'
  77. end
  78. should_respond_with :success
  79. should_respond_with_content_type 'application/json'
  80. should 'return a valid JSON string' do
  81. assert ActiveSupport::JSON.decode(response.body)
  82. end
  83. should "show only issues with the status_id" do
  84. json = ActiveSupport::JSON.decode(response.body)
  85. status_ids_used = json.collect {|j| j['status_id'] }
  86. assert_equal 3, status_ids_used.length
  87. assert status_ids_used.all? {|id| id == 5 }
  88. end
  89. end
  90. context "/issues/1.xml" do
  91. setup do
  92. get '/issues/1.xml'
  93. end
  94. should_respond_with :success
  95. should_respond_with_content_type 'application/xml'
  96. end
  97. context "/issues/1.json" do
  98. setup do
  99. get '/issues/1.json'
  100. end
  101. should_respond_with :success
  102. should_respond_with_content_type 'application/json'
  103. should 'return a valid JSON string' do
  104. assert ActiveSupport::JSON.decode(response.body)
  105. end
  106. end
  107. context "POST /issues.xml" do
  108. setup do
  109. @issue_count = Issue.count
  110. @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
  111. post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
  112. end
  113. should_respond_with :created
  114. should_respond_with_content_type 'application/xml'
  115. should "create an issue with the attributes" do
  116. assert_equal Issue.count, @issue_count + 1
  117. issue = Issue.first(:order => 'id DESC')
  118. @attributes.each do |attribute, value|
  119. assert_equal value, issue.send(attribute)
  120. end
  121. end
  122. end
  123. context "POST /issues.xml with failure" do
  124. setup do
  125. @attributes = {:project_id => 1}
  126. post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
  127. end
  128. should_respond_with :unprocessable_entity
  129. should_respond_with_content_type 'application/xml'
  130. should "have an errors tag" do
  131. assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
  132. end
  133. end
  134. context "POST /issues.json" do
  135. setup do
  136. @issue_count = Issue.count
  137. @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
  138. post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
  139. end
  140. should_respond_with :created
  141. should_respond_with_content_type 'application/json'
  142. should "create an issue with the attributes" do
  143. assert_equal Issue.count, @issue_count + 1
  144. issue = Issue.first(:order => 'id DESC')
  145. @attributes.each do |attribute, value|
  146. assert_equal value, issue.send(attribute)
  147. end
  148. end
  149. end
  150. context "POST /issues.json with failure" do
  151. setup do
  152. @attributes = {:project_id => 1}
  153. post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
  154. end
  155. should_respond_with :unprocessable_entity
  156. should_respond_with_content_type 'application/json'
  157. should "have an errors element" do
  158. json = ActiveSupport::JSON.decode(response.body)
  159. assert_equal "can't be blank", json.first['subject']
  160. end
  161. end
  162. context "PUT /issues/1.xml" do
  163. setup do
  164. @issue_count = Issue.count
  165. @journal_count = Journal.count
  166. @attributes = {:subject => 'API update', :notes => 'A new note'}
  167. put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
  168. end
  169. should_respond_with :ok
  170. should_respond_with_content_type 'application/xml'
  171. should "not create a new issue" do
  172. assert_equal Issue.count, @issue_count
  173. end
  174. should "create a new journal" do
  175. assert_equal Journal.count, @journal_count + 1
  176. end
  177. should "add the note to the journal" do
  178. journal = Journal.last
  179. assert_equal "A new note", journal.notes
  180. end
  181. should "update the issue" do
  182. issue = Issue.find(1)
  183. @attributes.each do |attribute, value|
  184. assert_equal value, issue.send(attribute) unless attribute == :notes
  185. end
  186. end
  187. end
  188. context "PUT /issues/1.xml with failed update" do
  189. setup do
  190. @attributes = {:subject => ''}
  191. @issue_count = Issue.count
  192. @journal_count = Journal.count
  193. put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
  194. end
  195. should_respond_with :unprocessable_entity
  196. should_respond_with_content_type 'application/xml'
  197. should "not create a new issue" do
  198. assert_equal Issue.count, @issue_count
  199. end
  200. should "not create a new journal" do
  201. assert_equal Journal.count, @journal_count
  202. end
  203. should "have an errors tag" do
  204. assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
  205. end
  206. end
  207. context "PUT /issues/1.json" do
  208. setup do
  209. @issue_count = Issue.count
  210. @journal_count = Journal.count
  211. @attributes = {:subject => 'API update', :notes => 'A new note'}
  212. put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
  213. end
  214. should_respond_with :ok
  215. should_respond_with_content_type 'application/json'
  216. should "not create a new issue" do
  217. assert_equal Issue.count, @issue_count
  218. end
  219. should "create a new journal" do
  220. assert_equal Journal.count, @journal_count + 1
  221. end
  222. should "add the note to the journal" do
  223. journal = Journal.last
  224. assert_equal "A new note", journal.notes
  225. end
  226. should "update the issue" do
  227. issue = Issue.find(1)
  228. @attributes.each do |attribute, value|
  229. assert_equal value, issue.send(attribute) unless attribute == :notes
  230. end
  231. end
  232. end
  233. context "PUT /issues/1.json with failed update" do
  234. setup do
  235. @attributes = {:subject => ''}
  236. @issue_count = Issue.count
  237. @journal_count = Journal.count
  238. put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
  239. end
  240. should_respond_with :unprocessable_entity
  241. should_respond_with_content_type 'application/json'
  242. should "not create a new issue" do
  243. assert_equal Issue.count, @issue_count
  244. end
  245. should "not create a new journal" do
  246. assert_equal Journal.count, @journal_count
  247. end
  248. should "have an errors attribute" do
  249. json = ActiveSupport::JSON.decode(response.body)
  250. assert_equal "can't be blank", json.first['subject']
  251. end
  252. end
  253. context "DELETE /issues/1.xml" do
  254. setup do
  255. @issue_count = Issue.count
  256. delete '/issues/1.xml', {}, :authorization => credentials('jsmith')
  257. end
  258. should_respond_with :ok
  259. should_respond_with_content_type 'application/xml'
  260. should "delete the issue" do
  261. assert_equal Issue.count, @issue_count -1
  262. assert_nil Issue.find_by_id(1)
  263. end
  264. end
  265. context "DELETE /issues/1.json" do
  266. setup do
  267. @issue_count = Issue.count
  268. delete '/issues/1.json', {}, :authorization => credentials('jsmith')
  269. end
  270. should_respond_with :ok
  271. should_respond_with_content_type 'application/json'
  272. should "delete the issue" do
  273. assert_equal Issue.count, @issue_count -1
  274. assert_nil Issue.find_by_id(1)
  275. end
  276. end
  277. def credentials(user, password=nil)
  278. ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
  279. end
  280. end