From 676675dc0b95194be72dfa13829b5ba06e0d1844 Mon Sep 17 00:00:00 2001 From: Krasimir Angelov Date: Fri, 6 Sep 2019 17:20:05 +1200 Subject: Add support for custom domains to the internal Pages API Update the `/internal/pages` endpoint to return virtual domain configuration for custom domains. --- .../api/schemas/internal/pages/lookup_path.json | 25 +++++++++ .../api/schemas/internal/pages/virtual_domain.json | 16 ++++++ spec/models/pages/lookup_path_spec.rb | 64 ++++++++++++++++++++++ spec/requests/api/internal/pages_spec.rb | 28 +++++++++- 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/api/schemas/internal/pages/lookup_path.json create mode 100644 spec/fixtures/api/schemas/internal/pages/virtual_domain.json create mode 100644 spec/models/pages/lookup_path_spec.rb (limited to 'spec') diff --git a/spec/fixtures/api/schemas/internal/pages/lookup_path.json b/spec/fixtures/api/schemas/internal/pages/lookup_path.json new file mode 100644 index 00000000000..b2b3d3f9d0a --- /dev/null +++ b/spec/fixtures/api/schemas/internal/pages/lookup_path.json @@ -0,0 +1,25 @@ +{ + "type": "object", + "required": [ + "project_id", + "https_only", + "access_control", + "source", + "prefix" + ], + "properties": { + "project_id": { "type": "integer" }, + "https_only": { "type": "boolean" }, + "access_control": { "type": "boolean" }, + "source": { "type": "object", + "required": ["type", "path"], + "properties" : { + "type": { "type": "string", "enum": ["file"] }, + "path": { "type": "string" } + }, + "additionalProperties": false + }, + "prefix": { "type": "string" } + }, + "additionalProperties": false +} diff --git a/spec/fixtures/api/schemas/internal/pages/virtual_domain.json b/spec/fixtures/api/schemas/internal/pages/virtual_domain.json new file mode 100644 index 00000000000..02df69026b0 --- /dev/null +++ b/spec/fixtures/api/schemas/internal/pages/virtual_domain.json @@ -0,0 +1,16 @@ +{ + "type": "object", + "required": [ + "lookup_paths" + ], + "optional": [ + "certificate", + "key" + ], + "properties": { + "certificate": { "type": ["string", "null"] }, + "key": { "type": ["string", "null"] }, + "lookup_paths": { "type": "array", "items": { "$ref": "lookup_path.json" } } + }, + "additionalProperties": false +} diff --git a/spec/models/pages/lookup_path_spec.rb b/spec/models/pages/lookup_path_spec.rb new file mode 100644 index 00000000000..2146b0c9abd --- /dev/null +++ b/spec/models/pages/lookup_path_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Pages::LookupPath do + let(:project) do + instance_double(Project, + id: 12345, + private_pages?: true, + pages_https_only?: true, + full_path: 'the/full/path' + ) + end + + subject(:lookup_path) { described_class.new(project) } + + describe '#project_id' do + it 'delegates to Project#id' do + expect(lookup_path.project_id).to eq(12345) + end + end + + describe '#access_control' do + it 'delegates to Project#private_pages?' do + expect(lookup_path.access_control).to eq(true) + end + end + + describe '#https_only' do + subject(:lookup_path) { described_class.new(project, domain: domain) } + + context 'when no domain provided' do + let(:domain) { nil } + + it 'delegates to Project#pages_https_only?' do + expect(lookup_path.https_only).to eq(true) + end + end + + context 'when there is domain provided' do + let(:domain) { instance_double(PagesDomain, https?: false) } + + it 'takes into account the https setting of the domain' do + expect(lookup_path.https_only).to eq(false) + end + end + end + + describe '#source' do + it 'sets the source type to "file"' do + expect(lookup_path.source[:type]).to eq('file') + end + + it 'sets the source path to the project full path suffixed with "public/' do + expect(lookup_path.source[:path]).to eq('the/full/path/public/') + end + end + + describe '#prefix' do + it 'returns "/"' do + expect(lookup_path.prefix).to eq('/') + end + end +end diff --git a/spec/requests/api/internal/pages_spec.rb b/spec/requests/api/internal/pages_spec.rb index 0b3c5be9c45..e1b563b92f4 100644 --- a/spec/requests/api/internal/pages_spec.rb +++ b/spec/requests/api/internal/pages_spec.rb @@ -43,10 +43,32 @@ describe API::Internal::Pages do super(host, headers) end - it 'responds with 200 OK' do - query_host('pages.gitlab.io') + context 'not existing host' do + it 'responds with 404 Not Found' do + query_host('pages.gitlab.io') + + expect(response).to have_gitlab_http_status(404) + 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) } + + it 'responds with the correct domain configuration' do + query_host('pages.gitlab.io') + + expect(response).to have_gitlab_http_status(200) + 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(response).to have_gitlab_http_status(200) + lookup_path = json_response['lookup_paths'][0] + expect(lookup_path['prefix']).to eq('/') + expect(lookup_path['source']['path']).to eq('gitlab-org/gitlab-ce/public/') + end end end end -- cgit v1.2.3