debian-mirror-gitlab/spec/services/wiki_pages/create_service_spec.rb

99 lines
2.4 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe WikiPages::CreateService do
2018-10-15 14:42:47 +05:30
let(:project) { create(:project, :wiki_repo) }
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
2020-04-22 19:07:51 +05:30
let(:page_title) { 'Title' }
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
let(:opts) do
{
2020-04-22 19:07:51 +05:30
title: page_title,
2017-08-17 22:00:37 +05:30
content: 'Content for wiki page',
format: 'markdown'
}
end
2017-09-10 17:25:29 +05:30
subject(:service) { described_class.new(project, user, opts) }
before do
project.add_developer(user)
end
2017-08-17 22:00:37 +05:30
describe '#execute' do
2017-09-10 17:25:29 +05:30
it 'creates wiki page with valid attributes' do
page = service.execute
expect(page).to be_valid
expect(page.title).to eq(opts[:title])
expect(page.content).to eq(opts[:content])
expect(page.format).to eq(opts[:format].to_sym)
end
it 'executes webhooks' do
2020-04-22 19:07:51 +05:30
expect(service).to receive(:execute_hooks).once.with(WikiPage)
2017-09-10 17:25:29 +05:30
service.execute
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
it 'counts wiki page creation' do
counter = Gitlab::UsageDataCounters::WikiPageCounter
expect { service.execute }.to change { counter.read(:create) }.by 1
end
2020-04-22 19:07:51 +05:30
shared_examples 'correct event created' do
it 'creates appropriate events' do
expect { service.execute }.to change { Event.count }.by 1
expect(Event.recent.first).to have_attributes(
action: Event::CREATED,
target: have_attributes(canonical_slug: page_title)
)
end
end
context 'the new page is at the top level' do
let(:page_title) { 'root-level-page' }
include_examples 'correct event created'
end
context 'the new page is in a subsection' do
let(:page_title) { 'subsection/page' }
include_examples 'correct event created'
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 }.not_to change(Event, :count)
end
end
2019-10-12 21:52:04 +05:30
context 'when the options are bad' do
2020-04-22 19:07:51 +05:30
let(:page_title) { '' }
2019-10-12 21:52:04 +05:30
it 'does not count a creation event' do
counter = Gitlab::UsageDataCounters::WikiPageCounter
expect { service.execute }.not_to change { counter.read(:create) }
end
2020-04-22 19:07:51 +05:30
it 'does not record the activity' do
expect { service.execute }.not_to change(Event, :count)
end
2019-10-12 21:52:04 +05:30
it 'reports the error' do
expect(service.execute).to be_invalid
.and have_attributes(errors: be_present)
end
end
2017-08-17 22:00:37 +05:30
end
end