2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe API::API do
|
2020-04-22 19:07:51 +05:30
|
|
|
include GroupAPIHelpers
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
describe 'Record user last activity in after hook' do
|
|
|
|
# It does not matter which endpoint is used because last_activity_on should
|
|
|
|
# be updated on every request. `/groups` is used as an example
|
|
|
|
# to represent any API endpoint
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:user) { create(:user, last_activity_on: Date.yesterday) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'updates the users last_activity_on to the current date' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect { get api('/groups', user) }.to change { user.reload.last_activity_on }.to(Date.today)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'User with only read_api scope personal access token' do
|
|
|
|
# It does not matter which endpoint is used because this should behave
|
|
|
|
# in the same way for every request. `/groups` is used as an example
|
|
|
|
# to represent any API endpoint
|
|
|
|
|
|
|
|
context 'when personal access token has only read_api scope' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:token) { create(:personal_access_token, user: user, scopes: [:read_api]) }
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does authorize user for get request' do
|
|
|
|
get api('/groups', personal_access_token: token)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'does authorize user for head request' do
|
|
|
|
head api('/groups', personal_access_token: token)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'does not authorize user for revoked token' do
|
|
|
|
revoked = create(:personal_access_token, :revoked, user: user, scopes: [:read_api])
|
|
|
|
|
|
|
|
get api('/groups', personal_access_token: revoked)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does not authorize user for post request' do
|
|
|
|
params = attributes_for_group_api
|
|
|
|
|
|
|
|
post api("/groups", personal_access_token: token), params: params
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not authorize user for put request' do
|
|
|
|
group_param = { name: 'Test' }
|
|
|
|
|
|
|
|
put api("/groups/#{group.id}", personal_access_token: token), params: group_param
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does not authorize user for delete request' do
|
|
|
|
delete api("/groups/#{group.id}", personal_access_token: token)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-04 03:57:07 +05:30
|
|
|
|
|
|
|
describe 'authentication with deploy token' do
|
|
|
|
context 'admin mode' do
|
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:package) { create(:maven_package, project: project, name: project.full_path) }
|
|
|
|
let_it_be(:maven_metadatum) { package.maven_metadatum }
|
|
|
|
let_it_be(:package_file) { package.package_files.first }
|
|
|
|
let_it_be(:deploy_token) { create(:deploy_token) }
|
|
|
|
let(:headers_with_deploy_token) do
|
|
|
|
{
|
|
|
|
Gitlab::Auth::AuthFinders::DEPLOY_TOKEN_HEADER => deploy_token.token
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not bypass the session' do
|
|
|
|
expect(Gitlab::Auth::CurrentUserMode).not_to receive(:bypass_session!)
|
|
|
|
|
|
|
|
get(api("/packages/maven/#{maven_metadatum.path}/#{package_file.file_name}"),
|
|
|
|
headers: headers_with_deploy_token)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.media_type).to eq('application/octet-stream')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
context 'application context' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:user) { project.owner }
|
|
|
|
|
|
|
|
it 'logs all application context fields' do
|
|
|
|
allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do
|
2021-04-29 21:17:54 +05:30
|
|
|
Gitlab::ApplicationContext.current.tap do |log_context|
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(log_context).to match('correlation_id' => an_instance_of(String),
|
2021-04-29 21:17:54 +05:30
|
|
|
'meta.caller_id' => 'GET /api/:version/projects/:id/issues',
|
2021-03-08 18:12:59 +05:30
|
|
|
'meta.remote_ip' => an_instance_of(String),
|
2021-01-29 00:20:46 +05:30
|
|
|
'meta.project' => project.full_path,
|
|
|
|
'meta.root_namespace' => project.namespace.full_path,
|
|
|
|
'meta.user' => user.username,
|
2021-04-17 20:07:23 +05:30
|
|
|
'meta.client_id' => an_instance_of(String),
|
2021-01-29 00:20:46 +05:30
|
|
|
'meta.feature_category' => 'issue_tracking')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get(api("/projects/#{project.id}/issues", user))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips fields that do not apply' do
|
|
|
|
allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do
|
2021-04-29 21:17:54 +05:30
|
|
|
Gitlab::ApplicationContext.current.tap do |log_context|
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(log_context).to match('correlation_id' => an_instance_of(String),
|
2021-04-29 21:17:54 +05:30
|
|
|
'meta.caller_id' => 'GET /api/:version/users',
|
2021-03-08 18:12:59 +05:30
|
|
|
'meta.remote_ip' => an_instance_of(String),
|
2021-04-17 20:07:23 +05:30
|
|
|
'meta.client_id' => an_instance_of(String),
|
2021-01-29 00:20:46 +05:30
|
|
|
'meta.feature_category' => 'users')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get(api('/users'))
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe 'Marginalia comments' do
|
|
|
|
context 'GET /user/:id' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let(:component_map) do
|
|
|
|
{
|
|
|
|
"application" => "test",
|
2021-04-29 21:17:54 +05:30
|
|
|
"endpoint_id" => "GET /api/:version/users/:id"
|
2021-04-17 20:07:23 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { ActiveRecord::QueryRecorder.new { get api("/users/#{user.id}", user) } }
|
|
|
|
|
|
|
|
it 'generates a query that includes the expected annotations' do
|
|
|
|
expect(subject.log.last).to match(/correlation_id:.*/)
|
|
|
|
|
|
|
|
component_map.each do |component, value|
|
|
|
|
expect(subject.log.last).to include("#{component}:#{value}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
describe 'supported content-types' do
|
|
|
|
context 'GET /user/:id.txt' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { get api("/users/#{user.id}.txt", user) }
|
|
|
|
|
|
|
|
it 'returns application/json' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.media_type).to eq('application/json')
|
|
|
|
expect(response.body).to include('{"id":')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|