comparison test/integration/api_test/time_entries_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
15 # along with this program; if not, write to the Free Software 15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require File.expand_path('../../../test_helper', __FILE__) 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 class ApiTest::TimeEntriesTest < ActionController::IntegrationTest 20 class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
21 fixtures :projects, :trackers, :issue_statuses, :issues, 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 :enumerations, :users, :issue_categories, 22 :enumerations, :users, :issue_categories,
23 :projects_trackers, 23 :projects_trackers,
24 :roles, 24 :roles,
25 :member_roles, 25 :member_roles,
26 :members, 26 :members,
27 :enabled_modules, 27 :enabled_modules,
28 :workflows,
29 :time_entries 28 :time_entries
30 29
31 def setup 30 def setup
32 Setting.rest_api_enabled = '1' 31 Setting.rest_api_enabled = '1'
33 end 32 end
34 33
35 context "GET /time_entries.xml" do 34 test "GET /time_entries.xml should return time entries" do
36 should "return time entries" do 35 get '/time_entries.xml', {}, credentials('jsmith')
37 get '/time_entries.xml', {}, credentials('jsmith') 36 assert_response :success
38 assert_response :success 37 assert_equal 'application/xml', @response.content_type
39 assert_equal 'application/xml', @response.content_type 38 assert_tag :tag => 'time_entries',
40 assert_tag :tag => 'time_entries', 39 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
41 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
42 end
43
44 context "with limit" do
45 should "return limited results" do
46 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
47 assert_response :success
48 assert_equal 'application/xml', @response.content_type
49 assert_tag :tag => 'time_entries',
50 :children => {:count => 2}
51 end
52 end
53 end 40 end
54 41
55 context "GET /time_entries/2.xml" do 42 test "GET /time_entries.xml with limit should return limited results" do
56 should "return requested time entry" do 43 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
57 get '/time_entries/2.xml', {}, credentials('jsmith') 44 assert_response :success
58 assert_response :success 45 assert_equal 'application/xml', @response.content_type
59 assert_equal 'application/xml', @response.content_type 46 assert_tag :tag => 'time_entries',
60 assert_tag :tag => 'time_entry', 47 :children => {:count => 2}
61 :child => {:tag => 'id', :content => '2'}
62 end
63 end 48 end
64 49
65 context "POST /time_entries.xml" do 50 test "GET /time_entries/:id.xml should return the time entry" do
66 context "with issue_id" do 51 get '/time_entries/2.xml', {}, credentials('jsmith')
67 should "return create time entry" do 52 assert_response :success
68 assert_difference 'TimeEntry.count' do 53 assert_equal 'application/xml', @response.content_type
69 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') 54 assert_tag :tag => 'time_entry',
70 end 55 :child => {:tag => 'id', :content => '2'}
71 assert_response :created
72 assert_equal 'application/xml', @response.content_type
73
74 entry = TimeEntry.first(:order => 'id DESC')
75 assert_equal 'jsmith', entry.user.login
76 assert_equal Issue.find(1), entry.issue
77 assert_equal Project.find(1), entry.project
78 assert_equal Date.parse('2010-12-02'), entry.spent_on
79 assert_equal 3.5, entry.hours
80 assert_equal TimeEntryActivity.find(11), entry.activity
81 end
82
83 should "accept custom fields" do
84 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
85
86 assert_difference 'TimeEntry.count' do
87 post '/time_entries.xml', {:time_entry => {
88 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
89 }}, credentials('jsmith')
90 end
91 assert_response :created
92 assert_equal 'application/xml', @response.content_type
93
94 entry = TimeEntry.first(:order => 'id DESC')
95 assert_equal 'accepted', entry.custom_field_value(field)
96 end
97 end
98
99 context "with project_id" do
100 should "return create time entry" do
101 assert_difference 'TimeEntry.count' do
102 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
103 end
104 assert_response :created
105 assert_equal 'application/xml', @response.content_type
106
107 entry = TimeEntry.first(:order => 'id DESC')
108 assert_equal 'jsmith', entry.user.login
109 assert_nil entry.issue
110 assert_equal Project.find(1), entry.project
111 assert_equal Date.parse('2010-12-02'), entry.spent_on
112 assert_equal 3.5, entry.hours
113 assert_equal TimeEntryActivity.find(11), entry.activity
114 end
115 end
116
117 context "with invalid parameters" do
118 should "return errors" do
119 assert_no_difference 'TimeEntry.count' do
120 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
121 end
122 assert_response :unprocessable_entity
123 assert_equal 'application/xml', @response.content_type
124
125 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
126 end
127 end
128 end 56 end
129 57
130 context "PUT /time_entries/2.xml" do 58 test "POST /time_entries.xml with issue_id should create time entry" do
131 context "with valid parameters" do 59 assert_difference 'TimeEntry.count' do
132 should "update time entry" do 60 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
133 assert_no_difference 'TimeEntry.count' do
134 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
135 end
136 assert_response :ok
137 assert_equal '', @response.body
138 assert_equal 'API Update', TimeEntry.find(2).comments
139 end
140 end 61 end
62 assert_response :created
63 assert_equal 'application/xml', @response.content_type
141 64
142 context "with invalid parameters" do 65 entry = TimeEntry.order('id DESC').first
143 should "return errors" do 66 assert_equal 'jsmith', entry.user.login
144 assert_no_difference 'TimeEntry.count' do 67 assert_equal Issue.find(1), entry.issue
145 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith') 68 assert_equal Project.find(1), entry.project
146 end 69 assert_equal Date.parse('2010-12-02'), entry.spent_on
147 assert_response :unprocessable_entity 70 assert_equal 3.5, entry.hours
148 assert_equal 'application/xml', @response.content_type 71 assert_equal TimeEntryActivity.find(11), entry.activity
149
150 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
151 end
152 end
153 end 72 end
154 73
155 context "DELETE /time_entries/2.xml" do 74 test "POST /time_entries.xml with issue_id should accept custom fields" do
156 should "destroy time entry" do 75 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
157 assert_difference 'TimeEntry.count', -1 do 76
158 delete '/time_entries/2.xml', {}, credentials('jsmith') 77 assert_difference 'TimeEntry.count' do
159 end 78 post '/time_entries.xml', {:time_entry => {
160 assert_response :ok 79 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
161 assert_equal '', @response.body 80 }}, credentials('jsmith')
162 assert_nil TimeEntry.find_by_id(2)
163 end 81 end
82 assert_response :created
83 assert_equal 'application/xml', @response.content_type
84
85 entry = TimeEntry.order('id DESC').first
86 assert_equal 'accepted', entry.custom_field_value(field)
87 end
88
89 test "POST /time_entries.xml with project_id should create time entry" do
90 assert_difference 'TimeEntry.count' do
91 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
92 end
93 assert_response :created
94 assert_equal 'application/xml', @response.content_type
95
96 entry = TimeEntry.order('id DESC').first
97 assert_equal 'jsmith', entry.user.login
98 assert_nil entry.issue
99 assert_equal Project.find(1), entry.project
100 assert_equal Date.parse('2010-12-02'), entry.spent_on
101 assert_equal 3.5, entry.hours
102 assert_equal TimeEntryActivity.find(11), entry.activity
103 end
104
105 test "POST /time_entries.xml with invalid parameters should return errors" do
106 assert_no_difference 'TimeEntry.count' do
107 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
108 end
109 assert_response :unprocessable_entity
110 assert_equal 'application/xml', @response.content_type
111
112 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
113 end
114
115 test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
116 assert_no_difference 'TimeEntry.count' do
117 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
118 end
119 assert_response :ok
120 assert_equal '', @response.body
121 assert_equal 'API Update', TimeEntry.find(2).comments
122 end
123
124 test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
125 assert_no_difference 'TimeEntry.count' do
126 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
127 end
128 assert_response :unprocessable_entity
129 assert_equal 'application/xml', @response.content_type
130
131 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
132 end
133
134 test "DELETE /time_entries/:id.xml should destroy time entry" do
135 assert_difference 'TimeEntry.count', -1 do
136 delete '/time_entries/2.xml', {}, credentials('jsmith')
137 end
138 assert_response :ok
139 assert_equal '', @response.body
140 assert_nil TimeEntry.find_by_id(2)
164 end 141 end
165 end 142 end