Commit dc15201c authored by Horatiu Eugen Vlad's avatar Horatiu Eugen Vlad Committed by Rémy Coutable

Added git http requests tests for user with LDAP identity

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 3b206ccb
require "spec_helper" require "spec_helper"
describe 'Git HTTP requests', lib: true do describe 'Git HTTP requests', lib: true do
include GitHttpHelpers
include WorkhorseHelpers include WorkhorseHelpers
let(:user) { create(:user) }
let(:project) { create(:project, path: 'project.git-project') }
it "gives WWW-Authenticate hints" do it "gives WWW-Authenticate hints" do
clone_get('doesnt/exist.git') clone_get('doesnt/exist.git')
expect(response.header['WWW-Authenticate']).to start_with('Basic ') expect(response.header['WWW-Authenticate']).to start_with('Basic ')
end end
describe "User with no identities" do
let(:user) { create(:user) }
let(:project) { create(:project, path: 'project.git-project') }
context "when the project doesn't exist" do context "when the project doesn't exist" do
context "when no authentication is provided" do context "when no authentication is provided" do
it "responds with status 401 (no project existence information leak)" do it "responds with status 401 (no project existence information leak)" do
...@@ -458,51 +460,58 @@ describe 'Git HTTP requests', lib: true do ...@@ -458,51 +460,58 @@ describe 'Git HTTP requests', lib: true do
end end
end end
end end
def clone_get(project, options = {})
get "/#{project}/info/refs", { service: 'git-upload-pack' }, auth_env(*options.values_at(:user, :password, :spnego_request_token))
end end
def clone_post(project, options = {}) describe "User with LDAP identity" do
post "/#{project}/git-upload-pack", {}, auth_env(*options.values_at(:user, :password, :spnego_request_token)) let(:user) { create(:omniauth_user, extern_uid: dn) }
end let(:dn) { 'uid=john,ou=people,dc=example,dc=com' }
def push_get(project, options = {}) before do
get "/#{project}/info/refs", { service: 'git-receive-pack' }, auth_env(*options.values_at(:user, :password, :spnego_request_token)) allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
allow(Gitlab::LDAP::Authentication).to receive(:login).and_return(nil)
allow(Gitlab::LDAP::Authentication).to receive(:login).with(user.username, user.password).and_return(user)
end end
def push_post(project, options = {}) context "when authentication fails" do
post "/#{project}/git-receive-pack", {}, auth_env(*options.values_at(:user, :password, :spnego_request_token)) context "when no authentication is provided" do
it "responds with status 401" do
download('doesnt/exist.git') do |response|
expect(response).to have_http_status(401)
end
end
end end
def download(project, user: nil, password: nil, spnego_request_token: nil) context "when username and invalid password are provided" do
args = [project, { user: user, password: password, spnego_request_token: spnego_request_token }] it "responds with status 401" do
download('doesnt/exist.git', user: user.username, password: "nope") do |response|
clone_get(*args) expect(response).to have_http_status(401)
yield response end
end
clone_post(*args) end
yield response
end end
def upload(project, user: nil, password: nil, spnego_request_token: nil) context "when authentication succeeds" do
args = [project, { user: user, password: password, spnego_request_token: spnego_request_token }] context "when the project doesn't exist" do
it "responds with status 404" do
download('/doesnt/exist.git', user: user.username, password: user.password) do |response|
expect(response).to have_http_status(404)
end
end
end
push_get(*args) context "when the project exists" do
yield response let(:project) { create(:project, path: 'project.git-project') }
push_post(*args) before do
yield response project.team << [user, :master]
end end
def auth_env(user, password, spnego_request_token) it "responds with status 200" do
env = workhorse_internal_api_request_header clone_get(path, user: user.username, password: user.password) do |response|
if user && password expect(response).to have_http_status(200)
env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user, password) end
elsif spnego_request_token end
env['HTTP_AUTHORIZATION'] = "Negotiate #{::Base64.strict_encode64('opaque_request_token')}" end
end end
env
end end
end end
module GitHttpHelpers
def clone_get(project, options = {})
get "/#{project}/info/refs", { service: 'git-upload-pack' }, auth_env(*options.values_at(:user, :password, :spnego_request_token))
end
def clone_post(project, options = {})
post "/#{project}/git-upload-pack", {}, auth_env(*options.values_at(:user, :password, :spnego_request_token))
end
def push_get(project, options = {})
get "/#{project}/info/refs", { service: 'git-receive-pack' }, auth_env(*options.values_at(:user, :password, :spnego_request_token))
end
def push_post(project, options = {})
post "/#{project}/git-receive-pack", {}, auth_env(*options.values_at(:user, :password, :spnego_request_token))
end
def download(project, user: nil, password: nil, spnego_request_token: nil)
args = [project, { user: user, password: password, spnego_request_token: spnego_request_token }]
clone_get(*args)
yield response
clone_post(*args)
yield response
end
def upload(project, user: nil, password: nil, spnego_request_token: nil)
args = [project, { user: user, password: password, spnego_request_token: spnego_request_token }]
push_get(*args)
yield response
push_post(*args)
yield response
end
def auth_env(user, password, spnego_request_token)
env = workhorse_internal_api_request_header
if user && password
env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user, password)
elsif spnego_request_token
env['HTTP_AUTHORIZATION'] = "Negotiate #{::Base64.strict_encode64('opaque_request_token')}"
end
env
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment