Welcome to mirror list, hosted at ThFree Co, Russian Federation.

helpers_spec.rb « chain « pipeline « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 84c2fb6525e293f3ee926f18adf38b43360e0535 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Pipeline::Chain::Helpers, feature_category: :continuous_integration do
  let(:helper_class) do
    Class.new do
      include Gitlab::Ci::Pipeline::Chain::Helpers

      attr_accessor :pipeline, :command

      def initialize(pipeline, command)
        self.pipeline = pipeline
        self.command = command
      end
    end
  end

  subject(:helper) { helper_class.new(pipeline, command) }

  let(:pipeline) { build(:ci_empty_pipeline) }
  let(:command) { double(save_incompleted: true) }
  let(:message) { 'message' }

  describe '.warning' do
    context 'when the warning includes malicious HTML' do
      let(:message) { '<div>gimme your password</div>' }
      let(:sanitized_message) { 'gimme your password' }

      it 'sanitizes' do
        subject.warning(message)

        expect(pipeline.warning_messages[0].content).to include(sanitized_message)
      end
    end
  end

  describe '.error' do
    shared_examples 'error function' do
      specify do
        expect(pipeline).to receive(:add_error_message).with(message).and_call_original

        if command.save_incompleted
          expect(pipeline).to receive(:ensure_project_iid!).twice.and_call_original
          expect(pipeline).to receive(:drop!).with(drop_reason).and_call_original
        end

        subject.error(message, config_error: config_error, drop_reason: drop_reason)

        expect(pipeline.yaml_errors).to eq(yaml_error)
        expect(pipeline.errors[:base]).to include(message)
        expect(pipeline.status).to eq 'failed'
        expect(pipeline.failure_reason).to eq drop_reason.to_s
      end

      context 'when feature flag always_set_pipeline_failure_reason is false' do
        before do
          stub_feature_flags(always_set_pipeline_failure_reason: false)
        end

        specify do
          subject.error(message, config_error: config_error, drop_reason: drop_reason)

          if command.save_incompleted
            expect(pipeline.failure_reason).to eq drop_reason.to_s
          else
            expect(pipeline.failure_reason).not_to be_present
          end
        end
      end
    end

    context 'when the error includes malicious HTML' do
      let(:message) { '<div>gimme your password</div>' }
      let(:sanitized_message) { 'gimme your password' }

      it 'sanitizes the error and removes the HTML tags' do
        subject.error(message, config_error: true, drop_reason: :config_error)

        expect(pipeline.yaml_errors).to eq(sanitized_message)
        expect(pipeline.errors[:base]).to include(sanitized_message)
      end
    end

    context 'when given a drop reason' do
      context 'when config error is true' do
        context 'sets the yaml error and overrides the drop reason' do
          let(:drop_reason) { :config_error }
          let(:config_error) { true }
          let(:yaml_error) { message }

          it_behaves_like "error function"
        end
      end

      context 'when config error is false' do
        context 'does not set the yaml error or override the drop reason' do
          let(:drop_reason) { :size_limit_exceeded }
          let(:config_error) { false }
          let(:yaml_error) { nil }

          it_behaves_like "error function"

          specify do
            subject.error(message, config_error: config_error, drop_reason: drop_reason)

            expect(pipeline).to be_persisted
          end

          context ' when the drop reason is not persistable' do
            let(:drop_reason) { :filtered_by_rules }
            let(:command) { double(project: nil) }

            specify do
              expect(command).to receive(:increment_pipeline_failure_reason_counter)

              subject.error(message, config_error: config_error, drop_reason: drop_reason)

              expect(pipeline).to be_failed
              expect(pipeline.failure_reason).to eq drop_reason.to_s
              expect(pipeline).not_to be_persisted
            end
          end

          context 'when save_incompleted is false' do
            let(:command) { double(save_incompleted: false, project: nil) }

            before do
              allow(command).to receive(:increment_pipeline_failure_reason_counter)
            end

            it_behaves_like "error function"

            specify do
              subject.error(message, config_error: config_error, drop_reason: drop_reason)

              expect(pipeline).not_to be_persisted
            end
          end
        end
      end
    end
  end
end