1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Internal::Pages, feature_category: :pages do
let_it_be(:group) { create(:group) }
let_it_be_with_reload(:project) { create(:project, group: group) }
let(:auth_header) do
{
Gitlab::Pages::INTERNAL_API_REQUEST_HEADER => JWT.encode(
{ 'iss' => 'gitlab-pages' },
Gitlab::Pages.secret, 'HS256')
}
end
before do
allow(Gitlab::Pages)
.to receive(:secret)
.and_return(SecureRandom.random_bytes(Gitlab::Pages::SECRET_LENGTH))
stub_pages_object_storage(::Pages::DeploymentUploader)
end
describe 'GET /internal/pages/status' do
it 'responds with 401 Unauthorized' do
get api('/internal/pages/status')
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'responds with 204 no content' do
get api('/internal/pages/status'), headers: auth_header
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to be_empty
end
end
describe 'GET /internal/pages' do
context 'when not authenticated' do
it 'responds with 401 Unauthorized' do
get api('/internal/pages')
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when authenticated', :freeze_time do
context 'when domain does not exist' do
it 'responds with 204 no content' do
get api('/internal/pages'), headers: auth_header, params: { host: 'any-domain.gitlab.io' }
expect(response).to have_gitlab_http_status(:no_content)
expect(response.body).to be_empty
end
end
context 'when querying a custom domain' do
let_it_be(:pages_domain) { create(:pages_domain, domain: 'pages.io', project: project) }
# We need to ensure not to return the unique domain when requesting a custom domain
# https://gitlab.com/gitlab-org/gitlab/-/issues/426435
before_all do
project.project_setting.update!(
pages_unique_domain: 'unique-domain',
pages_unique_domain_enabled: true
)
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: 'pages.io' }
expect(response).to have_gitlab_http_status(:no_content)
end
end
context 'when there are pages deployed for the related project' do
let!(:deployment) { create(:pages_deployment, project: project) }
it 'domain lookup is case insensitive' do
get api('/internal/pages'), headers: auth_header, params: { host: 'Pages.IO' }
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: 'pages.io' }
expect(response).to have_gitlab_http_status(:ok)
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' => '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
},
'unique_host' => nil,
'root_directory' => deployment.root_directory
}
]
)
end
end
end
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
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
context 'when there are pages deployed for the related project' do
let!(:deployment) { create(:pages_deployment, project: project) }
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' }
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(
[
{
'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
},
'unique_host' => 'unique-domain.example.com',
'root_directory' => 'public'
}
]
)
end
end
end
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
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: "#{group.path}.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
end
context 'when there are pages deployed for the related project' do
let!(:deployment) { create(:pages_deployment, project: project) }
context 'with a regular project' do
it 'responds with the correct domain configuration' do
get api('/internal/pages'), headers: auth_header, params: { host: "#{group.path}.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(
[
{
'project_id' => project.id,
'access_control' => false,
'https_only' => false,
'prefix' => "/#{project.path}/",
'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
},
'unique_host' => nil,
'root_directory' => 'public'
}
]
)
end
end
it 'avoids N+1 queries' do
control = ActiveRecord::QueryRecorder.new do
get api('/internal/pages'), headers: auth_header, params: { host: "#{group.path}.gitlab-pages.io" }
end
3.times do
project = create(:project, group: group)
create(:pages_deployment, project: project)
end
expect { get api('/internal/pages'), headers: auth_header, params: { host: "#{group.path}.gitlab-pages.io" } }
.not_to exceed_query_limit(control)
end
context 'with a group root project' do
before do
project.update!(path: "#{group.path}.gitlab-pages.io")
end
it 'responds with the correct domain configuration' do
get api('/internal/pages'), headers: auth_header, params: { host: "#{group.path}.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(
[
{
'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
},
'unique_host' => nil,
'root_directory' => 'public'
}
]
)
end
end
end
end
end
end
end
|