##// END OF EJS Templates
Use assert_nil instead of assert_equal....
Jean-Philippe Lang -
r15677:e937cb533efa
parent child
Show More
@@ -1,401 +1,401
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 if ENV["COVERAGE"]
19 19 require 'simplecov'
20 20 require File.expand_path(File.dirname(__FILE__) + "/coverage/html_formatter")
21 21 SimpleCov.formatter = Redmine::Coverage::HtmlFormatter
22 22 SimpleCov.start 'rails'
23 23 end
24 24
25 25 $redmine_test_ldap_server = ENV['REDMINE_TEST_LDAP_SERVER'] || '127.0.0.1'
26 26
27 27 ENV["RAILS_ENV"] = "test"
28 28 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
29 29 require 'rails/test_help'
30 30 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
31 31
32 32 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
33 33 include ObjectHelpers
34 34
35 35 require 'net/ldap'
36 36 require 'mocha/setup'
37 37
38 38 Redmine::SudoMode.disable!
39 39
40 40 class ActionView::TestCase
41 41 helper :application
42 42 include ApplicationHelper
43 43 end
44 44
45 45 class ActiveSupport::TestCase
46 46 include ActionDispatch::TestProcess
47 47
48 48 self.use_transactional_fixtures = true
49 49 self.use_instantiated_fixtures = false
50 50
51 51 def uploaded_test_file(name, mime)
52 52 fixture_file_upload("files/#{name}", mime, true)
53 53 end
54 54
55 55 # Mock out a file
56 56 def self.mock_file
57 57 file = 'a_file.png'
58 58 file.stubs(:size).returns(32)
59 59 file.stubs(:original_filename).returns('a_file.png')
60 60 file.stubs(:content_type).returns('image/png')
61 61 file.stubs(:read).returns(false)
62 62 file
63 63 end
64 64
65 65 def mock_file
66 66 self.class.mock_file
67 67 end
68 68
69 69 def mock_file_with_options(options={})
70 70 file = ''
71 71 file.stubs(:size).returns(32)
72 72 original_filename = options[:original_filename] || nil
73 73 file.stubs(:original_filename).returns(original_filename)
74 74 content_type = options[:content_type] || nil
75 75 file.stubs(:content_type).returns(content_type)
76 76 file.stubs(:read).returns(false)
77 77 file
78 78 end
79 79
80 80 # Use a temporary directory for attachment related tests
81 81 def set_tmp_attachments_directory
82 82 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
83 83 unless File.directory?("#{Rails.root}/tmp/test/attachments")
84 84 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
85 85 end
86 86 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
87 87 end
88 88
89 89 def set_fixtures_attachments_directory
90 90 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
91 91 end
92 92
93 93 def with_settings(options, &block)
94 94 saved_settings = options.keys.inject({}) do |h, k|
95 95 h[k] = case Setting[k]
96 96 when Symbol, false, true, nil
97 97 Setting[k]
98 98 else
99 99 Setting[k].dup
100 100 end
101 101 h
102 102 end
103 103 options.each {|k, v| Setting[k] = v}
104 104 yield
105 105 ensure
106 106 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
107 107 end
108 108
109 109 # Yields the block with user as the current user
110 110 def with_current_user(user, &block)
111 111 saved_user = User.current
112 112 User.current = user
113 113 yield
114 114 ensure
115 115 User.current = saved_user
116 116 end
117 117
118 118 def with_locale(locale, &block)
119 119 saved_localed = ::I18n.locale
120 120 ::I18n.locale = locale
121 121 yield
122 122 ensure
123 123 ::I18n.locale = saved_localed
124 124 end
125 125
126 126 def self.ldap_configured?
127 127 @test_ldap = Net::LDAP.new(:host => $redmine_test_ldap_server, :port => 389)
128 128 return @test_ldap.bind
129 129 rescue Exception => e
130 130 # LDAP is not listening
131 131 return nil
132 132 end
133 133
134 134 def self.convert_installed?
135 135 Redmine::Thumbnail.convert_available?
136 136 end
137 137
138 138 def convert_installed?
139 139 self.class.convert_installed?
140 140 end
141 141
142 142 # Returns the path to the test +vendor+ repository
143 143 def self.repository_path(vendor)
144 144 path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
145 145 # Unlike ruby, JRuby returns Rails.root with backslashes under Windows
146 146 path.tr("\\", "/")
147 147 end
148 148
149 149 # Returns the url of the subversion test repository
150 150 def self.subversion_repository_url
151 151 path = repository_path('subversion')
152 152 path = '/' + path unless path.starts_with?('/')
153 153 "file://#{path}"
154 154 end
155 155
156 156 # Returns true if the +vendor+ test repository is configured
157 157 def self.repository_configured?(vendor)
158 158 File.directory?(repository_path(vendor))
159 159 end
160 160
161 161 def repository_path_hash(arr)
162 162 hs = {}
163 163 hs[:path] = arr.join("/")
164 164 hs[:param] = arr.join("/")
165 165 hs
166 166 end
167 167
168 168 def sqlite?
169 169 ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
170 170 end
171 171
172 172 def mysql?
173 173 ActiveRecord::Base.connection.adapter_name =~ /mysql/i
174 174 end
175 175
176 176 def postgresql?
177 177 ActiveRecord::Base.connection.adapter_name =~ /postgresql/i
178 178 end
179 179
180 180 def quoted_date(date)
181 181 date = Date.parse(date) if date.is_a?(String)
182 182 ActiveRecord::Base.connection.quoted_date(date)
183 183 end
184 184
185 185 # Asserts that a new record for the given class is created
186 186 # and returns it
187 187 def new_record(klass, &block)
188 188 new_records(klass, 1, &block).first
189 189 end
190 190
191 191 # Asserts that count new records for the given class are created
192 192 # and returns them as an array order by object id
193 193 def new_records(klass, count, &block)
194 194 assert_difference "#{klass}.count", count do
195 195 yield
196 196 end
197 197 klass.order(:id => :desc).limit(count).to_a.reverse
198 198 end
199 199
200 200 def assert_save(object)
201 201 saved = object.save
202 202 message = "#{object.class} could not be saved"
203 203 errors = object.errors.full_messages.map {|m| "- #{m}"}
204 204 message << ":\n#{errors.join("\n")}" if errors.any?
205 205 assert_equal true, saved, message
206 206 end
207 207
208 208 def assert_select_error(arg)
209 209 assert_select '#errorExplanation', :text => arg
210 210 end
211 211
212 212 def assert_include(expected, s, message=nil)
213 213 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
214 214 end
215 215
216 216 def assert_not_include(expected, s, message=nil)
217 217 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
218 218 end
219 219
220 220 def assert_select_in(text, *args, &block)
221 221 d = Nokogiri::HTML(CGI::unescapeHTML(String.new(text))).root
222 222 assert_select(d, *args, &block)
223 223 end
224 224
225 225 def assert_select_email(*args, &block)
226 226 email = ActionMailer::Base.deliveries.last
227 227 assert_not_nil email
228 228 html_body = email.parts.detect {|part| part.content_type.include?('text/html')}.try(&:body)
229 229 assert_not_nil html_body
230 230 assert_select_in html_body.encoded, *args, &block
231 231 end
232 232
233 233 def assert_mail_body_match(expected, mail, message=nil)
234 234 if expected.is_a?(String)
235 235 assert_include expected, mail_body(mail), message
236 236 else
237 237 assert_match expected, mail_body(mail), message
238 238 end
239 239 end
240 240
241 241 def assert_mail_body_no_match(expected, mail, message=nil)
242 242 if expected.is_a?(String)
243 243 assert_not_include expected, mail_body(mail), message
244 244 else
245 245 assert_no_match expected, mail_body(mail), message
246 246 end
247 247 end
248 248
249 249 def mail_body(mail)
250 250 mail.parts.first.body.encoded
251 251 end
252 252
253 253 # Returns the lft value for a new root issue
254 254 def new_issue_lft
255 255 1
256 256 end
257 257 end
258 258
259 259 module Redmine
260 260 class RoutingTest < ActionDispatch::IntegrationTest
261 261 def should_route(arg)
262 262 arg = arg.dup
263 263 request = arg.keys.detect {|key| key.is_a?(String)}
264 264 raise ArgumentError unless request
265 265 options = arg.slice!(request)
266 266
267 267 raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/
268 268 method, path = $1.downcase.to_sym, $2
269 269
270 270 raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/
271 271 controller, action = $1, $2
272 272
273 273 assert_routing(
274 274 {:method => method, :path => path},
275 275 options.merge(:controller => controller, :action => action)
276 276 )
277 277 end
278 278 end
279 279
280 280 class HelperTest < ActionView::TestCase
281 281
282 282 end
283 283
284 284 class ControllerTest < ActionController::TestCase
285 285 # Returns the issues that are displayed in the list in the same order
286 286 def issues_in_list
287 287 ids = css_select('tr.issue td.id').map(&:text).map(&:to_i)
288 288 Issue.where(:id => ids).sort_by {|issue| ids.index(issue.id)}
289 289 end
290 290
291 291 # Return the columns that are displayed in the list
292 292 def columns_in_issues_list
293 293 css_select('table.issues thead th:not(.checkbox)').map(&:text)
294 294 end
295 295
296 296 # Verifies that the query filters match the expected filters
297 297 def assert_query_filters(expected_filters)
298 298 response.body =~ /initFilters\(\);\s*((addFilter\(.+\);\s*)*)/
299 299 filter_init = $1.to_s
300 300
301 301 expected_filters.each do |field, operator, values|
302 302 s = "addFilter(#{field.to_json}, #{operator.to_json}, #{Array(values).to_json});"
303 303 assert_include s, filter_init
304 304 end
305 305 assert_equal expected_filters.size, filter_init.scan("addFilter").size, "filters counts don't match"
306 306 end
307 307
308 308 def process(method, path, parameters={}, session={}, flash={})
309 309 if parameters.key?(:params) || parameters.key?(:session)
310 310 raise ArgumentError if session.present?
311 311 super method, path, parameters[:params], parameters[:session], parameters.except(:params, :session)
312 312 else
313 313 super
314 314 end
315 315 end
316 316 end
317 317
318 318 class IntegrationTest < ActionDispatch::IntegrationTest
319 319 def log_user(login, password)
320 320 User.anonymous
321 321 get "/login"
322 assert_equal nil, session[:user_id]
322 assert_nil session[:user_id]
323 323 assert_response :success
324 324
325 325 post "/login", :username => login, :password => password
326 326 assert_equal login, User.find(session[:user_id]).login
327 327 end
328 328
329 329 def credentials(user, password=nil)
330 330 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
331 331 end
332 332 end
333 333
334 334 module ApiTest
335 335 API_FORMATS = %w(json xml).freeze
336 336
337 337 # Base class for API tests
338 338 class Base < Redmine::IntegrationTest
339 339 def setup
340 340 Setting.rest_api_enabled = '1'
341 341 end
342 342
343 343 def teardown
344 344 Setting.rest_api_enabled = '0'
345 345 end
346 346
347 347 # Uploads content using the XML API and returns the attachment token
348 348 def xml_upload(content, credentials)
349 349 upload('xml', content, credentials)
350 350 end
351 351
352 352 # Uploads content using the JSON API and returns the attachment token
353 353 def json_upload(content, credentials)
354 354 upload('json', content, credentials)
355 355 end
356 356
357 357 def upload(format, content, credentials)
358 358 set_tmp_attachments_directory
359 359 assert_difference 'Attachment.count' do
360 360 post "/uploads.#{format}", content, {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials)
361 361 assert_response :created
362 362 end
363 363 data = response_data
364 364 assert_kind_of Hash, data['upload']
365 365 token = data['upload']['token']
366 366 assert_not_nil token
367 367 token
368 368 end
369 369
370 370 # Parses the response body based on its content type
371 371 def response_data
372 372 unless response.content_type.to_s =~ /^application\/(.+)/
373 373 raise "Unexpected response type: #{response.content_type}"
374 374 end
375 375 format = $1
376 376 case format
377 377 when 'xml'
378 378 Hash.from_xml(response.body)
379 379 when 'json'
380 380 ActiveSupport::JSON.decode(response.body)
381 381 else
382 382 raise "Unknown response format: #{format}"
383 383 end
384 384 end
385 385 end
386 386
387 387 class Routing < Redmine::RoutingTest
388 388 def should_route(arg)
389 389 arg = arg.dup
390 390 request = arg.keys.detect {|key| key.is_a?(String)}
391 391 raise ArgumentError unless request
392 392 options = arg.slice!(request)
393 393
394 394 API_FORMATS.each do |format|
395 395 format_request = request.sub /$/, ".#{format}"
396 396 super options.merge(format_request => arg[request], :format => format)
397 397 end
398 398 end
399 399 end
400 400 end
401 401 end
General Comments 0
You need to be logged in to leave comments. Login now