diff --git a/app/models/pages/lookup_path.rb b/app/models/pages/lookup_path.rb index d79e4a5c12eab2c73ba7828879461aec1195a06a..91707cbe92e9b1284723877bfe27bb71815ae50c 100644 --- a/app/models/pages/lookup_path.rb +++ b/app/models/pages/lookup_path.rb @@ -68,6 +68,11 @@ def root_directory end strong_memoize_attr :root_directory + def default_domain_redirect + project&.project_setting&.pages_default_domain_redirect + end + strong_memoize_attr :default_domain_redirect + private attr_reader :project, :deployment, :trim_prefix, :domain diff --git a/lib/api/entities/internal/pages/lookup_path.rb b/lib/api/entities/internal/pages/lookup_path.rb index 3d16864e58720e401f7f7f10d7cd64484bb98975..f6272fd7f92b4d178d8262697a0ffee269b9041c 100644 --- a/lib/api/entities/internal/pages/lookup_path.rb +++ b/lib/api/entities/internal/pages/lookup_path.rb @@ -11,7 +11,8 @@ class LookupPath < Grape::Entity :project_id, :source, :unique_host, - :root_directory + :root_directory, + :default_domain_redirect end end end diff --git a/spec/fixtures/api/schemas/internal/pages/lookup_path.json b/spec/fixtures/api/schemas/internal/pages/lookup_path.json index fba3efc4ded1605b137b3e5ecf5cd04669c1a066..1cc5e7d4cbd5ea650b2d82219df07f7eced98401 100644 --- a/spec/fixtures/api/schemas/internal/pages/lookup_path.json +++ b/spec/fixtures/api/schemas/internal/pages/lookup_path.json @@ -63,6 +63,12 @@ }, "root_directory": { "type": "string" + }, + "default_domain_redirect": { + "type": [ + "string", + "null" + ] } }, "additionalProperties": false diff --git a/spec/models/pages/lookup_path_spec.rb b/spec/models/pages/lookup_path_spec.rb index ccca6e7a48a05f83a323d6d26b308a29fe0fea52..a5759659871a67a5b3b1b91f858419fbc9c7701b 100644 --- a/spec/models/pages/lookup_path_spec.rb +++ b/spec/models/pages/lookup_path_spec.rb @@ -50,6 +50,14 @@ end end + describe '#default_domain_redirect' do + it 'delegates to Project#project_setting#pages_default_domain_redirect' do + project.project_setting.pages_default_domain_redirect = 'my.domain.com' + + expect(lookup_path.default_domain_redirect).to eq('my.domain.com') + end + end + describe '#https_only' do subject(:lookup_path) { described_class.new(deployment: deployment, domain: domain) } diff --git a/spec/requests/api/internal/pages_spec.rb b/spec/requests/api/internal/pages_spec.rb index 7e2a778f433959d364ba16d9ce928b4c6d454fd9..d9cac45d487e2671c43912ded0a28762408db4ff 100644 --- a/spec/requests/api/internal/pages_spec.rb +++ b/spec/requests/api/internal/pages_spec.rb @@ -110,7 +110,8 @@ 'file_count' => deployment.file_count }, 'unique_host' => nil, - 'root_directory' => deployment.root_directory + 'root_directory' => deployment.root_directory, + 'default_domain_redirect' => nil } ] ) @@ -179,7 +180,53 @@ 'file_count' => deployment.file_count }, 'unique_host' => 'unique-domain.example.com', - 'root_directory' => 'public' + 'root_directory' => 'public', + 'default_domain_redirect' => nil + } + ] + ) + end + end + end + + context 'when querying a default domain redirect' do + let_it_be(:pages_domain) { create(:pages_domain, domain: 'pages.io', project: project) } + + context 'when there are pages deployed for the related project' do + let!(:deployment) { create(:pages_deployment, project: project) } + + before do + project.project_setting.update!( + pages_default_domain_redirect: 'https://pages.io', + pages_unique_domain: 'unique-domain', + pages_unique_domain_enabled: true + ) + 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', + 'default_domain_redirect' => 'https://pages.io' } ] ) @@ -229,7 +276,8 @@ 'file_count' => deployment.file_count }, 'unique_host' => nil, - 'root_directory' => 'public' + 'root_directory' => 'public', + 'default_domain_redirect' => nil } ] ) @@ -277,7 +325,8 @@ 'file_count' => deployment.file_count }, 'unique_host' => nil, - 'root_directory' => 'public' + 'root_directory' => 'public', + 'default_domain_redirect' => nil } ] )