annotate test/integration/api_test/time_entries_test.rb @ 1480:75fd8eace091 issue_556

Close obsolete branch issue_556
author Chris Cannam
date Sat, 13 Jul 2013 15:26:30 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@119 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@119 20 class ApiTest::TimeEntriesTest < ActionController::IntegrationTest
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 :workflows,
Chris@909 29 :time_entries
Chris@909 30
Chris@119 31 def setup
Chris@119 32 Setting.rest_api_enabled = '1'
Chris@119 33 end
Chris@909 34
Chris@119 35 context "GET /time_entries.xml" do
Chris@119 36 should "return time entries" do
Chris@1115 37 get '/time_entries.xml', {}, credentials('jsmith')
Chris@119 38 assert_response :success
Chris@119 39 assert_equal 'application/xml', @response.content_type
Chris@119 40 assert_tag :tag => 'time_entries',
Chris@119 41 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
Chris@119 42 end
Chris@909 43
Chris@441 44 context "with limit" do
Chris@441 45 should "return limited results" do
Chris@1115 46 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
Chris@441 47 assert_response :success
Chris@441 48 assert_equal 'application/xml', @response.content_type
Chris@441 49 assert_tag :tag => 'time_entries',
Chris@441 50 :children => {:count => 2}
Chris@441 51 end
Chris@441 52 end
Chris@119 53 end
Chris@909 54
Chris@119 55 context "GET /time_entries/2.xml" do
Chris@119 56 should "return requested time entry" do
Chris@1115 57 get '/time_entries/2.xml', {}, credentials('jsmith')
Chris@119 58 assert_response :success
Chris@119 59 assert_equal 'application/xml', @response.content_type
Chris@119 60 assert_tag :tag => 'time_entry',
Chris@119 61 :child => {:tag => 'id', :content => '2'}
Chris@119 62 end
Chris@119 63 end
Chris@909 64
Chris@119 65 context "POST /time_entries.xml" do
Chris@119 66 context "with issue_id" do
Chris@119 67 should "return create time entry" do
Chris@119 68 assert_difference 'TimeEntry.count' do
Chris@1115 69 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
Chris@119 70 end
Chris@119 71 assert_response :created
Chris@119 72 assert_equal 'application/xml', @response.content_type
Chris@909 73
Chris@119 74 entry = TimeEntry.first(:order => 'id DESC')
Chris@119 75 assert_equal 'jsmith', entry.user.login
Chris@119 76 assert_equal Issue.find(1), entry.issue
Chris@119 77 assert_equal Project.find(1), entry.project
Chris@119 78 assert_equal Date.parse('2010-12-02'), entry.spent_on
Chris@119 79 assert_equal 3.5, entry.hours
Chris@119 80 assert_equal TimeEntryActivity.find(11), entry.activity
Chris@119 81 end
Chris@1115 82
Chris@1115 83 should "accept custom fields" do
Chris@1115 84 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
Chris@1115 85
Chris@1115 86 assert_difference 'TimeEntry.count' do
Chris@1115 87 post '/time_entries.xml', {:time_entry => {
Chris@1115 88 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
Chris@1115 89 }}, credentials('jsmith')
Chris@1115 90 end
Chris@1115 91 assert_response :created
Chris@1115 92 assert_equal 'application/xml', @response.content_type
Chris@1115 93
Chris@1115 94 entry = TimeEntry.first(:order => 'id DESC')
Chris@1115 95 assert_equal 'accepted', entry.custom_field_value(field)
Chris@1115 96 end
Chris@119 97 end
Chris@909 98
Chris@119 99 context "with project_id" do
Chris@119 100 should "return create time entry" do
Chris@119 101 assert_difference 'TimeEntry.count' do
Chris@1115 102 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
Chris@119 103 end
Chris@119 104 assert_response :created
Chris@119 105 assert_equal 'application/xml', @response.content_type
Chris@909 106
Chris@119 107 entry = TimeEntry.first(:order => 'id DESC')
Chris@119 108 assert_equal 'jsmith', entry.user.login
Chris@119 109 assert_nil entry.issue
Chris@119 110 assert_equal Project.find(1), entry.project
Chris@119 111 assert_equal Date.parse('2010-12-02'), entry.spent_on
Chris@119 112 assert_equal 3.5, entry.hours
Chris@119 113 assert_equal TimeEntryActivity.find(11), entry.activity
Chris@119 114 end
Chris@119 115 end
Chris@909 116
Chris@119 117 context "with invalid parameters" do
Chris@119 118 should "return errors" do
Chris@119 119 assert_no_difference 'TimeEntry.count' do
Chris@1115 120 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
Chris@119 121 end
Chris@119 122 assert_response :unprocessable_entity
Chris@119 123 assert_equal 'application/xml', @response.content_type
Chris@119 124
Chris@119 125 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
Chris@119 126 end
Chris@119 127 end
Chris@119 128 end
Chris@909 129
Chris@119 130 context "PUT /time_entries/2.xml" do
Chris@119 131 context "with valid parameters" do
Chris@119 132 should "update time entry" do
Chris@119 133 assert_no_difference 'TimeEntry.count' do
Chris@1115 134 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
Chris@119 135 end
Chris@119 136 assert_response :ok
Chris@1115 137 assert_equal '', @response.body
Chris@119 138 assert_equal 'API Update', TimeEntry.find(2).comments
Chris@119 139 end
Chris@119 140 end
Chris@119 141
Chris@119 142 context "with invalid parameters" do
Chris@119 143 should "return errors" do
Chris@119 144 assert_no_difference 'TimeEntry.count' do
Chris@1115 145 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
Chris@119 146 end
Chris@119 147 assert_response :unprocessable_entity
Chris@119 148 assert_equal 'application/xml', @response.content_type
Chris@119 149
Chris@119 150 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
Chris@119 151 end
Chris@119 152 end
Chris@119 153 end
Chris@909 154
Chris@119 155 context "DELETE /time_entries/2.xml" do
Chris@119 156 should "destroy time entry" do
Chris@119 157 assert_difference 'TimeEntry.count', -1 do
Chris@1115 158 delete '/time_entries/2.xml', {}, credentials('jsmith')
Chris@119 159 end
Chris@119 160 assert_response :ok
Chris@1115 161 assert_equal '', @response.body
Chris@119 162 assert_nil TimeEntry.find_by_id(2)
Chris@119 163 end
Chris@119 164 end
Chris@119 165 end