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.

time_entries_test.rb 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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::TimeEntriesTest < 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. :time_entries
  27. def setup
  28. Setting.rest_api_enabled = '1'
  29. end
  30. context "GET /time_entries.xml" do
  31. should "return time entries" do
  32. get '/time_entries.xml', {}, credentials('jsmith')
  33. assert_response :success
  34. assert_equal 'application/xml', @response.content_type
  35. assert_tag :tag => 'time_entries',
  36. :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
  37. end
  38. context "with limit" do
  39. should "return limited results" do
  40. get '/time_entries.xml?limit=2', {}, credentials('jsmith')
  41. assert_response :success
  42. assert_equal 'application/xml', @response.content_type
  43. assert_tag :tag => 'time_entries',
  44. :children => {:count => 2}
  45. end
  46. end
  47. end
  48. context "GET /time_entries/2.xml" do
  49. should "return requested time entry" do
  50. get '/time_entries/2.xml', {}, credentials('jsmith')
  51. assert_response :success
  52. assert_equal 'application/xml', @response.content_type
  53. assert_tag :tag => 'time_entry',
  54. :child => {:tag => 'id', :content => '2'}
  55. end
  56. end
  57. context "POST /time_entries.xml" do
  58. context "with issue_id" do
  59. should "return create time entry" do
  60. assert_difference 'TimeEntry.count' do
  61. post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
  62. end
  63. assert_response :created
  64. assert_equal 'application/xml', @response.content_type
  65. entry = TimeEntry.first(:order => 'id DESC')
  66. assert_equal 'jsmith', entry.user.login
  67. assert_equal Issue.find(1), entry.issue
  68. assert_equal Project.find(1), entry.project
  69. assert_equal Date.parse('2010-12-02'), entry.spent_on
  70. assert_equal 3.5, entry.hours
  71. assert_equal TimeEntryActivity.find(11), entry.activity
  72. end
  73. should "accept custom fields" do
  74. field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
  75. assert_difference 'TimeEntry.count' do
  76. post '/time_entries.xml', {:time_entry => {
  77. :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
  78. }}, credentials('jsmith')
  79. end
  80. assert_response :created
  81. assert_equal 'application/xml', @response.content_type
  82. entry = TimeEntry.first(:order => 'id DESC')
  83. assert_equal 'accepted', entry.custom_field_value(field)
  84. end
  85. end
  86. context "with project_id" do
  87. should "return create time entry" do
  88. assert_difference 'TimeEntry.count' do
  89. post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
  90. end
  91. assert_response :created
  92. assert_equal 'application/xml', @response.content_type
  93. entry = TimeEntry.first(:order => 'id DESC')
  94. assert_equal 'jsmith', entry.user.login
  95. assert_nil entry.issue
  96. assert_equal Project.find(1), entry.project
  97. assert_equal Date.parse('2010-12-02'), entry.spent_on
  98. assert_equal 3.5, entry.hours
  99. assert_equal TimeEntryActivity.find(11), entry.activity
  100. end
  101. end
  102. context "with invalid parameters" do
  103. should "return errors" do
  104. assert_no_difference 'TimeEntry.count' do
  105. post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
  106. end
  107. assert_response :unprocessable_entity
  108. assert_equal 'application/xml', @response.content_type
  109. assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
  110. end
  111. end
  112. end
  113. context "PUT /time_entries/2.xml" do
  114. context "with valid parameters" do
  115. should "update time entry" do
  116. assert_no_difference 'TimeEntry.count' do
  117. put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
  118. end
  119. assert_response :ok
  120. assert_equal '', @response.body
  121. assert_equal 'API Update', TimeEntry.find(2).comments
  122. end
  123. end
  124. context "with invalid parameters" do
  125. should "return errors" do
  126. assert_no_difference 'TimeEntry.count' do
  127. put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
  128. end
  129. assert_response :unprocessable_entity
  130. assert_equal 'application/xml', @response.content_type
  131. assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
  132. end
  133. end
  134. end
  135. context "DELETE /time_entries/2.xml" do
  136. should "destroy time entry" do
  137. assert_difference 'TimeEntry.count', -1 do
  138. delete '/time_entries/2.xml', {}, credentials('jsmith')
  139. end
  140. assert_response :ok
  141. assert_equal '', @response.body
  142. assert_nil TimeEntry.find_by_id(2)
  143. end
  144. end
  145. end