Skip to content
This repository was archived by the owner on Feb 14, 2019. It is now read-only.

Commit 3edb2cb

Browse files
committed
Fix rails 5 tests
1 parent ad2fe5c commit 3edb2cb

File tree

7 files changed

+174
-63
lines changed

7 files changed

+174
-63
lines changed

app/models/ldap_setting.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ def auth_source_ldap=(source)
221221
# Sets attributes from attrs that are safe
222222
# attrs is a Hash with string keys
223223
def safe_attributes=(attrs, user = User.current)
224+
if attrs.respond_to?(:to_unsafe_hash)
225+
attrs = attrs.to_unsafe_hash
226+
end
224227
@attributes.merge!(delete_unsafe_attributes(attrs, user))
225228
end
226229

config/Gemfile.travis

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ group :test do
66
gem 'rack-cache', '= 1.2'
77
gem 'test-unit', '~> 3.0.0'
88
end
9+
if ENV['REDMINE'] == 'master'
10+
gem 'rails-controller-testing', '~> 1.0.2'
11+
end
912
end

test/functional/ldap_settings_controller_test.rb

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ def test_should_get_base_settings_js
4545
end
4646

4747
def test_should_redirect_to_get_edit_on_get_show
48-
get :show, :id => 1
48+
get :show, params: { id: 1 }
4949
assert_redirected_to edit_ldap_setting_path(1)
5050
end
5151

5252
def test_should_get_edit
53-
get :edit, :id => @auth_source.id
53+
get :edit, params: { id: @auth_source.id }
5454
assert_response :success
5555
end
5656

5757
def test_should_get_404
58-
get :edit, :id => 999
58+
get :edit, params: { id: 999 }
5959
assert_response :not_found
6060
end
6161

@@ -65,7 +65,7 @@ def test_should_disable_ldap_setting
6565
assert_equal 'member', @ldap_setting.member_group
6666

6767
# When we do
68-
get :disable, :id => @ldap_setting.id
68+
get :disable, params: { id: @ldap_setting.id }
6969
assert_redirected_to ldap_settings_path
7070
assert_match /success/, flash[:notice]
7171

@@ -81,13 +81,13 @@ def test_should_disable_an_invalid_ldap_setting
8181
assert ldap_setting.active?
8282

8383
# When we do
84-
get :disable, :id => ldap_setting.id
84+
get :disable, params: { id: ldap_setting.id }
8585
assert_redirected_to ldap_settings_path
8686
assert_match /success/, flash[:notice]
8787

8888
# We should have
8989
ldap_setting = LdapSetting.find_by_auth_source_ldap_id(2)
90-
assert_equal nil, ldap_setting.member_group, 'LdapSetting is not the same'
90+
assert_nil ldap_setting.member_group, 'LdapSetting is not the same'
9191
assert !ldap_setting.active?, "LdapSetting must be disabled"
9292
end
9393

@@ -99,7 +99,7 @@ def test_should_enable_ldap_setting
9999
assert_equal 'member', ldap_setting.member_group
100100

101101
# When we do
102-
get :enable, :id => ldap_setting.id
102+
get :enable, params: { id: ldap_setting.id }
103103
assert_redirected_to ldap_settings_path
104104
assert_match /success/, flash[:notice]
105105

@@ -120,7 +120,7 @@ def test_should_not_enable_ldap_setting_with_errors
120120
assert_equal 'member', @ldap_setting.member_group
121121

122122
# When we do
123-
get :enable, :id => @ldap_setting.id
123+
get :enable, params: { id: @ldap_setting.id }
124124
assert_redirected_to ldap_settings_path
125125
assert_match /invalid settings/, flash[:error]
126126

@@ -131,67 +131,76 @@ def test_should_not_enable_ldap_setting_with_errors
131131
end
132132

133133
def test_should_fail_with_error
134-
put :update, :id => @ldap_setting.id, :ldap_setting => {
135-
:auth_source_ldap_id => @auth_source_id,
136-
:active => true,
137-
:groupname => 'cn',
138-
:groups_base_dn => 'groups_base_dn',
139-
:class_group => 'group',
140-
:class_user => nil, # Missing required field
141-
:group_membership => 'on_members',
142-
:groupid => 'groupid',
143-
:nested_groups => '',
144-
:user_groups => 'memberof',
145-
:sync_on_login => '',
146-
:dyngroups => ''
134+
put :update, params: {
135+
id: @ldap_setting.id,
136+
ldap_setting: {
137+
auth_source_ldap_id: @auth_source_id,
138+
active: true,
139+
groupname: 'cn',
140+
groups_base_dn: 'groups_base_dn',
141+
class_group: 'group',
142+
class_user: nil, # Missing required field
143+
group_membership: 'on_members',
144+
groupid: 'groupid',
145+
nested_groups: '',
146+
user_groups: 'memberof',
147+
sync_on_login: '',
148+
dyngroups: ''
149+
}
147150
}
148151
assert assigns(:ldap_setting).errors.added?(:class_user, :blank), 'An error must be reported for :class_user'
149152
assert_response :success
150153
end
151154

152155
def test_should_update_ldap_setting
153-
put :update, :id => @ldap_setting.id, :ldap_setting => {
154-
:auth_source_ldap_id => @auth_source_id,
155-
:active => true,
156-
:account_disabled_test => '',
157-
:account_flags => '',
158-
:attributes_to_sync => '',
159-
:class_group => 'group',
160-
:class_user => 'user',
161-
:create_groups => '',
162-
:create_users => '',
163-
:fixed_group => '',
164-
:group_memberid => '',
165-
:group_membership => 'on_members',
166-
:group_parentid => '',
167-
:group_search_filter => '',
168-
:groupid => 'groupid',
169-
:groupname => 'cn',
170-
:groupname_pattern => '',
171-
:groups_base_dn => 'groups_base_dn',
172-
:member => '',
173-
:member_group => '',
174-
:nested_groups => '',
175-
:parent_group => '',
176-
:required_group => '',
177-
:user_fields_to_sync => [],
178-
:group_fields_to_sync => [],
179-
:user_ldap_attrs => {},
180-
:group_ldap_attrs => {},
181-
:user_groups => 'memberof',
182-
:user_memberid => '',
183-
:sync_on_login => '',
184-
:dyngroups => ''
156+
put :update, params: {
157+
id: @ldap_setting.id,
158+
ldap_setting: {
159+
auth_source_ldap_id: @auth_source_id,
160+
active: true,
161+
account_disabled_test: '',
162+
account_flags: '',
163+
attributes_to_sync: '',
164+
class_group: 'group',
165+
class_user: 'user',
166+
create_groups: '',
167+
create_users: '',
168+
fixed_group: '',
169+
group_memberid: '',
170+
group_membership: 'on_members',
171+
group_parentid: '',
172+
group_search_filter: '',
173+
groupid: 'groupid',
174+
groupname: 'cn',
175+
groupname_pattern: '',
176+
groups_base_dn: 'groups_base_dn',
177+
member: '',
178+
member_group: '',
179+
nested_groups: '',
180+
parent_group: '',
181+
required_group: '',
182+
user_fields_to_sync: [],
183+
group_fields_to_sync: [],
184+
user_ldap_attrs: {},
185+
group_ldap_attrs: {},
186+
user_groups: 'memberof',
187+
user_memberid: '',
188+
sync_on_login: '',
189+
dyngroups: ''
190+
}
185191
}
186192
assert_redirected_to ldap_settings_path
187193
assert assigns(:ldap_setting).valid?
188194
assert_match /success/, flash[:notice]
189195
end
190196

191197
def test_should_test
192-
put :test, :id => @ldap_setting.id, :format => 'text',
193-
:ldap_setting => @ldap_setting.send(:attributes),
194-
:ldap_test => { :test_users => 'example1', :test_groups => 'Therß' }
198+
put :test, params: {
199+
id: @ldap_setting.id,
200+
format: 'text',
201+
ldap_setting: @ldap_setting.send(:attributes),
202+
ldap_test: { test_users: 'example1', test_groups: 'Therß' }
203+
}
195204

196205
assert_response :success
197206
assert_equal 'text/plain', response.content_type
@@ -211,9 +220,12 @@ def test_should_test
211220
def test_should_validate_on_test
212221
@ldap_setting.dyngroups = 'invalid'
213222

214-
put :test, :id => @ldap_setting.id, :format => 'text',
215-
:ldap_setting => @ldap_setting.send(:attributes),
216-
:ldap_test => { :test_users => 'example1', :test_groups => 'Therß' }
223+
put :test, params: {
224+
id: @ldap_setting.id,
225+
format: 'text',
226+
ldap_setting: @ldap_setting.send(:attributes),
227+
ldap_test: { :test_users => 'example1', :test_groups => 'Therß' }
228+
}
217229

218230
assert_response :success
219231
assert_equal 'text/plain', response.content_type

test/test_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ def clear_ldap_cache!
4444

4545
class ActionDispatch::IntegrationTest
4646
self.fixture_path = File.expand_path(File.dirname(__FILE__) + '/fixtures')
47+
end
48+
49+
module ActionController::TestCase::Behavior
50+
def process_patched(action, method, *args)
51+
options = args.extract_options!
52+
if options.present?
53+
params = options.delete(:params)
54+
options = options.merge(params) if params.present?
55+
args << options
56+
end
57+
process_unpatched(action, method, *args)
58+
end
59+
60+
if Rails::VERSION::MAJOR < 5
61+
alias_method :process_unpatched, :process
62+
alias_method :process, :process_patched
63+
end
4764
end

test/ui/base.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Redmine - project management software
2+
# Copyright (C) 2006-2017 Jean-Philippe Lang
3+
#
4+
# This program is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU General Public License
6+
# as published by the Free Software Foundation; either version 2
7+
# of the License, or (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
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
16+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
18+
require File.expand_path('../../test_helper', __FILE__)
19+
require 'capybara/rails'
20+
21+
Capybara.default_driver = :selenium
22+
Capybara.register_driver :selenium do |app|
23+
# Use the following driver definition to test locally using Chrome
24+
# (also requires chromedriver to be in PATH)
25+
# Capybara::Selenium::Driver.new(app, :browser => :chrome)
26+
# Add :switches => %w[--lang=en] to force default browser locale to English
27+
# Default for Selenium remote driver is to connect to local host on port 4444
28+
# This can be change using :url => 'http://localhost:9195' if necessary
29+
# PhantomJS 1.8 now directly supports Webdriver Wire API,
30+
# simply run it with `phantomjs --webdriver 4444`
31+
# Add :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.internet_explorer)
32+
# to run on Selenium Grid Hub with IE
33+
Capybara::Selenium::Driver.new(app, :browser => :remote)
34+
end
35+
36+
# default: 2
37+
Capybara.default_wait_time = 2
38+
39+
module Redmine
40+
module UiTest
41+
# Base class for UI tests
42+
class Base < ActionDispatch::IntegrationTest
43+
include Capybara::DSL
44+
45+
# Stop ActiveRecord from wrapping tests in transactions
46+
# Transactional fixtures do not work with Selenium tests, because Capybara
47+
# uses a separate server thread, which the transactions would be hidden
48+
if defined? self.use_transactional_tests
49+
self.use_transactional_tests = false
50+
else
51+
self.use_transactional_fixtures = false
52+
end
53+
54+
# Should not depend on locale since Redmine displays login page
55+
# using default browser locale which depend on system locale for "real" browsers drivers
56+
def log_user(login, password)
57+
visit '/my/page'
58+
assert_equal '/login', current_path
59+
within('#login-form form') do
60+
fill_in 'username', :with => login
61+
fill_in 'password', :with => password
62+
find('input[name=login]').click
63+
end
64+
assert_equal '/my/page', current_path
65+
end
66+
67+
setup do
68+
# Set the page width higher than 900 to get the full layout with sidebar
69+
page.driver.browser.manage.window.resize_to(1024, 900)
70+
end
71+
72+
teardown do
73+
Capybara.reset_sessions! # Forget the (simulated) browser state
74+
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
75+
end
76+
end
77+
end
78+
end

test/ui/ldap_setting_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
18-
require File.expand_path('../../../../../test/ui/base', __FILE__)
19-
require File.expand_path('../../test_helper', __FILE__)
18+
require File.expand_path('../base', __FILE__)
2019

2120
if RUBY_VERSION >= '2.0.0'
2221
require 'simplecov'

test/ui/login_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with Redmine LDAP Sync. If not, see <http://www.gnu.org/licenses/>.
18-
require File.expand_path('../../../../../test/ui/base', __FILE__)
19-
require File.expand_path('../../test_helper', __FILE__)
18+
require File.expand_path('../base', __FILE__)
2019

2120
if RUBY_VERSION >= '2.0.0'
2221
require 'simplecov'

0 commit comments

Comments
 (0)