2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-20 13:31:21 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe WikiPages::UpdateService do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2016-12-20 13:31:21 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-06-01 19:27:52 +00:00
|
|
|
let(:page) { create(:wiki_page) }
|
2020-03-24 12:09:42 +00:00
|
|
|
let(:page_title) { 'New Title' }
|
2017-06-01 19:27:52 +00:00
|
|
|
|
2016-12-20 13:31:21 +00:00
|
|
|
let(:opts) do
|
|
|
|
{
|
|
|
|
content: 'New content for wiki page',
|
|
|
|
format: 'markdown',
|
2017-07-23 08:19:10 +00:00
|
|
|
message: 'New wiki message',
|
2020-03-24 12:09:42 +00:00
|
|
|
title: page_title
|
2016-12-20 13:31:21 +00:00
|
|
|
}
|
|
|
|
end
|
2017-06-01 19:27:52 +00:00
|
|
|
|
|
|
|
subject(:service) { described_class.new(project, user, opts) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
2016-12-20 13:31:21 +00:00
|
|
|
|
|
|
|
describe '#execute' do
|
2017-06-01 19:27:52 +00:00
|
|
|
it 'updates the wiki page' do
|
|
|
|
updated_page = service.execute(page)
|
|
|
|
|
|
|
|
expect(updated_page).to be_valid
|
|
|
|
expect(updated_page.message).to eq(opts[:message])
|
|
|
|
expect(updated_page.content).to eq(opts[:content])
|
|
|
|
expect(updated_page.format).to eq(opts[:format].to_sym)
|
2020-03-24 12:09:42 +00:00
|
|
|
expect(updated_page.title).to eq(page_title)
|
2017-06-01 19:27:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes webhooks' do
|
2020-03-24 12:09:42 +00:00
|
|
|
expect(service).to receive(:execute_hooks).once.with(WikiPage)
|
2017-06-01 19:27:52 +00:00
|
|
|
|
|
|
|
service.execute(page)
|
2016-12-20 13:31:21 +00:00
|
|
|
end
|
2019-07-21 01:26:19 +00:00
|
|
|
|
|
|
|
it 'counts edit events' do
|
|
|
|
counter = Gitlab::UsageDataCounters::WikiPageCounter
|
|
|
|
|
|
|
|
expect { service.execute page }.to change { counter.read(:update) }.by 1
|
|
|
|
end
|
|
|
|
|
2020-03-24 12:09:42 +00:00
|
|
|
shared_examples 'adds activity event' do
|
|
|
|
it 'adds a new wiki page activity event' do
|
|
|
|
expect { service.execute(page) }.to change { Event.count }.by 1
|
|
|
|
|
|
|
|
expect(Event.recent.first).to have_attributes(
|
|
|
|
action: Event::UPDATED,
|
|
|
|
wiki_page: page,
|
|
|
|
target_title: page.title
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the page is at the top level' do
|
|
|
|
let(:page_title) { 'Top level page' }
|
|
|
|
|
|
|
|
include_examples 'adds activity event'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the page is in a subsection' do
|
|
|
|
let(:page_title) { 'Subsection / secondary page' }
|
|
|
|
|
|
|
|
include_examples 'adds activity event'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the feature is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(wiki_events: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not record the activity' do
|
|
|
|
expect { service.execute(page) }.not_to change(Event, :count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-21 01:26:19 +00:00
|
|
|
context 'when the options are bad' do
|
2020-03-24 12:09:42 +00:00
|
|
|
let(:page_title) { '' }
|
2019-07-21 01:26:19 +00:00
|
|
|
|
|
|
|
it 'does not count an edit event' do
|
|
|
|
counter = Gitlab::UsageDataCounters::WikiPageCounter
|
|
|
|
|
|
|
|
expect { service.execute page }.not_to change { counter.read(:update) }
|
|
|
|
end
|
|
|
|
|
2020-03-24 12:09:42 +00:00
|
|
|
it 'does not record the activity' do
|
|
|
|
expect { service.execute page }.not_to change(Event, :count)
|
|
|
|
end
|
|
|
|
|
2019-07-21 01:26:19 +00:00
|
|
|
it 'reports the error' do
|
2020-03-27 12:07:43 +00:00
|
|
|
expect(service.execute(page)).to be_invalid
|
2019-07-21 01:26:19 +00:00
|
|
|
.and have_attributes(errors: be_present)
|
|
|
|
end
|
|
|
|
end
|
2016-12-20 13:31:21 +00:00
|
|
|
end
|
|
|
|
end
|