diff options
author | Alfredo Sumaran <alfredo@gitlab.com> | 2016-02-24 14:06:55 -0500 |
---|---|---|
committer | Alfredo Sumaran <alfredo@gitlab.com> | 2016-02-24 14:06:55 -0500 |
commit | a4e522a889b24d7d7768ac3b77d0b544a25c3782 (patch) | |
tree | 5133df88a0bf7d5cdb8aeef7acd6232f9d6aadcb /spec/lib | |
parent | 9f0a71f4fe0eade788c1c722c70873b319492dbe (diff) | |
parent | 4a9affa7330f2099f6385d3d74150f93cfd008f1 (diff) | |
download | gitlab-ce-a4e522a889b24d7d7768ac3b77d0b544a25c3782.tar.gz |
Merge branch 'master' into minimize-on-diff
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/banzai/filter/emoji_filter_spec.rb | 4 | ||||
-rw-r--r-- | spec/lib/ci/status_spec.rb | 71 | ||||
-rw-r--r-- | spec/lib/gitlab/o_auth/user_spec.rb | 26 | ||||
-rw-r--r-- | spec/lib/gitlab/saml/user_spec.rb | 271 | ||||
-rw-r--r-- | spec/lib/gitlab/workhorse_spec.rb | 18 |
5 files changed, 385 insertions, 5 deletions
diff --git a/spec/lib/banzai/filter/emoji_filter_spec.rb b/spec/lib/banzai/filter/emoji_filter_spec.rb index cf314058158..b5b38cf0c8c 100644 --- a/spec/lib/banzai/filter/emoji_filter_spec.rb +++ b/spec/lib/banzai/filter/emoji_filter_spec.rb @@ -14,7 +14,7 @@ describe Banzai::Filter::EmojiFilter, lib: true do it 'replaces supported emoji' do doc = filter('<p>:heart:</p>') - expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/emoji/2764.png' + expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/2764.png' end it 'ignores unsupported emoji' do @@ -25,7 +25,7 @@ describe Banzai::Filter::EmojiFilter, lib: true do it 'correctly encodes the URL' do doc = filter('<p>:+1:</p>') - expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/emoji/1F44D.png' + expect(doc.css('img').first.attr('src')).to eq 'https://foo.com/assets/1F44D.png' end it 'matches at the start of a string' do diff --git a/spec/lib/ci/status_spec.rb b/spec/lib/ci/status_spec.rb new file mode 100644 index 00000000000..1539720bb8d --- /dev/null +++ b/spec/lib/ci/status_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper' + +describe Ci::Status do + describe '.get_status' do + subject { described_class.get_status(statuses) } + + shared_examples 'build status summary' do + context 'all successful' do + let(:statuses) { Array.new(2) { create(type, status: :success) } } + it { is_expected.to eq 'success' } + end + + context 'at least one failed' do + let(:statuses) do + [create(type, status: :success), create(type, status: :failed)] + end + + it { is_expected.to eq 'failed' } + end + + context 'at least one running' do + let(:statuses) do + [create(type, status: :success), create(type, status: :running)] + end + + it { is_expected.to eq 'running' } + end + + context 'at least one pending' do + let(:statuses) do + [create(type, status: :success), create(type, status: :pending)] + end + + it { is_expected.to eq 'running' } + end + + context 'success and failed but allowed to fail' do + let(:statuses) do + [create(type, status: :success), + create(type, status: :failed, allow_failure: true)] + end + + it { is_expected.to eq 'success' } + end + + context 'one failed but allowed to fail' do + let(:statuses) { [create(type, status: :failed, allow_failure: true)] } + it { is_expected.to eq 'success' } + end + + context 'one finished and second running but allowed to fail' do + let(:statuses) do + [create(type, status: :success), + create(type, status: :running, allow_failure: true)] + end + + it { is_expected.to eq 'running' } + end + end + + context 'ci build statuses' do + let(:type) { :ci_build } + it_behaves_like 'build status summary' + end + + context 'generic commit statuses' do + let(:type) { :generic_commit_status } + it_behaves_like 'build status summary' + end + end +end diff --git a/spec/lib/gitlab/o_auth/user_spec.rb b/spec/lib/gitlab/o_auth/user_spec.rb index 925bc442a90..3a769acfdc0 100644 --- a/spec/lib/gitlab/o_auth/user_spec.rb +++ b/spec/lib/gitlab/o_auth/user_spec.rb @@ -41,7 +41,20 @@ describe Gitlab::OAuth::User, lib: true do describe 'signup' do shared_examples "to verify compliance with allow_single_sign_on" do - context "with allow_single_sign_on enabled" do + context "with new allow_single_sign_on enabled syntax" do + before { stub_omniauth_config(allow_single_sign_on: ['twitter']) } + + it "creates a user from Omniauth" do + oauth_user.save + + expect(gl_user).to be_valid + identity = gl_user.identities.first + expect(identity.extern_uid).to eql uid + expect(identity.provider).to eql 'twitter' + end + end + + context "with old allow_single_sign_on enabled syntax" do before { stub_omniauth_config(allow_single_sign_on: true) } it "creates a user from Omniauth" do @@ -54,7 +67,14 @@ describe Gitlab::OAuth::User, lib: true do end end - context "with allow_single_sign_on disabled (Default)" do + context "with new allow_single_sign_on disabled syntax" do + before { stub_omniauth_config(allow_single_sign_on: []) } + it "throws an error" do + expect{ oauth_user.save }.to raise_error StandardError + end + end + + context "with old allow_single_sign_on disabled (Default)" do before { stub_omniauth_config(allow_single_sign_on: false) } it "throws an error" do expect{ oauth_user.save }.to raise_error StandardError @@ -135,7 +155,7 @@ describe Gitlab::OAuth::User, lib: true do describe 'blocking' do let(:provider) { 'twitter' } - before { stub_omniauth_config(allow_single_sign_on: true) } + before { stub_omniauth_config(allow_single_sign_on: ['twitter']) } context 'signup with omniauth only' do context 'dont block on create' do diff --git a/spec/lib/gitlab/saml/user_spec.rb b/spec/lib/gitlab/saml/user_spec.rb new file mode 100644 index 00000000000..de7cd99d49d --- /dev/null +++ b/spec/lib/gitlab/saml/user_spec.rb @@ -0,0 +1,271 @@ +require 'spec_helper' + +describe Gitlab::Saml::User, lib: true do + let(:saml_user) { described_class.new(auth_hash) } + let(:gl_user) { saml_user.gl_user } + let(:uid) { 'my-uid' } + let(:provider) { 'saml' } + let(:auth_hash) { OmniAuth::AuthHash.new(uid: uid, provider: provider, info: info_hash) } + let(:info_hash) do + { + name: 'John', + email: 'john@mail.com' + } + end + let(:ldap_user) { Gitlab::LDAP::Person.new(Net::LDAP::Entry.new, 'ldapmain') } + + 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 + + describe 'account exists on server' do + before { stub_omniauth_config({ allow_single_sign_on: ['saml'], auto_link_saml_user: true }) } + context 'and should bind with SAML' do + let!(:existing_user) { create(:user, email: 'john@mail.com', username: 'john') } + it 'adds the SAML identity to the existing user' do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).to eq existing_user + identity = gl_user.identities.first + expect(identity.extern_uid).to eql uid + expect(identity.provider).to eql 'saml' + end + end + end + + describe 'no account exists on server' do + shared_examples 'to verify compliance with allow_single_sign_on' do + context 'with allow_single_sign_on enabled' do + before { stub_omniauth_config(allow_single_sign_on: ['saml']) } + + it 'creates a user from SAML' do + saml_user.save + + expect(gl_user).to be_valid + identity = gl_user.identities.first + expect(identity.extern_uid).to eql uid + expect(identity.provider).to eql 'saml' + end + end + + context 'with allow_single_sign_on default (["saml"])' do + before { stub_omniauth_config(allow_single_sign_on: ['saml']) } + it 'should not throw an error' do + expect{ saml_user.save }.not_to raise_error + end + end + + context 'with allow_single_sign_on disabled' do + before { stub_omniauth_config(allow_single_sign_on: false) } + it 'should throw an error' do + expect{ saml_user.save }.to raise_error StandardError + end + end + end + + context 'with auto_link_ldap_user disabled (default)' do + before { stub_omniauth_config({ auto_link_ldap_user: false, auto_link_saml_user: false, allow_single_sign_on: ['saml'] }) } + include_examples 'to verify compliance with allow_single_sign_on' + end + + context 'with auto_link_ldap_user enabled' do + before { stub_omniauth_config({ auto_link_ldap_user: true, auto_link_saml_user: false }) } + + context 'and no LDAP provider defined' do + before { stub_ldap_config(providers: []) } + + include_examples 'to verify compliance with allow_single_sign_on' + end + + context 'and at least one LDAP provider is defined' do + before { stub_ldap_config(providers: %w(ldapmain)) } + + context 'and a corresponding LDAP person' do + before do + 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(Gitlab::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user) + end + + context 'and no account for the LDAP user' do + + it 'creates a user with dual LDAP and SAML identities' do + saml_user.save + + expect(gl_user).to be_valid + expect(gl_user.username).to eql uid + expect(gl_user.email).to eql 'johndoe@example.com' + expect(gl_user.identities.length).to eql 2 + identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } } + expect(identities_as_hash).to match_array([ { provider: 'ldapmain', extern_uid: 'uid=user1,ou=People,dc=example' }, + { provider: 'saml', extern_uid: uid } + ]) + end + end + + 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') } + it "adds the omniauth identity to the LDAP account" do + saml_user.save + + expect(gl_user).to be_valid + expect(gl_user.username).to eql 'john' + expect(gl_user.email).to eql 'john@example.com' + expect(gl_user.identities.length).to eql 2 + identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } } + expect(identities_as_hash).to match_array([ { provider: 'ldapmain', extern_uid: 'uid=user1,ou=People,dc=example' }, + { provider: 'saml', extern_uid: uid } + ]) + end + end + end + + context 'and no corresponding LDAP person' do + before { allow(Gitlab::LDAP::Person).to receive(:find_by_uid).and_return(nil) } + + include_examples 'to verify compliance with allow_single_sign_on' + end + end + end + + end + + describe 'blocking' do + before { stub_omniauth_config({ allow_saml_sign_up: true, auto_link_saml_user: true }) } + + context 'signup with SAML only' do + context 'dont block on create' do + before { stub_omniauth_config(block_auto_created_users: false) } + + it 'should not block the user' do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create' do + before { stub_omniauth_config(block_auto_created_users: true) } + + it 'should block user' do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).to be_blocked + end + end + end + + context 'signup with linked omniauth and LDAP account' do + before do + stub_omniauth_config(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(saml_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 { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: false) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create (LDAP)' do + before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: true) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).to be_blocked + end + end + end + + 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') } + + context 'dont block on create (LDAP)' do + before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: false) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create (LDAP)' do + before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: true) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + end + end + + + context 'sign-in' do + before do + saml_user.save + saml_user.gl_user.activate + end + + context 'dont block on create' do + before { stub_omniauth_config(block_auto_created_users: false) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create' do + before { stub_omniauth_config(block_auto_created_users: true) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'dont block on create (LDAP)' do + before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: false) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create (LDAP)' do + before { allow_any_instance_of(Gitlab::LDAP::Config).to receive_messages(block_auto_created_users: true) } + + it do + saml_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + end + end + end +end diff --git a/spec/lib/gitlab/workhorse_spec.rb b/spec/lib/gitlab/workhorse_spec.rb new file mode 100644 index 00000000000..d940bf05061 --- /dev/null +++ b/spec/lib/gitlab/workhorse_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe Gitlab::Workhorse, lib: true do + let(:project) { create(:project) } + let(:subject) { Gitlab::Workhorse } + + describe "#send_git_archive" do + context "when the repository doesn't have an archive file path" do + before do + allow(project.repository).to receive(:archive_metadata).and_return(Hash.new) + end + + it "raises an error" do + expect { subject.send_git_archive(project, "master", "zip") }.to raise_error(RuntimeError) + end + end + end +end |