debian-mirror-gitlab/spec/lib/gitlab/ldap/user_spec.rb

154 lines
4.8 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
describe Gitlab::LDAP::User do
let(:ldap_user) { Gitlab::LDAP::User.new(auth_hash) }
let(:gl_user) { ldap_user.gl_user }
let(:info) do
{
name: 'John',
email: 'john@example.com',
nickname: 'john'
}
end
let(:auth_hash) do
2015-09-25 12:07:36 +05:30
OmniAuth::AuthHash.new(uid: 'my-uid', provider: 'ldapmain', info: info)
2015-04-26 12:48:37 +05:30
end
2015-10-24 18:46:33 +05:30
let(:ldap_user_upper_case) { Gitlab::LDAP::User.new(auth_hash_upper_case) }
let(:info_upper_case) do
{
name: 'John',
email: 'John@Example.com', # Email address has upper case chars
nickname: 'john'
}
end
let(:auth_hash_upper_case) do
OmniAuth::AuthHash.new(uid: 'my-uid', provider: 'ldapmain', info: info_upper_case)
end
2015-04-26 12:48:37 +05:30
describe :changed? do
it "marks existing ldap user as changed" do
2015-09-11 14:41:01 +05:30
create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain')
2015-04-26 12:48:37 +05:30
expect(ldap_user.changed?).to be_truthy
end
it "marks existing non-ldap user if the email matches as changed" do
2015-09-11 14:41:01 +05:30
create(:user, email: 'john@example.com')
2015-04-26 12:48:37 +05:30
expect(ldap_user.changed?).to be_truthy
end
it "dont marks existing ldap user as changed" do
2015-09-11 14:41:01 +05:30
create(:omniauth_user, email: 'john@example.com', extern_uid: 'my-uid', provider: 'ldapmain')
2015-04-26 12:48:37 +05:30
expect(ldap_user.changed?).to be_falsey
end
end
describe :find_or_create do
it "finds the user if already existing" do
2015-09-11 14:41:01 +05:30
create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain')
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
expect{ ldap_user.save }.not_to change{ User.count }
2015-04-26 12:48:37 +05:30
end
it "connects to existing non-ldap user if the email matches" do
existing_user = create(:omniauth_user, email: 'john@example.com', provider: "twitter")
2015-09-11 14:41:01 +05:30
expect{ ldap_user.save }.not_to change{ User.count }
2015-04-26 12:48:37 +05:30
existing_user.reload
expect(existing_user.ldap_identity.extern_uid).to eql 'my-uid'
expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
end
2015-09-25 12:07:36 +05:30
it 'connects to existing ldap user if the extern_uid changes' do
existing_user = create(:omniauth_user, email: 'john@example.com', extern_uid: 'old-uid', provider: 'ldapmain')
expect{ ldap_user.save }.not_to change{ User.count }
existing_user.reload
expect(existing_user.ldap_identity.extern_uid).to eql 'my-uid'
expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
2015-10-24 18:46:33 +05:30
expect(existing_user.id).to eql ldap_user.gl_user.id
end
it 'connects to existing ldap user if the extern_uid changes and email address has upper case characters' do
existing_user = create(:omniauth_user, email: 'john@example.com', extern_uid: 'old-uid', provider: 'ldapmain')
expect{ ldap_user_upper_case.save }.not_to change{ User.count }
existing_user.reload
expect(existing_user.ldap_identity.extern_uid).to eql 'my-uid'
expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
2015-09-25 12:07:36 +05:30
expect(existing_user.id).to eql ldap_user.gl_user.id
end
it 'maintains an identity per provider' do
existing_user = create(:omniauth_user, email: 'john@example.com', provider: 'twitter')
expect(existing_user.identities.count).to eql(1)
ldap_user.save
expect(ldap_user.gl_user.identities.count).to eql(2)
# Expect that find_by provider only returns a single instance of an identity and not an Enumerable
expect(ldap_user.gl_user.identities.find_by(provider: 'twitter')).to be_instance_of Identity
expect(ldap_user.gl_user.identities.find_by(provider: auth_hash.provider)).to be_instance_of Identity
end
2015-04-26 12:48:37 +05:30
it "creates a new user if not found" do
expect{ ldap_user.save }.to change{ User.count }.by(1)
end
end
describe 'blocking' do
2015-09-11 14:41:01 +05:30
def configure_block(value)
allow_any_instance_of(Gitlab::LDAP::Config).
to receive(:block_auto_created_users).and_return(value)
end
2015-04-26 12:48:37 +05:30
context 'signup' do
context 'dont block on create' do
2015-09-11 14:41:01 +05:30
before { configure_block(false) }
2015-04-26 12:48:37 +05:30
it do
ldap_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create' do
2015-09-11 14:41:01 +05:30
before { configure_block(true) }
2015-04-26 12:48:37 +05:30
it do
ldap_user.save
expect(gl_user).to be_valid
expect(gl_user).to be_blocked
end
end
end
context 'sign-in' do
before do
ldap_user.save
ldap_user.gl_user.activate
end
context 'dont block on create' do
2015-09-11 14:41:01 +05:30
before { configure_block(false) }
2015-04-26 12:48:37 +05:30
it do
ldap_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create' do
2015-09-11 14:41:01 +05:30
before { configure_block(true) }
2015-04-26 12:48:37 +05:30
it do
ldap_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
end
end
end