Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code/tests to apply almost rubocop rules #60

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move ldap_test_helper to the test root as a unit-test entrypoint
  • Loading branch information
nthachus committed Nov 13, 2019
commit 6584b5aaa0a7fb286de0092ae97b7665bdd44103
47 changes: 24 additions & 23 deletions test/ad_member_services_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'lib/ldap_test_helper'
# frozen_string_literal: true

require 'ldap_test_helper'

class TestADMemberService < MiniTest::Test
include LdapTestHelper
Expand All @@ -10,58 +12,58 @@ def setup
end

def basic_user
@ldap.expect(:search, ad_user_payload, [:filter => ad_name_filter("john")])
@ldap.expect(:search, ad_parent_payload(1), [:base => ad_group_dn, :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, ad_user_payload, [filter: ad_name_filter('john')])
@ldap.expect(:search, ad_parent_payload(1), [base: ad_group_dn, scope: 0, attributes: ['memberof']])
end

def basic_group
@ldap.expect(:search, ad_group_payload, [:filter => ad_group_filter("broze"), :base => @config.group_base])
@ldap.expect(:search, ad_group_payload, [filter: ad_group_filter('broze'), base: @config.group_base])
end

def nest_deep(n)
# add all the expects
1.upto(n-1) do |i|
@ldap.expect(:search, ad_parent_payload(i + 1), [:base => ad_group_dn("bros#{i}"), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, ad_parent_payload(i + 1), [base: ad_group_dn("bros#{i}"), scope: 0, attributes: ['memberof']])
end
# terminate or we loop FOREVER
@ldap.expect(:search, [], [:base => ad_group_dn("bros#{n}"), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, [], [base: ad_group_dn("bros#{n}"), scope: 0, attributes: ['memberof']])
end

def double_nested(n)
# add all the expects
1.upto(n - 1) do |i|
@ldap.expect(:search, ad_double_payload(i + 1), [:base => ad_group_dn("bros#{i}"), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, ad_double_payload(i + 1), [base: ad_group_dn("bros#{i}"), scope: 0, attributes: ['memberof']])
end
# terminate or we loop FOREVER
@ldap.expect(:search, [], [:base => ad_group_dn("bros#{n}"), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, [], [base: ad_group_dn("bros#{n}"), scope: 0, attributes: ['memberof']])
(n - 1).downto(1) do |j|
@ldap.expect(:search, [], [:base => ad_group_dn("broskies#{j + 1}"), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, [], [base: ad_group_dn("broskies#{j + 1}"), scope: 0, attributes: ['memberof']])
end
end

def test_find_user
basic_user
@ldap.expect(:search, [], [:base => ad_group_dn('bros1'), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, [], [base: ad_group_dn('bros1'), scope: 0, attributes: ['memberof']])
@adms.ldap = @ldap
assert_equal(%w(group bros1), @adms.find_user_groups("john"))
assert_equal(%w[group bros1], @adms.find_user_groups('john'))
@ldap.verify
end

def test_nested_groups
basic_user
# basic user is memberof 'group'... and 'group' is memberof 'bros1'
# now make 'bros1' be memberof 'group' again
@ldap.expect(:search, ad_user_payload, [:base => ad_group_dn('bros1'), :scope => 0, :attributes => ['memberof']])
@ldap.expect(:search, ad_user_payload, [base: ad_group_dn('bros1'), scope: 0, attributes: ['memberof']])
@adms.ldap = @ldap
assert_equal(%w(group bros1), @adms.find_user_groups("john"))
assert_equal(%w[group bros1], @adms.find_user_groups('john'))
@ldap.verify
end

def test_missing_user
@ldap.expect(:search, nil, [:filter => ad_name_filter("john")])
@ldap.expect(:search, nil, [filter: ad_name_filter('john')])
@adms.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException) do
@adms.find_user_groups("john").data
@adms.find_user_groups('john').data
end
@ldap.verify
end
Expand All @@ -87,10 +89,10 @@ def test_nil_payload
end

def test_empty_user
@ldap.expect(:search, [], [:filter => ad_name_filter("john")])
@ldap.expect(:search, [], [filter: ad_name_filter('john')])
@adms.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException) do
@adms.find_user_groups("john").data
@adms.find_user_groups('john').data
end
@ldap.verify
end
Expand All @@ -102,7 +104,7 @@ def test_find_good_user
end

def test_find_missing_user
@ldap.expect(:search, nil, [:filter => ad_name_filter("john")])
@ldap.expect(:search, nil, [filter: ad_name_filter('john')])
@adms.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException) do
@adms.find_user('john')
Expand All @@ -116,15 +118,15 @@ def test_find_good_group
end

def test_find_missing_group
@ldap.expect(:search, nil, [:filter => ad_group_filter("broze"), :base => @config.group_base])
@ldap.expect(:search, nil, [filter: ad_group_filter('broze'), base: @config.group_base])
@adms.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::MemberService::GIDNotFoundException) do
@adms.find_group('broze')
end
end

def test_find_by_dn
@ldap.expect(:search, [:result], [:filter => Net::LDAP::Filter.eq('cn', 'Foo Bar'), :base => 'dc=example,dc=com'])
@ldap.expect(:search, [:result], [filter: Net::LDAP::Filter.eq('cn', 'Foo Bar'), base: 'dc=example,dc=com'])
@adms.ldap = @ldap
assert_equal([:result], @adms.find_by_dn('cn=Foo Bar,dc=example,dc=com'))
@ldap.verify
Expand All @@ -135,14 +137,14 @@ def test_find_by_dn_comma_in_cn
# returned by the server in answer to a group membership query with
# backslashes before the commas in the CNs. Such escaped commas should not
# be used when splitting the DN.
@ldap.expect(:search, [:result], [:filter => Net::LDAP::Filter.eq('cn', 'Bar, Foo'), :base => 'dc=example,dc=com'])
@ldap.expect(:search, [:result], [filter: Net::LDAP::Filter.eq('cn', 'Bar, Foo'), base: 'dc=example,dc=com'])
@adms.ldap = @ldap
assert_equal([:result], @adms.find_by_dn('cn=Bar\, Foo,dc=example,dc=com'))
@ldap.verify
end

def test_find_by_dn_missing_entry
@ldap.expect(:search, nil, [:filter => Net::LDAP::Filter.eq('cn', 'Foo Bar'), :base => 'dc=example,dc=com'])
@ldap.expect(:search, nil, [filter: Net::LDAP::Filter.eq('cn', 'Foo Bar'), base: 'dc=example,dc=com'])
@adms.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException) do
@adms.find_by_dn('cn=Foo Bar,dc=example,dc=com')
Expand All @@ -160,5 +162,4 @@ def test_get_login_from_entry_missing_attr
entry = Net::LDAP::Entry.new('Example User')
assert_nil(@adms.get_login_from_entry(entry))
end

end
43 changes: 22 additions & 21 deletions test/ad_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'lib/ldap_test_helper'
# frozen_string_literal: true

require 'ldap_test_helper'

class TestAD < MiniTest::Test
include LdapTestHelper
Expand All @@ -10,13 +12,13 @@ def setup

# default setup for service bind users
def service_bind
@ldap.expect(:auth, nil, %w(service pass))
@ldap.expect(:auth, nil, %w[service pass])
super
end

def test_good_bind
# no expectation on the service account
@ldap.expect(:auth, nil, ['EXAMPLE\\internet', "password"])
@ldap.expect(:auth, nil, ['EXAMPLE\\internet', 'password'])
@ldap.expect(:bind, true)
@ad.ldap = @ldap
assert_equal(@ad.bind?('EXAMPLE\\internet', 'password'), true)
Expand All @@ -25,7 +27,7 @@ def test_good_bind

def test_good_bind_with_dn
# no expectation on the service account
@ldap.expect(:auth, nil, [ad_user_dn('Internet User'), "password"])
@ldap.expect(:auth, nil, [ad_user_dn('Internet User'), 'password'])
@ldap.expect(:bind, true)
@ad.ldap = @ldap
assert_equal(@ad.bind?(ad_user_dn('Internet User'), 'password'), true)
Expand All @@ -37,33 +39,33 @@ def test_good_bind_with_account_name
@md = MiniTest::Mock.new
user_result = MiniTest::Mock.new
user_result.expect(:dn, ad_user_dn('Internet User'))
@md.expect(:find_user, [user_result], %w(internet))
@md.expect(:find_user, [user_result], %w[internet])
@ad.member_service = @md
service_bind
@ldap.expect(:auth, nil, [ad_user_dn('Internet User'), "password"])
@ldap.expect(:auth, nil, [ad_user_dn('Internet User'), 'password'])
@ldap.expect(:bind, true)
assert_equal(@ad.bind?('internet', 'password'), true)
@ldap.verify
end

def test_bad_bind
@ldap.expect(:auth, nil, %w(EXAMPLE\\internet password))
@ldap.expect(:auth, nil, %w[EXAMPLE\\internet password])
@ldap.expect(:bind, false)
@ad.ldap = @ldap
assert_equal(@ad.bind?("EXAMPLE\\internet", "password"), false)
assert_equal(@ad.bind?('EXAMPLE\\internet', 'password'), false)
@ldap.verify
end

def test_groups
service_bind
basic_user
assert_equal(@ad.groups_for_uid('john'), %w(bros))
assert_equal(@ad.groups_for_uid('john'), %w[bros])
end

def test_bad_user
service_bind
md = MiniTest::Mock.new
md.expect(:find_user_groups, nil, %w(john))
md.expect(:find_user_groups, nil, %w[john])
def md.find_user_groups(*args)
raise LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException
end
Expand All @@ -72,7 +74,7 @@ def md.find_user_groups(*args)
end

def test_bad_service_user
@ldap.expect(:auth, nil, %w(service pass))
@ldap.expect(:auth, nil, %w[service pass])
@ldap.expect(:bind, false)
@ad.ldap = @ldap
assert_raises(LdapFluff::ActiveDirectory::UnauthenticatedException) do
Expand All @@ -83,31 +85,31 @@ def test_bad_service_user
def test_is_in_groups
service_bind
basic_user
assert_equal(@ad.is_in_groups("john", %w(bros), false), true)
assert_equal(@ad.is_in_groups('john', %w[bros], false), true)
end

def test_is_some_groups
service_bind
basic_user
assert_equal(@ad.is_in_groups("john", %w(bros buds), false), true)
assert_equal(@ad.is_in_groups('john', %w[bros buds], false), true)
end

def test_isnt_in_all_groups
service_bind
basic_user
assert_equal(@ad.is_in_groups("john", %w(bros buds), true), false)
assert_equal(@ad.is_in_groups('john', %w[bros buds], true), false)
end

def test_isnt_in_groups
service_bind
basic_user
assert_equal(@ad.is_in_groups("john", %w(broskies), false), false)
assert_equal(@ad.is_in_groups('john', %w[broskies], false), false)
end

def test_group_subset
service_bind
bigtime_user
assert_equal(@ad.is_in_groups("john", %w(broskies), true), true)
assert_equal(@ad.is_in_groups('john', %w[broskies], true), true)
end

def test_subgroups_in_groups_are_ignored
Expand All @@ -125,15 +127,15 @@ def md.find_by_dn(dn)

def test_user_exists
md = MiniTest::Mock.new
md.expect(:find_user, 'notnilluser', %w(john))
md.expect(:find_user, 'notnilluser', %w[john])
@ad.member_service = md
service_bind
assert(@ad.user_exists?('john'))
end

def test_missing_user
md = MiniTest::Mock.new
md.expect(:find_user, nil, %w(john))
md.expect(:find_user, nil, %w[john])
def md.find_user(uid)
raise LdapFluff::ActiveDirectory::MemberService::UIDNotFoundException
end
Expand All @@ -144,15 +146,15 @@ def md.find_user(uid)

def test_group_exists
md = MiniTest::Mock.new
md.expect(:find_group, 'notnillgroup', %w(broskies))
md.expect(:find_group, 'notnillgroup', %w[broskies])
@ad.member_service = md
service_bind
assert(@ad.group_exists?('broskies'))
end

def test_missing_group
md = MiniTest::Mock.new
md.expect(:find_group, nil, %w(broskies))
md.expect(:find_group, nil, %w[broskies])
def md.find_group(uid)
raise LdapFluff::ActiveDirectory::MemberService::GIDNotFoundException
end
Expand Down Expand Up @@ -208,5 +210,4 @@ def test_find_users_with_empty_nested_group
assert_equal @ad.users_for_gid('foremaners'), ['testuser']
md.verify
end

end
14 changes: 8 additions & 6 deletions test/config_test.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
require 'lib/ldap_test_helper'
# frozen_string_literal: true

require 'ldap_test_helper'

class ConfigTest < MiniTest::Test
include LdapTestHelper

def test_unsupported_type
assert_raises(LdapFluff::Config::ConfigError) { LdapFluff.new(config_hash.update :server_type => 'inactive_directory') }
assert_raises(LdapFluff::Config::ConfigError) { LdapFluff.new(config_hash.update server_type: 'inactive_directory') }
end

def test_load_posix
ldap = LdapFluff.new(config_hash.update :server_type => 'posix')
ldap = LdapFluff.new(config_hash.update server_type: 'posix')
assert_instance_of LdapFluff::Posix, ldap.ldap
end

def test_load_ad
ldap = LdapFluff.new(config_hash.update :server_type => 'active_directory')
ldap = LdapFluff.new(config_hash.update server_type: 'active_directory')
assert_instance_of LdapFluff::ActiveDirectory, ldap.ldap
end

def test_load_free_ipa
ldap = LdapFluff.new(config_hash.update :server_type => 'free_ipa')
ldap = LdapFluff.new(config_hash.update server_type: 'free_ipa')
assert_instance_of LdapFluff::FreeIPA, ldap.ldap
end

def test_instrumentation_service
is = Object.new
net_ldap = LdapFluff.new(config_hash.update :instrumentation_service => is).ldap.ldap
net_ldap = LdapFluff.new(config_hash.update instrumentation_service: is).ldap.ldap
assert_equal is, net_ldap.send(:instrumentation_service)
end
end
Loading