debian-mirror-gitlab/spec/support/helpers/api_helpers.rb

82 lines
2.6 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
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
2022-08-27 11:52:29 +05:30
def api(path, user = nil, version: API::API.version, personal_access_token: nil, oauth_access_token: nil, job_token: nil, access_token: nil)
2018-03-17 18:26:18 +05:30
full_path = "/api/#{version}#{path}"
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
if oauth_access_token
2022-08-27 11:52:29 +05:30
query_string = "access_token=#{oauth_access_token.plaintext_token}"
2018-03-17 18:26:18 +05:30
elsif personal_access_token
query_string = "private_token=#{personal_access_token.token}"
2022-01-26 12:08:38 +05:30
elsif job_token
query_string = "job_token=#{job_token}"
2022-08-27 11:52:29 +05:30
elsif access_token
query_string = "access_token=#{access_token.token}"
2018-03-17 18:26:18 +05:30
elsif user
personal_access_token = create(:personal_access_token, user: user)
query_string = "private_token=#{personal_access_token.token}"
end
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
if query_string
2019-10-12 21:52:04 +05:30
separator = path.index('?') ? '&' : '?'
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
full_path + separator + query_string
else
full_path
end
2014-09-02 18:07:02 +05:30
end
2019-03-02 22:35:43 +05:30
2020-03-13 15:44:24 +05:30
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
2020-04-22 19:07:51 +05:30
def expect_paginated_array_response(*items)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-03-02 22:35:43 +05:30
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
2020-04-22 19:07:51 +05:30
expect(json_response.map { |item| item['id'] }).to eq(items.flatten)
2019-03-02 22:35:43 +05:30
end
2020-03-13 15:44:24 +05:30
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
2022-08-27 11:52:29 +05:30
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
2020-03-13 15:44:24 +05:30
def stub_last_activity_update
allow_any_instance_of(Users::ActivityService).to receive(:execute)
end
2014-09-02 18:07:02 +05:30
end