2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::Internal::Pages do
|
|
|
|
describe "GET /internal/pages" do
|
2020-01-01 13:55:28 +05:30
|
|
|
let(:pages_secret) { SecureRandom.random_bytes(Gitlab::Pages::SECRET_LENGTH) }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
before do
|
2020-01-01 13:55:28 +05:30
|
|
|
allow(Gitlab::Pages).to receive(:secret).and_return(pages_secret)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def query_host(host, headers = {})
|
|
|
|
get api("/internal/pages"), headers: headers, params: { host: host }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'feature flag disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(pages_internal_api: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found' do
|
|
|
|
query_host('pages.gitlab.io')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'feature flag enabled' do
|
|
|
|
context 'not authenticated' do
|
|
|
|
it 'responds with 401 Unauthorized' do
|
|
|
|
query_host('pages.gitlab.io')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated' do
|
|
|
|
def query_host(host)
|
|
|
|
jwt_token = JWT.encode({ 'iss' => 'gitlab-pages' }, Gitlab::Pages.secret, 'HS256')
|
|
|
|
headers = { Gitlab::Pages::INTERNAL_API_REQUEST_HEADER => jwt_token }
|
|
|
|
|
|
|
|
super(host, headers)
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def deploy_pages(project)
|
|
|
|
project.mark_pages_as_deployed
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
context 'domain does not exist' do
|
|
|
|
it 'responds with 204 no content' do
|
2019-12-04 20:38:33 +05:30
|
|
|
query_host('pages.gitlab.io')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(response.body).to be_empty
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'custom domain' do
|
|
|
|
let(:namespace) { create(:namespace, name: 'gitlab-org') }
|
|
|
|
let(:project) { create(:project, namespace: namespace, name: 'gitlab-ce') }
|
|
|
|
let!(:pages_domain) { create(:pages_domain, domain: 'pages.gitlab.io', project: project) }
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when there are no pages deployed for the related project' do
|
|
|
|
it 'responds with 204 No Content' do
|
|
|
|
query_host('pages.gitlab.io')
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when there are pages deployed for the related project' do
|
|
|
|
it 'responds with the correct domain configuration' do
|
|
|
|
deploy_pages(project)
|
|
|
|
|
|
|
|
query_host('pages.gitlab.io')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(response).to match_response_schema('internal/pages/virtual_domain')
|
|
|
|
|
|
|
|
expect(json_response['certificate']).to eq(pages_domain.certificate)
|
|
|
|
expect(json_response['key']).to eq(pages_domain.key)
|
|
|
|
|
|
|
|
expect(json_response['lookup_paths']).to eq(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'project_id' => project.id,
|
|
|
|
'access_control' => false,
|
|
|
|
'https_only' => false,
|
|
|
|
'prefix' => '/',
|
|
|
|
'source' => {
|
|
|
|
'type' => 'file',
|
|
|
|
'path' => 'gitlab-org/gitlab-ce/public/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'namespaced domain' do
|
|
|
|
let(:group) { create(:group, name: 'mygroup') }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'regular project' do
|
|
|
|
it 'responds with the correct domain configuration' do
|
|
|
|
project = create(:project, group: group, name: 'myproject')
|
|
|
|
deploy_pages(project)
|
|
|
|
|
|
|
|
query_host('mygroup.gitlab-pages.io')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(response).to match_response_schema('internal/pages/virtual_domain')
|
|
|
|
|
|
|
|
expect(json_response['lookup_paths']).to eq(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'project_id' => project.id,
|
|
|
|
'access_control' => false,
|
|
|
|
'https_only' => false,
|
|
|
|
'prefix' => '/myproject/',
|
|
|
|
'source' => {
|
|
|
|
'type' => 'file',
|
|
|
|
'path' => 'mygroup/myproject/public/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'group root project' do
|
|
|
|
it 'responds with the correct domain configuration' do
|
|
|
|
project = create(:project, group: group, name: 'mygroup.gitlab-pages.io')
|
|
|
|
deploy_pages(project)
|
|
|
|
|
|
|
|
query_host('mygroup.gitlab-pages.io')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(response).to match_response_schema('internal/pages/virtual_domain')
|
|
|
|
|
|
|
|
expect(json_response['lookup_paths']).to eq(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'project_id' => project.id,
|
|
|
|
'access_control' => false,
|
|
|
|
'https_only' => false,
|
|
|
|
'prefix' => '/',
|
|
|
|
'source' => {
|
|
|
|
'type' => 'file',
|
|
|
|
'path' => 'mygroup/mygroup.gitlab-pages.io/public/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|