summaryrefslogtreecommitdiff
path: root/spec/services/wiki_pages/update_service_spec.rb
blob: d5f46e7b2db54fc9f0e4af083dd43c4d51144f6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

require 'spec_helper'

describe WikiPages::UpdateService do
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:page) { create(:wiki_page) }

  let(:opts) do
    {
      content: 'New content for wiki page',
      format: 'markdown',
      message: 'New wiki message',
      title: 'New Title'
    }
  end

  let(:bad_opts) do
    { title: '' }
  end

  subject(:service) { described_class.new(project, user, opts) }

  before do
    project.add_developer(user)
  end

  describe '#execute' do
    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)
      expect(updated_page.title).to eq(opts[:title])
    end

    it 'executes webhooks' do
      expect(service).to receive(:execute_hooks).once
        .with(instance_of(WikiPage), 'update')

      service.execute(page)
    end

    it 'counts edit events' do
      counter = Gitlab::UsageDataCounters::WikiPageCounter

      expect { service.execute page }.to change { counter.read(:update) }.by 1
    end

    context 'when the options are bad' do
      subject(:service) { described_class.new(project, user, bad_opts) }

      it 'does not count an edit event' do
        counter = Gitlab::UsageDataCounters::WikiPageCounter

        expect { service.execute page }.not_to change { counter.read(:update) }
      end

      it 'reports the error' do
        expect(service.execute page).to be_invalid
          .and have_attributes(errors: be_present)
      end
    end
  end
end