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