summaryrefslogtreecommitdiff
path: root/spec/services/metrics/dashboard/annotations/create_service_spec.rb
blob: c3fe7238047337821c4f56324bec80e96d9ad456 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Metrics::Dashboard::Annotations::CreateService do
  let_it_be(:user) { create(:user) }
  let(:description) { 'test annotation' }
  let(:dashboard_path) { 'config/prometheus/common_metrics.yml' }
  let(:starting_at) { 15.minutes.ago }
  let(:ending_at) { nil }
  let(:service_instance) { described_class.new(user, annotation_params) }
  let(:annotation_params) do
    {
      environment: environment,
      cluster: cluster,
      description: description,
      dashboard_path: dashboard_path,
      starting_at: starting_at,
      ending_at: ending_at
    }
  end

  shared_examples 'executed annotation creation' do
    it 'returns success response', :aggregate_failures do
      annotation = instance_double(::Metrics::Dashboard::Annotation)
      allow(::Metrics::Dashboard::Annotation).to receive(:new).and_return(annotation)
      allow(annotation).to receive(:save).and_return(true)

      response = service_instance.execute

      expect(response[:status]).to be :success
      expect(response[:annotation]).to be annotation
    end

    it 'creates annotation', :aggregate_failures do
      annotation = instance_double(::Metrics::Dashboard::Annotation)

      expect(::Metrics::Dashboard::Annotation)
        .to receive(:new).with(annotation_params).and_return(annotation)
      expect(annotation).to receive(:save).and_return(true)

      service_instance.execute
    end
  end

  shared_examples 'prevented annotation creation' do |message|
    it 'returns error response', :aggregate_failures do
      response = service_instance.execute

      expect(response[:status]).to be :error
      expect(response[:message]).to eql message
    end

    it 'does not change db state' do
      expect(::Metrics::Dashboard::Annotation).not_to receive(:new)

      service_instance.execute
    end
  end

  shared_examples 'annotation creation failure' do
    it 'returns error response', :aggregate_failures do
      annotation = instance_double(::Metrics::Dashboard::Annotation)

      expect(annotation).to receive(:errors).and_return('Model validation error')
      expect(::Metrics::Dashboard::Annotation)
        .to receive(:new).with(annotation_params).and_return(annotation)
      expect(annotation).to receive(:save).and_return(false)

      response = service_instance.execute

      expect(response[:status]).to be :error
      expect(response[:message]).to eql 'Model validation error'
    end
  end

  describe '.execute' do
    context 'with environment' do
      let(:environment) { create(:environment) }
      let(:cluster) { nil }

      context 'with anonymous user' do
        it_behaves_like 'prevented annotation creation', 'You are not authorized to create annotation for selected environment'
      end

      context 'with maintainer user' do
        before do
          environment.project.add_maintainer(user)
        end

        it_behaves_like 'executed annotation creation'
      end
    end

    context 'with cluster' do
      let(:environment) { nil }

      context 'with anonymous user' do
        let(:cluster) { create(:cluster, :project) }

        it_behaves_like 'prevented annotation creation', 'You are not authorized to create annotation for selected cluster'
      end

      context 'with maintainer user' do
        let(:cluster) { create(:cluster, :project) }

        before do
          cluster.project.add_maintainer(user)
        end

        it_behaves_like 'executed annotation creation'
      end

      context 'with owner user' do
        let(:cluster) { create(:cluster, :group) }

        before do
          cluster.group.add_owner(user)
        end

        it_behaves_like 'executed annotation creation'
      end
    end

    context 'non cluster nor environment is supplied' do
      let(:environment) { nil }
      let(:cluster) { nil }

      it_behaves_like 'annotation creation failure'
    end

    context 'missing dashboard_path' do
      let(:cluster) { create(:cluster, :project) }
      let(:environment) { nil }
      let(:dashboard_path) { nil }

      context 'with maintainer user' do
        before do
          cluster.project.add_maintainer(user)
        end

        it_behaves_like 'annotation creation failure'
      end
    end

    context 'incorrect dashboard_path' do
      let(:cluster) { create(:cluster, :project) }
      let(:environment) { nil }
      let(:dashboard_path) { 'something_incorrect.yml' }

      context 'with maintainer user' do
        before do
          cluster.project.add_maintainer(user)
        end

        it_behaves_like 'prevented annotation creation', 'Dashboard with requested path can not be found'
      end
    end
  end
end