PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/lib/gitlab/auth_spec.rb

https://gitlab.com/nick.volynkin/gitlab-ee
Ruby | 108 lines | 84 code | 24 blank | 0 comment | 1 complexity | 9523e5273f2da07374c95293016f6f40 MD5 | raw file
  1. require 'spec_helper'
  2. describe Gitlab::Auth, lib: true do
  3. let(:gl_auth) { described_class }
  4. describe 'find_for_git_client' do
  5. it 'recognizes CI' do
  6. token = '123'
  7. project = create(:empty_project)
  8. project.update_attributes(runners_token: token)
  9. ip = 'ip'
  10. expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: 'gitlab-ci-token')
  11. expect(gl_auth.find_for_git_client('gitlab-ci-token', token, project: project, ip: ip)).to eq(Gitlab::Auth::Result.new(nil, :ci))
  12. end
  13. it 'recognizes master passwords' do
  14. user = create(:user, password: 'password')
  15. ip = 'ip'
  16. expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username)
  17. expect(gl_auth.find_for_git_client(user.username, 'password', project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, :gitlab_or_ldap))
  18. end
  19. it 'recognizes OAuth tokens' do
  20. user = create(:user)
  21. application = Doorkeeper::Application.create!(name: "MyApp", redirect_uri: "https://app.com", owner: user)
  22. token = Doorkeeper::AccessToken.create!(application_id: application.id, resource_owner_id: user.id)
  23. ip = 'ip'
  24. expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: 'oauth2')
  25. expect(gl_auth.find_for_git_client("oauth2", token.token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, :oauth))
  26. end
  27. it 'returns double nil for invalid credentials' do
  28. login = 'foo'
  29. ip = 'ip'
  30. expect(gl_auth).to receive(:rate_limit!).with(ip, success: false, login: login)
  31. expect(gl_auth.find_for_git_client(login, 'bar', project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new)
  32. end
  33. end
  34. describe 'find_with_user_password' do
  35. let!(:user) do
  36. create(:user,
  37. username: username,
  38. password: password,
  39. password_confirmation: password)
  40. end
  41. let(:username) { 'John' } # username isn't lowercase, test this
  42. let(:password) { 'my-secret' }
  43. it "finds user by valid login/password" do
  44. expect( gl_auth.find_with_user_password(username, password) ).to eql user
  45. end
  46. it 'finds user by valid email/password with case-insensitive email' do
  47. expect(gl_auth.find_with_user_password(user.email.upcase, password)).to eql user
  48. end
  49. it 'finds user by valid username/password with case-insensitive username' do
  50. expect(gl_auth.find_with_user_password(username.upcase, password)).to eql user
  51. end
  52. it "does not find user with invalid password" do
  53. password = 'wrong'
  54. expect( gl_auth.find_with_user_password(username, password) ).not_to eql user
  55. end
  56. it "does not find user with invalid login" do
  57. user = 'wrong'
  58. expect( gl_auth.find_with_user_password(username, password) ).not_to eql user
  59. end
  60. context "with kerberos" do
  61. before do
  62. allow(Devise).to receive_messages(omniauth_providers: [:kerberos])
  63. end
  64. it "finds user" do
  65. allow(Gitlab::Kerberos::Authentication).to receive_messages(valid?: true)
  66. allow(Gitlab::Kerberos::Authentication).to receive_messages(email: user.email)
  67. expect( gl_auth.find_with_user_password(username, password) ).to eql user
  68. end
  69. end
  70. context "with ldap enabled" do
  71. before do
  72. allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
  73. end
  74. it "tries to autheticate with db before ldap" do
  75. expect(Gitlab::LDAP::Authentication).not_to receive(:login)
  76. gl_auth.find_with_user_password(username, password)
  77. end
  78. it "uses ldap as fallback to for authentication" do
  79. expect(Gitlab::LDAP::Authentication).to receive(:login)
  80. gl_auth.find_with_user_password('ldap_user', 'password')
  81. end
  82. end
  83. end
  84. end