Commit 9082e910 authored by Krasimir Angelov's avatar Krasimir Angelov

Fix lookup of namespace by pages host

Ensure only top level namespaces are returned
parent 13590b3f
......@@ -130,7 +130,7 @@ class Namespace < ApplicationRecord
return unless host.ends_with?(gitlab_host)
name = host.delete_suffix(gitlab_host)
Namespace.find_by_path(name)
Namespace.where(parent_id: nil).find_by_path(name)
end
# overridden in ee
......
......@@ -201,6 +201,19 @@ describe Namespace do
expect(described_class.find_by_pages_host(host)).to eq(namespace)
end
it 'finds correct namespace even if there is sub-namespace with same path' do
group = create(:group, path: 'pages')
subgroup = create(:group, :nested, path: 'pages')
host = "pages.#{Settings.pages.host.upcase}"
# This is to ensure the method does the right thing
# becasue there is no way to be 100% sure which exact
# group will be returned by the database
expect(described_class).to receive(:where).with(parent_id: nil).and_call_original
expect(described_class.find_by_pages_host(host)).to eq(group)
end
it "returns no result if the provided host is not subdomain of the Pages host" do
create(:namespace, name: 'namespace.io')
host = "namespace.io"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment