From b919e74ce1c20e26c2476c8580fe2e180ba72438 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 9 Jun 2015 00:03:52 -0400 Subject: Fix OAuth::User spec --- spec/lib/gitlab/o_auth/user_spec.rb | 65 +++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/spec/lib/gitlab/o_auth/user_spec.rb b/spec/lib/gitlab/o_auth/user_spec.rb index 2a982e8b107..18c1d8d8b0c 100644 --- a/spec/lib/gitlab/o_auth/user_spec.rb +++ b/spec/lib/gitlab/o_auth/user_spec.rb @@ -24,18 +24,24 @@ describe Gitlab::OAuth::User do end 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 end end describe :save do + def stub_omniauth(messages = {}) + messages.each_pair do |key, val| + allow(Gitlab.config.omniauth).to receive(key).and_return(val) + end + end + let(:provider) { 'twitter' } describe 'signup' do shared_examples "to verify compliance with allow_single_sign_on" do context "with allow_single_sign_on enabled" do - before { Gitlab.config.omniauth.stub allow_single_sign_on: true } + before { stub_omniauth allow_single_sign_on: true } it "creates a user from Omniauth" do oauth_user.save @@ -48,7 +54,7 @@ describe Gitlab::OAuth::User do end context "with allow_single_sign_on disabled (Default)" do - before { Gitlab.config.omniauth.stub allow_single_sign_on: false } + before { stub_omniauth allow_single_sign_on: false } it "throws an error" do expect{ oauth_user.save }.to raise_error StandardError end @@ -56,19 +62,19 @@ describe Gitlab::OAuth::User do end context "with auto_link_ldap_user disabled (default)" do - before { Gitlab.config.omniauth.stub auto_link_ldap_user: false } + before { stub_omniauth auto_link_ldap_user: false } include_examples "to verify compliance with allow_single_sign_on" end context "with auto_link_ldap_user enabled" do - before { Gitlab.config.omniauth.stub auto_link_ldap_user: true } + before { stub_omniauth auto_link_ldap_user: true } context "and a corresponding LDAP person" do before do - ldap_user.stub(:uid) { uid } - ldap_user.stub(:username) { uid } - ldap_user.stub(:email) { ['johndoe@example.com','john2@example.com'] } - ldap_user.stub(:dn) { 'uid=user1,ou=People,dc=example' } + allow(ldap_user).to receive(:uid) { uid } + allow(ldap_user).to receive(:username) { uid } + allow(ldap_user).to receive(:email) { ['johndoe@example.com','john2@example.com'] } + allow(ldap_user).to receive(:dn) { 'uid=user1,ou=People,dc=example' } allow(oauth_user).to receive(:ldap_person).and_return(ldap_user) end @@ -118,11 +124,11 @@ describe Gitlab::OAuth::User do describe 'blocking' do let(:provider) { 'twitter' } - before { Gitlab.config.omniauth.stub allow_single_sign_on: true } + before { stub_omniauth allow_single_sign_on: true } context 'signup with omniauth only' do context 'dont block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: false } + before { stub_omniauth block_auto_created_users: false } it do oauth_user.save @@ -132,7 +138,7 @@ describe Gitlab::OAuth::User do end context 'block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: true } + before { stub_omniauth block_auto_created_users: true } it do oauth_user.save @@ -144,17 +150,17 @@ describe Gitlab::OAuth::User do context 'signup with linked omniauth and LDAP account' do before do - Gitlab.config.omniauth.stub auto_link_ldap_user: true - ldap_user.stub(:uid) { uid } - ldap_user.stub(:username) { uid } - ldap_user.stub(:email) { ['johndoe@example.com','john2@example.com'] } - ldap_user.stub(:dn) { 'uid=user1,ou=People,dc=example' } + stub_omniauth auto_link_ldap_user: true + allow(ldap_user).to receive(:uid) { uid } + allow(ldap_user).to receive(:username) { uid } + allow(ldap_user).to receive(:email) { ['johndoe@example.com','john2@example.com'] } + allow(ldap_user).to receive(:dn) { 'uid=user1,ou=People,dc=example' } allow(oauth_user).to receive(:ldap_person).and_return(ldap_user) end context "and no account for the LDAP user" do context 'dont block on create (LDAP)' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } + before { stub_omniauth block_auto_created_users: false } it do oauth_user.save @@ -164,7 +170,10 @@ describe Gitlab::OAuth::User do end context 'block on create (LDAP)' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } + before do + allow_any_instance_of(Gitlab::LDAP::Config). + to receive_messages(block_auto_created_users: true) + end it do oauth_user.save @@ -178,7 +187,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') } context 'dont block on create (LDAP)' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } + before { stub_omniauth block_auto_created_users: false } it do oauth_user.save @@ -188,7 +197,7 @@ describe Gitlab::OAuth::User do end context 'block on create (LDAP)' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } + before { stub_omniauth block_auto_created_users: true } it do oauth_user.save @@ -207,7 +216,7 @@ describe Gitlab::OAuth::User do end context 'dont block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: false } + before { stub_omniauth block_auto_created_users: false } it do oauth_user.save @@ -217,7 +226,7 @@ describe Gitlab::OAuth::User do end context 'block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: true } + before { stub_omniauth block_auto_created_users: true } it do oauth_user.save @@ -227,7 +236,10 @@ describe Gitlab::OAuth::User do end context 'dont block on create (LDAP)' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } + before do + allow_any_instance_of(Gitlab::LDAP::Config). + to receive_messages(block_auto_created_users: false) + end it do oauth_user.save @@ -237,7 +249,10 @@ describe Gitlab::OAuth::User do end context 'block on create (LDAP)' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } + before do + allow_any_instance_of(Gitlab::LDAP::Config). + to receive_messages(block_auto_created_users: true) + end it do oauth_user.save -- cgit v1.2.1