PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ee/spec/lib/gitlab/ci/templates/dast_latest_gitlab_ci_yaml_spec.rb

https://gitlab.com/realsatomic/gitlab
Ruby | 172 lines | 132 code | 39 blank | 1 comment | 0 complexity | 5cf33d004b4c51489e3c27d09fc4ce45 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.shared_examples 'includes no jobs' do
  4. it 'includes no jobs' do
  5. expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError, 'No stages / jobs for this pipeline.')
  6. end
  7. end
  8. RSpec.shared_examples 'includes dast job' do
  9. it 'includes dast job' do
  10. expect(build_names).to match_array(%w[dast])
  11. end
  12. end
  13. RSpec.describe 'DAST.latest.gitlab-ci.yml' do
  14. subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('DAST.latest') }
  15. describe 'the created pipeline' do
  16. let(:default_branch) { project.default_branch_or_main }
  17. let(:pipeline_branch) { default_branch }
  18. let(:project) { create(:project, :custom_repo, files: { 'README.txt' => '' }) }
  19. let(:user) { project.first_owner }
  20. let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_branch ) }
  21. let(:pipeline) { service.execute!(:push).payload }
  22. let(:build_names) { pipeline.builds.pluck(:name) }
  23. let(:ci_pipeline_yaml) { "stages: [\"dast\"]\n" }
  24. specify { expect(template).not_to be_nil }
  25. context 'when ci yaml is just template' do
  26. before do
  27. stub_ci_pipeline_yaml_file(template.content)
  28. allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
  29. allow(worker).to receive(:perform).and_return(true)
  30. end
  31. allow(project).to receive(:default_branch).and_return(default_branch)
  32. end
  33. context 'when project has no license' do
  34. it 'includes no jobs' do
  35. expect(build_names).to be_empty
  36. end
  37. end
  38. end
  39. context 'when stages includes dast' do
  40. before do
  41. stub_ci_pipeline_yaml_file(ci_pipeline_yaml + template.content)
  42. allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
  43. allow(worker).to receive(:perform).and_return(true)
  44. end
  45. allow(project).to receive(:default_branch).and_return(default_branch)
  46. end
  47. context 'when project has no license' do
  48. include_examples 'includes no jobs'
  49. end
  50. context 'when project has cluster' do
  51. before do
  52. create(:cluster, :project, :provided_by_gcp, projects: [project])
  53. end
  54. context 'by default' do
  55. include_examples 'includes no jobs'
  56. end
  57. context 'when project has Ultimate license' do
  58. let(:license) { build(:license, plan: License::ULTIMATE_PLAN) }
  59. before do
  60. allow(License).to receive(:current).and_return(license)
  61. end
  62. context 'when no specification provided' do
  63. include_examples 'includes dast job'
  64. end
  65. end
  66. end
  67. context 'by default' do
  68. include_examples 'includes no jobs'
  69. end
  70. context 'when DAST_WEBSITE is present' do
  71. before do
  72. create(:ci_variable, project: project, key: 'DAST_WEBSITE', value: 'http://example.com')
  73. end
  74. include_examples 'includes dast job'
  75. end
  76. context 'when DAST_API_SPECIFICATION is present' do
  77. before do
  78. create(:ci_variable, project: project, key: 'DAST_API_SPECIFICATION', value: 'http://my.api/api-specification.yml')
  79. end
  80. include_examples 'includes dast job'
  81. end
  82. context 'when project has Ultimate license' do
  83. let(:license) { build(:license, plan: License::ULTIMATE_PLAN) }
  84. before do
  85. allow(License).to receive(:current).and_return(license)
  86. end
  87. context 'when project has cluster' do
  88. before do
  89. create(:cluster, :project, :provided_by_gcp, projects: [project])
  90. end
  91. context 'when DAST_DISABLED=1' do
  92. before do
  93. create(:ci_variable, project: project, key: 'DAST_DISABLED', value: '1')
  94. end
  95. include_examples 'includes no jobs'
  96. end
  97. context 'when DAST_DISABLED_FOR_DEFAULT_BRANCH=1' do
  98. before do
  99. create(:ci_variable, project: project, key: 'DAST_DISABLED_FOR_DEFAULT_BRANCH', value: '1')
  100. end
  101. context 'when on default branch' do
  102. include_examples 'includes no jobs'
  103. end
  104. context 'when on feature branch' do
  105. let(:pipeline_branch) { 'patch-1' }
  106. before do
  107. project.repository.create_branch(pipeline_branch, default_branch)
  108. end
  109. it 'includes dast job' do
  110. expect(build_names).to match_array(%w[dast])
  111. end
  112. end
  113. end
  114. context 'when REVIEW_DISABLED=true' do
  115. before do
  116. create(:ci_variable, project: project, key: 'REVIEW_DISABLED', value: 'true')
  117. end
  118. context 'when on default branch' do
  119. include_examples 'includes dast job'
  120. end
  121. context 'when on feature branch' do
  122. let(:pipeline_branch) { 'patch-1' }
  123. before do
  124. project.repository.create_branch(pipeline_branch, default_branch)
  125. end
  126. include_examples 'includes no jobs'
  127. end
  128. end
  129. end
  130. end
  131. end
  132. end
  133. end