debian-mirror-gitlab/spec/requests/api/internal/pages_spec.rb

328 lines
12 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe API::Internal::Pages, feature_category: :pages do
2023-05-27 22:25:52 +05:30
let_it_be(:group) { create(:group, name: 'mygroup') }
let_it_be_with_reload(:project) { create(:project, name: 'myproject', group: group) }
let(:auth_header) do
{
Gitlab::Pages::INTERNAL_API_REQUEST_HEADER => JWT.encode(
{ 'iss' => 'gitlab-pages' },
Gitlab::Pages.secret, 'HS256')
}
2020-04-22 19:07:51 +05:30
end
2020-10-24 23:57:45 +05:30
2020-04-22 19:07:51 +05:30
before do
2023-05-27 22:25:52 +05:30
allow(Gitlab::Pages)
.to receive(:secret)
.and_return(SecureRandom.random_bytes(Gitlab::Pages::SECRET_LENGTH))
2021-01-29 00:20:46 +05:30
stub_pages_object_storage(::Pages::DeploymentUploader)
2020-04-22 19:07:51 +05:30
end
2023-05-27 22:25:52 +05:30
describe 'GET /internal/pages/status' do
2020-04-22 19:07:51 +05:30
it 'responds with 401 Unauthorized' do
2023-05-27 22:25:52 +05:30
get api('/internal/pages/status')
2019-12-04 20:38:33 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2019-12-04 20:38:33 +05:30
end
2020-04-22 19:07:51 +05:30
it 'responds with 204 no content' do
2023-05-27 22:25:52 +05:30
get api('/internal/pages/status'), headers: auth_header
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to be_empty
end
end
2023-05-27 22:25:52 +05:30
describe 'GET /internal/pages' do
context 'when not authenticated' do
2020-04-22 19:07:51 +05:30
it 'responds with 401 Unauthorized' do
2023-05-27 22:25:52 +05:30
get api('/internal/pages')
2019-12-04 20:38:33 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2019-12-04 20:38:33 +05:30
end
end
2023-05-27 22:25:52 +05:30
context 'when authenticated' do
before do
project.update_pages_deployment!(create(:pages_deployment, project: project))
2019-12-04 20:38:33 +05:30
end
2023-05-27 22:25:52 +05:30
around do |example|
freeze_time do
example.run
end
2020-04-22 19:07:51 +05:30
end
2019-12-04 20:38:33 +05:30
2023-05-27 22:25:52 +05:30
context 'when domain does not exist' do
2020-04-22 19:07:51 +05:30
it 'responds with 204 no content' do
2023-05-27 22:25:52 +05:30
get api('/internal/pages'), headers: auth_header, params: { host: 'any-domain.gitlab.io' }
2019-12-04 20:38:33 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to be_empty
2019-12-21 20:55:43 +05:30
end
2020-04-22 19:07:51 +05:30
end
2023-05-27 22:25:52 +05:30
context 'when querying a custom domain' do
let_it_be(:pages_domain) { create(:pages_domain, domain: 'pages.io', project: project) }
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
context 'when there are no pages deployed for the related project' do
before do
project.mark_pages_as_not_deployed
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
it 'responds with 204 No Content' do
2023-05-27 22:25:52 +05:30
get api('/internal/pages'), headers: auth_header, params: { host: 'pages.io' }
2019-12-04 20:38:33 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2019-12-21 20:55:43 +05:30
end
2020-04-22 19:07:51 +05:30
end
2019-12-04 20:38:33 +05:30
2020-04-22 19:07:51 +05:30
context 'when there are pages deployed for the related project' do
2023-05-27 22:25:52 +05:30
before do
project.mark_pages_as_deployed
end
2020-04-08 14:13:33 +05:30
2023-05-27 22:25:52 +05:30
it 'domain lookup is case insensitive' do
get api('/internal/pages'), headers: auth_header, params: { host: 'Pages.IO' }
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
end
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
it 'responds with the correct domain configuration' do
2023-05-27 22:25:52 +05:30
get api('/internal/pages'), headers: auth_header, params: { host: 'pages.io' }
2019-12-21 20:55:43 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain')
2019-12-21 20:55:43 +05:30
2020-04-22 19:07:51 +05:30
expect(json_response['certificate']).to eq(pages_domain.certificate)
expect(json_response['key']).to eq(pages_domain.key)
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
deployment = project.pages_metadatum.pages_deployment
2020-04-22 19:07:51 +05:30
expect(json_response['lookup_paths']).to eq(
[
{
'project_id' => project.id,
'access_control' => false,
'https_only' => false,
'prefix' => '/',
'source' => {
2021-01-29 00:20:46 +05:30
'type' => 'zip',
'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
2023-05-27 22:25:52 +05:30
},
2023-06-20 00:43:36 +05:30
'unique_host' => nil,
'root_directory' => deployment.root_directory
2020-04-22 19:07:51 +05:30
}
]
)
2019-12-21 20:55:43 +05:30
end
end
2020-04-22 19:07:51 +05:30
end
2019-12-21 20:55:43 +05:30
2023-05-27 22:25:52 +05:30
context 'when querying a unique domain' do
before_all do
project.project_setting.update!(
pages_unique_domain: 'unique-domain',
pages_unique_domain_enabled: true
)
end
2019-12-21 20:55:43 +05:30
2023-05-27 22:25:52 +05:30
context 'when there are no pages deployed for the related project' do
before do
project.mark_pages_as_not_deployed
end
it 'responds with 204 No Content' do
get api('/internal/pages'), headers: auth_header, params: { host: 'unique-domain.example.com' }
expect(response).to have_gitlab_http_status(:no_content)
end
2020-04-22 19:07:51 +05:30
end
2019-12-21 20:55:43 +05:30
2023-05-27 22:25:52 +05:30
context 'when there are pages deployed for the related project' do
before do
project.mark_pages_as_deployed
end
context 'when the feature flag is disabled' do
before do
stub_feature_flags(pages_unique_domain: false)
end
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
context 'when there are no pages deployed for the related project' do
it 'responds with 204 No Content' do
get api('/internal/pages'), headers: auth_header, params: { host: 'unique-domain.example.com' }
expect(response).to have_gitlab_http_status(:no_content)
end
end
end
context 'when the unique domain is disabled' do
before do
project.project_setting.update!(pages_unique_domain_enabled: false)
end
context 'when there are no pages deployed for the related project' do
it 'responds with 204 No Content' do
get api('/internal/pages'), headers: auth_header, params: { host: 'unique-domain.example.com' }
expect(response).to have_gitlab_http_status(:no_content)
end
end
end
it 'domain lookup is case insensitive' do
get api('/internal/pages'), headers: auth_header, params: { host: 'Unique-Domain.example.com' }
expect(response).to have_gitlab_http_status(:ok)
end
it 'responds with the correct domain configuration' do
get api('/internal/pages'), headers: auth_header, params: { host: 'unique-domain.example.com' }
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain')
2021-01-29 00:20:46 +05:30
deployment = project.pages_metadatum.pages_deployment
2020-04-22 19:07:51 +05:30
expect(json_response['lookup_paths']).to eq(
[
{
'project_id' => project.id,
'access_control' => false,
'https_only' => false,
2023-05-27 22:25:52 +05:30
'prefix' => '/',
2020-04-22 19:07:51 +05:30
'source' => {
2021-01-29 00:20:46 +05:30
'type' => 'zip',
'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
2023-05-27 22:25:52 +05:30
},
2023-06-20 00:43:36 +05:30
'unique_host' => 'unique-domain.example.com',
'root_directory' => 'public'
2020-04-22 19:07:51 +05:30
}
]
)
2019-12-21 20:55:43 +05:30
end
2020-04-22 19:07:51 +05:30
end
2023-05-27 22:25:52 +05:30
end
2019-12-04 20:38:33 +05:30
2023-05-27 22:25:52 +05:30
context 'when querying a namespaced domain' do
before do
allow(Settings.pages).to receive(:host).and_return('gitlab-pages.io')
allow(Gitlab.config.pages).to receive(:url).and_return("http://gitlab-pages.io")
end
2021-01-29 00:20:46 +05:30
2023-05-27 22:25:52 +05:30
context 'when there are no pages deployed for the related project' do
before do
project.mark_pages_as_not_deployed
2021-01-29 00:20:46 +05:30
end
2023-05-27 22:25:52 +05:30
it 'responds with 204 No Content' do
get api('/internal/pages'), headers: auth_header, params: { host: 'mygroup.gitlab-pages.io' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain')
expect(json_response['lookup_paths']).to eq([])
end
2021-01-29 00:20:46 +05:30
end
2023-05-27 22:25:52 +05:30
context 'when there are pages deployed for the related project' do
before do
project.mark_pages_as_deployed
end
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
context 'with a regular project' do
it 'responds with the correct domain configuration' do
get api('/internal/pages'), headers: auth_header, params: { host: 'mygroup.gitlab-pages.io' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain')
deployment = project.pages_metadatum.pages_deployment
expect(json_response['lookup_paths']).to eq(
[
{
'project_id' => project.id,
'access_control' => false,
'https_only' => false,
'prefix' => '/myproject/',
'source' => {
'type' => 'zip',
'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
},
2023-06-20 00:43:36 +05:30
'unique_host' => nil,
'root_directory' => 'public'
2023-05-27 22:25:52 +05:30
}
]
)
end
end
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
it 'avoids N+1 queries' do
control = ActiveRecord::QueryRecorder.new do
get api('/internal/pages'), headers: auth_header, params: { host: 'mygroup.gitlab-pages.io' }
end
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
3.times do
project = create(:project, group: group)
project.mark_pages_as_deployed
end
expect { get api('/internal/pages'), headers: auth_header, params: { host: 'mygroup.gitlab-pages.io' } }
.not_to exceed_query_limit(control)
end
context 'with a group root project' do
before do
project.update!(path: 'mygroup.gitlab-pages.io')
end
it 'responds with the correct domain configuration' do
get api('/internal/pages'), headers: auth_header, params: { host: 'mygroup.gitlab-pages.io' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain')
deployment = project.pages_metadatum.pages_deployment
expect(json_response['lookup_paths']).to eq(
[
{
'project_id' => project.id,
'access_control' => false,
'https_only' => false,
'prefix' => '/',
'source' => {
'type' => 'zip',
'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
},
2023-06-20 00:43:36 +05:30
'unique_host' => nil,
'root_directory' => 'public'
2019-12-21 20:55:43 +05:30
}
2023-05-27 22:25:52 +05:30
]
)
end
2019-12-04 20:38:33 +05:30
end
end
end
end
end
end