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

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