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