##// END OF EJS Templates
Removed some shoulda context....
Jean-Philippe Lang -
r11633:8f7b69f77e7f
parent child
Show More
@@ -1,297 +1,235
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::ProjectsTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 :attachments, :custom_fields, :custom_values, :time_entries, :issue_categories
23 :attachments, :custom_fields, :custom_values, :time_entries, :issue_categories
24
24
25 def setup
25 def setup
26 Setting.rest_api_enabled = '1'
26 Setting.rest_api_enabled = '1'
27 set_tmp_attachments_directory
27 set_tmp_attachments_directory
28 end
28 end
29
29
30 context "GET /projects" do
30 # TODO: A private project is needed because should_allow_api_authentication
31 context ".xml" do
31 # actually tests that authentication is *required*, not just allowed
32 should "return projects" do
32 should_allow_api_authentication(:get, "/projects/2.xml")
33 should_allow_api_authentication(:get, "/projects/2.json")
34 should_allow_api_authentication(:post,
35 '/projects.xml',
36 {:project => {:name => 'API test', :identifier => 'api-test'}},
37 {:success_code => :created})
38 should_allow_api_authentication(:put,
39 '/projects/2.xml',
40 {:project => {:name => 'API update'}},
41 {:success_code => :ok})
42 should_allow_api_authentication(:delete,
43 '/projects/2.xml',
44 {},
45 {:success_code => :ok})
46
47 test "GET /projects.xml should return projects" do
33 get '/projects.xml'
48 get '/projects.xml'
34 assert_response :success
49 assert_response :success
35 assert_equal 'application/xml', @response.content_type
50 assert_equal 'application/xml', @response.content_type
36
51
37 assert_tag :tag => 'projects',
52 assert_tag :tag => 'projects',
38 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
53 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
39 end
54 end
40 end
41
55
42 context ".json" do
56 test "GET /projects.json should return projects" do
43 should "return projects" do
44 get '/projects.json'
57 get '/projects.json'
45 assert_response :success
58 assert_response :success
46 assert_equal 'application/json', @response.content_type
59 assert_equal 'application/json', @response.content_type
47
60
48 json = ActiveSupport::JSON.decode(response.body)
61 json = ActiveSupport::JSON.decode(response.body)
49 assert_kind_of Hash, json
62 assert_kind_of Hash, json
50 assert_kind_of Array, json['projects']
63 assert_kind_of Array, json['projects']
51 assert_kind_of Hash, json['projects'].first
64 assert_kind_of Hash, json['projects'].first
52 assert json['projects'].first.has_key?('id')
65 assert json['projects'].first.has_key?('id')
53 end
66 end
54 end
55 end
56
57 context "GET /projects/:id" do
58 context ".xml" do
59 # TODO: A private project is needed because should_allow_api_authentication
60 # actually tests that authentication is *required*, not just allowed
61 should_allow_api_authentication(:get, "/projects/2.xml")
62
67
63 should "return requested project" do
68 test "GET /projects/:id.xml should return the project" do
64 get '/projects/1.xml'
69 get '/projects/1.xml'
65 assert_response :success
70 assert_response :success
66 assert_equal 'application/xml', @response.content_type
71 assert_equal 'application/xml', @response.content_type
67
72
68 assert_tag :tag => 'project',
73 assert_tag :tag => 'project',
69 :child => {:tag => 'id', :content => '1'}
74 :child => {:tag => 'id', :content => '1'}
70 assert_tag :tag => 'custom_field',
75 assert_tag :tag => 'custom_field',
71 :attributes => {:name => 'Development status'}, :content => 'Stable'
76 :attributes => {:name => 'Development status'}, :content => 'Stable'
72
77
73 assert_no_tag 'trackers'
78 assert_no_tag 'trackers'
74 assert_no_tag 'issue_categories'
79 assert_no_tag 'issue_categories'
75 end
80 end
76
81
77 context "with hidden custom fields" do
82 test "GET /projects/:id.json should return the project" do
78 setup do
83 get '/projects/1.json'
79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
84
85 json = ActiveSupport::JSON.decode(response.body)
86 assert_kind_of Hash, json
87 assert_kind_of Hash, json['project']
88 assert_equal 1, json['project']['id']
80 end
89 end
81
90
82 should "not display hidden custom fields" do
91 test "GET /projects/:id.xml with hidden custom fields should not display hidden custom fields" do
92 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
93
83 get '/projects/1.xml'
94 get '/projects/1.xml'
84 assert_response :success
95 assert_response :success
85 assert_equal 'application/xml', @response.content_type
96 assert_equal 'application/xml', @response.content_type
86
97
87 assert_no_tag 'custom_field',
98 assert_no_tag 'custom_field',
88 :attributes => {:name => 'Development status'}
99 :attributes => {:name => 'Development status'}
89 end
100 end
90 end
91
101
92 should "return categories with include=issue_categories" do
102 test "GET /projects/:id.xml with include=issue_categories should return categories" do
93 get '/projects/1.xml?include=issue_categories'
103 get '/projects/1.xml?include=issue_categories'
94 assert_response :success
104 assert_response :success
95 assert_equal 'application/xml', @response.content_type
105 assert_equal 'application/xml', @response.content_type
96
106
97 assert_tag 'issue_categories',
107 assert_tag 'issue_categories',
98 :attributes => {:type => 'array'},
108 :attributes => {:type => 'array'},
99 :child => {
109 :child => {
100 :tag => 'issue_category',
110 :tag => 'issue_category',
101 :attributes => {
111 :attributes => {
102 :id => '2',
112 :id => '2',
103 :name => 'Recipes'
113 :name => 'Recipes'
104 }
114 }
105 }
115 }
106 end
116 end
107
117
108 should "return trackers with include=trackers" do
118 test "GET /projects/:id.xml with include=trackers should return trackers" do
109 get '/projects/1.xml?include=trackers'
119 get '/projects/1.xml?include=trackers'
110 assert_response :success
120 assert_response :success
111 assert_equal 'application/xml', @response.content_type
121 assert_equal 'application/xml', @response.content_type
112
122
113 assert_tag 'trackers',
123 assert_tag 'trackers',
114 :attributes => {:type => 'array'},
124 :attributes => {:type => 'array'},
115 :child => {
125 :child => {
116 :tag => 'tracker',
126 :tag => 'tracker',
117 :attributes => {
127 :attributes => {
118 :id => '2',
128 :id => '2',
119 :name => 'Feature request'
129 :name => 'Feature request'
120 }
130 }
121 }
131 }
122 end
132 end
123 end
124
125 context ".json" do
126 should_allow_api_authentication(:get, "/projects/2.json")
127
128 should "return requested project" do
129 get '/projects/1.json'
130
133
131 json = ActiveSupport::JSON.decode(response.body)
134 test "POST /projects.xml with valid parameters should create the project" do
132 assert_kind_of Hash, json
133 assert_kind_of Hash, json['project']
134 assert_equal 1, json['project']['id']
135 end
136 end
137 end
138
139 context "POST /projects" do
140 context "with valid parameters" do
141 setup do
142 Setting.default_projects_modules = ['issue_tracking', 'repository']
135 Setting.default_projects_modules = ['issue_tracking', 'repository']
143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
144 end
145
136
146 context ".xml" do
147 should_allow_api_authentication(:post,
148 '/projects.xml',
149 {:project => {:name => 'API test', :identifier => 'api-test'}},
150 {:success_code => :created})
151
152
153 should "create a project with the attributes" do
154 assert_difference('Project.count') do
137 assert_difference('Project.count') do
155 post '/projects.xml', @parameters, credentials('admin')
138 post '/projects.xml',
139 {:project => {:name => 'API test', :identifier => 'api-test'}},
140 credentials('admin')
156 end
141 end
157
142
158 project = Project.first(:order => 'id DESC')
143 project = Project.first(:order => 'id DESC')
159 assert_equal 'API test', project.name
144 assert_equal 'API test', project.name
160 assert_equal 'api-test', project.identifier
145 assert_equal 'api-test', project.identifier
161 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
146 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
162 assert_equal Tracker.all.size, project.trackers.size
147 assert_equal Tracker.all.size, project.trackers.size
163
148
164 assert_response :created
149 assert_response :created
165 assert_equal 'application/xml', @response.content_type
150 assert_equal 'application/xml', @response.content_type
166 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
151 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
167 end
152 end
168
153
169 should "accept enabled_module_names attribute" do
154 test "POST /projects.xml should accept enabled_module_names attribute" do
170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
171
172 assert_difference('Project.count') do
155 assert_difference('Project.count') do
173 post '/projects.xml', @parameters, credentials('admin')
156 post '/projects.xml',
157 {:project => {:name => 'API test', :identifier => 'api-test', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}},
158 credentials('admin')
174 end
159 end
175
160
176 project = Project.first(:order => 'id DESC')
161 project = Project.first(:order => 'id DESC')
177 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
162 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
178 end
163 end
179
164
180 should "accept tracker_ids attribute" do
165 test "POST /projects.xml should accept tracker_ids attribute" do
181 @parameters[:project].merge!({:tracker_ids => [1, 3]})
182
183 assert_difference('Project.count') do
166 assert_difference('Project.count') do
184 post '/projects.xml', @parameters, credentials('admin')
167 post '/projects.xml',
168 {:project => {:name => 'API test', :identifier => 'api-test', :tracker_ids => [1, 3]}},
169 credentials('admin')
185 end
170 end
186
171
187 project = Project.first(:order => 'id DESC')
172 project = Project.first(:order => 'id DESC')
188 assert_equal [1, 3], project.trackers.map(&:id).sort
173 assert_equal [1, 3], project.trackers.map(&:id).sort
189 end
174 end
190 end
191 end
192
193 context "with invalid parameters" do
194 setup do
195 @parameters = {:project => {:name => 'API test'}}
196 end
197
175
198 context ".xml" do
176 test "POST /projects.xml with invalid parameters should return errors" do
199 should "return errors" do
200 assert_no_difference('Project.count') do
177 assert_no_difference('Project.count') do
201 post '/projects.xml', @parameters, credentials('admin')
178 post '/projects.xml', {:project => {:name => 'API test'}}, credentials('admin')
202 end
179 end
203
180
204 assert_response :unprocessable_entity
181 assert_response :unprocessable_entity
205 assert_equal 'application/xml', @response.content_type
182 assert_equal 'application/xml', @response.content_type
206 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
183 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
207 end
184 end
208 end
209 end
210 end
211
212 context "PUT /projects/:id" do
213 context "with valid parameters" do
214 setup do
215 @parameters = {:project => {:name => 'API update'}}
216 end
217
185
218 context ".xml" do
186 test "PUT /projects/:id.xml with valid parameters should update the project" do
219 should_allow_api_authentication(:put,
220 '/projects/2.xml',
221 {:project => {:name => 'API update'}},
222 {:success_code => :ok})
223
224 should "update the project" do
225 assert_no_difference 'Project.count' do
187 assert_no_difference 'Project.count' do
226 put '/projects/2.xml', @parameters, credentials('jsmith')
188 put '/projects/2.xml', {:project => {:name => 'API update'}}, credentials('jsmith')
227 end
189 end
228 assert_response :ok
190 assert_response :ok
229 assert_equal '', @response.body
191 assert_equal '', @response.body
230 assert_equal 'application/xml', @response.content_type
192 assert_equal 'application/xml', @response.content_type
231 project = Project.find(2)
193 project = Project.find(2)
232 assert_equal 'API update', project.name
194 assert_equal 'API update', project.name
233 end
195 end
234
196
235 should "accept enabled_module_names attribute" do
197 test "PUT /projects/:id.xml should accept enabled_module_names attribute" do
236 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
237
238 assert_no_difference 'Project.count' do
198 assert_no_difference 'Project.count' do
239 put '/projects/2.xml', @parameters, credentials('admin')
199 put '/projects/2.xml', {:project => {:name => 'API update', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, credentials('admin')
240 end
200 end
241 assert_response :ok
201 assert_response :ok
242 assert_equal '', @response.body
202 assert_equal '', @response.body
243 project = Project.find(2)
203 project = Project.find(2)
244 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
204 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
245 end
205 end
246
206
247 should "accept tracker_ids attribute" do
207 test "PUT /projects/:id.xml should accept tracker_ids attribute" do
248 @parameters[:project].merge!({:tracker_ids => [1, 3]})
249
250 assert_no_difference 'Project.count' do
208 assert_no_difference 'Project.count' do
251 put '/projects/2.xml', @parameters, credentials('admin')
209 put '/projects/2.xml', {:project => {:name => 'API update', :tracker_ids => [1, 3]}}, credentials('admin')
252 end
210 end
253 assert_response :ok
211 assert_response :ok
254 assert_equal '', @response.body
212 assert_equal '', @response.body
255 project = Project.find(2)
213 project = Project.find(2)
256 assert_equal [1, 3], project.trackers.map(&:id).sort
214 assert_equal [1, 3], project.trackers.map(&:id).sort
257 end
215 end
258 end
259 end
260
261 context "with invalid parameters" do
262 setup do
263 @parameters = {:project => {:name => ''}}
264 end
265
216
266 context ".xml" do
217 test "PUT /projects/:id.xml with invalid parameters should return errors" do
267 should "return errors" do
268 assert_no_difference('Project.count') do
218 assert_no_difference('Project.count') do
269 put '/projects/2.xml', @parameters, credentials('admin')
219 put '/projects/2.xml', {:project => {:name => ''}}, credentials('admin')
270 end
220 end
271
221
272 assert_response :unprocessable_entity
222 assert_response :unprocessable_entity
273 assert_equal 'application/xml', @response.content_type
223 assert_equal 'application/xml', @response.content_type
274 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
224 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
275 end
225 end
276 end
277 end
278 end
279
280 context "DELETE /projects/:id" do
281 context ".xml" do
282 should_allow_api_authentication(:delete,
283 '/projects/2.xml',
284 {},
285 {:success_code => :ok})
286
226
287 should "delete the project" do
227 test "DELETE /projects/:id.xml should delete the project" do
288 assert_difference('Project.count',-1) do
228 assert_difference('Project.count',-1) do
289 delete '/projects/2.xml', {}, credentials('admin')
229 delete '/projects/2.xml', {}, credentials('admin')
290 end
230 end
291 assert_response :ok
231 assert_response :ok
292 assert_equal '', @response.body
232 assert_equal '', @response.body
293 assert_nil Project.find_by_id(2)
233 assert_nil Project.find_by_id(2)
294 end
234 end
295 end
235 end
296 end
297 end
@@ -1,58 +1,53
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::QueriesTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::QueriesTest < 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 :queries
28 :queries
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 context "/queries" do
34 test "GET /queries.xml should return queries" do
35 context "GET" do
36
37 should "return queries" do
38 get '/queries.xml'
35 get '/queries.xml'
39
36
40 assert_response :success
37 assert_response :success
41 assert_equal 'application/xml', @response.content_type
38 assert_equal 'application/xml', @response.content_type
42 assert_tag :tag => 'queries',
39 assert_tag :tag => 'queries',
43 :attributes => {:type => 'array'},
40 :attributes => {:type => 'array'},
44 :child => {
41 :child => {
45 :tag => 'query',
42 :tag => 'query',
46 :child => {
43 :child => {
47 :tag => 'id',
44 :tag => 'id',
48 :content => '4',
45 :content => '4',
49 :sibling => {
46 :sibling => {
50 :tag => 'name',
47 :tag => 'name',
51 :content => 'Public query for all projects'
48 :content => 'Public query for all projects'
52 }
49 }
53 }
50 }
54 }
51 }
55 end
52 end
56 end
53 end
57 end
58 end
@@ -1,90 +1,76
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::RolesTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::RolesTest < Redmine::ApiTest::Base
21 fixtures :roles
21 fixtures :roles
22
22
23 def setup
23 def setup
24 Setting.rest_api_enabled = '1'
24 Setting.rest_api_enabled = '1'
25 end
25 end
26
26
27 context "/roles" do
27 test "GET /roles.xml should return the roles" do
28 context "GET" do
29 context "xml" do
30 should "return the roles" do
31 get '/roles.xml'
28 get '/roles.xml'
32
29
33 assert_response :success
30 assert_response :success
34 assert_equal 'application/xml', @response.content_type
31 assert_equal 'application/xml', @response.content_type
35 assert_equal 3, assigns(:roles).size
32 assert_equal 3, assigns(:roles).size
36
33
37 assert_tag :tag => 'roles',
34 assert_tag :tag => 'roles',
38 :attributes => {:type => 'array'},
35 :attributes => {:type => 'array'},
39 :child => {
36 :child => {
40 :tag => 'role',
37 :tag => 'role',
41 :child => {
38 :child => {
42 :tag => 'id',
39 :tag => 'id',
43 :content => '2',
40 :content => '2',
44 :sibling => {
41 :sibling => {
45 :tag => 'name',
42 :tag => 'name',
46 :content => 'Developer'
43 :content => 'Developer'
47 }
44 }
48 }
45 }
49 }
46 }
50 end
47 end
51 end
52
48
53 context "json" do
49 test "GET /roles.json should return the roles" do
54 should "return the roles" do
55 get '/roles.json'
50 get '/roles.json'
56
51
57 assert_response :success
52 assert_response :success
58 assert_equal 'application/json', @response.content_type
53 assert_equal 'application/json', @response.content_type
59 assert_equal 3, assigns(:roles).size
54 assert_equal 3, assigns(:roles).size
60
55
61 json = ActiveSupport::JSON.decode(response.body)
56 json = ActiveSupport::JSON.decode(response.body)
62 assert_kind_of Hash, json
57 assert_kind_of Hash, json
63 assert_kind_of Array, json['roles']
58 assert_kind_of Array, json['roles']
64 assert_include({'id' => 2, 'name' => 'Developer'}, json['roles'])
59 assert_include({'id' => 2, 'name' => 'Developer'}, json['roles'])
65 end
60 end
66 end
67 end
68 end
69
61
70 context "/roles/:id" do
62 test "GET /roles/:id.xml should return the role" do
71 context "GET" do
72 context "xml" do
73 should "return the role" do
74 get '/roles/1.xml'
63 get '/roles/1.xml'
75
64
76 assert_response :success
65 assert_response :success
77 assert_equal 'application/xml', @response.content_type
66 assert_equal 'application/xml', @response.content_type
78
67
79 assert_select 'role' do
68 assert_select 'role' do
80 assert_select 'name', :text => 'Manager'
69 assert_select 'name', :text => 'Manager'
81 assert_select 'role permissions[type=array]' do
70 assert_select 'role permissions[type=array]' do
82 assert_select 'permission', Role.find(1).permissions.size
71 assert_select 'permission', Role.find(1).permissions.size
83 assert_select 'permission', :text => 'view_issues'
72 assert_select 'permission', :text => 'view_issues'
84 end
73 end
85 end
74 end
86 end
75 end
87 end
76 end
88 end
89 end
90 end
@@ -1,164 +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 context "GET /time_entries.xml" do
34 test "GET /time_entries.xml should return time entries" do
35 should "return time entries" do
36 get '/time_entries.xml', {}, credentials('jsmith')
35 get '/time_entries.xml', {}, credentials('jsmith')
37 assert_response :success
36 assert_response :success
38 assert_equal 'application/xml', @response.content_type
37 assert_equal 'application/xml', @response.content_type
39 assert_tag :tag => 'time_entries',
38 assert_tag :tag => 'time_entries',
40 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
39 :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
41 end
40 end
42
41
43 context "with limit" do
42 test "GET /time_entries.xml with limit should return limited results" do
44 should "return limited results" do
45 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
43 get '/time_entries.xml?limit=2', {}, credentials('jsmith')
46 assert_response :success
44 assert_response :success
47 assert_equal 'application/xml', @response.content_type
45 assert_equal 'application/xml', @response.content_type
48 assert_tag :tag => 'time_entries',
46 assert_tag :tag => 'time_entries',
49 :children => {:count => 2}
47 :children => {:count => 2}
50 end
48 end
51 end
52 end
53
49
54 context "GET /time_entries/2.xml" do
50 test "GET /time_entries/:id.xml should return the time entry" do
55 should "return requested time entry" do
56 get '/time_entries/2.xml', {}, credentials('jsmith')
51 get '/time_entries/2.xml', {}, credentials('jsmith')
57 assert_response :success
52 assert_response :success
58 assert_equal 'application/xml', @response.content_type
53 assert_equal 'application/xml', @response.content_type
59 assert_tag :tag => 'time_entry',
54 assert_tag :tag => 'time_entry',
60 :child => {:tag => 'id', :content => '2'}
55 :child => {:tag => 'id', :content => '2'}
61 end
56 end
62 end
63
57
64 context "POST /time_entries.xml" do
58 test "POST /time_entries.xml with issue_id should create time entry" do
65 context "with issue_id" do
66 should "return create time entry" do
67 assert_difference 'TimeEntry.count' do
59 assert_difference 'TimeEntry.count' do
68 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')
69 end
61 end
70 assert_response :created
62 assert_response :created
71 assert_equal 'application/xml', @response.content_type
63 assert_equal 'application/xml', @response.content_type
72
64
73 entry = TimeEntry.first(:order => 'id DESC')
65 entry = TimeEntry.first(:order => 'id DESC')
74 assert_equal 'jsmith', entry.user.login
66 assert_equal 'jsmith', entry.user.login
75 assert_equal Issue.find(1), entry.issue
67 assert_equal Issue.find(1), entry.issue
76 assert_equal Project.find(1), entry.project
68 assert_equal Project.find(1), entry.project
77 assert_equal Date.parse('2010-12-02'), entry.spent_on
69 assert_equal Date.parse('2010-12-02'), entry.spent_on
78 assert_equal 3.5, entry.hours
70 assert_equal 3.5, entry.hours
79 assert_equal TimeEntryActivity.find(11), entry.activity
71 assert_equal TimeEntryActivity.find(11), entry.activity
80 end
72 end
81
73
82 should "accept custom fields" do
74 test "POST /time_entries.xml with issue_id should accept custom fields" do
83 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
75 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
84
76
85 assert_difference 'TimeEntry.count' do
77 assert_difference 'TimeEntry.count' do
86 post '/time_entries.xml', {:time_entry => {
78 post '/time_entries.xml', {:time_entry => {
87 :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'}]
88 }}, credentials('jsmith')
80 }}, credentials('jsmith')
89 end
81 end
90 assert_response :created
82 assert_response :created
91 assert_equal 'application/xml', @response.content_type
83 assert_equal 'application/xml', @response.content_type
92
84
93 entry = TimeEntry.first(:order => 'id DESC')
85 entry = TimeEntry.first(:order => 'id DESC')
94 assert_equal 'accepted', entry.custom_field_value(field)
86 assert_equal 'accepted', entry.custom_field_value(field)
95 end
87 end
96 end
97
88
98 context "with project_id" do
89 test "POST /time_entries.xml with project_id should create time entry" do
99 should "return create time entry" do
100 assert_difference 'TimeEntry.count' do
90 assert_difference 'TimeEntry.count' do
101 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')
102 end
92 end
103 assert_response :created
93 assert_response :created
104 assert_equal 'application/xml', @response.content_type
94 assert_equal 'application/xml', @response.content_type
105
95
106 entry = TimeEntry.first(:order => 'id DESC')
96 entry = TimeEntry.first(:order => 'id DESC')
107 assert_equal 'jsmith', entry.user.login
97 assert_equal 'jsmith', entry.user.login
108 assert_nil entry.issue
98 assert_nil entry.issue
109 assert_equal Project.find(1), entry.project
99 assert_equal Project.find(1), entry.project
110 assert_equal Date.parse('2010-12-02'), entry.spent_on
100 assert_equal Date.parse('2010-12-02'), entry.spent_on
111 assert_equal 3.5, entry.hours
101 assert_equal 3.5, entry.hours
112 assert_equal TimeEntryActivity.find(11), entry.activity
102 assert_equal TimeEntryActivity.find(11), entry.activity
113 end
103 end
114 end
115
104
116 context "with invalid parameters" do
105 test "POST /time_entries.xml with invalid parameters should return errors" do
117 should "return errors" do
118 assert_no_difference 'TimeEntry.count' do
106 assert_no_difference 'TimeEntry.count' do
119 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')
120 end
108 end
121 assert_response :unprocessable_entity
109 assert_response :unprocessable_entity
122 assert_equal 'application/xml', @response.content_type
110 assert_equal 'application/xml', @response.content_type
123
111
124 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"}
125 end
113 end
126 end
127 end
128
114
129 context "PUT /time_entries/2.xml" do
115 test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
130 context "with valid parameters" do
131 should "update time entry" do
132 assert_no_difference 'TimeEntry.count' do
116 assert_no_difference 'TimeEntry.count' do
133 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')
134 end
118 end
135 assert_response :ok
119 assert_response :ok
136 assert_equal '', @response.body
120 assert_equal '', @response.body
137 assert_equal 'API Update', TimeEntry.find(2).comments
121 assert_equal 'API Update', TimeEntry.find(2).comments
138 end
122 end
139 end
140
123
141 context "with invalid parameters" do
124 test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
142 should "return errors" do
143 assert_no_difference 'TimeEntry.count' do
125 assert_no_difference 'TimeEntry.count' do
144 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')
145 end
127 end
146 assert_response :unprocessable_entity
128 assert_response :unprocessable_entity
147 assert_equal 'application/xml', @response.content_type
129 assert_equal 'application/xml', @response.content_type
148
130
149 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"}
150 end
132 end
151 end
152 end
153
133
154 context "DELETE /time_entries/2.xml" do
134 test "DELETE /time_entries/:id.xml should destroy time entry" do
155 should "destroy time entry" do
156 assert_difference 'TimeEntry.count', -1 do
135 assert_difference 'TimeEntry.count', -1 do
157 delete '/time_entries/2.xml', {}, credentials('jsmith')
136 delete '/time_entries/2.xml', {}, credentials('jsmith')
158 end
137 end
159 assert_response :ok
138 assert_response :ok
160 assert_equal '', @response.body
139 assert_equal '', @response.body
161 assert_nil TimeEntry.find_by_id(2)
140 assert_nil TimeEntry.find_by_id(2)
162 end
141 end
163 end
142 end
164 end
@@ -1,49 +1,42
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::TokenAuthenticationTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::TokenAuthenticationTest < 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
28
29 def setup
29 def setup
30 Setting.rest_api_enabled = '1'
30 Setting.rest_api_enabled = '1'
31 Setting.login_required = '1'
31 Setting.login_required = '1'
32 end
32 end
33
33
34 def teardown
34 def teardown
35 Setting.rest_api_enabled = '0'
35 Setting.rest_api_enabled = '0'
36 Setting.login_required = '0'
36 Setting.login_required = '0'
37 end
37 end
38
38
39 # Using the NewsController because it's a simple API.
39 # Using the NewsController because it's a simple API.
40 context "get /news" do
41 context "in :xml format" do
42 should_allow_key_based_auth(:get, "/news.xml")
40 should_allow_key_based_auth(:get, "/news.xml")
43 end
44
45 context "in :json format" do
46 should_allow_key_based_auth(:get, "/news.json")
41 should_allow_key_based_auth(:get, "/news.json")
47 end
42 end
48 end
49 end
@@ -1,51 +1,46
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::TrackersTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::TrackersTest < Redmine::ApiTest::Base
21 fixtures :trackers
21 fixtures :trackers
22
22
23 def setup
23 def setup
24 Setting.rest_api_enabled = '1'
24 Setting.rest_api_enabled = '1'
25 end
25 end
26
26
27 context "/trackers" do
27 test "GET /trackers.xml should return trackers" do
28 context "GET" do
29
30 should "return trackers" do
31 get '/trackers.xml'
28 get '/trackers.xml'
32
29
33 assert_response :success
30 assert_response :success
34 assert_equal 'application/xml', @response.content_type
31 assert_equal 'application/xml', @response.content_type
35 assert_tag :tag => 'trackers',
32 assert_tag :tag => 'trackers',
36 :attributes => {:type => 'array'},
33 :attributes => {:type => 'array'},
37 :child => {
34 :child => {
38 :tag => 'tracker',
35 :tag => 'tracker',
39 :child => {
36 :child => {
40 :tag => 'id',
37 :tag => 'id',
41 :content => '2',
38 :content => '2',
42 :sibling => {
39 :sibling => {
43 :tag => 'name',
40 :tag => 'name',
44 :content => 'Feature request'
41 :content => 'Feature request'
45 }
42 }
46 }
43 }
47 }
44 }
48 end
45 end
49 end
46 end
50 end
51 end
@@ -1,383 +1,327
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::UsersTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
21 fixtures :users, :members, :member_roles, :roles, :projects
21 fixtures :users, :members, :member_roles, :roles, :projects
22
22
23 def setup
23 def setup
24 Setting.rest_api_enabled = '1'
24 Setting.rest_api_enabled = '1'
25 end
25 end
26
26
27 context "GET /users" do
28 should_allow_api_authentication(:get, "/users.xml")
27 should_allow_api_authentication(:get, "/users.xml")
29 should_allow_api_authentication(:get, "/users.json")
28 should_allow_api_authentication(:get, "/users.json")
30 end
29 should_allow_api_authentication(:post,
30 '/users.xml',
31 {:user => {
32 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
33 :mail => 'foo@example.net', :password => 'secret123'
34 }},
35 {:success_code => :created})
36 should_allow_api_authentication(:post,
37 '/users.json',
38 {:user => {
39 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
40 :mail => 'foo@example.net'
41 }},
42 {:success_code => :created})
43 should_allow_api_authentication(:put,
44 '/users/2.xml',
45 {:user => {
46 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
47 :mail => 'jsmith@somenet.foo'
48 }},
49 {:success_code => :ok})
50 should_allow_api_authentication(:put,
51 '/users/2.json',
52 {:user => {
53 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
54 :mail => 'jsmith@somenet.foo'
55 }},
56 {:success_code => :ok})
57 should_allow_api_authentication(:delete,
58 '/users/2.xml',
59 {},
60 {:success_code => :ok})
61 should_allow_api_authentication(:delete,
62 '/users/2.xml',
63 {},
64 {:success_code => :ok})
31
65
32 context "GET /users/2" do
66 test "GET /users/:id.xml should return the user" do
33 context ".xml" do
34 should "return requested user" do
35 get '/users/2.xml'
67 get '/users/2.xml'
36
68
37 assert_response :success
69 assert_response :success
38 assert_tag :tag => 'user',
70 assert_tag :tag => 'user',
39 :child => {:tag => 'id', :content => '2'}
71 :child => {:tag => 'id', :content => '2'}
40 end
72 end
41
73
42 context "with include=memberships" do
74 test "GET /users/:id.json should return the user" do
43 should "include memberships" do
44 get '/users/2.xml?include=memberships'
45
46 assert_response :success
47 assert_tag :tag => 'memberships',
48 :parent => {:tag => 'user'},
49 :children => {:count => 1}
50 end
51 end
52 end
53
54 context ".json" do
55 should "return requested user" do
56 get '/users/2.json'
75 get '/users/2.json'
57
76
58 assert_response :success
77 assert_response :success
59 json = ActiveSupport::JSON.decode(response.body)
78 json = ActiveSupport::JSON.decode(response.body)
60 assert_kind_of Hash, json
79 assert_kind_of Hash, json
61 assert_kind_of Hash, json['user']
80 assert_kind_of Hash, json['user']
62 assert_equal 2, json['user']['id']
81 assert_equal 2, json['user']['id']
63 end
82 end
64
83
65 context "with include=memberships" do
84 test "GET /users/:id.xml with include=memberships should include memberships" do
66 should "include memberships" do
85 get '/users/2.xml?include=memberships'
86
87 assert_response :success
88 assert_tag :tag => 'memberships',
89 :parent => {:tag => 'user'},
90 :children => {:count => 1}
91 end
92
93 test "GET /users/:id.json with include=memberships should include memberships" do
67 get '/users/2.json?include=memberships'
94 get '/users/2.json?include=memberships'
68
95
69 assert_response :success
96 assert_response :success
70 json = ActiveSupport::JSON.decode(response.body)
97 json = ActiveSupport::JSON.decode(response.body)
71 assert_kind_of Array, json['user']['memberships']
98 assert_kind_of Array, json['user']['memberships']
72 assert_equal [{
99 assert_equal [{
73 "id"=>1,
100 "id"=>1,
74 "project"=>{"name"=>"eCookbook", "id"=>1},
101 "project"=>{"name"=>"eCookbook", "id"=>1},
75 "roles"=>[{"name"=>"Manager", "id"=>1}]
102 "roles"=>[{"name"=>"Manager", "id"=>1}]
76 }], json['user']['memberships']
103 }], json['user']['memberships']
77 end
104 end
78 end
79 end
80 end
81
105
82 context "GET /users/current" do
106 test "GET /users/current.xml should require authentication" do
83 context ".xml" do
84 should "require authentication" do
85 get '/users/current.xml'
107 get '/users/current.xml'
86
108
87 assert_response 401
109 assert_response 401
88 end
110 end
89
111
90 should "return current user" do
112 test "GET /users/current.xml should return current user" do
91 get '/users/current.xml', {}, credentials('jsmith')
113 get '/users/current.xml', {}, credentials('jsmith')
92
114
93 assert_tag :tag => 'user',
115 assert_tag :tag => 'user',
94 :child => {:tag => 'id', :content => '2'}
116 :child => {:tag => 'id', :content => '2'}
95 end
117 end
96 end
97 end
98
118
99 test "GET /users/:id should not return login for other user" do
119 test "GET /users/:id should not return login for other user" do
100 get '/users/3.xml', {}, credentials('jsmith')
120 get '/users/3.xml', {}, credentials('jsmith')
101 assert_response :success
121 assert_response :success
102 assert_no_tag 'user', :child => {:tag => 'login'}
122 assert_no_tag 'user', :child => {:tag => 'login'}
103 end
123 end
104
124
105 test "GET /users/:id should return login for current user" do
125 test "GET /users/:id should return login for current user" do
106 get '/users/2.xml', {}, credentials('jsmith')
126 get '/users/2.xml', {}, credentials('jsmith')
107 assert_response :success
127 assert_response :success
108 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
128 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
109 end
129 end
110
130
111 test "GET /users/:id should not return api_key for other user" do
131 test "GET /users/:id should not return api_key for other user" do
112 get '/users/3.xml', {}, credentials('jsmith')
132 get '/users/3.xml', {}, credentials('jsmith')
113 assert_response :success
133 assert_response :success
114 assert_no_tag 'user', :child => {:tag => 'api_key'}
134 assert_no_tag 'user', :child => {:tag => 'api_key'}
115 end
135 end
116
136
117 test "GET /users/:id should return api_key for current user" do
137 test "GET /users/:id should return api_key for current user" do
118 get '/users/2.xml', {}, credentials('jsmith')
138 get '/users/2.xml', {}, credentials('jsmith')
119 assert_response :success
139 assert_response :success
120 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
140 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
121 end
141 end
122
142
123 test "GET /users/:id should not return status for standard user" do
143 test "GET /users/:id should not return status for standard user" do
124 get '/users/3.xml', {}, credentials('jsmith')
144 get '/users/3.xml', {}, credentials('jsmith')
125 assert_response :success
145 assert_response :success
126 assert_no_tag 'user', :child => {:tag => 'status'}
146 assert_no_tag 'user', :child => {:tag => 'status'}
127 end
147 end
128
148
129 test "GET /users/:id should return status for administrators" do
149 test "GET /users/:id should return status for administrators" do
130 get '/users/2.xml', {}, credentials('admin')
150 get '/users/2.xml', {}, credentials('admin')
131 assert_response :success
151 assert_response :success
132 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s}
152 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s}
133 end
153 end
134
154
135 context "POST /users" do
155 test "POST /users.xml with valid parameters should create the user" do
136 context "with valid parameters" do
156 assert_difference('User.count') do
137 setup do
157 post '/users.xml', {
138 @parameters = {
139 :user => {
158 :user => {
140 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
159 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
141 :mail => 'foo@example.net', :password => 'secret123',
160 :mail => 'foo@example.net', :password => 'secret123',
142 :mail_notification => 'only_assigned'
161 :mail_notification => 'only_assigned'}
143 }
162 },
144 }
163 credentials('admin')
145 end
146
147 context ".xml" do
148 should_allow_api_authentication(:post,
149 '/users.xml',
150 {:user => {
151 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
152 :mail => 'foo@example.net', :password => 'secret123'
153 }},
154 {:success_code => :created})
155
156 should "create a user with the attributes" do
157 assert_difference('User.count') do
158 post '/users.xml', @parameters, credentials('admin')
159 end
164 end
160
165
161 user = User.first(:order => 'id DESC')
166 user = User.first(:order => 'id DESC')
162 assert_equal 'foo', user.login
167 assert_equal 'foo', user.login
163 assert_equal 'Firstname', user.firstname
168 assert_equal 'Firstname', user.firstname
164 assert_equal 'Lastname', user.lastname
169 assert_equal 'Lastname', user.lastname
165 assert_equal 'foo@example.net', user.mail
170 assert_equal 'foo@example.net', user.mail
166 assert_equal 'only_assigned', user.mail_notification
171 assert_equal 'only_assigned', user.mail_notification
167 assert !user.admin?
172 assert !user.admin?
168 assert user.check_password?('secret123')
173 assert user.check_password?('secret123')
169
174
170 assert_response :created
175 assert_response :created
171 assert_equal 'application/xml', @response.content_type
176 assert_equal 'application/xml', @response.content_type
172 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
177 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
173 end
178 end
174 end
175
176 context ".json" do
177 should_allow_api_authentication(:post,
178 '/users.json',
179 {:user => {
180 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
181 :mail => 'foo@example.net'
182 }},
183 {:success_code => :created})
184
179
185 should "create a user with the attributes" do
180 test "POST /users.json with valid parameters should create the user" do
186 assert_difference('User.count') do
181 assert_difference('User.count') do
187 post '/users.json', @parameters, credentials('admin')
182 post '/users.json', {
183 :user => {
184 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
185 :mail => 'foo@example.net', :password => 'secret123',
186 :mail_notification => 'only_assigned'}
187 },
188 credentials('admin')
188 end
189 end
189
190
190 user = User.first(:order => 'id DESC')
191 user = User.first(:order => 'id DESC')
191 assert_equal 'foo', user.login
192 assert_equal 'foo', user.login
192 assert_equal 'Firstname', user.firstname
193 assert_equal 'Firstname', user.firstname
193 assert_equal 'Lastname', user.lastname
194 assert_equal 'Lastname', user.lastname
194 assert_equal 'foo@example.net', user.mail
195 assert_equal 'foo@example.net', user.mail
195 assert !user.admin?
196 assert !user.admin?
196
197
197 assert_response :created
198 assert_response :created
198 assert_equal 'application/json', @response.content_type
199 assert_equal 'application/json', @response.content_type
199 json = ActiveSupport::JSON.decode(response.body)
200 json = ActiveSupport::JSON.decode(response.body)
200 assert_kind_of Hash, json
201 assert_kind_of Hash, json
201 assert_kind_of Hash, json['user']
202 assert_kind_of Hash, json['user']
202 assert_equal user.id, json['user']['id']
203 assert_equal user.id, json['user']['id']
203 end
204 end
204 end
205 end
206
205
207 context "with invalid parameters" do
206 test "POST /users.xml with with invalid parameters should return errors" do
208 setup do
209 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
210 end
211
212 context ".xml" do
213 should "return errors" do
214 assert_no_difference('User.count') do
207 assert_no_difference('User.count') do
215 post '/users.xml', @parameters, credentials('admin')
208 post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
216 end
209 end
217
210
218 assert_response :unprocessable_entity
211 assert_response :unprocessable_entity
219 assert_equal 'application/xml', @response.content_type
212 assert_equal 'application/xml', @response.content_type
220 assert_tag 'errors', :child => {
213 assert_tag 'errors', :child => {
221 :tag => 'error',
214 :tag => 'error',
222 :content => "First name can't be blank"
215 :content => "First name can't be blank"
223 }
216 }
224 end
217 end
225 end
226
218
227 context ".json" do
219 test "POST /users.json with with invalid parameters should return errors" do
228 should "return errors" do
229 assert_no_difference('User.count') do
220 assert_no_difference('User.count') do
230 post '/users.json', @parameters, credentials('admin')
221 post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
231 end
222 end
232
223
233 assert_response :unprocessable_entity
224 assert_response :unprocessable_entity
234 assert_equal 'application/json', @response.content_type
225 assert_equal 'application/json', @response.content_type
235 json = ActiveSupport::JSON.decode(response.body)
226 json = ActiveSupport::JSON.decode(response.body)
236 assert_kind_of Hash, json
227 assert_kind_of Hash, json
237 assert json.has_key?('errors')
228 assert json.has_key?('errors')
238 assert_kind_of Array, json['errors']
229 assert_kind_of Array, json['errors']
239 end
230 end
240 end
241 end
242 end
243
231
244 context "PUT /users/2" do
232 test "PUT /users/:id.xml with valid parameters should update the user" do
245 context "with valid parameters" do
233 assert_no_difference('User.count') do
246 setup do
234 put '/users/2.xml', {
247 @parameters = {
248 :user => {
235 :user => {
249 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
236 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
250 :mail => 'jsmith@somenet.foo'
237 :mail => 'jsmith@somenet.foo'}
251 }
238 },
252 }
239 credentials('admin')
253 end
254
255 context ".xml" do
256 should_allow_api_authentication(:put,
257 '/users/2.xml',
258 {:user => {
259 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
260 :mail => 'jsmith@somenet.foo'
261 }},
262 {:success_code => :ok})
263
264 should "update user with the attributes" do
265 assert_no_difference('User.count') do
266 put '/users/2.xml', @parameters, credentials('admin')
267 end
240 end
268
241
269 user = User.find(2)
242 user = User.find(2)
270 assert_equal 'jsmith', user.login
243 assert_equal 'jsmith', user.login
271 assert_equal 'John', user.firstname
244 assert_equal 'John', user.firstname
272 assert_equal 'Renamed', user.lastname
245 assert_equal 'Renamed', user.lastname
273 assert_equal 'jsmith@somenet.foo', user.mail
246 assert_equal 'jsmith@somenet.foo', user.mail
274 assert !user.admin?
247 assert !user.admin?
275
248
276 assert_response :ok
249 assert_response :ok
277 assert_equal '', @response.body
250 assert_equal '', @response.body
278 end
251 end
279 end
280
252
281 context ".json" do
253 test "PUT /users/:id.json with valid parameters should update the user" do
282 should_allow_api_authentication(:put,
283 '/users/2.json',
284 {:user => {
285 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
286 :mail => 'jsmith@somenet.foo'
287 }},
288 {:success_code => :ok})
289
290 should "update user with the attributes" do
291 assert_no_difference('User.count') do
254 assert_no_difference('User.count') do
292 put '/users/2.json', @parameters, credentials('admin')
255 put '/users/2.json', {
256 :user => {
257 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
258 :mail => 'jsmith@somenet.foo'}
259 },
260 credentials('admin')
293 end
261 end
294
262
295 user = User.find(2)
263 user = User.find(2)
296 assert_equal 'jsmith', user.login
264 assert_equal 'jsmith', user.login
297 assert_equal 'John', user.firstname
265 assert_equal 'John', user.firstname
298 assert_equal 'Renamed', user.lastname
266 assert_equal 'Renamed', user.lastname
299 assert_equal 'jsmith@somenet.foo', user.mail
267 assert_equal 'jsmith@somenet.foo', user.mail
300 assert !user.admin?
268 assert !user.admin?
301
269
302 assert_response :ok
270 assert_response :ok
303 assert_equal '', @response.body
271 assert_equal '', @response.body
304 end
272 end
305 end
306 end
307
273
308 context "with invalid parameters" do
274 test "PUT /users/:id.xml with invalid parameters" do
309 setup do
275 assert_no_difference('User.count') do
310 @parameters = {
276 put '/users/2.xml', {
311 :user => {
277 :user => {
312 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
278 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
313 :mail => 'foo'
279 :mail => 'foo'}
314 }
280 },
315 }
281 credentials('admin')
316 end
317
318 context ".xml" do
319 should "return errors" do
320 assert_no_difference('User.count') do
321 put '/users/2.xml', @parameters, credentials('admin')
322 end
282 end
323
283
324 assert_response :unprocessable_entity
284 assert_response :unprocessable_entity
325 assert_equal 'application/xml', @response.content_type
285 assert_equal 'application/xml', @response.content_type
326 assert_tag 'errors', :child => {
286 assert_tag 'errors', :child => {
327 :tag => 'error',
287 :tag => 'error',
328 :content => "First name can't be blank"
288 :content => "First name can't be blank"
329 }
289 }
330 end
290 end
331 end
332
291
333 context ".json" do
292 test "PUT /users/:id.json with invalid parameters" do
334 should "return errors" do
335 assert_no_difference('User.count') do
293 assert_no_difference('User.count') do
336 put '/users/2.json', @parameters, credentials('admin')
294 put '/users/2.json', {
295 :user => {
296 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
297 :mail => 'foo'}
298 },
299 credentials('admin')
337 end
300 end
338
301
339 assert_response :unprocessable_entity
302 assert_response :unprocessable_entity
340 assert_equal 'application/json', @response.content_type
303 assert_equal 'application/json', @response.content_type
341 json = ActiveSupport::JSON.decode(response.body)
304 json = ActiveSupport::JSON.decode(response.body)
342 assert_kind_of Hash, json
305 assert_kind_of Hash, json
343 assert json.has_key?('errors')
306 assert json.has_key?('errors')
344 assert_kind_of Array, json['errors']
307 assert_kind_of Array, json['errors']
345 end
308 end
346 end
347 end
348 end
349
350 context "DELETE /users/2" do
351 context ".xml" do
352 should_allow_api_authentication(:delete,
353 '/users/2.xml',
354 {},
355 {:success_code => :ok})
356
309
357 should "delete user" do
310 test "DELETE /users/:id.xml should delete the user" do
358 assert_difference('User.count', -1) do
311 assert_difference('User.count', -1) do
359 delete '/users/2.xml', {}, credentials('admin')
312 delete '/users/2.xml', {}, credentials('admin')
360 end
313 end
361
314
362 assert_response :ok
315 assert_response :ok
363 assert_equal '', @response.body
316 assert_equal '', @response.body
364 end
317 end
365 end
366
318
367 context ".json" do
319 test "DELETE /users/:id.json should delete the user" do
368 should_allow_api_authentication(:delete,
369 '/users/2.xml',
370 {},
371 {:success_code => :ok})
372
373 should "delete user" do
374 assert_difference('User.count', -1) do
320 assert_difference('User.count', -1) do
375 delete '/users/2.json', {}, credentials('admin')
321 delete '/users/2.json', {}, credentials('admin')
376 end
322 end
377
323
378 assert_response :ok
324 assert_response :ok
379 assert_equal '', @response.body
325 assert_equal '', @response.body
380 end
326 end
381 end
327 end
382 end
383 end
@@ -1,158 +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::VersionsTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::VersionsTest < 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 :versions
28 :versions
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 context "/projects/:project_id/versions" do
34 test "GET /projects/:project_id/versions.xml should return project versions" do
35 context "GET" do
36 should "return project versions" do
37 get '/projects/1/versions.xml'
35 get '/projects/1/versions.xml'
38
36
39 assert_response :success
37 assert_response :success
40 assert_equal 'application/xml', @response.content_type
38 assert_equal 'application/xml', @response.content_type
41 assert_tag :tag => 'versions',
39 assert_tag :tag => 'versions',
42 :attributes => {:type => 'array'},
40 :attributes => {:type => 'array'},
43 :child => {
41 :child => {
44 :tag => 'version',
42 :tag => 'version',
45 :child => {
43 :child => {
46 :tag => 'id',
44 :tag => 'id',
47 :content => '2',
45 :content => '2',
48 :sibling => {
46 :sibling => {
49 :tag => 'name',
47 :tag => 'name',
50 :content => '1.0'
48 :content => '1.0'
51 }
49 }
52 }
50 }
53 }
51 }
54 end
52 end
55 end
56
53
57 context "POST" do
54 test "POST /projects/:project_id/versions.xml should create the version" do
58 should "create the version" do
59 assert_difference 'Version.count' do
55 assert_difference 'Version.count' do
60 post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith')
56 post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith')
61 end
57 end
62
58
63 version = Version.first(:order => 'id DESC')
59 version = Version.first(:order => 'id DESC')
64 assert_equal 'API test', version.name
60 assert_equal 'API test', version.name
65
61
66 assert_response :created
62 assert_response :created
67 assert_equal 'application/xml', @response.content_type
63 assert_equal 'application/xml', @response.content_type
68 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
64 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
69 end
65 end
70
66
71 should "create the version with due date" do
67 test "POST /projects/:project_id/versions.xml should create the version with due date" do
72 assert_difference 'Version.count' do
68 assert_difference 'Version.count' do
73 post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith')
69 post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith')
74 end
70 end
75
71
76 version = Version.first(:order => 'id DESC')
72 version = Version.first(:order => 'id DESC')
77 assert_equal 'API test', version.name
73 assert_equal 'API test', version.name
78 assert_equal Date.parse('2012-01-24'), version.due_date
74 assert_equal Date.parse('2012-01-24'), version.due_date
79
75
80 assert_response :created
76 assert_response :created
81 assert_equal 'application/xml', @response.content_type
77 assert_equal 'application/xml', @response.content_type
82 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
78 assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
83 end
79 end
84
80
85 should "create the version with custom fields" do
81 test "POST /projects/:project_id/versions.xml should create the version with custom fields" do
86 field = VersionCustomField.generate!
82 field = VersionCustomField.generate!
87
83
88 assert_difference 'Version.count' do
84 assert_difference 'Version.count' do
89 post '/projects/1/versions.xml', {
85 post '/projects/1/versions.xml', {
90 :version => {
86 :version => {
91 :name => 'API test',
87 :name => 'API test',
92 :custom_fields => [
88 :custom_fields => [
93 {'id' => field.id.to_s, 'value' => 'Some value'}
89 {'id' => field.id.to_s, 'value' => 'Some value'}
94 ]
90 ]
95 }
91 }
96 }, credentials('jsmith')
92 }, credentials('jsmith')
97 end
93 end
98
94
99 version = Version.first(:order => 'id DESC')
95 version = Version.first(:order => 'id DESC')
100 assert_equal 'API test', version.name
96 assert_equal 'API test', version.name
101 assert_equal 'Some value', version.custom_field_value(field)
97 assert_equal 'Some value', version.custom_field_value(field)
102
98
103 assert_response :created
99 assert_response :created
104 assert_equal 'application/xml', @response.content_type
100 assert_equal 'application/xml', @response.content_type
105 assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value'
101 assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value'
106 end
102 end
107
103
108 context "with failure" do
104 test "POST /projects/:project_id/versions.xml with failure should return the errors" do
109 should "return the errors" do
110 assert_no_difference('Version.count') do
105 assert_no_difference('Version.count') do
111 post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith')
106 post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith')
112 end
107 end
113
108
114 assert_response :unprocessable_entity
109 assert_response :unprocessable_entity
115 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
110 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
116 end
111 end
117 end
118 end
119 end
120
112
121 context "/versions/:id" do
113 test "GET /versions/:id.xml should return the version" do
122 context "GET" do
123 should "return the version" do
124 get '/versions/2.xml'
114 get '/versions/2.xml'
125
115
126 assert_response :success
116 assert_response :success
127 assert_equal 'application/xml', @response.content_type
117 assert_equal 'application/xml', @response.content_type
128 assert_select 'version' do
118 assert_select 'version' do
129 assert_select 'id', :text => '2'
119 assert_select 'id', :text => '2'
130 assert_select 'name', :text => '1.0'
120 assert_select 'name', :text => '1.0'
131 assert_select 'sharing', :text => 'none'
121 assert_select 'sharing', :text => 'none'
132 end
122 end
133 end
123 end
134 end
135
124
136 context "PUT" do
125 test "PUT /versions/:id.xml should update the version" do
137 should "update the version" do
138 put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith')
126 put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith')
139
127
140 assert_response :ok
128 assert_response :ok
141 assert_equal '', @response.body
129 assert_equal '', @response.body
142 assert_equal 'API update', Version.find(2).name
130 assert_equal 'API update', Version.find(2).name
143 end
131 end
144 end
145
132
146 context "DELETE" do
133 test "DELETE /versions/:id.xml should destroy the version" do
147 should "destroy the version" do
148 assert_difference 'Version.count', -1 do
134 assert_difference 'Version.count', -1 do
149 delete '/versions/3.xml', {}, credentials('jsmith')
135 delete '/versions/3.xml', {}, credentials('jsmith')
150 end
136 end
151
137
152 assert_response :ok
138 assert_response :ok
153 assert_equal '', @response.body
139 assert_equal '', @response.body
154 assert_nil Version.find_by_id(3)
140 assert_nil Version.find_by_id(3)
155 end
141 end
156 end
142 end
157 end
158 end
General Comments 0
You need to be logged in to leave comments. Login now