File: api_helpers.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (91 lines) | stat: -rw-r--r-- 3,092 bytes parent folder | download
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
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module ApiHelpers
  # Public: Prepend a request path with the path to the API
  #
  # path - Path to append
  # user - User object - If provided, automatically appends private_token query
  #          string for authenticated requests
  #
  # Examples
  #
  #   >> api('/issues')
  #   => "/api/v2/issues"
  #
  #   >> api('/issues', User.last)
  #   => "/api/v2/issues?private_token=..."
  #
  #   >> api('/issues?foo=bar', User.last)
  #   => "/api/v2/issues?foo=bar&private_token=..."
  #
  # Returns the relative path to the requested API resource
  def api(path, user = nil, version: API::API.version, personal_access_token: nil, oauth_access_token: nil, job_token: nil, access_token: nil, admin_mode: false)
    full_path = "/api/#{version}#{path}"

    if oauth_access_token
      query_string = "access_token=#{oauth_access_token.plaintext_token}"
    elsif personal_access_token
      query_string = "private_token=#{personal_access_token.token}"
    elsif job_token
      query_string = "job_token=#{job_token}"
    elsif access_token
      query_string = "access_token=#{access_token.token}"
    elsif user

      organization = Organizations::Organization.first || build(:organization)

      personal_access_token = if admin_mode && user.admin?
                                create(:personal_access_token, :admin_mode, user: user, organization: organization)
                              else
                                create(:personal_access_token, user: user, organization: organization)
                              end

      query_string = "private_token=#{personal_access_token.token}"
    end

    if query_string
      separator = path.index('?') ? '&' : '?'

      full_path + separator + query_string
    else
      full_path
    end
  end

  def expect_empty_array_response
    expect_successful_response_with_paginated_array
    expect(json_response.length).to eq(0)
  end

  def expect_successful_response_with_paginated_array
    expect(response).to have_gitlab_http_status(:ok)
    expect(response).to include_pagination_headers
    expect(json_response).to be_an Array
  end

  def expect_paginated_array_response(*items)
    expect(response).to have_gitlab_http_status(:ok)
    expect(response).to include_pagination_headers
    expect(json_response).to be_an Array
    expect(json_response.map { |item| item['id'] }).to eq(items.flatten)
  end

  def expect_response_contain_exactly(*items)
    expect(response).to have_gitlab_http_status(:ok)
    expect(json_response).to be_an Array
    expect(json_response.map { |item| item['id'] }).to contain_exactly(*items)
  end

  def expect_paginated_array_response_contain_exactly(*items)
    expect(response).to have_gitlab_http_status(:ok)
    expect(response).to include_pagination_headers
    expect(json_response).to be_an Array
    expect(json_response.map { |item| item['id'] }).to contain_exactly(*items)
  end

  def stub_last_activity_update
    allow_next_instance_of(Users::ActivityService) do |service|
      allow(service).to receive(:execute)
    end
  end
end