diff options
author | Robert Speicher <rspeicher@gmail.com> | 2015-05-23 17:56:29 -0400 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2015-06-14 23:06:17 -0400 |
commit | b9352d9236f783d73b8a33831d8b6b399bbc1e0f (patch) | |
tree | cd66a0fe2a0f5183f66c9a57b08faeab46c6f80b | |
parent | 7b7ba11218dab0a75b5715ff697ca020bb12f5d8 (diff) | |
download | gitlab-ce-b9352d9236f783d73b8a33831d8b6b399bbc1e0f.tar.gz |
Update mock/expect syntax for spec/lib
-rw-r--r-- | spec/lib/extracts_path_spec.rb | 7 | ||||
-rw-r--r-- | spec/lib/gitlab/auth_spec.rb | 4 | ||||
-rw-r--r-- | spec/lib/gitlab/backend/shell_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ldap/access_spec.rb | 30 | ||||
-rw-r--r-- | spec/lib/gitlab/ldap/adapter_spec.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/ldap/authentication_spec.rb | 53 | ||||
-rw-r--r-- | spec/lib/gitlab/ldap/user_spec.rb | 22 | ||||
-rw-r--r-- | spec/lib/gitlab/upgrader_spec.rb | 4 |
8 files changed, 84 insertions, 53 deletions
diff --git a/spec/lib/extracts_path_spec.rb b/spec/lib/extracts_path_spec.rb index 05bcebaa3a2..8e05e44defc 100644 --- a/spec/lib/extracts_path_spec.rb +++ b/spec/lib/extracts_path_spec.rb @@ -9,8 +9,11 @@ describe ExtractsPath do before do @project = project - project.stub(repository: double(ref_names: ['master', 'foo/bar/baz', 'v1.0.0', 'v2.0.0'])) - project.stub(path_with_namespace: 'gitlab/gitlab-ci') + + repo = double(ref_names: ['master', 'foo/bar/baz', 'v1.0.0', 'v2.0.0']) + allow(project).to receive(:repository).and_return(repo) + allow(project).to receive(:path_with_namespace). + and_return('gitlab/gitlab-ci') end describe '#assign_ref' do diff --git a/spec/lib/gitlab/auth_spec.rb b/spec/lib/gitlab/auth_spec.rb index 95fc7e16a11..81850d42c90 100644 --- a/spec/lib/gitlab/auth_spec.rb +++ b/spec/lib/gitlab/auth_spec.rb @@ -36,7 +36,9 @@ describe Gitlab::Auth do end context "with ldap enabled" do - before { Gitlab::LDAP::Config.stub(enabled?: true) } + before do + allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true) + end it "tries to autheticate with db before ldap" do expect(Gitlab::LDAP::Authentication).not_to receive(:login) diff --git a/spec/lib/gitlab/backend/shell_spec.rb b/spec/lib/gitlab/backend/shell_spec.rb index 27279465c1a..b6d04330599 100644 --- a/spec/lib/gitlab/backend/shell_spec.rb +++ b/spec/lib/gitlab/backend/shell_spec.rb @@ -5,7 +5,7 @@ describe Gitlab::Shell do let(:gitlab_shell) { Gitlab::Shell.new } before do - Project.stub(find: project) + allow(Project).to receive(:find).and_return(project) end it { is_expected.to respond_to :add_key } diff --git a/spec/lib/gitlab/ldap/access_spec.rb b/spec/lib/gitlab/ldap/access_spec.rb index 038ac7e0d75..c38f212b405 100644 --- a/spec/lib/gitlab/ldap/access_spec.rb +++ b/spec/lib/gitlab/ldap/access_spec.rb @@ -8,16 +8,24 @@ describe Gitlab::LDAP::Access do subject { access.allowed? } context 'when the user cannot be found' do - before { Gitlab::LDAP::Person.stub(find_by_dn: nil) } + before do + allow(Gitlab::LDAP::Person).to receive(:find_by_dn).and_return(nil) + end it { is_expected.to be_falsey } end context 'when the user is found' do - before { Gitlab::LDAP::Person.stub(find_by_dn: :ldap_user) } + before do + allow(Gitlab::LDAP::Person). + to receive(:find_by_dn).and_return(:ldap_user) + end context 'and the user is disabled via active directory' do - before { Gitlab::LDAP::Person.stub(disabled_via_active_directory?: true) } + before do + allow(Gitlab::LDAP::Person). + to receive(:disabled_via_active_directory?).and_return(true) + end it { is_expected.to be_falsey } @@ -30,8 +38,9 @@ describe Gitlab::LDAP::Access do context 'and has no disabled flag in active diretory' do before do user.block - - Gitlab::LDAP::Person.stub(disabled_via_active_directory?: false) + + allow(Gitlab::LDAP::Person). + to receive(:disabled_via_active_directory?).and_return(false) end it { is_expected.to be_truthy } @@ -39,7 +48,8 @@ describe Gitlab::LDAP::Access do context 'when auto-created users are blocked' do before do - Gitlab::LDAP::Config.any_instance.stub(block_auto_created_users: true) + allow_any_instance_of(Gitlab::LDAP::Config). + to receive(:block_auto_created_users).and_return(true) end it "does not unblock user in GitLab" do @@ -51,7 +61,8 @@ describe Gitlab::LDAP::Access do context "when auto-created users are not blocked" do before do - Gitlab::LDAP::Config.any_instance.stub(block_auto_created_users: false) + allow_any_instance_of(Gitlab::LDAP::Config). + to receive(:block_auto_created_users).and_return(false) end it "should unblock user in GitLab" do @@ -63,8 +74,9 @@ describe Gitlab::LDAP::Access do context 'without ActiveDirectory enabled' do before do - Gitlab::LDAP::Config.stub(enabled?: true) - Gitlab::LDAP::Config.any_instance.stub(active_directory: false) + allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true) + allow_any_instance_of(Gitlab::LDAP::Config). + to receive(:active_directory).and_return(false) end it { is_expected.to be_truthy } diff --git a/spec/lib/gitlab/ldap/adapter_spec.rb b/spec/lib/gitlab/ldap/adapter_spec.rb index b609e4b38f2..38076602df9 100644 --- a/spec/lib/gitlab/ldap/adapter_spec.rb +++ b/spec/lib/gitlab/ldap/adapter_spec.rb @@ -3,27 +3,32 @@ require 'spec_helper' describe Gitlab::LDAP::Adapter do let(:adapter) { Gitlab::LDAP::Adapter.new 'ldapmain' } - describe :dn_matches_filter? do + describe '#dn_matches_filter?' do let(:ldap) { double(:ldap) } subject { adapter.dn_matches_filter?(:dn, :filter) } - before { adapter.stub(ldap: ldap) } + before { allow(adapter).to receive(:ldap).and_return(ldap) } context "when the search is successful" do context "and the result is non-empty" do - before { ldap.stub(search: [:foo]) } + before { allow(ldap).to receive(:search).and_return([:foo]) } it { is_expected.to be_truthy } end context "and the result is empty" do - before { ldap.stub(search: []) } + before { allow(ldap).to receive(:search).and_return([]) } it { is_expected.to be_falsey } end end context "when the search encounters an error" do - before { ldap.stub(search: nil, get_operation_result: double(code: 1, message: 'some error')) } + before do + allow(ldap).to receive_messages( + search: nil, + get_operation_result: double(code: 1, message: 'some error') + ) + end it { is_expected.to be_falsey } end diff --git a/spec/lib/gitlab/ldap/authentication_spec.rb b/spec/lib/gitlab/ldap/authentication_spec.rb index 8afc2b21f46..6e3de914a45 100644 --- a/spec/lib/gitlab/ldap/authentication_spec.rb +++ b/spec/lib/gitlab/ldap/authentication_spec.rb @@ -1,53 +1,58 @@ require 'spec_helper' describe Gitlab::LDAP::Authentication do - let(:klass) { Gitlab::LDAP::Authentication } - let(:user) { create(:omniauth_user, extern_uid: dn) } - let(:dn) { 'uid=john,ou=people,dc=example,dc=com' } - let(:login) { 'john' } + let(:user) { create(:omniauth_user, extern_uid: dn) } + let(:dn) { 'uid=john,ou=people,dc=example,dc=com' } + let(:login) { 'john' } let(:password) { 'password' } - describe :login do - let(:adapter) { double :adapter } + describe 'login' do before do - Gitlab::LDAP::Config.stub(enabled?: true) + allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true) end it "finds the user if authentication is successful" do - user + expect(user).not_to be_nil + # try only to fake the LDAP call - klass.any_instance.stub(adapter: double(:adapter, - bind_as: double(:ldap_user, dn: dn) - )) - expect(klass.login(login, password)).to be_truthy + adapter = double('adapter', dn: dn).as_null_object + allow_any_instance_of(described_class). + to receive(:adapter).and_return(adapter) + + expect(described_class.login(login, password)).to be_truthy end it "is false if the user does not exist" do # try only to fake the LDAP call - klass.any_instance.stub(adapter: double(:adapter, - bind_as: double(:ldap_user, dn: dn) - )) - expect(klass.login(login, password)).to be_falsey + adapter = double('adapter', dn: dn).as_null_object + allow_any_instance_of(described_class). + to receive(:adapter).and_return(adapter) + + expect(described_class.login(login, password)).to be_falsey end it "is false if authentication fails" do - user + expect(user).not_to be_nil + # try only to fake the LDAP call - klass.any_instance.stub(adapter: double(:adapter, bind_as: nil)) - expect(klass.login(login, password)).to be_falsey + adapter = double('adapter', bind_as: nil).as_null_object + allow_any_instance_of(described_class). + to receive(:adapter).and_return(adapter) + + expect(described_class.login(login, password)).to be_falsey end it "fails if ldap is disabled" do - Gitlab::LDAP::Config.stub(enabled?: false) - expect(klass.login(login, password)).to be_falsey + allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(false) + expect(described_class.login(login, password)).to be_falsey end it "fails if no login is supplied" do - expect(klass.login('', password)).to be_falsey + expect(described_class.login('', password)).to be_falsey end it "fails if no password is supplied" do - expect(klass.login(login, '')).to be_falsey + expect(described_class.login(login, '')).to be_falsey end end -end
\ No newline at end of file +end diff --git a/spec/lib/gitlab/ldap/user_spec.rb b/spec/lib/gitlab/ldap/user_spec.rb index 42015c28c81..8689b8c7edd 100644 --- a/spec/lib/gitlab/ldap/user_spec.rb +++ b/spec/lib/gitlab/ldap/user_spec.rb @@ -16,24 +16,24 @@ describe Gitlab::LDAP::User do describe :changed? do it "marks existing ldap user as changed" do - existing_user = create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain') + create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain') expect(ldap_user.changed?).to be_truthy end it "marks existing non-ldap user if the email matches as changed" do - existing_user = create(:user, email: 'john@example.com') + create(:user, email: 'john@example.com') expect(ldap_user.changed?).to be_truthy end it "dont marks existing ldap user as changed" do - existing_user = create(:omniauth_user, email: 'john@example.com', extern_uid: 'my-uid', provider: 'ldapmain') + create(:omniauth_user, email: 'john@example.com', extern_uid: 'my-uid', provider: 'ldapmain') expect(ldap_user.changed?).to be_falsey end end describe :find_or_create do it "finds the user if already existing" do - existing_user = create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain') + create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain') expect{ ldap_user.save }.to_not change{ User.count } end @@ -52,11 +52,15 @@ describe Gitlab::LDAP::User do end end - describe 'blocking' do + def configure_block(value) + allow_any_instance_of(Gitlab::LDAP::Config). + to receive(:block_auto_created_users).and_return(value) + end + context 'signup' do context 'dont block on create' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } + before { configure_block(false) } it do ldap_user.save @@ -66,7 +70,7 @@ describe Gitlab::LDAP::User do end context 'block on create' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } + before { configure_block(true) } it do ldap_user.save @@ -83,7 +87,7 @@ describe Gitlab::LDAP::User do end context 'dont block on create' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: false } + before { configure_block(false) } it do ldap_user.save @@ -93,7 +97,7 @@ describe Gitlab::LDAP::User do end context 'block on create' do - before { Gitlab::LDAP::Config.any_instance.stub block_auto_created_users: true } + before { configure_block(true) } it do ldap_user.save diff --git a/spec/lib/gitlab/upgrader_spec.rb b/spec/lib/gitlab/upgrader_spec.rb index baa4bd0f28f..8df84665e16 100644 --- a/spec/lib/gitlab/upgrader_spec.rb +++ b/spec/lib/gitlab/upgrader_spec.rb @@ -10,14 +10,14 @@ describe Gitlab::Upgrader do describe 'latest_version?' do it 'should be true if newest version' do - upgrader.stub(latest_version_raw: current_version) + allow(upgrader).to receive(:latest_version_raw).and_return(current_version) expect(upgrader.latest_version?).to be_truthy end end describe 'latest_version_raw' do it 'should be latest version for GitLab 5' do - upgrader.stub(current_version_raw: "5.3.0") + allow(upgrader).to receive(:current_version_raw).and_return("5.3.0") expect(upgrader.latest_version_raw).to eq("v5.4.2") end |