diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-09-03 14:52:40 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-09-03 14:52:40 +0400 |
commit | 596bf4c2a59434c35eabd2336e249ebc3eb03f56 (patch) | |
tree | ab440378cc41e7581f3c5b57a9034f369bfc6c2b /spec | |
parent | 00ccaed029efbbdff96a4b64628a3a2d9bf5125d (diff) | |
parent | c0323b40ee5633f2808f52f98bde0509a2f3ee59 (diff) |
Merge branch 'feaure-ldap-oauth-tests'
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Conflicts:
spec/lib/gitlab/oauth/user_spec.rb
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/ldap/access_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/gitlab/ldap/user_spec.rb | 52 | ||||
-rw-r--r-- | spec/lib/gitlab/oauth/user_spec.rb | 56 |
3 files changed, 67 insertions, 49 deletions
diff --git a/spec/lib/gitlab/ldap/access_spec.rb b/spec/lib/gitlab/ldap/access_spec.rb index d8c107502ba..2307a03f656 100644 --- a/spec/lib/gitlab/ldap/access_spec.rb +++ b/spec/lib/gitlab/ldap/access_spec.rb @@ -16,14 +16,14 @@ describe Gitlab::LDAP::Access do context 'when the user is found' do before { Gitlab::LDAP::Person.stub(find_by_dn: :ldap_user) } - context 'and the Active Directory disabled flag is set' do - before { Gitlab::LDAP::Person.stub(active_directory_disabled?: true) } + context 'and the user is diabled via active directory' do + before { Gitlab::LDAP::Person.stub(disabled_via_active_directory?: true) } it { should be_false } end - context 'and the Active Directory disabled flag is not set' do - before { Gitlab::LDAP::Person.stub(active_directory_disabled?: false) } + context 'and has no disabled flag in active diretory' do + before { Gitlab::LDAP::Person.stub(disabled_via_active_directory?: false) } it { should be_true } end diff --git a/spec/lib/gitlab/ldap/user_spec.rb b/spec/lib/gitlab/ldap/user_spec.rb index de5717417f1..725338965be 100644 --- a/spec/lib/gitlab/ldap/user_spec.rb +++ b/spec/lib/gitlab/ldap/user_spec.rb @@ -2,45 +2,37 @@ require 'spec_helper' describe Gitlab::LDAP::User do let(:gl_auth) { Gitlab::LDAP::User } - - before do - Gitlab.config.stub(omniauth: {}) - - @info = double( - uid: '12djsak321', + let(:info) do + double( name: 'John', - email: 'john@mail.com', + email: 'john@example.com', nickname: 'john' ) end + before { Gitlab.config.stub(omniauth: {}) } + + describe :find_or_create do + let(:auth) do + double(info: info, provider: 'ldap', uid: 'my-uid') + end + + it "finds the user if already existing" do + existing_user = create(:user, extern_uid: 'my-uid', provider: 'ldap') - describe :find_for_ldap_auth do - before do - @auth = double( - uid: '12djsak321', - info: @info, - provider: 'ldap' - ) + expect{ gl_auth.find_or_create(auth) }.to_not change{ User.count } end - it "should update credentials by email if missing uid" do - user = double('User') - User.stub find_by_extern_uid_and_provider: nil - User.stub(:find_by).with(hash_including(email: anything())) { user } - user.should_receive :update_attributes - gl_auth.find_or_create(@auth) + it "connects to existing non-ldap user if the email matches" do + existing_user = create(:user, email: 'john@example.com') + expect{ gl_auth.find_or_create(auth) }.to_not change{ User.count } + + existing_user.reload + expect(existing_user.extern_uid).to eql 'my-uid' + expect(existing_user.provider).to eql 'ldap' end - it "should not update credentials by username if missing uid and Gitlab.config.ldap.allow_username_or_email_login is false" do - user = double('User') - value = Gitlab.config.ldap.allow_username_or_email_login - Gitlab.config.ldap['allow_username_or_email_login'] = false - User.stub find_by_extern_uid_and_provider: nil - User.stub(:find_by).with(hash_including(email: anything())) { nil } - User.stub(:find_by).with(hash_including(username: anything())) { user } - user.should_not_receive :update_attributes - gl_auth.find_or_create(@auth) - Gitlab.config.ldap['allow_username_or_email_login'] = value + it "creates a new user if not found" do + expect{ gl_auth.find_or_create(auth) }.to change{ User.count }.by(1) end end end diff --git a/spec/lib/gitlab/oauth/user_spec.rb b/spec/lib/gitlab/oauth/user_spec.rb index 60d9c4f8a9b..c241e198609 100644 --- a/spec/lib/gitlab/oauth/user_spec.rb +++ b/spec/lib/gitlab/oauth/user_spec.rb @@ -2,40 +2,54 @@ require 'spec_helper' describe Gitlab::OAuth::User do let(:gl_auth) { Gitlab::OAuth::User } - - before do - Gitlab.config.stub(omniauth: {}) - - @info = double( - uid: '12djsak321', + let(:info) do + double( nickname: 'john', name: 'John', email: 'john@mail.com' ) end + before do + Gitlab.config.stub(omniauth: {}) + end + + describe :find do + let!(:existing_user) { create(:user, extern_uid: 'my-uid', provider: 'my-provider') } + + it "finds an existing user based on uid and provider (facebook)" do + auth = double(info: double(name: 'John'), uid: 'my-uid', provider: 'my-provider') + assert gl_auth.find(auth) + end + + it "finds an existing user based on nested uid and provider" do + auth = double(info: info, uid: 'my-uid', provider: 'my-provider') + assert gl_auth.find(auth) + end + end + describe :create do it "should create user from LDAP" do - @auth = double(info: @info, provider: 'ldap') - user = gl_auth.create(@auth) + auth = double(info: info, uid: 'my-uid', provider: 'ldap') + user = gl_auth.create(auth) user.should be_valid - user.extern_uid.should == @info.uid + user.extern_uid.should == auth.uid user.provider.should == 'ldap' end it "should create user from Omniauth" do - @auth = double(info: @info, provider: 'twitter') - user = gl_auth.create(@auth) + auth = double(info: info, uid: 'my-uid', provider: 'twitter') + user = gl_auth.create(auth) user.should be_valid - user.extern_uid.should == @info.uid + user.extern_uid.should == auth.uid user.provider.should == 'twitter' end it "should apply defaults to user" do - @auth = double(info: @info, provider: 'ldap') - user = gl_auth.create(@auth) + auth = double(info: info, uid: 'my-uid', provider: 'ldap') + user = gl_auth.create(auth) user.should be_valid user.projects_limit.should == Gitlab.config.gitlab.default_projects_limit @@ -48,10 +62,22 @@ describe Gitlab::OAuth::User do nickname: 'john', name: 'John' ) - auth = double(info: info, provider: 'my-provider') + auth = double(info: info, uid: 'my-uid', provider: 'my-provider') user = gl_auth.create(auth) expect(user.email).to_not be_empty end + + it 'generates a username if non provided (google)' do + info = double( + uid: 'my-uid', + name: 'John', + email: 'john@example.com' + ) + auth = double(info: info, uid: 'my-uid', provider: 'my-provider') + + user = gl_auth.create(auth) + expect(user.username).to eql 'john' + end end end |