Mercurial > hg > soundsoftware-site
comparison test/integration/api_test/projects_test.rb @ 1526:404aa68d4227
Merge from live branch
author | Chris Cannam |
---|---|
date | Thu, 11 Sep 2014 12:46:20 +0100 |
parents | dffacf8a6908 |
children |
comparison
equal
deleted
inserted
replaced
1493:a5f2bdf3b486 | 1526:404aa68d4227 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 Jean-Philippe Lang | 2 # Copyright (C) 2006-2014 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
15 # along with this program; if not, write to the Free Software | 15 # along with this program; if not, write to the Free Software |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 |
18 require File.expand_path('../../../test_helper', __FILE__) | 18 require File.expand_path('../../../test_helper', __FILE__) |
19 | 19 |
20 class ApiTest::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_select 'projects>project>id', :text => '1' |
53 end | 53 assert_select 'projects>project>status', :text => '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_select 'project>id', :text => '1' |
74 assert_no_tag 'issue_categories' | 74 assert_select 'project>status', :text => '1' |
75 end | 75 assert_select 'custom_field[name=Development status]', :text => 'Stable' |
76 | 76 |
77 context "with hidden custom fields" do | 77 assert_no_tag 'trackers' |
78 setup do | 78 assert_no_tag 'issue_categories' |
79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false | 79 end |
80 end | 80 |
81 | 81 test "GET /projects/:id.json should return the project" do |
82 should "not display hidden custom fields" do | 82 get '/projects/1.json' |
83 get '/projects/1.xml' | 83 |
84 assert_response :success | 84 json = ActiveSupport::JSON.decode(response.body) |
85 assert_equal 'application/xml', @response.content_type | 85 assert_kind_of Hash, json |
86 | 86 assert_kind_of Hash, json['project'] |
87 assert_no_tag 'custom_field', | 87 assert_equal 1, json['project']['id'] |
88 :attributes => {:name => 'Development status'} | 88 end |
89 end | 89 |
90 end | 90 test "GET /projects/:id.xml with hidden custom fields should not display hidden custom fields" do |
91 | 91 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false |
92 should "return categories with include=issue_categories" do | 92 |
93 get '/projects/1.xml?include=issue_categories' | 93 get '/projects/1.xml' |
94 assert_response :success | 94 assert_response :success |
95 assert_equal 'application/xml', @response.content_type | 95 assert_equal 'application/xml', @response.content_type |
96 | 96 |
97 assert_tag 'issue_categories', | 97 assert_no_tag 'custom_field', |
98 :attributes => {:type => 'array'}, | 98 :attributes => {:name => 'Development status'} |
99 :child => { | 99 end |
100 :tag => 'issue_category', | 100 |
101 :attributes => { | 101 test "GET /projects/:id.xml with include=issue_categories should return categories" do |
102 :id => '2', | 102 get '/projects/1.xml?include=issue_categories' |
103 :name => 'Recipes' | 103 assert_response :success |
104 } | 104 assert_equal 'application/xml', @response.content_type |
105 } | 105 |
106 end | 106 assert_tag 'issue_categories', |
107 | 107 :attributes => {:type => 'array'}, |
108 should "return trackers with include=trackers" do | 108 :child => { |
109 get '/projects/1.xml?include=trackers' | 109 :tag => 'issue_category', |
110 assert_response :success | 110 :attributes => { |
111 assert_equal 'application/xml', @response.content_type | 111 :id => '2', |
112 | 112 :name => 'Recipes' |
113 assert_tag 'trackers', | 113 } |
114 :attributes => {:type => 'array'}, | 114 } |
115 :child => { | 115 end |
116 :tag => 'tracker', | 116 |
117 :attributes => { | 117 test "GET /projects/:id.xml with include=trackers should return trackers" do |
118 :id => '2', | 118 get '/projects/1.xml?include=trackers' |
119 :name => 'Feature request' | 119 assert_response :success |
120 } | 120 assert_equal 'application/xml', @response.content_type |
121 } | 121 |
122 end | 122 assert_tag 'trackers', |
123 end | 123 :attributes => {:type => 'array'}, |
124 | 124 :child => { |
125 context ".json" do | 125 :tag => 'tracker', |
126 should_allow_api_authentication(:get, "/projects/2.json") | 126 :attributes => { |
127 | 127 :id => '2', |
128 should "return requested project" do | 128 :name => 'Feature request' |
129 get '/projects/1.json' | 129 } |
130 | 130 } |
131 json = ActiveSupport::JSON.decode(response.body) | 131 end |
132 assert_kind_of Hash, json | 132 |
133 assert_kind_of Hash, json['project'] | 133 test "POST /projects.xml with valid parameters should create the project" do |
134 assert_equal 1, json['project']['id'] | 134 Setting.default_projects_modules = ['issue_tracking', 'repository'] |
135 end | 135 |
136 end | 136 assert_difference('Project.count') do |
137 end | 137 post '/projects.xml', |
138 | 138 {:project => {:name => 'API test', :identifier => 'api-test'}}, |
139 context "POST /projects" do | 139 credentials('admin') |
140 context "with valid parameters" do | 140 end |
141 setup do | 141 |
142 Setting.default_projects_modules = ['issue_tracking', 'repository'] | 142 project = Project.order('id DESC').first |
143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} | 143 assert_equal 'API test', project.name |
144 end | 144 assert_equal 'api-test', project.identifier |
145 | 145 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort |
146 context ".xml" do | 146 assert_equal Tracker.all.size, project.trackers.size |
147 should_allow_api_authentication(:post, | 147 |
148 '/projects.xml', | 148 assert_response :created |
149 {:project => {:name => 'API test', :identifier => 'api-test'}}, | 149 assert_equal 'application/xml', @response.content_type |
150 {:success_code => :created}) | 150 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} |
151 | 151 end |
152 | 152 |
153 should "create a project with the attributes" do | 153 test "POST /projects.xml should accept enabled_module_names attribute" do |
154 assert_difference('Project.count') do | 154 assert_difference('Project.count') do |
155 post '/projects.xml', @parameters, credentials('admin') | 155 post '/projects.xml', |
156 end | 156 {:project => {:name => 'API test', :identifier => 'api-test', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, |
157 | 157 credentials('admin') |
158 project = Project.first(:order => 'id DESC') | 158 end |
159 assert_equal 'API test', project.name | 159 |
160 assert_equal 'api-test', project.identifier | 160 project = Project.order('id DESC').first |
161 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort | 161 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort |
162 assert_equal Tracker.all.size, project.trackers.size | 162 end |
163 | 163 |
164 assert_response :created | 164 test "POST /projects.xml should accept tracker_ids attribute" do |
165 assert_equal 'application/xml', @response.content_type | 165 assert_difference('Project.count') do |
166 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} | 166 post '/projects.xml', |
167 end | 167 {:project => {:name => 'API test', :identifier => 'api-test', :tracker_ids => [1, 3]}}, |
168 | 168 credentials('admin') |
169 should "accept enabled_module_names attribute" do | 169 end |
170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) | 170 |
171 | 171 project = Project.order('id DESC').first |
172 assert_difference('Project.count') do | 172 assert_equal [1, 3], project.trackers.map(&:id).sort |
173 post '/projects.xml', @parameters, credentials('admin') | 173 end |
174 end | 174 |
175 | 175 test "POST /projects.xml with invalid parameters should return errors" do |
176 project = Project.first(:order => 'id DESC') | 176 assert_no_difference('Project.count') do |
177 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort | 177 post '/projects.xml', {:project => {:name => 'API test'}}, credentials('admin') |
178 end | 178 end |
179 | 179 |
180 should "accept tracker_ids attribute" do | 180 assert_response :unprocessable_entity |
181 @parameters[:project].merge!({:tracker_ids => [1, 3]}) | 181 assert_equal 'application/xml', @response.content_type |
182 | 182 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} |
183 assert_difference('Project.count') do | 183 end |
184 post '/projects.xml', @parameters, credentials('admin') | 184 |
185 end | 185 test "PUT /projects/:id.xml with valid parameters should update the project" do |
186 | 186 assert_no_difference 'Project.count' do |
187 project = Project.first(:order => 'id DESC') | 187 put '/projects/2.xml', {:project => {:name => 'API update'}}, credentials('jsmith') |
188 assert_equal [1, 3], project.trackers.map(&:id).sort | 188 end |
189 end | 189 assert_response :ok |
190 end | 190 assert_equal '', @response.body |
191 end | 191 assert_equal 'application/xml', @response.content_type |
192 | 192 project = Project.find(2) |
193 context "with invalid parameters" do | 193 assert_equal 'API update', project.name |
194 setup do | 194 end |
195 @parameters = {:project => {:name => 'API test'}} | 195 |
196 end | 196 test "PUT /projects/:id.xml should accept enabled_module_names attribute" do |
197 | 197 assert_no_difference 'Project.count' do |
198 context ".xml" do | 198 put '/projects/2.xml', {:project => {:name => 'API update', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, credentials('admin') |
199 should "return errors" do | 199 end |
200 assert_no_difference('Project.count') do | 200 assert_response :ok |
201 post '/projects.xml', @parameters, credentials('admin') | 201 assert_equal '', @response.body |
202 end | 202 project = Project.find(2) |
203 | 203 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort |
204 assert_response :unprocessable_entity | 204 end |
205 assert_equal 'application/xml', @response.content_type | 205 |
206 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} | 206 test "PUT /projects/:id.xml should accept tracker_ids attribute" do |
207 end | 207 assert_no_difference 'Project.count' do |
208 end | 208 put '/projects/2.xml', {:project => {:name => 'API update', :tracker_ids => [1, 3]}}, credentials('admin') |
209 end | 209 end |
210 end | 210 assert_response :ok |
211 | 211 assert_equal '', @response.body |
212 context "PUT /projects/:id" do | 212 project = Project.find(2) |
213 context "with valid parameters" do | 213 assert_equal [1, 3], project.trackers.map(&:id).sort |
214 setup do | 214 end |
215 @parameters = {:project => {:name => 'API update'}} | 215 |
216 end | 216 test "PUT /projects/:id.xml with invalid parameters should return errors" do |
217 | 217 assert_no_difference('Project.count') do |
218 context ".xml" do | 218 put '/projects/2.xml', {:project => {:name => ''}}, credentials('admin') |
219 should_allow_api_authentication(:put, | 219 end |
220 '/projects/2.xml', | 220 |
221 {:project => {:name => 'API update'}}, | 221 assert_response :unprocessable_entity |
222 {:success_code => :ok}) | 222 assert_equal 'application/xml', @response.content_type |
223 | 223 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} |
224 should "update the project" do | 224 end |
225 assert_no_difference 'Project.count' do | 225 |
226 put '/projects/2.xml', @parameters, credentials('jsmith') | 226 test "DELETE /projects/:id.xml should delete the project" do |
227 end | 227 assert_difference('Project.count',-1) do |
228 assert_response :ok | 228 delete '/projects/2.xml', {}, credentials('admin') |
229 assert_equal '', @response.body | 229 end |
230 assert_equal 'application/xml', @response.content_type | 230 assert_response :ok |
231 project = Project.find(2) | 231 assert_equal '', @response.body |
232 assert_equal 'API update', project.name | 232 assert_nil Project.find_by_id(2) |
233 end | |
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 | 233 end |
297 end | 234 end |