File: parse_annotations_artifact_service_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (182 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::ParseAnnotationsArtifactService, feature_category: :job_artifacts do
  let_it_be(:project) { create(:project) }

  let_it_be_with_reload(:build) { create(:ci_build, project: project) }
  let(:service) { described_class.new(project, nil) }

  describe '#execute' do
    subject { service.execute(artifact) }

    context 'when build has an annotations artifact' do
      let_it_be(:artifact) { create(:ci_job_artifact, :annotations, job: build) }

      context 'when artifact does not have the specified blob' do
        before do
          allow(artifact).to receive(:each_blob)
        end

        it 'parses nothing' do
          expect(subject[:status]).to eq(:success)

          expect(build.job_annotations).to be_empty
        end
      end

      context 'when artifact has the specified blob' do
        let(:blob) { data.to_json }

        before do
          allow(artifact).to receive(:each_blob).and_yield(blob)
        end

        context 'when valid annotations are given' do
          let(:data) do
            {
              external_links: [
                {
                  external_link: {
                    label: 'URL 1',
                    url: 'https://url1.example.com/'
                  }
                },
                {
                  external_link: {
                    label: 'URL 2',
                    url: 'https://url2.example.com/'
                  }
                }
              ]
            }
          end

          it 'parses the artifact' do
            subject

            expect(build.job_annotations.as_json).to contain_exactly(
              hash_including('name' => 'external_links', 'data' => [
                hash_including('external_link' => hash_including('label' => 'URL 1', 'url' => 'https://url1.example.com/')),
                hash_including('external_link' => hash_including('label' => 'URL 2', 'url' => 'https://url2.example.com/'))
              ])
            )
          end
        end

        context 'when valid annotations are given and annotation list name is the same' do
          before do
            build.job_annotations.create!(name: 'external_links', data: [
              {
                external_link: {
                  label: 'URL 1',
                  url: 'https://url1.example.com/'
                }
              }
            ])
          end

          let(:data) do
            {
              external_links: [
                {
                  external_link: {
                    label: 'URL 2',
                    url: 'https://url2.example.com/'
                  }
                }
              ]
            }
          end

          it 'parses the artifact' do
            subject

            expect(build.job_annotations.as_json).to contain_exactly(
              hash_including('name' => 'external_links', 'data' => [
                hash_including('external_link' => hash_including('label' => 'URL 2', 'url' => 'https://url2.example.com/'))
              ])
            )
          end
        end

        context 'when invalid JSON is given' do
          let(:blob) { 'Invalid JSON!' }

          it 'returns error' do
            expect(subject[:status]).to eq(:error)
            expect(subject[:http_status]).to eq(:bad_request)
          end
        end

        context 'when root is not an object' do
          let(:data) { [] }

          it 'returns error' do
            expect(subject[:status]).to eq(:error)
            expect(subject[:message]).to eq('Annotations files must be a JSON object')
            expect(subject[:http_status]).to eq(:bad_request)
          end
        end

        context 'when item is not a valid annotation list' do
          let(:data) { { external_links: {} } }

          it 'returns error' do
            expect(subject[:status]).to eq(:error)
            expect(subject[:message]).to eq('Validation failed: Data must be a valid json schema')
            expect(subject[:http_status]).to eq(:bad_request)
          end
        end

        context 'when more than limitated annotations are specified in annotations' do
          let(:data) do
            {
              external_links_1: [
                {
                  external_link: {
                    label: 'URL',
                    url: 'https://example.com/'
                  }
                }
              ],
              external_links_2: [
                {
                  external_link: {
                    label: 'URL',
                    url: 'https://example.com/'
                  }
                }
              ]
            }
          end

          before do
            allow(service).to receive(:annotations_num_limit).and_return(1)
          end

          it 'returns error' do
            expect(subject[:status]).to eq(:error)
            expect(subject[:message]).to eq(
              "Annotations files cannot have more than #{service.send(:annotations_num_limit)} annotation lists")
            expect(subject[:http_status]).to eq(:bad_request)
          end
        end
      end

      context 'when artifact size is too big' do
        before do
          allow(artifact.file).to receive(:size) { service.send(:annotations_size_limit) + 1.kilobyte }
        end

        it 'returns error' do
          expect(subject[:status]).to eq(:error)
          expect(subject[:message]).to eq(
            "Annotations Artifact Too Big. Maximum Allowable Size: #{service.send(:annotations_size_limit)}")
          expect(subject[:http_status]).to eq(:bad_request)
        end
      end
    end
  end
end