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.5KB

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