Commit b3b2ceee authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'ajk-wiki-owner' into 'master'

Ensure HasWiki constructs wikis with users as owners

See merge request gitlab-org/gitlab!39265
parents b3bede33 84f5ea34
......@@ -17,7 +17,7 @@ module HasWiki
def wiki
strong_memoize(:wiki) do
Wiki.for_container(self, self.owner)
Wiki.for_container(self, self.default_owner)
end
end
......
......@@ -563,6 +563,10 @@ class Group < Namespace
all_projects.update_all(shared_runners_enabled: false)
end
def default_owner
owners.first || parent&.default_owner || owner
end
private
def update_two_factor_requirement
......
......@@ -1395,6 +1395,16 @@ class Project < ApplicationRecord
group || namespace.try(:owner)
end
def default_owner
obj = owner
if obj.respond_to?(:default_owner)
obj.default_owner
else
obj
end
end
def to_ability_name
model_name.singular
end
......
......@@ -35,6 +35,7 @@ class Wiki
def initialize(container, user = nil)
@container = container
@user = user
raise ArgumentError, "user must be a User, got #{user.class}" if user && !user.is_a?(User)
end
def path
......
......@@ -6,8 +6,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
include FilterSpecHelper
let(:project) { create(:project) }
let(:user) { double }
let(:wiki) { ProjectWiki.new(project, user) }
let(:wiki) { ProjectWiki.new(project, nil) }
describe 'validation' do
it 'ensure that a :wiki key exists in context' do
......
......@@ -7,8 +7,7 @@ RSpec.describe Banzai::Filter::WikiLinkFilter do
let(:namespace) { build_stubbed(:namespace, name: "wiki_link_ns") }
let(:project) { build_stubbed(:project, :public, name: "wiki_link_project", namespace: namespace) }
let(:user) { double }
let(:wiki) { ProjectWiki.new(project, user) }
let(:wiki) { ProjectWiki.new(project, nil) }
let(:repository_upload_folder) { Wikis::CreateAttachmentService::ATTACHMENT_PATH }
it "doesn't rewrite absolute links" do
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe Banzai::Pipeline::WikiPipeline do
let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") }
let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
let_it_be(:wiki) { ProjectWiki.new(project, double(:user)) }
let_it_be(:wiki) { ProjectWiki.new(project, nil) }
let_it_be(:page) { build(:wiki_page, wiki: wiki, title: 'nested/twice/start-page') }
describe 'TableOfContents' do
......
......@@ -1541,4 +1541,48 @@ RSpec.describe Group do
end
end
end
describe '#default_owner' do
let(:group) { build(:group) }
context 'the group has owners' do
before do
group.add_owner(create(:user))
group.add_owner(create(:user))
end
it 'is the first owner' do
expect(group.default_owner)
.to eq(group.owners.first)
.and be_a(User)
end
end
context 'the group has a parent' do
let(:parent) { build(:group) }
before do
group.parent = parent
parent.add_owner(create(:user))
end
it 'is the first owner of the parent' do
expect(group.default_owner)
.to eq(parent.default_owner)
.and be_a(User)
end
end
context 'we fallback to group.owner' do
before do
group.owner = build(:user)
end
it 'is the group.owner' do
expect(group.default_owner)
.to eq(group.owner)
.and be_a(User)
end
end
end
end
......@@ -1090,6 +1090,30 @@ RSpec.describe Project do
end
end
describe '#default_owner' do
let_it_be(:owner) { create(:user) }
let_it_be(:namespace) { create(:namespace, owner: owner) }
context 'the project does not have a group' do
let(:project) { build(:project, namespace: namespace) }
it 'is the namespace owner' do
expect(project.default_owner).to eq(owner)
end
end
context 'the project is in a group' do
let(:group) { build(:group) }
let(:project) { build(:project, group: group, namespace: namespace) }
it 'is the group owner' do
allow(group).to receive(:default_owner).and_return(Object.new)
expect(project.default_owner).to eq(group.default_owner)
end
end
end
describe '#external_wiki' do
let(:project) { create(:project) }
......
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Wiki do
describe '.new' do
it 'verifies that the user is a User' do
expect { described_class.new(double, 1) }.to raise_error(ArgumentError)
expect { described_class.new(double, build(:group)) }.to raise_error(ArgumentError)
expect { described_class.new(double, build(:user)) }.not_to raise_error
expect { described_class.new(double, nil) }.not_to raise_error
end
end
end
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