##// END OF EJS Templates
Rails4: replace deprecated Relation#first with finder options at ApiTest::TimeEntriesTest...
Toshi MARUYAMA -
r12331:7f93cc74b4be
parent child
Show More
@@ -1,142 +1,142
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
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 Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
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 :time_entries
28 :time_entries
29
29
30 def setup
30 def setup
31 Setting.rest_api_enabled = '1'
31 Setting.rest_api_enabled = '1'
32 end
32 end
33
33
34 test "GET /time_entries.xml should return time entries" do
34 test "GET /time_entries.xml should return time entries" do
35 get '/time_entries.xml', {}, credentials('jsmith')
35 get '/time_entries.xml', {}, credentials('jsmith')
36 assert_response :success
36 assert_response :success
37 assert_equal 'application/xml', @response.content_type
37 assert_equal 'application/xml', @response.content_type
38 assert_tag :tag => 'time_entries',
38 assert_tag :tag => 'time_entries',
39 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
39 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
40 end
40 end
41
41
42 test "GET /time_entries.xml with limit should return limited results" do
42 test "GET /time_entries.xml with limit should return limited results" do
43 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
43 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
44 assert_response :success
44 assert_response :success
45 assert_equal 'application/xml', @response.content_type
45 assert_equal 'application/xml', @response.content_type
46 assert_tag :tag => 'time_entries',
46 assert_tag :tag => 'time_entries',
47 :children => {:count => 2}
47 :children => {:count => 2}
48 end
48 end
49
49
50 test "GET /time_entries/:id.xml should return the time entry" do
50 test "GET /time_entries/:id.xml should return the time entry" do
51 get '/time_entries/2.xml', {}, credentials('jsmith')
51 get '/time_entries/2.xml', {}, credentials('jsmith')
52 assert_response :success
52 assert_response :success
53 assert_equal 'application/xml', @response.content_type
53 assert_equal 'application/xml', @response.content_type
54 assert_tag :tag => 'time_entry',
54 assert_tag :tag => 'time_entry',
55 :child => {:tag => 'id', :content => '2'}
55 :child => {:tag => 'id', :content => '2'}
56 end
56 end
57
57
58 test "POST /time_entries.xml with issue_id should create time entry" do
58 test "POST /time_entries.xml with issue_id should create time entry" do
59 assert_difference 'TimeEntry.count' do
59 assert_difference 'TimeEntry.count' do
60 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
60 post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
61 end
61 end
62 assert_response :created
62 assert_response :created
63 assert_equal 'application/xml', @response.content_type
63 assert_equal 'application/xml', @response.content_type
64
64
65 entry = TimeEntry.first(:order => 'id DESC')
65 entry = TimeEntry.order('id DESC').first
66 assert_equal 'jsmith', entry.user.login
66 assert_equal 'jsmith', entry.user.login
67 assert_equal Issue.find(1), entry.issue
67 assert_equal Issue.find(1), entry.issue
68 assert_equal Project.find(1), entry.project
68 assert_equal Project.find(1), entry.project
69 assert_equal Date.parse('2010-12-02'), entry.spent_on
69 assert_equal Date.parse('2010-12-02'), entry.spent_on
70 assert_equal 3.5, entry.hours
70 assert_equal 3.5, entry.hours
71 assert_equal TimeEntryActivity.find(11), entry.activity
71 assert_equal TimeEntryActivity.find(11), entry.activity
72 end
72 end
73
73
74 test "POST /time_entries.xml with issue_id should accept custom fields" do
74 test "POST /time_entries.xml with issue_id should accept custom fields" do
75 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
75 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
76
76
77 assert_difference 'TimeEntry.count' do
77 assert_difference 'TimeEntry.count' do
78 post '/time_entries.xml', {:time_entry => {
78 post '/time_entries.xml', {:time_entry => {
79 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
79 :issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11', :custom_fields => [{:id => field.id.to_s, :value => 'accepted'}]
80 }}, credentials('jsmith')
80 }}, credentials('jsmith')
81 end
81 end
82 assert_response :created
82 assert_response :created
83 assert_equal 'application/xml', @response.content_type
83 assert_equal 'application/xml', @response.content_type
84
84
85 entry = TimeEntry.first(:order => 'id DESC')
85 entry = TimeEntry.order('id DESC').first
86 assert_equal 'accepted', entry.custom_field_value(field)
86 assert_equal 'accepted', entry.custom_field_value(field)
87 end
87 end
88
88
89 test "POST /time_entries.xml with project_id should create time entry" do
89 test "POST /time_entries.xml with project_id should create time entry" do
90 assert_difference 'TimeEntry.count' 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')
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
92 end
93 assert_response :created
93 assert_response :created
94 assert_equal 'application/xml', @response.content_type
94 assert_equal 'application/xml', @response.content_type
95
95
96 entry = TimeEntry.first(:order => 'id DESC')
96 entry = TimeEntry.order('id DESC').first
97 assert_equal 'jsmith', entry.user.login
97 assert_equal 'jsmith', entry.user.login
98 assert_nil entry.issue
98 assert_nil entry.issue
99 assert_equal Project.find(1), entry.project
99 assert_equal Project.find(1), entry.project
100 assert_equal Date.parse('2010-12-02'), entry.spent_on
100 assert_equal Date.parse('2010-12-02'), entry.spent_on
101 assert_equal 3.5, entry.hours
101 assert_equal 3.5, entry.hours
102 assert_equal TimeEntryActivity.find(11), entry.activity
102 assert_equal TimeEntryActivity.find(11), entry.activity
103 end
103 end
104
104
105 test "POST /time_entries.xml with invalid parameters should return errors" do
105 test "POST /time_entries.xml with invalid parameters should return errors" do
106 assert_no_difference 'TimeEntry.count' 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')
107 post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
108 end
108 end
109 assert_response :unprocessable_entity
109 assert_response :unprocessable_entity
110 assert_equal 'application/xml', @response.content_type
110 assert_equal 'application/xml', @response.content_type
111
111
112 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
112 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
113 end
113 end
114
114
115 test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
115 test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
116 assert_no_difference 'TimeEntry.count' do
116 assert_no_difference 'TimeEntry.count' do
117 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
117 put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
118 end
118 end
119 assert_response :ok
119 assert_response :ok
120 assert_equal '', @response.body
120 assert_equal '', @response.body
121 assert_equal 'API Update', TimeEntry.find(2).comments
121 assert_equal 'API Update', TimeEntry.find(2).comments
122 end
122 end
123
123
124 test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
124 test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
125 assert_no_difference 'TimeEntry.count' do
125 assert_no_difference 'TimeEntry.count' do
126 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
126 put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
127 end
127 end
128 assert_response :unprocessable_entity
128 assert_response :unprocessable_entity
129 assert_equal 'application/xml', @response.content_type
129 assert_equal 'application/xml', @response.content_type
130
130
131 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
131 assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
132 end
132 end
133
133
134 test "DELETE /time_entries/:id.xml should destroy time entry" do
134 test "DELETE /time_entries/:id.xml should destroy time entry" do
135 assert_difference 'TimeEntry.count', -1 do
135 assert_difference 'TimeEntry.count', -1 do
136 delete '/time_entries/2.xml', {}, credentials('jsmith')
136 delete '/time_entries/2.xml', {}, credentials('jsmith')
137 end
137 end
138 assert_response :ok
138 assert_response :ok
139 assert_equal '', @response.body
139 assert_equal '', @response.body
140 assert_nil TimeEntry.find_by_id(2)
140 assert_nil TimeEntry.find_by_id(2)
141 end
141 end
142 end
142 end
General Comments 0
You need to be logged in to leave comments. Login now