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