annotate test/integration/api_test/time_entries_test.rb @ 1613:90bed4e10cc8 deploy

Download file link
author Chris Cannam
date Wed, 30 Aug 2017 17:24:37 +0100
parents dffacf8a6908
children
rev   line source
Chris@119 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@119 3 #
Chris@119 4 # This program is free software; you can redistribute it and/or
Chris@119 5 # modify it under the terms of the GNU General Public License
Chris@119 6 # as published by the Free Software Foundation; either version 2
Chris@119 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@119 9 # This program is distributed in the hope that it will be useful,
Chris@119 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@119 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@119 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@119 14 # You should have received a copy of the GNU General Public License
Chris@119 15 # along with this program; if not, write to the Free Software
Chris@119 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@119 17
Chris@119 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@119 19
Chris@1464 20 class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
Chris@909 21 fixtures :projects, :trackers, :issue_statuses, :issues,
Chris@909 22 :enumerations, :users, :issue_categories,
Chris@909 23 :projects_trackers,
Chris@909 24 :roles,
Chris@909 25 :member_roles,
Chris@909 26 :members,
Chris@909 27 :enabled_modules,
Chris@909 28 :time_entries
Chris@909 29
Chris@119 30 def setup
Chris@119 31 Setting.rest_api_enabled = '1'
Chris@119 32 end
Chris@909 33
Chris@1464 34 test "GET /time_entries.xml should return time entries" do
Chris@1464 35 get '/time_entries.xml', {}, credentials('jsmith')
Chris@1464 36 assert_response :success
Chris@1464 37 assert_equal 'application/xml', @response.content_type
Chris@1464 38 assert_tag :tag => 'time_entries',
Chris@1464 39 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
Chris@119 40 end
Chris@909 41
Chris@1464 42 test "GET /time_entries.xml with limit should return limited results" do
Chris@1464 43 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
Chris@1464 44 assert_response :success
Chris@1464 45 assert_equal 'application/xml', @response.content_type
Chris@1464 46 assert_tag :tag => 'time_entries',
Chris@1464 47 :children => {:count => 2}
Chris@119 48 end
Chris@909 49
Chris@1464 50 test "GET /time_entries/:id.xml should return the time entry" do
Chris@1464 51 get '/time_entries/2.xml', {}, credentials('jsmith')
Chris@1464 52 assert_response :success
Chris@1464 53 assert_equal 'application/xml', @response.content_type
Chris@1464 54 assert_tag :tag => 'time_entry',
Chris@1464 55 :child => {:tag => 'id', :content => '2'}
Chris@119 56 end
Chris@909 57
Chris@1464 58 test "POST /time_entries.xml with issue_id should create time entry" do
Chris@1464 59 assert_difference 'TimeEntry.count' do
Chris@1464 60 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
Chris@119 61 end
Chris@1464 62 assert_response :created
Chris@1464 63 assert_equal 'application/xml', @response.content_type
Chris@119 64
Chris@1517 65 entry = TimeEntry.order('id DESC').first
Chris@1464 66 assert_equal 'jsmith', entry.user.login
Chris@1464 67 assert_equal Issue.find(1), entry.issue
Chris@1464 68 assert_equal Project.find(1), entry.project
Chris@1464 69 assert_equal Date.parse('2010-12-02'), entry.spent_on
Chris@1464 70 assert_equal 3.5, entry.hours
Chris@1464 71 assert_equal TimeEntryActivity.find(11), entry.activity
Chris@119 72 end
Chris@909 73
Chris@1464 74 test "POST /time_entries.xml with issue_id should accept custom fields" do
Chris@1464 75 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
Chris@1464 76
Chris@1464 77 assert_difference 'TimeEntry.count' do
Chris@1464 78 post '/time_entries.xml', {:time_entry => {
Chris@1464 79 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
Chris@1464 80 }}, credentials('jsmith')
Chris@119 81 end
Chris@1464 82 assert_response :created
Chris@1464 83 assert_equal 'application/xml', @response.content_type
Chris@1464 84
Chris@1517 85 entry = TimeEntry.order('id DESC').first
Chris@1464 86 assert_equal 'accepted', entry.custom_field_value(field)
Chris@1464 87 end
Chris@1464 88
Chris@1464 89 test "POST /time_entries.xml with project_id should create time entry" do
Chris@1464 90 assert_difference 'TimeEntry.count' do
Chris@1464 91 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
Chris@1464 92 end
Chris@1464 93 assert_response :created
Chris@1464 94 assert_equal 'application/xml', @response.content_type
Chris@1464 95
Chris@1517 96 entry = TimeEntry.order('id DESC').first
Chris@1464 97 assert_equal 'jsmith', entry.user.login
Chris@1464 98 assert_nil entry.issue
Chris@1464 99 assert_equal Project.find(1), entry.project
Chris@1464 100 assert_equal Date.parse('2010-12-02'), entry.spent_on
Chris@1464 101 assert_equal 3.5, entry.hours
Chris@1464 102 assert_equal TimeEntryActivity.find(11), entry.activity
Chris@1464 103 end
Chris@1464 104
Chris@1464 105 test "POST /time_entries.xml with invalid parameters should return errors" do
Chris@1464 106 assert_no_difference 'TimeEntry.count' do
Chris@1464 107 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
Chris@1464 108 end
Chris@1464 109 assert_response :unprocessable_entity
Chris@1464 110 assert_equal 'application/xml', @response.content_type
Chris@1464 111
Chris@1464 112 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
Chris@1464 113 end
Chris@1464 114
Chris@1464 115 test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
Chris@1464 116 assert_no_difference 'TimeEntry.count' do
Chris@1464 117 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
Chris@1464 118 end
Chris@1464 119 assert_response :ok
Chris@1464 120 assert_equal '', @response.body
Chris@1464 121 assert_equal 'API Update', TimeEntry.find(2).comments
Chris@1464 122 end
Chris@1464 123
Chris@1464 124 test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
Chris@1464 125 assert_no_difference 'TimeEntry.count' do
Chris@1464 126 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
Chris@1464 127 end
Chris@1464 128 assert_response :unprocessable_entity
Chris@1464 129 assert_equal 'application/xml', @response.content_type
Chris@1464 130
Chris@1464 131 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
Chris@1464 132 end
Chris@1464 133
Chris@1464 134 test "DELETE /time_entries/:id.xml should destroy time entry" do
Chris@1464 135 assert_difference 'TimeEntry.count', -1 do
Chris@1464 136 delete '/time_entries/2.xml', {}, credentials('jsmith')
Chris@1464 137 end
Chris@1464 138 assert_response :ok
Chris@1464 139 assert_equal '', @response.body
Chris@1464 140 assert_nil TimeEntry.find_by_id(2)
Chris@119 141 end
Chris@119 142 end