Commit b7ee1288 authored by Michael Kozono's avatar Michael Kozono

Refactor to make implementation details private

* Expand initialize spec coverage
* Made implementation details private
* Removed tests of now private methods
* Simplified the private methods
parent a790fbd0
...@@ -9,36 +9,36 @@ module EE ...@@ -9,36 +9,36 @@ module EE
def initialize(auth_hash) def initialize(auth_hash)
super super
with_proxy(auth_hash.provider) do |proxy| set_external_with_external_groups
set_external_with_external_groups(proxy)
end
end end
private
# Intended to be called during #initialize, and #save should be called # Intended to be called during #initialize, and #save should be called
# after initialize. # after initialize.
def set_external_with_external_groups(proxy) def set_external_with_external_groups
gl_user.external = in_any_external_group?(proxy) gl_user.external = in_any_external_group?
end end
# Returns true if the User is found in an external group listed in the # Returns true if the User is found in an external group listed in the
# config. # config.
# def in_any_external_group?
# Only checks the LDAP provider where the User was authorized. with_proxy do |proxy|
def in_any_external_group?(proxy) external_groups = proxy.adapter.config.external_groups
external_groups = proxy.adapter.config.external_groups external_groups.any? do |group_cn|
external_groups.any? do |group_cn| in_group?(group_cn, proxy)
in_group?(proxy, group_cn) end
end end
end end
# Returns true if the User is a member of the group. # Returns true if the User is a member of the group.
def in_group?(proxy, group_cn) def in_group?(group_cn, proxy)
member_dns = proxy.dns_for_group_cn(group_cn) member_dns = proxy.dns_for_group_cn(group_cn)
member_dns.include?(auth_hash.uid) member_dns.include?(auth_hash.uid)
end end
def with_proxy(provider, &block) def with_proxy(&block)
::EE::Gitlab::LDAP::Sync::Proxy.open(provider, &block) ::EE::Gitlab::LDAP::Sync::Proxy.open(auth_hash.provider, &block)
end end
end end
end end
......
...@@ -30,91 +30,75 @@ describe Gitlab::LDAP::User do ...@@ -30,91 +30,75 @@ describe Gitlab::LDAP::User do
end end
describe '#initialize' do describe '#initialize' do
context 'when the user is in an external group' do context 'when there is one external group' do
let(:external_groups) { [group_cn] } let(:external_groups) { [group_cn] }
it "sets the user's external flag to true" do context 'when there is another user in the external group' do
expect(gl_user.external).to be_truthy context 'when the user is in the external group' do
end let(:group_member_dns) { ['uid=someone_else,ou=people,dc=example,dc=com', auth_hash.uid] }
end
context 'when the user is not in an external group' do it "sets the user's external flag to true" do
it "sets the user's external flag to false" do expect(gl_user.external).to be_truthy
expect(gl_user.external).to be_falsey end
end end
end
end
describe '#set_external_with_external_groups' do context 'when the user is not in the external group' do
context 'when the LDAP user is in an external group' do let(:group_member_dns) { ['uid=someone_else,ou=people,dc=example,dc=com'] }
let(:external_groups) { [group_cn] }
before do it "sets the user's external flag to false" do
gl_user.update!(external: false) expect(gl_user.external).to be_falsey
end
end
end end
it 'sets the GitLab user external flag to true' do context 'when there are no other users in the external group' do
expect do context 'when the user is in the external group' do
ldap_user.set_external_with_external_groups(fake_proxy) let(:group_member_dns) { [auth_hash.uid] }
end.to change { gl_user.external }.from(false).to(true)
end
end
context 'when the LDAP user is not in an external group' do it "sets the user's external flag to true" do
before do expect(gl_user.external).to be_truthy
gl_user.update!(external: true) end
end end
context 'when the user is not in the external group' do
let(:group_member_dns) { [] }
it 'sets the GitLab user external flag to true' do it "sets the user's external flag to false" do
expect do expect(gl_user.external).to be_falsey
ldap_user.set_external_with_external_groups(fake_proxy) end
end.to change { gl_user.external }.from(true).to(false) end
end end
end end
end
describe '#in_any_external_group?' do context 'when there is more than one external group' do
subject { ldap_user.in_any_external_group?(fake_proxy) } let(:external_groups) { ['bar', group_cn] }
context 'when there is an external group' do before do
let(:external_groups) { [group_cn] } allow(fake_proxy).to receive(:dns_for_group_cn).with('bar').and_return(['uid=someone_else,ou=people,dc=example,dc=com'])
end
context 'when the user is in an external group' do context 'when the user is in an external group' do
it 'returns true' do let(:group_member_dns) { [auth_hash.uid] }
expect(subject).to be_truthy
it "sets the user's external flag to true" do
expect(gl_user.external).to be_truthy
end end
end end
context 'when the user is not in an external group' do context 'when the user is not in an external group' do
let(:group_member_dns) { ['uid=someone_else,ou=people,dc=example,dc=com'] } let(:group_member_dns) { [] }
it 'returns false' do it "sets the user's external flag to false" do
expect(subject).to be_falsey expect(gl_user.external).to be_falsey
end end
end end
end end
context 'when are no external groups' do context 'when there are no external groups' do
it 'returns false' do let(:external_groups) { [] }
expect(subject).to be_falsey
end
end
end
describe '#in_group?' do
subject { ldap_user.in_group?(fake_proxy, group_cn) }
context 'when the LDAP user is in the group' do
it 'returns true' do
expect(subject).to be_truthy
end
end
context 'when the LDAP user is not in the group' do
let(:group_member_dns) { ['uid=someone_else,ou=people,dc=example,dc=com'] }
it 'returns false' do it "sets the user's external flag to false" do
expect(subject).to be_falsey expect(gl_user.external).to be_falsey
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment