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