summaryrefslogtreecommitdiff
path: root/spec/support/helpers/git_http_helpers.rb
blob: b8289e6c5f1a6099897c77cfdd9a872a21861547 (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
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 download_or_upload(*args, &block)
    download(*args, &block)
    upload(*args, &block)
  end

  def auth_env(user, password, spnego_request_token)
    env = workhorse_internal_api_request_header
    if user
      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

  def git_access_error(error_key)
    message = Gitlab::GitAccess::ERROR_MESSAGES[error_key]
    message || raise("GitAccess error message key '#{error_key}' not found")
  end

  def git_access_wiki_error(error_key)
    message = Gitlab::GitAccessWiki::ERROR_MESSAGES[error_key]
    message || raise("GitAccessWiki error message key '#{error_key}' not found")
  end

  def change_access_error(error_key)
    message = Gitlab::Checks::ChangeAccess::ERROR_MESSAGES[error_key]
    message || raise("ChangeAccess error message key '#{error_key}' not found")
  end
end