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

rate_limit_spec.rb « create_pipeline_service « ci « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0000296230f717a538d718a7980d470679283cc0 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Ci::CreatePipelineService, :freeze_time, :clean_gitlab_redis_rate_limiting do
  describe 'rate limiting' do
    let_it_be(:project) { create(:project, :repository) }
    let_it_be(:user)    { project.first_owner }

    let(:ref) { 'refs/heads/master' }

    before do
      stub_ci_pipeline_yaml_file(gitlab_ci_yaml)
      stub_application_setting(pipeline_limit_per_project_user_sha: 1)
      stub_feature_flags(ci_enforce_throttle_pipelines_creation_override: false)
    end

    context 'when user is under the limit' do
      let(:pipeline) { create_pipelines(count: 1) }

      it 'allows pipeline creation' do
        expect(pipeline).to be_created_successfully
        expect(pipeline.statuses).not_to be_empty
      end
    end

    context 'when user is over the limit' do
      let(:pipeline) { create_pipelines }

      it 'blocks pipeline creation' do
        throttle_message = 'Too many pipelines created in the last minute. Try again later.'

        expect(pipeline).not_to be_persisted
        expect(pipeline.statuses).to be_empty
        expect(pipeline.errors.added?(:base, throttle_message)).to be_truthy
      end
    end

    context 'with different users' do
      let(:other_user) { create(:user) }

      before do
        project.add_maintainer(other_user)
      end

      it 'allows other members to create pipelines' do
        blocked_pipeline = create_pipelines(user: user)
        allowed_pipeline = create_pipelines(count: 1, user: other_user)

        expect(blocked_pipeline).not_to be_persisted
        expect(allowed_pipeline).to be_created_successfully
      end
    end

    context 'with different commits' do
      it 'allows user to create pipeline' do
        blocked_pipeline = create_pipelines(ref: ref)
        allowed_pipeline = create_pipelines(count: 1, ref: 'refs/heads/feature')

        expect(blocked_pipeline).not_to be_persisted
        expect(allowed_pipeline).to be_created_successfully
      end
    end

    context 'with different projects' do
      let_it_be(:other_project) { create(:project, :repository) }

      before do
        other_project.add_maintainer(user)
      end

      it 'allows user to create pipeline' do
        blocked_pipeline = create_pipelines(project: project)
        allowed_pipeline = create_pipelines(count: 1, project: other_project)

        expect(blocked_pipeline).not_to be_persisted
        expect(allowed_pipeline).to be_created_successfully
      end
    end
  end

  def create_pipelines(attrs = {})
    attrs.reverse_merge!(user: user, ref: ref, project: project, count: 2)

    service = described_class.new(attrs[:project], attrs[:user], { ref: attrs[:ref] })

    attrs[:count].pred.times { service.execute(:push) }
    service.execute(:push).payload
  end
end