Chris@119
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 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@119
|
37 get '/time_entries.xml', {}, :authorization => 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@441
|
46 get '/time_entries.xml?limit=2', {}, :authorization => 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@119
|
57 get '/time_entries/2.xml', {}, :authorization => 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@119
|
69 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => 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@119
|
82 end
|
Chris@909
|
83
|
Chris@119
|
84 context "with project_id" do
|
Chris@119
|
85 should "return create time entry" do
|
Chris@119
|
86 assert_difference 'TimeEntry.count' do
|
Chris@119
|
87 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
|
Chris@119
|
88 end
|
Chris@119
|
89 assert_response :created
|
Chris@119
|
90 assert_equal 'application/xml', @response.content_type
|
Chris@909
|
91
|
Chris@119
|
92 entry = TimeEntry.first(:order => 'id DESC')
|
Chris@119
|
93 assert_equal 'jsmith', entry.user.login
|
Chris@119
|
94 assert_nil entry.issue
|
Chris@119
|
95 assert_equal Project.find(1), entry.project
|
Chris@119
|
96 assert_equal Date.parse('2010-12-02'), entry.spent_on
|
Chris@119
|
97 assert_equal 3.5, entry.hours
|
Chris@119
|
98 assert_equal TimeEntryActivity.find(11), entry.activity
|
Chris@119
|
99 end
|
Chris@119
|
100 end
|
Chris@909
|
101
|
Chris@119
|
102 context "with invalid parameters" do
|
Chris@119
|
103 should "return errors" do
|
Chris@119
|
104 assert_no_difference 'TimeEntry.count' do
|
Chris@119
|
105 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, :authorization => credentials('jsmith')
|
Chris@119
|
106 end
|
Chris@119
|
107 assert_response :unprocessable_entity
|
Chris@119
|
108 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
109
|
Chris@119
|
110 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
|
Chris@119
|
111 end
|
Chris@119
|
112 end
|
Chris@119
|
113 end
|
Chris@909
|
114
|
Chris@119
|
115 context "PUT /time_entries/2.xml" do
|
Chris@119
|
116 context "with valid parameters" do
|
Chris@119
|
117 should "update time entry" do
|
Chris@119
|
118 assert_no_difference 'TimeEntry.count' do
|
Chris@119
|
119 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, :authorization => credentials('jsmith')
|
Chris@119
|
120 end
|
Chris@119
|
121 assert_response :ok
|
Chris@119
|
122 assert_equal 'API Update', TimeEntry.find(2).comments
|
Chris@119
|
123 end
|
Chris@119
|
124 end
|
Chris@119
|
125
|
Chris@119
|
126 context "with invalid parameters" do
|
Chris@119
|
127 should "return errors" do
|
Chris@119
|
128 assert_no_difference 'TimeEntry.count' do
|
Chris@119
|
129 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, :authorization => credentials('jsmith')
|
Chris@119
|
130 end
|
Chris@119
|
131 assert_response :unprocessable_entity
|
Chris@119
|
132 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
133
|
Chris@119
|
134 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
|
Chris@119
|
135 end
|
Chris@119
|
136 end
|
Chris@119
|
137 end
|
Chris@909
|
138
|
Chris@119
|
139 context "DELETE /time_entries/2.xml" do
|
Chris@119
|
140 should "destroy time entry" do
|
Chris@119
|
141 assert_difference 'TimeEntry.count', -1 do
|
Chris@119
|
142 delete '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
|
Chris@119
|
143 end
|
Chris@119
|
144 assert_response :ok
|
Chris@119
|
145 assert_nil TimeEntry.find_by_id(2)
|
Chris@119
|
146 end
|
Chris@119
|
147 end
|
Chris@909
|
148
|
Chris@119
|
149 def credentials(user, password=nil)
|
Chris@119
|
150 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
|
Chris@119
|
151 end
|
Chris@119
|
152 end
|