Mercurial > hg > soundsoftware-site
comparison test/integration/api_test/projects_test.rb @ 1464:261b3d9a4903 redmine-2.4
Update to Redmine 2.4 branch rev 12663
author | Chris Cannam |
---|---|
date | Tue, 14 Jan 2014 14:37:42 +0000 |
parents | 433d4f72a19b |
children | e248c7af89ec |
comparison
equal
deleted
inserted
replaced
1296:038ba2d95de8 | 1464:261b3d9a4903 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 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. |
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::ProjectsTest < ActionController::IntegrationTest | 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 get '/projects.xml' | 33 should_allow_api_authentication(:get, "/projects/2.json") |
34 assert_response :success | 34 should_allow_api_authentication(:post, |
35 assert_equal 'application/xml', @response.content_type | 35 '/projects.xml', |
36 | 36 {:project => {:name => 'API test', :identifier => 'api-test'}}, |
37 assert_tag :tag => 'projects', | 37 {:success_code => :created}) |
38 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} | 38 should_allow_api_authentication(:put, |
39 end | 39 '/projects/2.xml', |
40 end | 40 {:project => {:name => 'API update'}}, |
41 | 41 {:success_code => :ok}) |
42 context ".json" do | 42 should_allow_api_authentication(:delete, |
43 should "return projects" do | 43 '/projects/2.xml', |
44 get '/projects.json' | 44 {}, |
45 assert_response :success | 45 {:success_code => :ok}) |
46 assert_equal 'application/json', @response.content_type | 46 |
47 | 47 test "GET /projects.xml should return projects" do |
48 json = ActiveSupport::JSON.decode(response.body) | 48 get '/projects.xml' |
49 assert_kind_of Hash, json | 49 assert_response :success |
50 assert_kind_of Array, json['projects'] | 50 assert_equal 'application/xml', @response.content_type |
51 assert_kind_of Hash, json['projects'].first | 51 |
52 assert json['projects'].first.has_key?('id') | 52 assert_tag :tag => 'projects', |
53 end | 53 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} |
54 end | 54 end |
55 end | 55 |
56 | 56 test "GET /projects.json should return projects" do |
57 context "GET /projects/:id" do | 57 get '/projects.json' |
58 context ".xml" do | 58 assert_response :success |
59 # TODO: A private project is needed because should_allow_api_authentication | 59 assert_equal 'application/json', @response.content_type |
60 # actually tests that authentication is *required*, not just allowed | 60 |
61 should_allow_api_authentication(:get, "/projects/2.xml") | 61 json = ActiveSupport::JSON.decode(response.body) |
62 | 62 assert_kind_of Hash, json |
63 should "return requested project" do | 63 assert_kind_of Array, json['projects'] |
64 get '/projects/1.xml' | 64 assert_kind_of Hash, json['projects'].first |
65 assert_response :success | 65 assert json['projects'].first.has_key?('id') |
66 assert_equal 'application/xml', @response.content_type | 66 end |
67 | 67 |
68 assert_tag :tag => 'project', | 68 test "GET /projects/:id.xml should return the project" do |
69 :child => {:tag => 'id', :content => '1'} | 69 get '/projects/1.xml' |
70 assert_tag :tag => 'custom_field', | 70 assert_response :success |
71 :attributes => {:name => 'Development status'}, :content => 'Stable' | 71 assert_equal 'application/xml', @response.content_type |
72 | 72 |
73 assert_no_tag 'trackers' | 73 assert_tag :tag => 'project', |
74 assert_no_tag 'issue_categories' | 74 :child => {:tag => 'id', :content => '1'} |
75 end | 75 assert_tag :tag => 'custom_field', |
76 | 76 :attributes => {:name => 'Development status'}, :content => 'Stable' |
77 context "with hidden custom fields" do | 77 |
78 setup do | 78 assert_no_tag 'trackers' |
79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false | 79 assert_no_tag 'issue_categories' |
80 end | 80 end |
81 | 81 |
82 should "not display hidden custom fields" do | 82 test "GET /projects/:id.json should return the project" do |
83 get '/projects/1.xml' | 83 get '/projects/1.json' |
84 assert_response :success | 84 |
85 assert_equal 'application/xml', @response.content_type | 85 json = ActiveSupport::JSON.decode(response.body) |
86 | 86 assert_kind_of Hash, json |
87 assert_no_tag 'custom_field', | 87 assert_kind_of Hash, json['project'] |
88 :attributes => {:name => 'Development status'} | 88 assert_equal 1, json['project']['id'] |
89 end | 89 end |
90 end | 90 |
91 | 91 test "GET /projects/:id.xml with hidden custom fields should not display hidden custom fields" do |
92 should "return categories with include=issue_categories" do | 92 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false |
93 get '/projects/1.xml?include=issue_categories' | 93 |
94 assert_response :success | 94 get '/projects/1.xml' |
95 assert_equal 'application/xml', @response.content_type | 95 assert_response :success |
96 | 96 assert_equal 'application/xml', @response.content_type |
97 assert_tag 'issue_categories', | 97 |
98 :attributes => {:type => 'array'}, | 98 assert_no_tag 'custom_field', |
99 :child => { | 99 :attributes => {:name => 'Development status'} |
100 :tag => 'issue_category', | 100 end |
101 :attributes => { | 101 |
102 :id => '2', | 102 test "GET /projects/:id.xml with include=issue_categories should return categories" do |
103 :name => 'Recipes' | 103 get '/projects/1.xml?include=issue_categories' |
104 } | 104 assert_response :success |
105 } | 105 assert_equal 'application/xml', @response.content_type |
106 end | 106 |
107 | 107 assert_tag 'issue_categories', |
108 should "return trackers with include=trackers" do | 108 :attributes => {:type => 'array'}, |
109 get '/projects/1.xml?include=trackers' | 109 :child => { |
110 assert_response :success | 110 :tag => 'issue_category', |
111 assert_equal 'application/xml', @response.content_type | 111 :attributes => { |
112 | 112 :id => '2', |
113 assert_tag 'trackers', | 113 :name => 'Recipes' |
114 :attributes => {:type => 'array'}, | 114 } |
115 :child => { | 115 } |
116 :tag => 'tracker', | 116 end |
117 :attributes => { | 117 |
118 :id => '2', | 118 test "GET /projects/:id.xml with include=trackers should return trackers" do |
119 :name => 'Feature request' | 119 get '/projects/1.xml?include=trackers' |
120 } | 120 assert_response :success |
121 } | 121 assert_equal 'application/xml', @response.content_type |
122 end | 122 |
123 end | 123 assert_tag 'trackers', |
124 | 124 :attributes => {:type => 'array'}, |
125 context ".json" do | 125 :child => { |
126 should_allow_api_authentication(:get, "/projects/2.json") | 126 :tag => 'tracker', |
127 | 127 :attributes => { |
128 should "return requested project" do | 128 :id => '2', |
129 get '/projects/1.json' | 129 :name => 'Feature request' |
130 | 130 } |
131 json = ActiveSupport::JSON.decode(response.body) | 131 } |
132 assert_kind_of Hash, json | 132 end |
133 assert_kind_of Hash, json['project'] | 133 |
134 assert_equal 1, json['project']['id'] | 134 test "POST /projects.xml with valid parameters should create the project" do |
135 end | 135 Setting.default_projects_modules = ['issue_tracking', 'repository'] |
136 end | 136 |
137 end | 137 assert_difference('Project.count') do |
138 | 138 post '/projects.xml', |
139 context "POST /projects" do | 139 {:project => {:name => 'API test', :identifier => 'api-test'}}, |
140 context "with valid parameters" do | 140 credentials('admin') |
141 setup do | 141 end |
142 Setting.default_projects_modules = ['issue_tracking', 'repository'] | 142 |
143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} | 143 project = Project.first(:order => 'id DESC') |
144 end | 144 assert_equal 'API test', project.name |
145 | 145 assert_equal 'api-test', project.identifier |
146 context ".xml" do | 146 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort |
147 should_allow_api_authentication(:post, | 147 assert_equal Tracker.all.size, project.trackers.size |
148 '/projects.xml', | 148 |
149 {:project => {:name => 'API test', :identifier => 'api-test'}}, | 149 assert_response :created |
150 {:success_code => :created}) | 150 assert_equal 'application/xml', @response.content_type |
151 | 151 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} |
152 | 152 end |
153 should "create a project with the attributes" do | 153 |
154 assert_difference('Project.count') do | 154 test "POST /projects.xml should accept enabled_module_names attribute" do |
155 post '/projects.xml', @parameters, credentials('admin') | 155 assert_difference('Project.count') do |
156 end | 156 post '/projects.xml', |
157 | 157 {:project => {:name => 'API test', :identifier => 'api-test', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, |
158 project = Project.first(:order => 'id DESC') | 158 credentials('admin') |
159 assert_equal 'API test', project.name | 159 end |
160 assert_equal 'api-test', project.identifier | 160 |
161 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort | 161 project = Project.first(:order => 'id DESC') |
162 assert_equal Tracker.all.size, project.trackers.size | 162 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort |
163 | 163 end |
164 assert_response :created | 164 |
165 assert_equal 'application/xml', @response.content_type | 165 test "POST /projects.xml should accept tracker_ids attribute" do |
166 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} | 166 assert_difference('Project.count') do |
167 end | 167 post '/projects.xml', |
168 | 168 {:project => {:name => 'API test', :identifier => 'api-test', :tracker_ids => [1, 3]}}, |
169 should "accept enabled_module_names attribute" do | 169 credentials('admin') |
170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) | 170 end |
171 | 171 |
172 assert_difference('Project.count') do | 172 project = Project.first(:order => 'id DESC') |
173 post '/projects.xml', @parameters, credentials('admin') | 173 assert_equal [1, 3], project.trackers.map(&:id).sort |
174 end | 174 end |
175 | 175 |
176 project = Project.first(:order => 'id DESC') | 176 test "POST /projects.xml with invalid parameters should return errors" do |
177 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort | 177 assert_no_difference('Project.count') do |
178 end | 178 post '/projects.xml', {:project => {:name => 'API test'}}, credentials('admin') |
179 | 179 end |
180 should "accept tracker_ids attribute" do | 180 |
181 @parameters[:project].merge!({:tracker_ids => [1, 3]}) | 181 assert_response :unprocessable_entity |
182 | 182 assert_equal 'application/xml', @response.content_type |
183 assert_difference('Project.count') do | 183 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} |
184 post '/projects.xml', @parameters, credentials('admin') | 184 end |
185 end | 185 |
186 | 186 test "PUT /projects/:id.xml with valid parameters should update the project" do |
187 project = Project.first(:order => 'id DESC') | 187 assert_no_difference 'Project.count' do |
188 assert_equal [1, 3], project.trackers.map(&:id).sort | 188 put '/projects/2.xml', {:project => {:name => 'API update'}}, credentials('jsmith') |
189 end | 189 end |
190 end | 190 assert_response :ok |
191 end | 191 assert_equal '', @response.body |
192 | 192 assert_equal 'application/xml', @response.content_type |
193 context "with invalid parameters" do | 193 project = Project.find(2) |
194 setup do | 194 assert_equal 'API update', project.name |
195 @parameters = {:project => {:name => 'API test'}} | 195 end |
196 end | 196 |
197 | 197 test "PUT /projects/:id.xml should accept enabled_module_names attribute" do |
198 context ".xml" do | 198 assert_no_difference 'Project.count' do |
199 should "return errors" do | 199 put '/projects/2.xml', {:project => {:name => 'API update', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, credentials('admin') |
200 assert_no_difference('Project.count') do | 200 end |
201 post '/projects.xml', @parameters, credentials('admin') | 201 assert_response :ok |
202 end | 202 assert_equal '', @response.body |
203 | 203 project = Project.find(2) |
204 assert_response :unprocessable_entity | 204 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort |
205 assert_equal 'application/xml', @response.content_type | 205 end |
206 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} | 206 |
207 end | 207 test "PUT /projects/:id.xml should accept tracker_ids attribute" do |
208 end | 208 assert_no_difference 'Project.count' do |
209 end | 209 put '/projects/2.xml', {:project => {:name => 'API update', :tracker_ids => [1, 3]}}, credentials('admin') |
210 end | 210 end |
211 | 211 assert_response :ok |
212 context "PUT /projects/:id" do | 212 assert_equal '', @response.body |
213 context "with valid parameters" do | 213 project = Project.find(2) |
214 setup do | 214 assert_equal [1, 3], project.trackers.map(&:id).sort |
215 @parameters = {:project => {:name => 'API update'}} | 215 end |
216 end | 216 |
217 | 217 test "PUT /projects/:id.xml with invalid parameters should return errors" do |
218 context ".xml" do | 218 assert_no_difference('Project.count') do |
219 should_allow_api_authentication(:put, | 219 put '/projects/2.xml', {:project => {:name => ''}}, credentials('admin') |
220 '/projects/2.xml', | 220 end |
221 {:project => {:name => 'API update'}}, | 221 |
222 {:success_code => :ok}) | 222 assert_response :unprocessable_entity |
223 | 223 assert_equal 'application/xml', @response.content_type |
224 should "update the project" do | 224 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} |
225 assert_no_difference 'Project.count' do | 225 end |
226 put '/projects/2.xml', @parameters, credentials('jsmith') | 226 |
227 end | 227 test "DELETE /projects/:id.xml should delete the project" do |
228 assert_response :ok | 228 assert_difference('Project.count',-1) do |
229 assert_equal '', @response.body | 229 delete '/projects/2.xml', {}, credentials('admin') |
230 assert_equal 'application/xml', @response.content_type | 230 end |
231 project = Project.find(2) | 231 assert_response :ok |
232 assert_equal 'API update', project.name | 232 assert_equal '', @response.body |
233 end | 233 assert_nil Project.find_by_id(2) |
234 | |
235 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 | |
239 put '/projects/2.xml', @parameters, credentials('admin') | |
240 end | |
241 assert_response :ok | |
242 assert_equal '', @response.body | |
243 project = Project.find(2) | |
244 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort | |
245 end | |
246 | |
247 should "accept tracker_ids attribute" do | |
248 @parameters[:project].merge!({:tracker_ids => [1, 3]}) | |
249 | |
250 assert_no_difference 'Project.count' do | |
251 put '/projects/2.xml', @parameters, credentials('admin') | |
252 end | |
253 assert_response :ok | |
254 assert_equal '', @response.body | |
255 project = Project.find(2) | |
256 assert_equal [1, 3], project.trackers.map(&:id).sort | |
257 end | |
258 end | |
259 end | |
260 | |
261 context "with invalid parameters" do | |
262 setup do | |
263 @parameters = {:project => {:name => ''}} | |
264 end | |
265 | |
266 context ".xml" do | |
267 should "return errors" do | |
268 assert_no_difference('Project.count') do | |
269 put '/projects/2.xml', @parameters, credentials('admin') | |
270 end | |
271 | |
272 assert_response :unprocessable_entity | |
273 assert_equal 'application/xml', @response.content_type | |
274 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} | |
275 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 | |
287 should "delete the project" do | |
288 assert_difference('Project.count',-1) do | |
289 delete '/projects/2.xml', {}, credentials('admin') | |
290 end | |
291 assert_response :ok | |
292 assert_equal '', @response.body | |
293 assert_nil Project.find_by_id(2) | |
294 end | |
295 end | |
296 end | 234 end |
297 end | 235 end |