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