Commit e4e01dbf authored by Robert Speicher's avatar Robert Speicher Committed by Dmitriy Zaporozhets

Fix Gitlab::OAuth::User spec

parent 1dd42da8
...@@ -19,23 +19,34 @@ describe Gitlab::OAuth::User do ...@@ -19,23 +19,34 @@ describe Gitlab::OAuth::User do
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') } let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
it "finds an existing user based on uid and provider (facebook)" do it "finds an existing user based on uid and provider (facebook)" do
# FIXME (rspeicher): It's unlikely that this test is actually doing anything
# `auth` is never used and removing it entirely doesn't break the test, so
# what's it doing?
auth = double(info: double(name: 'John'), uid: 'my-uid', provider: 'my-provider') auth = double(info: double(name: 'John'), uid: 'my-uid', provider: 'my-provider')
expect( oauth_user.persisted? ).to be_truthy expect( oauth_user.persisted? ).to be_truthy
end end
it "returns false if use is not found in database" do it "returns false if use is not found in database" do
auth_hash.stub(uid: 'non-existing') allow(auth_hash).to receive(:uid).and_return('non-existing')
expect( oauth_user.persisted? ).to be_falsey expect( oauth_user.persisted? ).to be_falsey
end end
end end
describe :save do describe :save do
def stub_omniauth_config(messages)
allow(Gitlab.config.omniauth).to receive_messages(messages)
end
def stub_ldap_config(messages)
allow(Gitlab::LDAP::Config).to receive_messages(messages)
end
let(:provider) { 'twitter' } let(:provider) { 'twitter' }
describe 'signup' do describe 'signup' do
shared_examples "to verify compliance with allow_single_sign_on" do shared_examples "to verify compliance with allow_single_sign_on" do
context "with allow_single_sign_on enabled" do context "with allow_single_sign_on enabled" do
before { Gitlab.config.omniauth.stub allow_single_sign_on: true } before { stub_omniauth_config(allow_single_sign_on: true) }
it "creates a user from Omniauth" do it "creates a user from Omniauth" do
oauth_user.save oauth_user.save
...@@ -48,7 +59,7 @@ describe Gitlab::OAuth::User do ...@@ -48,7 +59,7 @@ describe Gitlab::OAuth::User do
end end
context "with allow_single_sign_on disabled (Default)" do context "with allow_single_sign_on disabled (Default)" do
before { Gitlab.config.omniauth.stub allow_single_sign_on: false } before { stub_omniauth_config(allow_single_sign_on: false) }
it "throws an error" do it "throws an error" do
expect{ oauth_user.save }.to raise_error StandardError expect{ oauth_user.save }.to raise_error StandardError
end end
...@@ -56,36 +67,36 @@ describe Gitlab::OAuth::User do ...@@ -56,36 +67,36 @@ describe Gitlab::OAuth::User do
end end
context "with auto_link_ldap_user disabled (default)" do context "with auto_link_ldap_user disabled (default)" do
before { Gitlab.config.omniauth.stub auto_link_ldap_user: false } before { stub_omniauth_config(auto_link_ldap_user: false) }
include_examples "to verify compliance with allow_single_sign_on" include_examples "to verify compliance with allow_single_sign_on"
end end
context "with auto_link_ldap_user enabled" do context "with auto_link_ldap_user enabled" do
before { Gitlab.config.omniauth.stub auto_link_ldap_user: true } before { stub_omniauth_config(auto_link_ldap_user: true) }
context "and no LDAP provider defined" do context "and no LDAP provider defined" do
before { allow(Gitlab::LDAP::Config).to receive(:providers).and_return([]) } before { stub_ldap_config(providers: []) }
include_examples "to verify compliance with allow_single_sign_on" include_examples "to verify compliance with allow_single_sign_on"
end end
context "and at least one LDAP provider is defined" do context "and at least one LDAP provider is defined" do
before { allow(Gitlab::LDAP::Config).to receive(:providers).and_return(['ldapmain']) } before { stub_ldap_config(providers: %w(ldapmain)) }
context "and a corresponding LDAP person" do context "and a corresponding LDAP person" do
before do before do
ldap_user.stub(:uid) { uid } allow(ldap_user).to receive(:uid) { uid }
ldap_user.stub(:username) { uid } allow(ldap_user).to receive(:username) { uid }
ldap_user.stub(:email) { ['johndoe@example.com','john2@example.com'] } allow(ldap_user).to receive(:email) { ['johndoe@example.com','john2@example.com'] }
ldap_user.stub(:dn) { 'uid=user1,ou=People,dc=example' } allow(ldap_user).to receive(:dn) { 'uid=user1,ou=People,dc=example' }
allow(Gitlab::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user) allow(Gitlab::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user)
end end
context "and no account for the LDAP user" do context "and no account for the LDAP user" do
it "creates a user with dual LDAP and omniauth identities" do it "creates a user with dual LDAP and omniauth identities" do
oauth_user.save oauth_user.save
expect(gl_user).to be_valid expect(gl_user).to be_valid
expect(gl_user.username).to eql uid expect(gl_user.username).to eql uid
expect(gl_user.email).to eql 'johndoe@example.com' expect(gl_user.email).to eql 'johndoe@example.com'
...@@ -97,12 +108,12 @@ describe Gitlab::OAuth::User do ...@@ -97,12 +108,12 @@ describe Gitlab::OAuth::User do
]) ])
end end
end end
context "and LDAP user has an account already" do context "and LDAP user has an account already" do
let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: 'uid=user1,ou=People,dc=example', provider: 'ldapmain', username: 'john') } let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: 'uid=user1,ou=People,dc=example', provider: 'ldapmain', username: 'john') }
it "adds the omniauth identity to the LDAP account" do it "adds the omniauth identity to the LDAP account" do
oauth_user.save oauth_user.save
expect(gl_user).to be_valid expect(gl_user).to be_valid
expect(gl_user.username).to eql 'john' expect(gl_user.username).to eql 'john'
expect(gl_user.email).to eql 'john@example.com' expect(gl_user.email).to eql 'john@example.com'
...@@ -115,10 +126,10 @@ describe Gitlab::OAuth::User do ...@@ -115,10 +126,10 @@ describe Gitlab::OAuth::User do
end end
end end
end end
context "and no corresponding LDAP person" do context "and no corresponding LDAP person" do
before { allow(Gitlab::LDAP::Person).to receive(:find_by_uid).and_return(nil) } before { allow(Gitlab::LDAP::Person).to receive(:find_by_uid).and_return(nil) }
include_examples "to verify compliance with allow_single_sign_on" include_examples "to verify compliance with allow_single_sign_on"
end end
end end
...@@ -128,11 +139,11 @@ describe Gitlab::OAuth::User do ...@@ -128,11 +139,11 @@ describe Gitlab::OAuth::User do
describe 'blocking' do describe 'blocking' do
let(:provider) { 'twitter' } let(:provider) { 'twitter' }
before { Gitlab.config.omniauth.stub allow_single_sign_on: true } before { stub_omniauth_config(allow_single_sign_on: true) }
context 'signup with omniauth only' do context 'signup with omniauth only' do
context 'dont block on create' do context 'dont block on create' do
before { Gitlab.config.omniauth.stub block_auto_created_users: false } before { stub_omniauth_config(block_auto_created_users: false) }
it do it do
oauth_user.save oauth_user.save
...@@ -142,7 +153,7 @@ describe Gitlab::OAuth::User do ...@@ -142,7 +153,7 @@ describe Gitlab::OAuth::User do
end end
context 'block on create' do context 'block on create' do
before { Gitlab.config.omniauth.stub block_auto_created_users: true } before { stub_omniauth_config(block_auto_created_users: true) }
it do it do
oauth_user.save oauth_user.save
...@@ -154,17 +165,17 @@ describe Gitlab::OAuth::User do ...@@ -154,17 +165,17 @@ describe Gitlab::OAuth::User do
context 'signup with linked omniauth and LDAP account' do context 'signup with linked omniauth and LDAP account' do
before do before do
Gitlab.config.omniauth.stub auto_link_ldap_user: true stub_omniauth_config(auto_link_ldap_user: true)
ldap_user.stub(:uid) { uid } allow(ldap_user).to receive(:uid) { uid }
ldap_user.stub(:username) { uid } allow(ldap_user).to receive(:username) { uid }
ldap_user.stub(:email) { ['johndoe@example.com','john2@example.com'] } allow(ldap_user).to receive(:email) { ['johndoe@example.com','john2@example.com'] }
ldap_user.stub(:dn) { 'uid=user1,ou=People,dc=example' } allow(ldap_user).to receive(:dn) { 'uid=user1,ou=People,dc=example' }
allow(oauth_user).to receive(:ldap_person).and_return(ldap_user) allow(oauth_user).to receive(:ldap_person).and_return(ldap_user)
end end
context "and no account for the LDAP user" do context "and no account for the LDAP user" do
context 'dont block on create (LDAP)' do context 'dont block on create (LDAP)' do
before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: false) }
it do it do
oauth_user.save oauth_user.save
...@@ -174,7 +185,7 @@ describe Gitlab::OAuth::User do ...@@ -174,7 +185,7 @@ describe Gitlab::OAuth::User do
end end
context 'block on create (LDAP)' do context 'block on create (LDAP)' do
before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: true) }
it do it do
oauth_user.save oauth_user.save
...@@ -188,7 +199,7 @@ describe Gitlab::OAuth::User do ...@@ -188,7 +199,7 @@ describe Gitlab::OAuth::User do
let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: 'uid=user1,ou=People,dc=example', provider: 'ldapmain', username: 'john') } let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: 'uid=user1,ou=People,dc=example', provider: 'ldapmain', username: 'john') }
context 'dont block on create (LDAP)' do context 'dont block on create (LDAP)' do
before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: false) }
it do it do
oauth_user.save oauth_user.save
...@@ -198,7 +209,7 @@ describe Gitlab::OAuth::User do ...@@ -198,7 +209,7 @@ describe Gitlab::OAuth::User do
end end
context 'block on create (LDAP)' do context 'block on create (LDAP)' do
before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: true) }
it do it do
oauth_user.save oauth_user.save
...@@ -217,7 +228,7 @@ describe Gitlab::OAuth::User do ...@@ -217,7 +228,7 @@ describe Gitlab::OAuth::User do
end end
context 'dont block on create' do context 'dont block on create' do
before { Gitlab.config.omniauth.stub block_auto_created_users: false } before { stub_omniauth_config(block_auto_created_users: false) }
it do it do
oauth_user.save oauth_user.save
...@@ -227,7 +238,7 @@ describe Gitlab::OAuth::User do ...@@ -227,7 +238,7 @@ describe Gitlab::OAuth::User do
end end
context 'block on create' do context 'block on create' do
before { Gitlab.config.omniauth.stub block_auto_created_users: true } before { stub_omniauth_config(block_auto_created_users: true) }
it do it do
oauth_user.save oauth_user.save
...@@ -237,7 +248,7 @@ describe Gitlab::OAuth::User do ...@@ -237,7 +248,7 @@ describe Gitlab::OAuth::User do
end end
context 'dont block on create (LDAP)' do context 'dont block on create (LDAP)' do
before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: false) }
it do it do
oauth_user.save oauth_user.save
...@@ -247,7 +258,7 @@ describe Gitlab::OAuth::User do ...@@ -247,7 +258,7 @@ describe Gitlab::OAuth::User do
end end
context 'block on create (LDAP)' do context 'block on create (LDAP)' do
before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: true) }
it do it do
oauth_user.save oauth_user.save
......
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