From 0c34fa3ea0662f94fdc565cfd3f921db40733821 Mon Sep 17 00:00:00 2001 From: Jan-Willem van der Meer Date: Mon, 1 Sep 2014 12:59:04 +0200 Subject: [PATCH] Add tests for finding an oauth authenticated user --- lib/gitlab/oauth/user.rb | 2 +- spec/lib/gitlab/oauth/user_spec.rb | 37 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb index 0056eb3a28b..8c426d810c2 100644 --- a/lib/gitlab/oauth/user.rb +++ b/lib/gitlab/oauth/user.rb @@ -67,7 +67,7 @@ module Gitlab end def uid - uid = auth.info.uid || auth.uid + uid = auth.info.try(:uid) || auth.uid uid = uid.to_s unless uid.nil? uid end diff --git a/spec/lib/gitlab/oauth/user_spec.rb b/spec/lib/gitlab/oauth/user_spec.rb index 2f15b5e0349..a79ba3b588e 100644 --- a/spec/lib/gitlab/oauth/user_spec.rb +++ b/spec/lib/gitlab/oauth/user_spec.rb @@ -2,39 +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( + uid: 'my-uid', 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, 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') + @auth = double(info: info, provider: 'ldap') user = gl_auth.create(@auth) user.should be_valid - user.extern_uid.should == @info.uid + user.extern_uid.should == info.uid user.provider.should == 'ldap' end it "should create user from Omniauth" do - @auth = double(info: @info, provider: 'twitter') + @auth = double(info: info, provider: 'twitter') user = gl_auth.create(@auth) user.should be_valid - user.extern_uid.should == @info.uid + user.extern_uid.should == info.uid user.provider.should == 'twitter' end it "should apply defaults to user" do - @auth = double(info: @info, provider: 'ldap') + @auth = double(info: info, provider: 'ldap') user = gl_auth.create(@auth) user.should be_valid