summaryrefslogtreecommitdiff
path: root/spec/services/projects/propagate_service_template_spec.rb
blob: ddc27c037f849948f092cdc5857db493acccf88e (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
# frozen_string_literal: true

require 'spec_helper'

describe Projects::PropagateServiceTemplate do
  describe '.propagate' do
    let!(:service_template) do
      PushoverService.create(
        template: true,
        active: true,
        push_events: false,
        properties: {
          device: 'MyDevice',
          sound: 'mic',
          priority: 4,
          user_key: 'asdf',
          api_key: '123456789'
        }
      )
    end

    let!(:project) { create(:project) }
    let(:excluded_attributes) { %w[id project_id template created_at updated_at title description] }

    it 'creates services for projects' do
      expect(project.pushover_service).to be_nil

      described_class.propagate(service_template)

      expect(project.reload.pushover_service).to be_present
    end

    it 'creates services for a project that has another service' do
      BambooService.create(
        template: true,
        active: true,
        project: project,
        properties: {
          bamboo_url: 'http://gitlab.com',
          username: 'mic',
          password: 'password',
          build_key: 'build'
        }
      )

      expect(project.pushover_service).to be_nil

      described_class.propagate(service_template)

      expect(project.reload.pushover_service).to be_present
    end

    it 'does not create the service if it exists already' do
      other_service = BambooService.create(
        template: true,
        active: true,
        properties: {
          bamboo_url: 'http://gitlab.com',
          username: 'mic',
          password: 'password',
          build_key: 'build'
        }
      )

      Service.build_from_integration(project.id, service_template).save!
      Service.build_from_integration(project.id, other_service).save!

      expect { described_class.propagate(service_template) }
        .not_to change { Service.count }
    end

    it 'creates the service containing the template attributes' do
      described_class.propagate(service_template)

      expect(project.pushover_service.properties).to eq(service_template.properties)

      expect(project.pushover_service.attributes.except(*excluded_attributes))
        .to eq(service_template.attributes.except(*excluded_attributes))
    end

    context 'service with data fields' do
      let(:service_template) do
        JiraService.create!(
          template: true,
          active: true,
          push_events: false,
          url: 'http://jira.instance.com',
          username: 'user',
          password: 'secret'
        )
      end

      it 'creates the service containing the template attributes' do
        described_class.propagate(service_template)

        expect(project.jira_service.attributes.except(*excluded_attributes))
          .to eq(service_template.attributes.except(*excluded_attributes))

        excluded_attributes = %w[id service_id created_at updated_at]
        expect(project.jira_service.data_fields.attributes.except(*excluded_attributes))
          .to eq(service_template.data_fields.attributes.except(*excluded_attributes))
      end
    end

    describe 'bulk update', :use_sql_query_cache do
      let(:project_total) { 5 }

      before do
        stub_const 'Projects::PropagateServiceTemplate::BATCH_SIZE', 3

        project_total.times { create(:project) }

        described_class.propagate(service_template)
      end

      it 'creates services for all projects' do
        expect(Service.all.reload.count).to eq(project_total + 2)
      end
    end

    describe 'external tracker' do
      it 'updates the project external tracker' do
        service_template.update!(category: 'issue_tracker', default: false)

        expect { described_class.propagate(service_template) }
          .to change { project.reload.has_external_issue_tracker }.to(true)
      end
    end

    describe 'external wiki' do
      it 'updates the project external tracker' do
        service_template.update!(type: 'ExternalWikiService')

        expect { described_class.propagate(service_template) }
          .to change { project.reload.has_external_wiki }.to(true)
      end
    end
  end
end