2019-10-09 08:06:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RackAttackSpecHelpers
|
|
|
|
def api_get_args_with_token_headers(partial_url, token_headers)
|
|
|
|
["/api/#{API::API.version}#{partial_url}", params: nil, headers: token_headers]
|
|
|
|
end
|
|
|
|
|
|
|
|
def rss_url(user)
|
|
|
|
"/dashboard/projects.atom?feed_token=#{user.feed_token}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_token_headers(user)
|
2022-05-02 20:08:25 -04:00
|
|
|
{ Gitlab::Auth::AuthFinders::PRIVATE_TOKEN_HEADER => user.private_token }
|
2019-10-09 08:06:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def personal_access_token_headers(personal_access_token)
|
2022-05-02 20:08:25 -04:00
|
|
|
{ Gitlab::Auth::AuthFinders::PRIVATE_TOKEN_HEADER => personal_access_token.token }
|
2019-10-09 08:06:13 -04:00
|
|
|
end
|
|
|
|
|
2022-08-12 17:11:43 -04:00
|
|
|
def bearer_headers(token)
|
|
|
|
{ 'AUTHORIZATION' => "Bearer #{token.token}" }
|
|
|
|
end
|
|
|
|
|
2019-10-09 08:06:13 -04:00
|
|
|
def oauth_token_headers(oauth_access_token)
|
2022-08-12 17:11:43 -04:00
|
|
|
{ 'AUTHORIZATION' => "Bearer #{oauth_access_token.plaintext_token}" }
|
2019-10-09 08:06:13 -04:00
|
|
|
end
|
|
|
|
|
2021-01-04 16:10:19 -05:00
|
|
|
def basic_auth_headers(user, personal_access_token)
|
|
|
|
encoded_login = ["#{user.username}:#{personal_access_token.token}"].pack('m0')
|
|
|
|
{ 'AUTHORIZATION' => "Basic #{encoded_login}" }
|
|
|
|
end
|
|
|
|
|
2022-05-02 20:08:25 -04:00
|
|
|
def deploy_token_headers(deploy_token)
|
|
|
|
basic_auth_headers(deploy_token, deploy_token)
|
|
|
|
end
|
|
|
|
|
2022-02-03 10:12:41 -05:00
|
|
|
def expect_rejection(name = nil, &block)
|
2019-10-09 08:06:13 -04:00
|
|
|
yield
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
2021-01-07 16:10:18 -05:00
|
|
|
|
|
|
|
expect(response.headers.to_h).to include(
|
|
|
|
'RateLimit-Limit' => a_string_matching(/^\d+$/),
|
2022-02-03 10:12:41 -05:00
|
|
|
'RateLimit-Name' => name || a_string_matching(/^throttle_.*$/),
|
2021-01-07 16:10:18 -05:00
|
|
|
'RateLimit-Observed' => a_string_matching(/^\d+$/),
|
|
|
|
'RateLimit-Remaining' => a_string_matching(/^\d+$/),
|
|
|
|
'Retry-After' => a_string_matching(/^\d+$/)
|
|
|
|
)
|
2021-01-18 07:10:41 -05:00
|
|
|
expect(response).to have_header('RateLimit-Reset')
|
|
|
|
expect do
|
|
|
|
DateTime.strptime(response.headers['RateLimit-Reset'], '%s')
|
|
|
|
end.not_to raise_error
|
2021-01-07 16:10:18 -05:00
|
|
|
expect(response).to have_header('RateLimit-ResetTime')
|
|
|
|
expect do
|
|
|
|
Time.httpdate(response.headers['RateLimit-ResetTime'])
|
|
|
|
end.not_to raise_error
|
2019-10-09 08:06:13 -04:00
|
|
|
end
|
2020-07-16 20:09:37 -04:00
|
|
|
|
|
|
|
def expect_ok(&block)
|
|
|
|
yield
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
|
|
|
def random_next_ip
|
|
|
|
allow_next_instance_of(Rack::Attack::Request) do |instance|
|
|
|
|
allow(instance).to receive(:ip).and_return(FFaker::Internet.ip_v4_address)
|
|
|
|
end
|
|
|
|
end
|
2019-10-09 08:06:13 -04:00
|
|
|
end
|