auth_spec.rb 4.1 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe Gitlab::Auth, lib: true do
4
  let(:gl_auth) { described_class }
D
Dmitriy Zaporozhets 已提交
5

J
Jacob Vosmaer 已提交
6
  describe 'find_for_git_client' do
7 8 9
    it 'recognizes CI' do
      token = '123'
      project = create(:empty_project)
F
Felipe Artur 已提交
10 11
      project.update_attributes(runners_token: token)

12 13 14
      ip = 'ip'

      expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: 'gitlab-ci-token')
J
Jacob Vosmaer 已提交
15
      expect(gl_auth.find_for_git_client('gitlab-ci-token', token, project: project, ip: ip)).to eq(Gitlab::Auth::Result.new(nil, :ci))
16 17 18 19 20 21 22
    end

    it 'recognizes master passwords' do
      user = create(:user, password: 'password')
      ip = 'ip'

      expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username)
J
Jacob Vosmaer 已提交
23
      expect(gl_auth.find_for_git_client(user.username, 'password', project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, :gitlab_or_ldap))
24 25
    end

P
Patricio Cano 已提交
26 27 28
    it 'recognizes user lfs tokens' do
      user = create(:user)
      ip = 'ip'
29
      token = Gitlab::LfsToken.new(user).generate
P
Patricio Cano 已提交
30 31

      expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username)
32
      expect(gl_auth.find_for_git_client(user.username, token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, :lfs_token))
P
Patricio Cano 已提交
33 34 35 36 37
    end

    it 'recognizes deploy key lfs tokens' do
      key = create(:deploy_key)
      ip = 'ip'
38
      token = Gitlab::LfsToken.new(key).generate
P
Patricio Cano 已提交
39

40 41
      expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: "lfs+deploy-key-#{key.id}")
      expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(key, :lfs_deploy_token))
P
Patricio Cano 已提交
42 43
    end

44 45 46 47 48 49 50
    it 'recognizes OAuth tokens' do
      user = create(:user)
      application = Doorkeeper::Application.create!(name: "MyApp", redirect_uri: "https://app.com", owner: user)
      token = Doorkeeper::AccessToken.create!(application_id: application.id, resource_owner_id: user.id)
      ip = 'ip'

      expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: 'oauth2')
J
Jacob Vosmaer 已提交
51
      expect(gl_auth.find_for_git_client("oauth2", token.token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, :oauth))
52 53 54 55 56 57
    end

    it 'returns double nil for invalid credentials' do
      login = 'foo'
      ip = 'ip'

58
      expect(gl_auth).to receive(:rate_limit!).with(ip, success: nil, login: login)
J
Jacob Vosmaer 已提交
59
      expect(gl_auth.find_for_git_client(login, 'bar', project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new)
60 61 62
    end
  end

63
  describe 'find_with_user_password' do
64 65 66 67 68
    let!(:user) do
      create(:user,
        username: username,
        password: password,
        password_confirmation: password)
D
Dmitriy Zaporozhets 已提交
69
    end
70
    let(:username) { 'John' }     # username isn't lowercase, test this
71
    let(:password) { 'my-secret' }
D
Dmitriy Zaporozhets 已提交
72

73
    it "finds user by valid login/password" do
74
      expect( gl_auth.find_with_user_password(username, password) ).to eql user
D
Dmitriy Zaporozhets 已提交
75 76
    end

77
    it 'finds user by valid email/password with case-insensitive email' do
78
      expect(gl_auth.find_with_user_password(user.email.upcase, password)).to eql user
79 80
    end

81
    it 'finds user by valid username/password with case-insensitive username' do
82
      expect(gl_auth.find_with_user_password(username.upcase, password)).to eql user
83 84
    end

85
    it "does not find user with invalid password" do
86
      password = 'wrong'
87
      expect( gl_auth.find_with_user_password(username, password) ).not_to eql user
D
Dmitriy Zaporozhets 已提交
88 89
    end

90
    it "does not find user with invalid login" do
91
      user = 'wrong'
92
      expect( gl_auth.find_with_user_password(username, password) ).not_to eql user
93
    end
94 95

    context "with ldap enabled" do
96 97 98
      before do
        allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
      end
99 100

      it "tries to autheticate with db before ldap" do
101
        expect(Gitlab::LDAP::Authentication).not_to receive(:login)
102

103
        gl_auth.find_with_user_password(username, password)
104 105 106
      end

      it "uses ldap as fallback to for authentication" do
107
        expect(Gitlab::LDAP::Authentication).to receive(:login)
108

109
        gl_auth.find_with_user_password('ldap_user', 'password')
110 111
      end
    end
D
Dmitriy Zaporozhets 已提交
112 113
  end
end