##// END OF EJS Templates
Fixed some more test/integration/*_test.rb breaking when run alone (#12285)...
Jean-Baptiste Barth -
r10566:7f3cc6e38b87
parent child
Show More
@@ -1,347 +1,347
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19 require 'pp'
20 20 class ApiTest::UsersTest < ActionController::IntegrationTest
21 fixtures :users
21 fixtures :users, :members, :member_roles, :roles, :projects
22 22
23 23 def setup
24 24 Setting.rest_api_enabled = '1'
25 25 end
26 26
27 27 context "GET /users" do
28 28 should_allow_api_authentication(:get, "/users.xml")
29 29 should_allow_api_authentication(:get, "/users.json")
30 30 end
31 31
32 32 context "GET /users/2" do
33 33 context ".xml" do
34 34 should "return requested user" do
35 35 get '/users/2.xml'
36 36
37 37 assert_response :success
38 38 assert_tag :tag => 'user',
39 39 :child => {:tag => 'id', :content => '2'}
40 40 end
41 41
42 42 context "with include=memberships" do
43 43 should "include memberships" do
44 44 get '/users/2.xml?include=memberships'
45 45
46 46 assert_response :success
47 47 assert_tag :tag => 'memberships',
48 48 :parent => {:tag => 'user'},
49 49 :children => {:count => 1}
50 50 end
51 51 end
52 52 end
53 53
54 54 context ".json" do
55 55 should "return requested user" do
56 56 get '/users/2.json'
57 57
58 58 assert_response :success
59 59 json = ActiveSupport::JSON.decode(response.body)
60 60 assert_kind_of Hash, json
61 61 assert_kind_of Hash, json['user']
62 62 assert_equal 2, json['user']['id']
63 63 end
64 64
65 65 context "with include=memberships" do
66 66 should "include memberships" do
67 67 get '/users/2.json?include=memberships'
68 68
69 69 assert_response :success
70 70 json = ActiveSupport::JSON.decode(response.body)
71 71 assert_kind_of Array, json['user']['memberships']
72 72 assert_equal [{
73 73 "id"=>1,
74 74 "project"=>{"name"=>"eCookbook", "id"=>1},
75 75 "roles"=>[{"name"=>"Manager", "id"=>1}]
76 76 }], json['user']['memberships']
77 77 end
78 78 end
79 79 end
80 80 end
81 81
82 82 context "GET /users/current" do
83 83 context ".xml" do
84 84 should "require authentication" do
85 85 get '/users/current.xml'
86 86
87 87 assert_response 401
88 88 end
89 89
90 90 should "return current user" do
91 91 get '/users/current.xml', {}, credentials('jsmith')
92 92
93 93 assert_tag :tag => 'user',
94 94 :child => {:tag => 'id', :content => '2'}
95 95 end
96 96 end
97 97 end
98 98
99 99 context "POST /users" do
100 100 context "with valid parameters" do
101 101 setup do
102 102 @parameters = {
103 103 :user => {
104 104 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
105 105 :mail => 'foo@example.net', :password => 'secret',
106 106 :mail_notification => 'only_assigned'
107 107 }
108 108 }
109 109 end
110 110
111 111 context ".xml" do
112 112 should_allow_api_authentication(:post,
113 113 '/users.xml',
114 114 {:user => {
115 115 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
116 116 :mail => 'foo@example.net', :password => 'secret'
117 117 }},
118 118 {:success_code => :created})
119 119
120 120 should "create a user with the attributes" do
121 121 assert_difference('User.count') do
122 122 post '/users.xml', @parameters, credentials('admin')
123 123 end
124 124
125 125 user = User.first(:order => 'id DESC')
126 126 assert_equal 'foo', user.login
127 127 assert_equal 'Firstname', user.firstname
128 128 assert_equal 'Lastname', user.lastname
129 129 assert_equal 'foo@example.net', user.mail
130 130 assert_equal 'only_assigned', user.mail_notification
131 131 assert !user.admin?
132 132 assert user.check_password?('secret')
133 133
134 134 assert_response :created
135 135 assert_equal 'application/xml', @response.content_type
136 136 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
137 137 end
138 138 end
139 139
140 140 context ".json" do
141 141 should_allow_api_authentication(:post,
142 142 '/users.json',
143 143 {:user => {
144 144 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
145 145 :mail => 'foo@example.net'
146 146 }},
147 147 {:success_code => :created})
148 148
149 149 should "create a user with the attributes" do
150 150 assert_difference('User.count') do
151 151 post '/users.json', @parameters, credentials('admin')
152 152 end
153 153
154 154 user = User.first(:order => 'id DESC')
155 155 assert_equal 'foo', user.login
156 156 assert_equal 'Firstname', user.firstname
157 157 assert_equal 'Lastname', user.lastname
158 158 assert_equal 'foo@example.net', user.mail
159 159 assert !user.admin?
160 160
161 161 assert_response :created
162 162 assert_equal 'application/json', @response.content_type
163 163 json = ActiveSupport::JSON.decode(response.body)
164 164 assert_kind_of Hash, json
165 165 assert_kind_of Hash, json['user']
166 166 assert_equal user.id, json['user']['id']
167 167 end
168 168 end
169 169 end
170 170
171 171 context "with invalid parameters" do
172 172 setup do
173 173 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
174 174 end
175 175
176 176 context ".xml" do
177 177 should "return errors" do
178 178 assert_no_difference('User.count') do
179 179 post '/users.xml', @parameters, credentials('admin')
180 180 end
181 181
182 182 assert_response :unprocessable_entity
183 183 assert_equal 'application/xml', @response.content_type
184 184 assert_tag 'errors', :child => {
185 185 :tag => 'error',
186 186 :content => "First name can't be blank"
187 187 }
188 188 end
189 189 end
190 190
191 191 context ".json" do
192 192 should "return errors" do
193 193 assert_no_difference('User.count') do
194 194 post '/users.json', @parameters, credentials('admin')
195 195 end
196 196
197 197 assert_response :unprocessable_entity
198 198 assert_equal 'application/json', @response.content_type
199 199 json = ActiveSupport::JSON.decode(response.body)
200 200 assert_kind_of Hash, json
201 201 assert json.has_key?('errors')
202 202 assert_kind_of Array, json['errors']
203 203 end
204 204 end
205 205 end
206 206 end
207 207
208 208 context "PUT /users/2" do
209 209 context "with valid parameters" do
210 210 setup do
211 211 @parameters = {
212 212 :user => {
213 213 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
214 214 :mail => 'jsmith@somenet.foo'
215 215 }
216 216 }
217 217 end
218 218
219 219 context ".xml" do
220 220 should_allow_api_authentication(:put,
221 221 '/users/2.xml',
222 222 {:user => {
223 223 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
224 224 :mail => 'jsmith@somenet.foo'
225 225 }},
226 226 {:success_code => :ok})
227 227
228 228 should "update user with the attributes" do
229 229 assert_no_difference('User.count') do
230 230 put '/users/2.xml', @parameters, credentials('admin')
231 231 end
232 232
233 233 user = User.find(2)
234 234 assert_equal 'jsmith', user.login
235 235 assert_equal 'John', user.firstname
236 236 assert_equal 'Renamed', user.lastname
237 237 assert_equal 'jsmith@somenet.foo', user.mail
238 238 assert !user.admin?
239 239
240 240 assert_response :ok
241 241 assert_equal '', @response.body
242 242 end
243 243 end
244 244
245 245 context ".json" do
246 246 should_allow_api_authentication(:put,
247 247 '/users/2.json',
248 248 {:user => {
249 249 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
250 250 :mail => 'jsmith@somenet.foo'
251 251 }},
252 252 {:success_code => :ok})
253 253
254 254 should "update user with the attributes" do
255 255 assert_no_difference('User.count') do
256 256 put '/users/2.json', @parameters, credentials('admin')
257 257 end
258 258
259 259 user = User.find(2)
260 260 assert_equal 'jsmith', user.login
261 261 assert_equal 'John', user.firstname
262 262 assert_equal 'Renamed', user.lastname
263 263 assert_equal 'jsmith@somenet.foo', user.mail
264 264 assert !user.admin?
265 265
266 266 assert_response :ok
267 267 assert_equal '', @response.body
268 268 end
269 269 end
270 270 end
271 271
272 272 context "with invalid parameters" do
273 273 setup do
274 274 @parameters = {
275 275 :user => {
276 276 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
277 277 :mail => 'foo'
278 278 }
279 279 }
280 280 end
281 281
282 282 context ".xml" do
283 283 should "return errors" do
284 284 assert_no_difference('User.count') do
285 285 put '/users/2.xml', @parameters, credentials('admin')
286 286 end
287 287
288 288 assert_response :unprocessable_entity
289 289 assert_equal 'application/xml', @response.content_type
290 290 assert_tag 'errors', :child => {
291 291 :tag => 'error',
292 292 :content => "First name can't be blank"
293 293 }
294 294 end
295 295 end
296 296
297 297 context ".json" do
298 298 should "return errors" do
299 299 assert_no_difference('User.count') do
300 300 put '/users/2.json', @parameters, credentials('admin')
301 301 end
302 302
303 303 assert_response :unprocessable_entity
304 304 assert_equal 'application/json', @response.content_type
305 305 json = ActiveSupport::JSON.decode(response.body)
306 306 assert_kind_of Hash, json
307 307 assert json.has_key?('errors')
308 308 assert_kind_of Array, json['errors']
309 309 end
310 310 end
311 311 end
312 312 end
313 313
314 314 context "DELETE /users/2" do
315 315 context ".xml" do
316 316 should_allow_api_authentication(:delete,
317 317 '/users/2.xml',
318 318 {},
319 319 {:success_code => :ok})
320 320
321 321 should "delete user" do
322 322 assert_difference('User.count', -1) do
323 323 delete '/users/2.xml', {}, credentials('admin')
324 324 end
325 325
326 326 assert_response :ok
327 327 assert_equal '', @response.body
328 328 end
329 329 end
330 330
331 331 context ".json" do
332 332 should_allow_api_authentication(:delete,
333 333 '/users/2.xml',
334 334 {},
335 335 {:success_code => :ok})
336 336
337 337 should "delete user" do
338 338 assert_difference('User.count', -1) do
339 339 delete '/users/2.json', {}, credentials('admin')
340 340 end
341 341
342 342 assert_response :ok
343 343 assert_equal '', @response.body
344 344 end
345 345 end
346 346 end
347 347 end
@@ -1,228 +1,229
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssuesTest < ActionController::IntegrationTest
21 21 fixtures :projects,
22 22 :users,
23 23 :roles,
24 24 :members,
25 :member_roles,
25 26 :trackers,
26 27 :projects_trackers,
27 28 :enabled_modules,
28 29 :issue_statuses,
29 30 :issues,
30 31 :enumerations,
31 32 :custom_fields,
32 33 :custom_values,
33 34 :custom_fields_trackers
34 35
35 36 # create an issue
36 37 def test_add_issue
37 38 log_user('jsmith', 'jsmith')
38 39 get 'projects/1/issues/new', :tracker_id => '1'
39 40 assert_response :success
40 41 assert_template 'issues/new'
41 42
42 43 post 'projects/1/issues', :tracker_id => "1",
43 44 :issue => { :start_date => "2006-12-26",
44 45 :priority_id => "4",
45 46 :subject => "new test issue",
46 47 :category_id => "",
47 48 :description => "new issue",
48 49 :done_ratio => "0",
49 50 :due_date => "",
50 51 :assigned_to_id => "" },
51 52 :custom_fields => {'2' => 'Value for field 2'}
52 53 # find created issue
53 54 issue = Issue.find_by_subject("new test issue")
54 55 assert_kind_of Issue, issue
55 56
56 57 # check redirection
57 58 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
58 59 follow_redirect!
59 60 assert_equal issue, assigns(:issue)
60 61
61 62 # check issue attributes
62 63 assert_equal 'jsmith', issue.author.login
63 64 assert_equal 1, issue.project.id
64 65 assert_equal 1, issue.status.id
65 66 end
66 67
67 68 def test_update_issue_form
68 69 log_user('jsmith', 'jsmith')
69 70 post 'projects/ecookbook/issues/new', :issue => { :tracker_id => "2"}
70 71 assert_response :success
71 72 assert_tag 'select',
72 73 :attributes => {:name => 'issue[tracker_id]'},
73 74 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}}
74 75 end
75 76
76 77 # add then remove 2 attachments to an issue
77 78 def test_issue_attachments
78 79 log_user('jsmith', 'jsmith')
79 80 set_tmp_attachments_directory
80 81
81 82 put 'issues/1',
82 83 :notes => 'Some notes',
83 84 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'This is an attachment'}}
84 85 assert_redirected_to "/issues/1"
85 86
86 87 # make sure attachment was saved
87 88 attachment = Issue.find(1).attachments.find_by_filename("testfile.txt")
88 89 assert_kind_of Attachment, attachment
89 90 assert_equal Issue.find(1), attachment.container
90 91 assert_equal 'This is an attachment', attachment.description
91 92 # verify the size of the attachment stored in db
92 93 #assert_equal file_data_1.length, attachment.filesize
93 94 # verify that the attachment was written to disk
94 95 assert File.exist?(attachment.diskfile)
95 96
96 97 # remove the attachments
97 98 Issue.find(1).attachments.each(&:destroy)
98 99 assert_equal 0, Issue.find(1).attachments.length
99 100 end
100 101
101 102 def test_other_formats_links_on_index
102 103 get '/projects/ecookbook/issues'
103 104
104 105 %w(Atom PDF CSV).each do |format|
105 106 assert_tag :a, :content => format,
106 107 :attributes => { :href => "/projects/ecookbook/issues.#{format.downcase}",
107 108 :rel => 'nofollow' }
108 109 end
109 110 end
110 111
111 112 def test_other_formats_links_on_index_without_project_id_in_url
112 113 get '/issues', :project_id => 'ecookbook'
113 114
114 115 %w(Atom PDF CSV).each do |format|
115 116 assert_tag :a, :content => format,
116 117 :attributes => { :href => "/projects/ecookbook/issues.#{format.downcase}",
117 118 :rel => 'nofollow' }
118 119 end
119 120 end
120 121
121 122 def test_pagination_links_on_index
122 123 Setting.per_page_options = '2'
123 124 get '/projects/ecookbook/issues'
124 125
125 126 assert_tag :a, :content => '2',
126 127 :attributes => { :href => '/projects/ecookbook/issues?page=2' }
127 128
128 129 end
129 130
130 131 def test_pagination_links_on_index_without_project_id_in_url
131 132 Setting.per_page_options = '2'
132 133 get '/issues', :project_id => 'ecookbook'
133 134
134 135 assert_tag :a, :content => '2',
135 136 :attributes => { :href => '/projects/ecookbook/issues?page=2' }
136 137
137 138 end
138 139
139 140 def test_issue_with_user_custom_field
140 141 @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all)
141 142 Role.anonymous.add_permission! :add_issues, :edit_issues
142 143 users = Project.find(1).users
143 144 tester = users.first
144 145
145 146 # Issue form
146 147 get '/projects/ecookbook/issues/new'
147 148 assert_response :success
148 149 assert_tag :select,
149 150 :attributes => {:name => "issue[custom_field_values][#{@field.id}]"},
150 151 :children => {:count => (users.size + 1)}, # +1 for blank value
151 152 :child => {
152 153 :tag => 'option',
153 154 :attributes => {:value => tester.id.to_s},
154 155 :content => tester.name
155 156 }
156 157
157 158 # Create issue
158 159 assert_difference 'Issue.count' do
159 160 post '/projects/ecookbook/issues',
160 161 :issue => {
161 162 :tracker_id => '1',
162 163 :priority_id => '4',
163 164 :subject => 'Issue with user custom field',
164 165 :custom_field_values => {@field.id.to_s => users.first.id.to_s}
165 166 }
166 167 end
167 168 issue = Issue.first(:order => 'id DESC')
168 169 assert_response 302
169 170
170 171 # Issue view
171 172 follow_redirect!
172 173 assert_tag :th,
173 174 :content => /Tester/,
174 175 :sibling => {
175 176 :tag => 'td',
176 177 :content => tester.name
177 178 }
178 179 assert_tag :select,
179 180 :attributes => {:name => "issue[custom_field_values][#{@field.id}]"},
180 181 :children => {:count => (users.size + 1)}, # +1 for blank value
181 182 :child => {
182 183 :tag => 'option',
183 184 :attributes => {:value => tester.id.to_s, :selected => 'selected'},
184 185 :content => tester.name
185 186 }
186 187
187 188 # Update issue
188 189 new_tester = users[1]
189 190 assert_difference 'Journal.count' do
190 191 put "/issues/#{issue.id}",
191 192 :notes => 'Updating custom field',
192 193 :issue => {
193 194 :custom_field_values => {@field.id.to_s => new_tester.id.to_s}
194 195 }
195 196 end
196 197 assert_response 302
197 198
198 199 # Issue view
199 200 follow_redirect!
200 201 assert_tag :content => 'Tester',
201 202 :ancestor => {:tag => 'ul', :attributes => {:class => /details/}},
202 203 :sibling => {
203 204 :content => tester.name,
204 205 :sibling => {
205 206 :content => new_tester.name
206 207 }
207 208 }
208 209 end
209 210
210 211 def test_update_using_invalid_http_verbs
211 212 subject = 'Updated by an invalid http verb'
212 213
213 214 get '/issues/update/1', {:issue => {:subject => subject}}, credentials('jsmith')
214 215 assert_response 404
215 216 assert_not_equal subject, Issue.find(1).subject
216 217
217 218 post '/issues/1', {:issue => {:subject => subject}}, credentials('jsmith')
218 219 assert_response 404
219 220 assert_not_equal subject, Issue.find(1).subject
220 221 end
221 222
222 223 def test_get_watch_should_be_invalid
223 224 assert_no_difference 'Watcher.count' do
224 225 get '/watchers/watch?object_type=issue&object_id=1', {}, credentials('jsmith')
225 226 assert_response 404
226 227 end
227 228 end
228 229 end
General Comments 0
You need to be logged in to leave comments. Login now