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.

versions_test.rb 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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::VersionsTest < Redmine::ApiTest::Base
  19. fixtures :projects, :trackers, :issue_statuses, :issues,
  20. :enumerations, :users, :issue_categories,
  21. :projects_trackers,
  22. :roles,
  23. :member_roles,
  24. :members,
  25. :enabled_modules,
  26. :versions
  27. def setup
  28. Setting.rest_api_enabled = '1'
  29. end
  30. context "/projects/:project_id/versions" do
  31. context "GET" do
  32. should "return project versions" do
  33. get '/projects/1/versions.xml'
  34. assert_response :success
  35. assert_equal 'application/xml', @response.content_type
  36. assert_tag :tag => 'versions',
  37. :attributes => {:type => 'array'},
  38. :child => {
  39. :tag => 'version',
  40. :child => {
  41. :tag => 'id',
  42. :content => '2',
  43. :sibling => {
  44. :tag => 'name',
  45. :content => '1.0'
  46. }
  47. }
  48. }
  49. end
  50. end
  51. context "POST" do
  52. should "create the version" do
  53. assert_difference 'Version.count' do
  54. post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith')
  55. end
  56. version = Version.first(:order => 'id DESC')
  57. assert_equal 'API test', version.name
  58. assert_response :created
  59. assert_equal 'application/xml', @response.content_type
  60. assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
  61. end
  62. should "create the version with due date" do
  63. assert_difference 'Version.count' do
  64. post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith')
  65. end
  66. version = Version.first(:order => 'id DESC')
  67. assert_equal 'API test', version.name
  68. assert_equal Date.parse('2012-01-24'), version.due_date
  69. assert_response :created
  70. assert_equal 'application/xml', @response.content_type
  71. assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
  72. end
  73. should "create the version with custom fields" do
  74. field = VersionCustomField.generate!
  75. assert_difference 'Version.count' do
  76. post '/projects/1/versions.xml', {
  77. :version => {
  78. :name => 'API test',
  79. :custom_fields => [
  80. {'id' => field.id.to_s, 'value' => 'Some value'}
  81. ]
  82. }
  83. }, credentials('jsmith')
  84. end
  85. version = Version.first(:order => 'id DESC')
  86. assert_equal 'API test', version.name
  87. assert_equal 'Some value', version.custom_field_value(field)
  88. assert_response :created
  89. assert_equal 'application/xml', @response.content_type
  90. assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value'
  91. end
  92. context "with failure" do
  93. should "return the errors" do
  94. assert_no_difference('Version.count') do
  95. post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith')
  96. end
  97. assert_response :unprocessable_entity
  98. assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
  99. end
  100. end
  101. end
  102. end
  103. context "/versions/:id" do
  104. context "GET" do
  105. should "return the version" do
  106. get '/versions/2.xml'
  107. assert_response :success
  108. assert_equal 'application/xml', @response.content_type
  109. assert_select 'version' do
  110. assert_select 'id', :text => '2'
  111. assert_select 'name', :text => '1.0'
  112. assert_select 'sharing', :text => 'none'
  113. end
  114. end
  115. end
  116. context "PUT" do
  117. should "update the version" do
  118. put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith')
  119. assert_response :ok
  120. assert_equal '', @response.body
  121. assert_equal 'API update', Version.find(2).name
  122. end
  123. end
  124. context "DELETE" do
  125. should "destroy the version" do
  126. assert_difference 'Version.count', -1 do
  127. delete '/versions/3.xml', {}, credentials('jsmith')
  128. end
  129. assert_response :ok
  130. assert_equal '', @response.body
  131. assert_nil Version.find_by_id(3)
  132. end
  133. end
  134. end
  135. end