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

rule_spec.rb « protection « packages « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 320c265239c54955f169d848617d49e25580ccab (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
256
257
258
259
260
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Protection::Rule, type: :model, feature_category: :package_registry do
  using RSpec::Parameterized::TableSyntax

  it_behaves_like 'having unique enum values'

  describe 'relationships' do
    it { is_expected.to belong_to(:project).inverse_of(:package_protection_rules) }
  end

  describe 'enums' do
    it { is_expected.to define_enum_for(:package_type).with_values(npm: Packages::Package.package_types[:npm]) }

    it {
      is_expected.to(
        define_enum_for(:push_protected_up_to_access_level)
          .with_values(
            developer: Gitlab::Access::DEVELOPER,
            maintainer: Gitlab::Access::MAINTAINER,
            owner: Gitlab::Access::OWNER
          )
          .with_prefix(:push_protected_up_to)
      )
    }
  end

  describe 'validations' do
    subject { build(:package_protection_rule) }

    describe '#package_name_pattern' do
      it { is_expected.to validate_presence_of(:package_name_pattern) }
      it { is_expected.to validate_uniqueness_of(:package_name_pattern).scoped_to(:project_id, :package_type) }
      it { is_expected.to validate_length_of(:package_name_pattern).is_at_most(255) }
    end

    describe '#package_type' do
      it { is_expected.to validate_presence_of(:package_type) }
    end

    describe '#push_protected_up_to_access_level' do
      it { is_expected.to validate_presence_of(:push_protected_up_to_access_level) }
    end
  end

  describe 'before_save' do
    describe '#set_package_name_pattern_ilike_query' do
      subject { create(:package_protection_rule, package_name_pattern: package_name_pattern) }

      context 'with different package name patterns' do
        where(:package_name_pattern, :expected_pattern_query) do
          '@my-scope/my-package'                              | '@my-scope/my-package'
          '*@my-scope/my-package-with-wildcard-start'         | '%@my-scope/my-package-with-wildcard-start'
          '@my-scope/my-package-with-wildcard-end*'           | '@my-scope/my-package-with-wildcard-end%'
          '@my-scope/*my-package-with-wildcard-inbetween'     | '@my-scope/%my-package-with-wildcard-inbetween'
          '**@my-scope/**my-package-with-wildcard-multiple**' | '%%@my-scope/%%my-package-with-wildcard-multiple%%'
          '@my-scope/my-package-with_____underscore'          | '@my-scope/my-package-with\_\_\_\_\_underscore'
          '@my-scope/my-package-with-percent-sign-%'          | '@my-scope/my-package-with-percent-sign-\%'
          '@my-scope/my-package-with-regex-characters.+'      | '@my-scope/my-package-with-regex-characters.+'
        end

        with_them do
          it { is_expected.to have_attributes(package_name_pattern_ilike_query: expected_pattern_query) }
        end
      end
    end
  end

  describe '.for_package_name' do
    let_it_be(:package_protection_rule) do
      create(:package_protection_rule, package_name_pattern: '@my-scope/my_package')
    end

    let_it_be(:ppr_with_wildcard_start) do
      create(:package_protection_rule, package_name_pattern: '*@my-scope/my_package-with-wildcard-start')
    end

    let_it_be(:ppr_with_wildcard_end) do
      create(:package_protection_rule, package_name_pattern: '@my-scope/my_package-with-wildcard-end*')
    end

    let_it_be(:ppr_with_wildcard_inbetween) do
      create(:package_protection_rule, package_name_pattern: '@my-scope/*my_package-with-wildcard-inbetween')
    end

    let_it_be(:ppr_with_wildcard_multiples) do
      create(:package_protection_rule, package_name_pattern: '**@my-scope/**my_package-with-wildcard-multiple**')
    end

    let_it_be(:ppr_with_underscore) do
      create(:package_protection_rule, package_name_pattern: '@my-scope/my_package-with_____underscore')
    end

    let_it_be(:ppr_with_regex_characters) do
      create(:package_protection_rule, package_name_pattern: '@my-scope/my_package-with-regex-characters.+')
    end

    let(:package_name) { package_protection_rule.package_name_pattern }

    subject { described_class.for_package_name(package_name) }

    context 'with several package protection rule scenarios' do
      where(:package_name, :expected_package_protection_rules) do
        '@my-scope/my_package'                                          | [ref(:package_protection_rule)]
        '@my-scope/my2package'                                          | []
        '@my-scope/my_package-2'                                        | []

        # With wildcard pattern at the start
        '@my-scope/my_package-with-wildcard-start'                      | [ref(:ppr_with_wildcard_start)]
        '@my-scope/my_package-with-wildcard-start-any'                  | []
        'prefix-@my-scope/my_package-with-wildcard-start'               | [ref(:ppr_with_wildcard_start)]
        'prefix-@my-scope/my_package-with-wildcard-start-any'           | []

        # With wildcard pattern at the end
        '@my-scope/my_package-with-wildcard-end'                        | [ref(:ppr_with_wildcard_end)]
        '@my-scope/my_package-with-wildcard-end:1234567890'             | [ref(:ppr_with_wildcard_end)]
        'prefix-@my-scope/my_package-with-wildcard-end'                 | []
        'prefix-@my-scope/my_package-with-wildcard-end:1234567890'      | []

        # With wildcard pattern inbetween
        '@my-scope/my_package-with-wildcard-inbetween'                  | [ref(:ppr_with_wildcard_inbetween)]
        '@my-scope/any-my_package-with-wildcard-inbetween'              | [ref(:ppr_with_wildcard_inbetween)]
        '@my-scope/any-my_package-my_package-wildcard-inbetween-any'    | []

        # With multiple wildcard pattern are used
        '@my-scope/my_package-with-wildcard-multiple'                   | [ref(:ppr_with_wildcard_multiples)]
        'prefix-@my-scope/any-my_package-with-wildcard-multiple-any'    | [ref(:ppr_with_wildcard_multiples)]
        '****@my-scope/****my_package-with-wildcard-multiple****'       | [ref(:ppr_with_wildcard_multiples)]
        'prefix-@other-scope/any-my_package-with-wildcard-multiple-any' | []

        # With underscore
        '@my-scope/my_package-with_____underscore'                      | [ref(:ppr_with_underscore)]
        '@my-scope/my_package-with_any_underscore'                      | []

        '@my-scope/my_package-with-regex-characters.+'                  | [ref(:ppr_with_regex_characters)]
        '@my-scope/my_package-with-regex-characters.'                   | []
        '@my-scope/my_package-with-regex-characters'                    | []
        '@my-scope/my_package-with-regex-characters-any'                | []

        # Special cases
        nil                                                             | []
        ''                                                              | []
        'any_package'                                                   | []
      end

      with_them do
        it { is_expected.to match_array(expected_package_protection_rules) }
      end
    end

    context 'with multiple matching package protection rules' do
      let!(:package_protection_rule_second_match) do
        create(:package_protection_rule, package_name_pattern: "#{package_name}*")
      end

      it { is_expected.to contain_exactly(package_protection_rule_second_match, package_protection_rule) }
    end
  end

  describe '.push_protected_from?' do
    let_it_be(:project_with_ppr) { create(:project) }
    let_it_be(:project_without_ppr) { create(:project) }

    let_it_be(:ppr_for_developer) do
      create(:package_protection_rule,
        package_name_pattern: '@my-scope/my-package-stage*',
        project: project_with_ppr,
        package_type: :npm,
        push_protected_up_to_access_level: :developer
      )
    end

    let_it_be(:ppr_for_maintainer) do
      create(:package_protection_rule,
        package_name_pattern: '@my-scope/my-package-prod*',
        project: project_with_ppr,
        package_type: :npm,
        push_protected_up_to_access_level: :maintainer
      )
    end

    let_it_be(:ppr_owner) do
      create(:package_protection_rule,
        package_name_pattern: '@my-scope/my-package-release*',
        project: project_with_ppr,
        package_type: :npm,
        push_protected_up_to_access_level: :owner
      )
    end

    let_it_be(:ppr_2_for_developer) do
      create(:package_protection_rule,
        package_name_pattern: '@my-scope/my-package-*',
        project: project_with_ppr,
        package_type: :npm,
        push_protected_up_to_access_level: :developer
      )
    end

    subject do
      project
        .package_protection_rules
        .push_protected_from?(
          access_level: access_level,
          package_name: package_name,
          package_type: package_type
        )
    end

    describe "with different users and protection levels" do
      # rubocop:disable Layout/LineLength
      where(:project, :access_level, :package_name, :package_type, :push_protected) do
        ref(:project_with_ppr)    | Gitlab::Access::REPORTER  | '@my-scope/my-package-stage-sha-1234' | :npm   | true
        ref(:project_with_ppr)    | :developer                | '@my-scope/my-package-stage-sha-1234' | :npm   | true
        ref(:project_with_ppr)    | :maintainer               | '@my-scope/my-package-stage-sha-1234' | :npm   | false
        ref(:project_with_ppr)    | :maintainer               | '@my-scope/my-package-stage-sha-1234' | :npm   | false
        ref(:project_with_ppr)    | :owner                    | '@my-scope/my-package-stage-sha-1234' | :npm   | false
        ref(:project_with_ppr)    | Gitlab::Access::ADMIN     | '@my-scope/my-package-stage-sha-1234' | :npm   | false

        ref(:project_with_ppr)    | :developer                | '@my-scope/my-package-prod-sha-1234'  | :npm   | true
        ref(:project_with_ppr)    | :maintainer               | '@my-scope/my-package-prod-sha-1234'  | :npm   | true
        ref(:project_with_ppr)    | :owner                    | '@my-scope/my-package-prod-sha-1234'  | :npm   | false
        ref(:project_with_ppr)    | Gitlab::Access::ADMIN     | '@my-scope/my-package-prod-sha-1234'  | :npm   | false

        ref(:project_with_ppr)    | :developer                | '@my-scope/my-package-release-v1'     | :npm   | true
        ref(:project_with_ppr)    | :owner                    | '@my-scope/my-package-release-v1'     | :npm   | true
        ref(:project_with_ppr)    | Gitlab::Access::ADMIN     | '@my-scope/my-package-release-v1'     | :npm   | false

        ref(:project_with_ppr)    | :developer                | '@my-scope/my-package-any-suffix'     | :npm   | true
        ref(:project_with_ppr)    | :maintainer               | '@my-scope/my-package-any-suffix'     | :npm   | false
        ref(:project_with_ppr)    | :owner                    | '@my-scope/my-package-any-suffix'     | :npm   | false

        # For non-matching package_name
        ref(:project_with_ppr)    | :developer                | '@my-scope/non-matching-package'      | :npm   | false

        # For non-matching package_type
        ref(:project_with_ppr)    | :developer                | '@my-scope/my-package-any-suffix'     | :conan | false

        # For no access level
        ref(:project_with_ppr)    | Gitlab::Access::NO_ACCESS | '@my-scope/my-package-prod'           | :npm   | true

        # Edge cases
        ref(:project_with_ppr)    | 0                         | ''                                    | nil    | true
        ref(:project_with_ppr)    | nil                       | nil                                   | nil    | true

        # For projects that have no package protection rules
        ref(:project_without_ppr) | :developer                | '@my-scope/my-package-prod'           | :npm   | false
        ref(:project_without_ppr) | :maintainer               | '@my-scope/my-package-prod'           | :npm   | false
        ref(:project_without_ppr) | :owner                    | '@my-scope/my-package-prod'           | :npm   | false
      end
      # rubocop:enable Layout/LineLength

      with_them do
        it { is_expected.to eq push_protected }
      end
    end
  end
end