@@ -1,292 +1,281 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2014 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. | |
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 | ENV["RAILS_ENV"] = "test" |
|
18 | ENV["RAILS_ENV"] = "test" | |
19 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
|
19 | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | |
20 | require 'rails/test_help' |
|
20 | require 'rails/test_help' | |
21 | require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s |
|
21 | require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s | |
22 |
|
22 | |||
23 | require File.expand_path(File.dirname(__FILE__) + '/object_helpers') |
|
23 | require File.expand_path(File.dirname(__FILE__) + '/object_helpers') | |
24 | include ObjectHelpers |
|
24 | include ObjectHelpers | |
25 |
|
25 | |||
26 | require 'awesome_nested_set/version' |
|
26 | require 'awesome_nested_set/version' | |
27 | require 'net/ldap' |
|
27 | require 'net/ldap' | |
28 |
|
28 | |||
29 | class ActionView::TestCase |
|
29 | class ActionView::TestCase | |
30 | helper :application |
|
30 | helper :application | |
31 | include ApplicationHelper |
|
31 | include ApplicationHelper | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | class ActiveSupport::TestCase |
|
34 | class ActiveSupport::TestCase | |
35 | include ActionDispatch::TestProcess |
|
35 | include ActionDispatch::TestProcess | |
36 |
|
36 | |||
37 | self.use_transactional_fixtures = true |
|
37 | self.use_transactional_fixtures = true | |
38 | self.use_instantiated_fixtures = false |
|
38 | self.use_instantiated_fixtures = false | |
39 |
|
39 | |||
40 | #ESCAPED_CANT = 'can't' |
|
40 | #ESCAPED_CANT = 'can't' | |
41 | #ESCAPED_UCANT = 'Can't' |
|
41 | #ESCAPED_UCANT = 'Can't' | |
42 | # Rails 4.0.2 |
|
42 | # Rails 4.0.2 | |
43 | ESCAPED_CANT = 'can't' |
|
43 | ESCAPED_CANT = 'can't' | |
44 | ESCAPED_UCANT = 'Can't' |
|
44 | ESCAPED_UCANT = 'Can't' | |
45 |
|
45 | |||
46 | def uploaded_test_file(name, mime) |
|
46 | def uploaded_test_file(name, mime) | |
47 | fixture_file_upload("files/#{name}", mime, true) |
|
47 | fixture_file_upload("files/#{name}", mime, true) | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | # Mock out a file |
|
50 | # Mock out a file | |
51 | def self.mock_file |
|
51 | def self.mock_file | |
52 | file = 'a_file.png' |
|
52 | file = 'a_file.png' | |
53 | file.stubs(:size).returns(32) |
|
53 | file.stubs(:size).returns(32) | |
54 | file.stubs(:original_filename).returns('a_file.png') |
|
54 | file.stubs(:original_filename).returns('a_file.png') | |
55 | file.stubs(:content_type).returns('image/png') |
|
55 | file.stubs(:content_type).returns('image/png') | |
56 | file.stubs(:read).returns(false) |
|
56 | file.stubs(:read).returns(false) | |
57 | file |
|
57 | file | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def mock_file |
|
60 | def mock_file | |
61 | self.class.mock_file |
|
61 | self.class.mock_file | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def mock_file_with_options(options={}) |
|
64 | def mock_file_with_options(options={}) | |
65 | file = '' |
|
65 | file = '' | |
66 | file.stubs(:size).returns(32) |
|
66 | file.stubs(:size).returns(32) | |
67 | original_filename = options[:original_filename] || nil |
|
67 | original_filename = options[:original_filename] || nil | |
68 | file.stubs(:original_filename).returns(original_filename) |
|
68 | file.stubs(:original_filename).returns(original_filename) | |
69 | content_type = options[:content_type] || nil |
|
69 | content_type = options[:content_type] || nil | |
70 | file.stubs(:content_type).returns(content_type) |
|
70 | file.stubs(:content_type).returns(content_type) | |
71 | file.stubs(:read).returns(false) |
|
71 | file.stubs(:read).returns(false) | |
72 | file |
|
72 | file | |
73 | end |
|
73 | end | |
74 |
|
74 | |||
75 | # Use a temporary directory for attachment related tests |
|
75 | # Use a temporary directory for attachment related tests | |
76 | def set_tmp_attachments_directory |
|
76 | def set_tmp_attachments_directory | |
77 | Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test") |
|
77 | Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test") | |
78 | unless File.directory?("#{Rails.root}/tmp/test/attachments") |
|
78 | unless File.directory?("#{Rails.root}/tmp/test/attachments") | |
79 | Dir.mkdir "#{Rails.root}/tmp/test/attachments" |
|
79 | Dir.mkdir "#{Rails.root}/tmp/test/attachments" | |
80 | end |
|
80 | end | |
81 | Attachment.storage_path = "#{Rails.root}/tmp/test/attachments" |
|
81 | Attachment.storage_path = "#{Rails.root}/tmp/test/attachments" | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def set_fixtures_attachments_directory |
|
84 | def set_fixtures_attachments_directory | |
85 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" |
|
85 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def with_settings(options, &block) |
|
88 | def with_settings(options, &block) | |
89 | saved_settings = options.keys.inject({}) do |h, k| |
|
89 | saved_settings = options.keys.inject({}) do |h, k| | |
90 | h[k] = case Setting[k] |
|
90 | h[k] = case Setting[k] | |
91 | when Symbol, false, true, nil |
|
91 | when Symbol, false, true, nil | |
92 | Setting[k] |
|
92 | Setting[k] | |
93 | else |
|
93 | else | |
94 | Setting[k].dup |
|
94 | Setting[k].dup | |
95 | end |
|
95 | end | |
96 | h |
|
96 | h | |
97 | end |
|
97 | end | |
98 | options.each {|k, v| Setting[k] = v} |
|
98 | options.each {|k, v| Setting[k] = v} | |
99 | yield |
|
99 | yield | |
100 | ensure |
|
100 | ensure | |
101 | saved_settings.each {|k, v| Setting[k] = v} if saved_settings |
|
101 | saved_settings.each {|k, v| Setting[k] = v} if saved_settings | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | # Yields the block with user as the current user |
|
104 | # Yields the block with user as the current user | |
105 | def with_current_user(user, &block) |
|
105 | def with_current_user(user, &block) | |
106 | saved_user = User.current |
|
106 | saved_user = User.current | |
107 | User.current = user |
|
107 | User.current = user | |
108 | yield |
|
108 | yield | |
109 | ensure |
|
109 | ensure | |
110 | User.current = saved_user |
|
110 | User.current = saved_user | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
113 | def with_locale(locale, &block) |
|
113 | def with_locale(locale, &block) | |
114 | saved_localed = ::I18n.locale |
|
114 | saved_localed = ::I18n.locale | |
115 | ::I18n.locale = locale |
|
115 | ::I18n.locale = locale | |
116 | yield |
|
116 | yield | |
117 | ensure |
|
117 | ensure | |
118 | ::I18n.locale = saved_localed |
|
118 | ::I18n.locale = saved_localed | |
119 | end |
|
119 | end | |
120 |
|
120 | |||
121 | def self.ldap_configured? |
|
121 | def self.ldap_configured? | |
122 | @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) |
|
122 | @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) | |
123 | return @test_ldap.bind |
|
123 | return @test_ldap.bind | |
124 | rescue Exception => e |
|
124 | rescue Exception => e | |
125 | # LDAP is not listening |
|
125 | # LDAP is not listening | |
126 | return nil |
|
126 | return nil | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | def self.convert_installed? |
|
129 | def self.convert_installed? | |
130 | Redmine::Thumbnail.convert_available? |
|
130 | Redmine::Thumbnail.convert_available? | |
131 | end |
|
131 | end | |
132 |
|
132 | |||
133 | # Returns the path to the test +vendor+ repository |
|
133 | # Returns the path to the test +vendor+ repository | |
134 | def self.repository_path(vendor) |
|
134 | def self.repository_path(vendor) | |
135 | path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s |
|
135 | path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s | |
136 | # Unlike ruby, JRuby returns Rails.root with backslashes under Windows |
|
136 | # Unlike ruby, JRuby returns Rails.root with backslashes under Windows | |
137 | path.tr("\\", "/") |
|
137 | path.tr("\\", "/") | |
138 | end |
|
138 | end | |
139 |
|
139 | |||
140 | # Returns the url of the subversion test repository |
|
140 | # Returns the url of the subversion test repository | |
141 | def self.subversion_repository_url |
|
141 | def self.subversion_repository_url | |
142 | path = repository_path('subversion') |
|
142 | path = repository_path('subversion') | |
143 | path = '/' + path unless path.starts_with?('/') |
|
143 | path = '/' + path unless path.starts_with?('/') | |
144 | "file://#{path}" |
|
144 | "file://#{path}" | |
145 | end |
|
145 | end | |
146 |
|
146 | |||
147 | # Returns true if the +vendor+ test repository is configured |
|
147 | # Returns true if the +vendor+ test repository is configured | |
148 | def self.repository_configured?(vendor) |
|
148 | def self.repository_configured?(vendor) | |
149 | File.directory?(repository_path(vendor)) |
|
149 | File.directory?(repository_path(vendor)) | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def repository_path_hash(arr) |
|
152 | def repository_path_hash(arr) | |
153 | hs = {} |
|
153 | hs = {} | |
154 | hs[:path] = arr.join("/") |
|
154 | hs[:path] = arr.join("/") | |
155 | hs[:param] = arr.join("/") |
|
155 | hs[:param] = arr.join("/") | |
156 | hs |
|
156 | hs | |
157 | end |
|
157 | end | |
158 |
|
158 | |||
159 | def assert_save(object) |
|
159 | def assert_save(object) | |
160 | saved = object.save |
|
160 | saved = object.save | |
161 | message = "#{object.class} could not be saved" |
|
161 | message = "#{object.class} could not be saved" | |
162 | errors = object.errors.full_messages.map {|m| "- #{m}"} |
|
162 | errors = object.errors.full_messages.map {|m| "- #{m}"} | |
163 | message << ":\n#{errors.join("\n")}" if errors.any? |
|
163 | message << ":\n#{errors.join("\n")}" if errors.any? | |
164 | assert_equal true, saved, message |
|
164 | assert_equal true, saved, message | |
165 | end |
|
165 | end | |
166 |
|
166 | |||
167 | def assert_select_error(arg) |
|
167 | def assert_select_error(arg) | |
168 | assert_select '#errorExplanation', :text => arg |
|
168 | assert_select '#errorExplanation', :text => arg | |
169 | end |
|
169 | end | |
170 |
|
170 | |||
171 | def assert_include(expected, s, message=nil) |
|
171 | def assert_include(expected, s, message=nil) | |
172 | assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") |
|
172 | assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") | |
173 | end |
|
173 | end | |
174 |
|
174 | |||
175 | def assert_not_include(expected, s, message=nil) |
|
175 | def assert_not_include(expected, s, message=nil) | |
176 | assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"") |
|
176 | assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"") | |
177 | end |
|
177 | end | |
178 |
|
178 | |||
179 | def assert_select_in(text, *args, &block) |
|
179 | def assert_select_in(text, *args, &block) | |
180 | d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root |
|
180 | d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root | |
181 | assert_select(d, *args, &block) |
|
181 | assert_select(d, *args, &block) | |
182 | end |
|
182 | end | |
183 |
|
183 | |||
184 | def assert_mail_body_match(expected, mail, message=nil) |
|
184 | def assert_mail_body_match(expected, mail, message=nil) | |
185 | if expected.is_a?(String) |
|
185 | if expected.is_a?(String) | |
186 | assert_include expected, mail_body(mail), message |
|
186 | assert_include expected, mail_body(mail), message | |
187 | else |
|
187 | else | |
188 | assert_match expected, mail_body(mail), message |
|
188 | assert_match expected, mail_body(mail), message | |
189 | end |
|
189 | end | |
190 | end |
|
190 | end | |
191 |
|
191 | |||
192 | def assert_mail_body_no_match(expected, mail, message=nil) |
|
192 | def assert_mail_body_no_match(expected, mail, message=nil) | |
193 | if expected.is_a?(String) |
|
193 | if expected.is_a?(String) | |
194 | assert_not_include expected, mail_body(mail), message |
|
194 | assert_not_include expected, mail_body(mail), message | |
195 | else |
|
195 | else | |
196 | assert_no_match expected, mail_body(mail), message |
|
196 | assert_no_match expected, mail_body(mail), message | |
197 | end |
|
197 | end | |
198 | end |
|
198 | end | |
199 |
|
199 | |||
200 | def mail_body(mail) |
|
200 | def mail_body(mail) | |
201 | mail.parts.first.body.encoded |
|
201 | mail.parts.first.body.encoded | |
202 | end |
|
202 | end | |
203 |
|
203 | |||
204 | # awesome_nested_set new node lft and rgt value changed this refactor revision. |
|
204 | # awesome_nested_set new node lft and rgt value changed this refactor revision. | |
205 | # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61 |
|
205 | # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61 | |
206 | # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line. |
|
206 | # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line. | |
207 | # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273 |
|
207 | # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273 | |
208 | # It seems correct behavior because of this line comment. |
|
208 | # It seems correct behavior because of this line comment. | |
209 | # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278 |
|
209 | # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278 | |
210 | def new_issue_lft |
|
210 | def new_issue_lft | |
211 | # ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1 |
|
211 | # ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1 | |
212 | Issue.maximum(:rgt) + 1 |
|
212 | Issue.maximum(:rgt) + 1 | |
213 | end |
|
213 | end | |
214 | end |
|
214 | end | |
215 |
|
215 | |||
216 | module Redmine |
|
216 | module Redmine | |
217 | class RoutingTest < ActionDispatch::IntegrationTest |
|
217 | class RoutingTest < ActionDispatch::IntegrationTest | |
218 | def should_route(arg) |
|
218 | def should_route(arg) | |
219 | arg = arg.dup |
|
219 | arg = arg.dup | |
220 | request = arg.keys.detect {|key| key.is_a?(String)} |
|
220 | request = arg.keys.detect {|key| key.is_a?(String)} | |
221 | raise ArgumentError unless request |
|
221 | raise ArgumentError unless request | |
222 | options = arg.slice!(request) |
|
222 | options = arg.slice!(request) | |
223 |
|
223 | |||
224 | raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/ |
|
224 | raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/ | |
225 | method, path = $1.downcase.to_sym, $2 |
|
225 | method, path = $1.downcase.to_sym, $2 | |
226 |
|
226 | |||
227 | raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/ |
|
227 | raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/ | |
228 | controller, action = $1, $2 |
|
228 | controller, action = $1, $2 | |
229 |
|
229 | |||
230 | assert_routing( |
|
230 | assert_routing( | |
231 | {:method => method, :path => path}, |
|
231 | {:method => method, :path => path}, | |
232 | options.merge(:controller => controller, :action => action) |
|
232 | options.merge(:controller => controller, :action => action) | |
233 | ) |
|
233 | ) | |
234 | end |
|
234 | end | |
235 | end |
|
235 | end | |
236 |
|
236 | |||
237 | class IntegrationTest < ActionDispatch::IntegrationTest |
|
237 | class IntegrationTest < ActionDispatch::IntegrationTest | |
238 | def log_user(login, password) |
|
238 | def log_user(login, password) | |
239 | User.anonymous |
|
239 | User.anonymous | |
240 | get "/login" |
|
240 | get "/login" | |
241 | assert_equal nil, session[:user_id] |
|
241 | assert_equal nil, session[:user_id] | |
242 | assert_response :success |
|
242 | assert_response :success | |
243 | assert_template "account/login" |
|
243 | assert_template "account/login" | |
244 | post "/login", :username => login, :password => password |
|
244 | post "/login", :username => login, :password => password | |
245 | assert_equal login, User.find(session[:user_id]).login |
|
245 | assert_equal login, User.find(session[:user_id]).login | |
246 | end |
|
246 | end | |
247 |
|
247 | |||
248 | def credentials(user, password=nil) |
|
248 | def credentials(user, password=nil) | |
249 | {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)} |
|
249 | {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)} | |
250 | end |
|
250 | end | |
251 | end |
|
251 | end | |
252 |
|
252 | |||
253 | module ApiTest |
|
253 | module ApiTest | |
254 | API_FORMATS = %w(json xml).freeze |
|
254 | API_FORMATS = %w(json xml).freeze | |
255 |
|
255 | |||
256 | # Base class for API tests |
|
256 | # Base class for API tests | |
257 | class Base < Redmine::IntegrationTest |
|
257 | class Base < Redmine::IntegrationTest | |
258 | def setup |
|
258 | def setup | |
259 | Setting.rest_api_enabled = '1' |
|
259 | Setting.rest_api_enabled = '1' | |
260 | end |
|
260 | end | |
261 |
|
261 | |||
262 | def teardown |
|
262 | def teardown | |
263 | Setting.rest_api_enabled = '0' |
|
263 | Setting.rest_api_enabled = '0' | |
264 | end |
|
264 | end | |
265 | end |
|
265 | end | |
266 |
|
266 | |||
267 | class Routing < Redmine::RoutingTest |
|
267 | class Routing < Redmine::RoutingTest | |
268 | def should_route(arg) |
|
268 | def should_route(arg) | |
269 | arg = arg.dup |
|
269 | arg = arg.dup | |
270 | request = arg.keys.detect {|key| key.is_a?(String)} |
|
270 | request = arg.keys.detect {|key| key.is_a?(String)} | |
271 | raise ArgumentError unless request |
|
271 | raise ArgumentError unless request | |
272 | options = arg.slice!(request) |
|
272 | options = arg.slice!(request) | |
273 |
|
273 | |||
274 | API_FORMATS.each do |format| |
|
274 | API_FORMATS.each do |format| | |
275 | format_request = request.sub /$/, ".#{format}" |
|
275 | format_request = request.sub /$/, ".#{format}" | |
276 | super options.merge(format_request => arg[request], :format => format) |
|
276 | super options.merge(format_request => arg[request], :format => format) | |
277 | end |
|
277 | end | |
278 | end |
|
278 | end | |
279 | end |
|
279 | end | |
280 | end |
|
280 | end | |
281 | end |
|
281 | end | |
282 |
|
||||
283 | # URL helpers do not work with config.threadsafe! |
|
|||
284 | # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454 |
|
|||
285 | ActionView::TestCase::TestController.instance_eval do |
|
|||
286 | helper Rails.application.routes.url_helpers |
|
|||
287 | end |
|
|||
288 | ActionView::TestCase::TestController.class_eval do |
|
|||
289 | def _routes |
|
|||
290 | Rails.application.routes |
|
|||
291 | end |
|
|||
292 | end |
|
General Comments 0
You need to be logged in to leave comments.
Login now