summaryrefslogtreecommitdiff
path: root/spec/migrations/rename_services_to_integrations_spec.rb
blob: 812dd5efecb201a1426c1aa71a86e844becab87d (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# frozen_string_literal: true

require 'spec_helper'

require_migration!

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

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

  RSpec.shared_examples 'a table (or view) with triggers' do
    describe 'INSERT tracker trigger' do
      it 'sets `has_external_issue_tracker` to true when active `issue_tracker` is inserted' do
        expect do
          subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)
        end.to change { @project.reload.has_external_issue_tracker }.to(true)
      end

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

        expect do
          subject.create!(category: 'issue_tracker', active: true, project_id: different_project.id)
        end.not_to change { @project.reload.has_external_issue_tracker }
      end

      it 'does not set `has_external_issue_tracker` to true when inactive `issue_tracker` is inserted' do
        expect do
          subject.create!(category: 'issue_tracker', active: false, project_id: @project.id)
        end.not_to change { @project.reload.has_external_issue_tracker }
      end

      it 'does not set `has_external_issue_tracker` to true when a non-`issue tracker` active integration is inserted' do
        expect do
          subject.create!(category: 'my_type', active: true, project_id: @project.id)
        end.not_to change { @project.reload.has_external_issue_tracker }
      end
    end

    describe 'UPDATE tracker trigger' do
      it 'sets `has_external_issue_tracker` to true when `issue_tracker` is made active' do
        integration = subject.create!(category: 'issue_tracker', active: false, project_id: @project.id)

        expect do
          integration.update!(active: true)
        end.to change { @project.reload.has_external_issue_tracker }.to(true)
      end

      it 'sets `has_external_issue_tracker` to false when `issue_tracker` is made inactive' do
        integration = subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)

        expect do
          integration.update!(active: false)
        end.to change { @project.reload.has_external_issue_tracker }.to(false)
      end

      it 'sets `has_external_issue_tracker` to false when `issue_tracker` is made inactive, and an inactive `issue_tracker` exists' do
        subject.create!(category: 'issue_tracker', active: false, project_id: @project.id)
        integration = subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)

        expect do
          integration.update!(active: false)
        end.to change { @project.reload.has_external_issue_tracker }.to(false)
      end

      it 'does not change `has_external_issue_tracker` when `issue_tracker` is made inactive, if an active `issue_tracker` exists' do
        subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)
        integration = subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)

        expect do
          integration.update!(active: false)
        end.not_to change { @project.reload.has_external_issue_tracker }
      end

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

        expect do
          integration.update!(active: true)
        end.not_to change { @project.reload.has_external_issue_tracker }
      end
    end

    describe 'DELETE tracker trigger' do
      it 'sets `has_external_issue_tracker` to false when `issue_tracker` is deleted' do
        integration = subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)

        expect do
          integration.delete
        end.to change { @project.reload.has_external_issue_tracker }.to(false)
      end

      it 'sets `has_external_issue_tracker` to false when `issue_tracker` is deleted, if an inactive `issue_tracker` still exists' do
        subject.create!(category: 'issue_tracker', active: false, project_id: @project.id)
        integration = subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)

        expect do
          integration.delete
        end.to change { @project.reload.has_external_issue_tracker }.to(false)
      end

      it 'does not change `has_external_issue_tracker` when `issue_tracker` is deleted, if an active `issue_tracker` still exists' do
        subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)
        integration = subject.create!(category: 'issue_tracker', active: true, project_id: @project.id)

        expect do
          integration.delete
        end.not_to change { @project.reload.has_external_issue_tracker }
      end

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

        expect do
          integration.delete
        end.not_to change { @project.reload.has_external_issue_tracker }
      end
    end

    describe 'INSERT wiki trigger' do
      it 'sets `has_external_wiki` to true when active `ExternalWikiService` is inserted' do
        expect do
          subject.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)
        end.to change { @project.reload.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
          subject.create!(type: 'ExternalWikiService', active: true, project_id: different_project.id)
        end.not_to change { @project.reload.has_external_wiki }
      end

      it 'does not set `has_external_wiki` to true when inactive `ExternalWikiService` is inserted' do
        expect do
          subject.create!(type: 'ExternalWikiService', active: false, project_id: @project.id)
        end.not_to change { @project.reload.has_external_wiki }
      end

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

    describe 'UPDATE wiki trigger' do
      it 'sets `has_external_wiki` to true when `ExternalWikiService` is made active' do
        integration = subject.create!(type: 'ExternalWikiService', active: false, project_id: @project.id)

        expect do
          integration.update!(active: true)
        end.to change { @project.reload.has_external_wiki }.to(true)
      end

      it 'sets `has_external_wiki` to false when `ExternalWikiService` is made inactive' do
        integration = subject.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)

        expect do
          integration.update!(active: false)
        end.to change { @project.reload.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)
        integration = subject.create!(type: 'ExternalWikiService', active: false, project_id: different_project.id)

        expect do
          integration.update!(active: true)
        end.not_to change { @project.reload.has_external_wiki }
      end
    end

    describe 'DELETE wiki trigger' do
      it 'sets `has_external_wiki` to false when `ExternalWikiService` is deleted' do
        integration = subject.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)

        expect do
          integration.delete
        end.to change { @project.reload.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)
        integration = subject.create!(type: 'ExternalWikiService', active: true, project_id: different_project.id)

        expect do
          integration.delete
        end.not_to change { @project.reload.has_external_wiki }
      end
    end
  end

  RSpec.shared_examples 'a table (or view) without triggers' do
    specify do
      number_of_triggers = ActiveRecord::Base.connection
                            .execute("SELECT count(*) FROM information_schema.triggers WHERE event_object_table = '#{subject.table_name}'")
                            .first['count']

      expect(number_of_triggers).to eq(0)
    end
  end

  describe '#up' do
    before do
      # LOCK TABLE statements must be in a transaction
      ActiveRecord::Base.transaction { migrate! }
    end

    context 'the integrations table' do
      subject { integrations }

      it_behaves_like 'a table (or view) with triggers'
    end

    context 'the services table' do
      subject { services }

      it_behaves_like 'a table (or view) without triggers'
    end
  end

  describe '#down' do
    before do
      # LOCK TABLE statements must be in a transaction
      ActiveRecord::Base.transaction do
        migration.up
        migration.down
      end
    end

    context 'the services table' do
      subject { services }

      it_behaves_like 'a table (or view) with triggers'
    end

    context 'the integrations table' do
      subject { integrations }

      it_behaves_like 'a table (or view) without triggers'
    end
  end
end