Chris@119
|
1 # Redmine - project management software
|
Chris@119
|
2 # Copyright (C) 2006-2010 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@119
|
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@119
|
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@119
|
21 fixtures :all
|
Chris@119
|
22
|
Chris@119
|
23 def setup
|
Chris@119
|
24 Setting.rest_api_enabled = '1'
|
Chris@119
|
25 end
|
Chris@119
|
26
|
Chris@119
|
27 context "GET /time_entries.xml" do
|
Chris@119
|
28 should "return time entries" do
|
Chris@119
|
29 get '/time_entries.xml', {}, :authorization => credentials('jsmith')
|
Chris@119
|
30 assert_response :success
|
Chris@119
|
31 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
32 assert_tag :tag => 'time_entries',
|
Chris@119
|
33 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
|
Chris@119
|
34 end
|
Chris@441
|
35
|
Chris@441
|
36 context "with limit" do
|
Chris@441
|
37 should "return limited results" do
|
Chris@441
|
38 get '/time_entries.xml?limit=2', {}, :authorization => credentials('jsmith')
|
Chris@441
|
39 assert_response :success
|
Chris@441
|
40 assert_equal 'application/xml', @response.content_type
|
Chris@441
|
41 assert_tag :tag => 'time_entries',
|
Chris@441
|
42 :children => {:count => 2}
|
Chris@441
|
43 end
|
Chris@441
|
44 end
|
Chris@119
|
45 end
|
Chris@119
|
46
|
Chris@119
|
47 context "GET /time_entries/2.xml" do
|
Chris@119
|
48 should "return requested time entry" do
|
Chris@119
|
49 get '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
|
Chris@119
|
50 assert_response :success
|
Chris@119
|
51 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
52 assert_tag :tag => 'time_entry',
|
Chris@119
|
53 :child => {:tag => 'id', :content => '2'}
|
Chris@119
|
54 end
|
Chris@119
|
55 end
|
Chris@119
|
56
|
Chris@119
|
57 context "POST /time_entries.xml" do
|
Chris@119
|
58 context "with issue_id" do
|
Chris@119
|
59 should "return create time entry" do
|
Chris@119
|
60 assert_difference 'TimeEntry.count' do
|
Chris@119
|
61 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
|
62 end
|
Chris@119
|
63 assert_response :created
|
Chris@119
|
64 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
65
|
Chris@119
|
66 entry = TimeEntry.first(:order => 'id DESC')
|
Chris@119
|
67 assert_equal 'jsmith', entry.user.login
|
Chris@119
|
68 assert_equal Issue.find(1), entry.issue
|
Chris@119
|
69 assert_equal Project.find(1), entry.project
|
Chris@119
|
70 assert_equal Date.parse('2010-12-02'), entry.spent_on
|
Chris@119
|
71 assert_equal 3.5, entry.hours
|
Chris@119
|
72 assert_equal TimeEntryActivity.find(11), entry.activity
|
Chris@119
|
73 end
|
Chris@119
|
74 end
|
Chris@119
|
75
|
Chris@119
|
76 context "with project_id" do
|
Chris@119
|
77 should "return create time entry" do
|
Chris@119
|
78 assert_difference 'TimeEntry.count' do
|
Chris@119
|
79 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
|
80 end
|
Chris@119
|
81 assert_response :created
|
Chris@119
|
82 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
83
|
Chris@119
|
84 entry = TimeEntry.first(:order => 'id DESC')
|
Chris@119
|
85 assert_equal 'jsmith', entry.user.login
|
Chris@119
|
86 assert_nil entry.issue
|
Chris@119
|
87 assert_equal Project.find(1), entry.project
|
Chris@119
|
88 assert_equal Date.parse('2010-12-02'), entry.spent_on
|
Chris@119
|
89 assert_equal 3.5, entry.hours
|
Chris@119
|
90 assert_equal TimeEntryActivity.find(11), entry.activity
|
Chris@119
|
91 end
|
Chris@119
|
92 end
|
Chris@119
|
93
|
Chris@119
|
94 context "with invalid parameters" do
|
Chris@119
|
95 should "return errors" do
|
Chris@119
|
96 assert_no_difference 'TimeEntry.count' do
|
Chris@119
|
97 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, :authorization => credentials('jsmith')
|
Chris@119
|
98 end
|
Chris@119
|
99 assert_response :unprocessable_entity
|
Chris@119
|
100 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
101
|
Chris@119
|
102 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
|
Chris@119
|
103 end
|
Chris@119
|
104 end
|
Chris@119
|
105 end
|
Chris@119
|
106
|
Chris@119
|
107 context "PUT /time_entries/2.xml" do
|
Chris@119
|
108 context "with valid parameters" do
|
Chris@119
|
109 should "update time entry" do
|
Chris@119
|
110 assert_no_difference 'TimeEntry.count' do
|
Chris@119
|
111 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, :authorization => credentials('jsmith')
|
Chris@119
|
112 end
|
Chris@119
|
113 assert_response :ok
|
Chris@119
|
114 assert_equal 'API Update', TimeEntry.find(2).comments
|
Chris@119
|
115 end
|
Chris@119
|
116 end
|
Chris@119
|
117
|
Chris@119
|
118 context "with invalid parameters" do
|
Chris@119
|
119 should "return errors" do
|
Chris@119
|
120 assert_no_difference 'TimeEntry.count' do
|
Chris@119
|
121 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, :authorization => credentials('jsmith')
|
Chris@119
|
122 end
|
Chris@119
|
123 assert_response :unprocessable_entity
|
Chris@119
|
124 assert_equal 'application/xml', @response.content_type
|
Chris@119
|
125
|
Chris@119
|
126 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
|
Chris@119
|
127 end
|
Chris@119
|
128 end
|
Chris@119
|
129 end
|
Chris@119
|
130
|
Chris@119
|
131 context "DELETE /time_entries/2.xml" do
|
Chris@119
|
132 should "destroy time entry" do
|
Chris@119
|
133 assert_difference 'TimeEntry.count', -1 do
|
Chris@119
|
134 delete '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
|
Chris@119
|
135 end
|
Chris@119
|
136 assert_response :ok
|
Chris@119
|
137 assert_nil TimeEntry.find_by_id(2)
|
Chris@119
|
138 end
|
Chris@119
|
139 end
|
Chris@119
|
140
|
Chris@119
|
141 def credentials(user, password=nil)
|
Chris@119
|
142 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
|
Chris@119
|
143 end
|
Chris@119
|
144 end
|