summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/oauth/user_spec.rb
blob: c241e19860924186707b9a017f437b9f7c284855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'spec_helper'

describe Gitlab::OAuth::User do
  let(:gl_auth) { Gitlab::OAuth::User }
  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, uid: 'my-uid', provider: 'ldap')
      user = gl_auth.create(auth)

      user.should be_valid
      user.extern_uid.should == auth.uid
      user.provider.should == 'ldap'
    end

    it "should create user from Omniauth" do
      auth = double(info: info, uid: 'my-uid', provider: 'twitter')
      user = gl_auth.create(auth)

      user.should be_valid
      user.extern_uid.should == auth.uid
      user.provider.should == 'twitter'
    end

    it "should apply defaults to user" do
      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
      user.can_create_group.should == Gitlab.config.gitlab.default_can_create_group
    end

    it "Set a temp email address if not provided (like twitter does)" do
      info = double(
        uid: 'my-uid',
        nickname: 'john',
        name: 'John'
      )
      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