summaryrefslogtreecommitdiff
path: root/spec/migrations/replace_external_wiki_triggers_spec.rb
blob: 392ef76c5baba3ec2bb5144e483802744bc5a3f8 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# frozen_string_literal: true

require 'spec_helper'

require_migration!

RSpec.describe ReplaceExternalWikiTriggers do
  let(:migration) { described_class.new }
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:integrations) { table(:integrations) }

  before do
    @namespace = namespaces.create!(name: 'foo', path: 'foo')
    @project = projects.create!(namespace_id: @namespace.id)
  end

  def create_external_wiki_integration(**attrs)
    attrs.merge!(type_info)

    integrations.create!(**attrs)
  end

  def has_external_wiki
    !!@project.reload.has_external_wiki
  end

  shared_examples 'external wiki triggers' do
    describe 'INSERT trigger' do
      it 'sets `has_external_wiki` to true when active external wiki integration is inserted' do
        expect do
          create_external_wiki_integration(active: true, project_id: @project.id)
        end.to change { has_external_wiki }.to(true)
      end

      it 'does not set `has_external_wiki` to true when integration is for a different project' do
        different_project = projects.create!(namespace_id: @namespace.id)

        expect do
          create_external_wiki_integration(active: true, project_id: different_project.id)
        end.not_to change { has_external_wiki }
      end

      it 'does not set `has_external_wiki` to true when inactive external wiki integration is inserted' do
        expect do
          create_external_wiki_integration(active: false, project_id: @project.id)
        end.not_to change { has_external_wiki }
      end

      it 'does not set `has_external_wiki` to true when active other service is inserted' do
        expect do
          integrations.create!(type_new: 'Integrations::MyService', type: 'MyService', active: true, project_id: @project.id)
        end.not_to change { has_external_wiki }
      end
    end

    describe 'UPDATE trigger' do
      it 'sets `has_external_wiki` to true when `ExternalWikiService` is made active' do
        service = create_external_wiki_integration(active: false, project_id: @project.id)

        expect do
          service.update!(active: true)
        end.to change { has_external_wiki }.to(true)
      end

      it 'sets `has_external_wiki` to false when integration is made inactive' do
        service = create_external_wiki_integration(active: true, project_id: @project.id)

        expect do
          service.update!(active: false)
        end.to change { has_external_wiki }.to(false)
      end

      it 'does not change `has_external_wiki` when integration is for a different project' do
        different_project = projects.create!(namespace_id: @namespace.id)
        service = create_external_wiki_integration(active: false, project_id: different_project.id)

        expect do
          service.update!(active: true)
        end.not_to change { has_external_wiki }
      end
    end

    describe 'DELETE trigger' do
      it 'sets `has_external_wiki` to false when integration is deleted' do
        service = create_external_wiki_integration(active: true, project_id: @project.id)

        expect do
          service.delete
        end.to change { has_external_wiki }.to(false)
      end

      it 'does not change `has_external_wiki` when integration is for a different project' do
        different_project = projects.create!(namespace_id: @namespace.id)
        service = create_external_wiki_integration(active: true, project_id: different_project.id)

        expect do
          service.delete
        end.not_to change { has_external_wiki }
      end
    end
  end

  describe '#up' do
    before do
      migrate!
    end

    context 'when integrations are created with the new STI value' do
      let(:type_info) { { type_new: 'Integrations::ExternalWiki' } }

      it_behaves_like 'external wiki triggers'
    end

    context 'when integrations are created with the old STI value' do
      let(:type_info) { { type: 'ExternalWikiService' } }

      it_behaves_like 'external wiki triggers'
    end
  end

  describe '#down' do
    before do
      migration.up
      migration.down
    end

    let(:type_info) { { type: 'ExternalWikiService' } }

    it_behaves_like 'external wiki triggers'
  end
end