summaryrefslogtreecommitdiff
path: root/spec/models/project_services/confluence_service_spec.rb
blob: 6c7ba2c9f329a88c3eec91780decc49fd1e179e4 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ConfluenceService do
  describe 'Associations' do
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
  end

  describe 'Validations' do
    before do
      subject.active = active
    end

    context 'when service is active' do
      let(:active) { true }

      it { is_expected.not_to allow_value('https://example.com').for(:confluence_url) }
      it { is_expected.not_to allow_value('example.com').for(:confluence_url) }
      it { is_expected.not_to allow_value('foo').for(:confluence_url) }
      it { is_expected.not_to allow_value('ftp://example.atlassian.net/wiki').for(:confluence_url) }
      it { is_expected.not_to allow_value('https://example.atlassian.net').for(:confluence_url) }
      it { is_expected.not_to allow_value('https://.atlassian.net/wiki').for(:confluence_url) }
      it { is_expected.not_to allow_value('https://example.atlassian.net/wikifoo').for(:confluence_url) }
      it { is_expected.not_to allow_value('').for(:confluence_url) }
      it { is_expected.not_to allow_value(nil).for(:confluence_url) }
      it { is_expected.not_to allow_value('😊').for(:confluence_url) }
      it { is_expected.to allow_value('https://example.atlassian.net/wiki').for(:confluence_url) }
      it { is_expected.to allow_value('http://example.atlassian.net/wiki').for(:confluence_url) }
      it { is_expected.to allow_value('https://example.atlassian.net/wiki/').for(:confluence_url) }
      it { is_expected.to allow_value('http://example.atlassian.net/wiki/').for(:confluence_url) }
      it { is_expected.to allow_value('https://example.atlassian.net/wiki/foo').for(:confluence_url) }

      it { is_expected.to validate_presence_of(:confluence_url) }
    end

    context 'when service is inactive' do
      let(:active) { false }

      it { is_expected.not_to validate_presence_of(:confluence_url) }
      it { is_expected.to allow_value('foo').for(:confluence_url) }
    end
  end

  describe '#help' do
    it 'can correctly return a link to the project wiki when active' do
      project = create(:project)
      subject.project = project
      subject.active = true

      expect(subject.help).to include(Gitlab::Routing.url_helpers.project_wikis_url(project))
    end

    context 'when the project wiki is not enabled' do
      it 'returns nil when both active or inactive', :aggregate_failures do
        project = create(:project, :wiki_disabled)
        subject.project = project

        [true, false].each do |active|
          subject.active = active

          expect(subject.help).to be_nil
        end
      end
    end
  end

  describe 'Caching has_confluence on project_settings' do
    let(:project) { create(:project) }

    subject { project.project_setting.has_confluence? }

    it 'sets the property to true when service is active' do
      create(:confluence_service, project: project, active: true)

      is_expected.to be(true)
    end

    it 'sets the property to false when service is not active' do
      create(:confluence_service, project: project, active: false)

      is_expected.to be(false)
    end

    it 'creates a project_setting record if one was not already created' do
      expect { create(:confluence_service) }.to change { ProjectSetting.count }.by(1)
    end
  end
end