PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/zillemarco/gitlab
Ruby | 130 lines | 117 code | 6 blank | 7 comment | 0 complexity | f7d3feefe8c18b9079811bc26ea9881e MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe 'DAST-API.latest.gitlab-ci.yml' do
  4. subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('DAST-API.latest') }
  5. specify { expect(template).not_to be_nil }
  6. describe 'the template file' do
  7. let(:template_filename) { Rails.root.join("lib/gitlab/ci/templates/" + template.full_name) }
  8. let(:contents) { File.read(template_filename) }
  9. let(:production_registry) { 'DAST_API_IMAGE: api-security' }
  10. let(:staging_registry) { 'DAST_API_IMAGE: api-fuzzing-src' }
  11. # Make sure future changes to the template use the production container registry.
  12. #
  13. # The DAST API template is developed against a dev container registry.
  14. # The registry is switched when releasing new versions. The difference in
  15. # names between development and production is also quite small making it
  16. # easy to miss during review.
  17. it 'uses the production repository' do
  18. expect(contents.include?(production_registry)).to be true
  19. end
  20. it "doesn't use the staging repository" do
  21. expect(contents.include?(staging_registry)).to be false
  22. end
  23. end
  24. describe 'the created pipeline' do
  25. let(:default_branch) { 'master' }
  26. let(:pipeline_branch) { default_branch }
  27. let_it_be(:project) { create(:project, :custom_repo, files: { 'README.txt' => '' }) }
  28. let(:user) { project.first_owner }
  29. let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_branch ) }
  30. let(:pipeline) { service.execute!(:push).payload }
  31. let(:build_names) { pipeline.builds.pluck(:name) }
  32. before do
  33. allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
  34. allow(worker).to receive(:perform).and_return(true)
  35. end
  36. allow(project).to receive(:default_branch).and_return(default_branch)
  37. end
  38. context 'when no stages' do
  39. before do
  40. stub_ci_pipeline_yaml_file(template.content)
  41. end
  42. context 'when project has no stages' do
  43. it 'includes no jobs' do
  44. expect(build_names).to be_empty
  45. end
  46. end
  47. end
  48. context 'when stages includes dast' do
  49. let(:ci_pipeline_yaml) { "stages: [\"dast\"]\n" }
  50. before do
  51. stub_ci_pipeline_yaml_file(ci_pipeline_yaml + template.content)
  52. end
  53. context 'when project has no license' do
  54. before do
  55. create(:ci_variable, project: project, key: 'DAST_API_HAR', value: 'testing.har')
  56. create(:ci_variable, project: project, key: 'DAST_API_TARGET_URL', value: 'http://example.com')
  57. end
  58. it 'includes job to display error' do
  59. expect(build_names).to match_array(%w[dast_api])
  60. end
  61. end
  62. context 'when project has Ultimate license' do
  63. before do
  64. stub_licensed_features(dast: true)
  65. end
  66. context 'by default' do
  67. it 'includes a job' do
  68. expect(build_names).to match_array(%w[dast_api])
  69. end
  70. end
  71. context 'when DAST_API_DISABLED=1' do
  72. before do
  73. create(:ci_variable, project: project, key: 'DAST_API_DISABLED', value: '1')
  74. create(:ci_variable, project: project, key: 'DAST_API_HAR', value: 'testing.har')
  75. create(:ci_variable, project: project, key: 'DAST_API_TARGET_URL', value: 'http://example.com')
  76. end
  77. it 'includes no jobs' do
  78. expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError)
  79. end
  80. end
  81. context 'when CI_GITLAB_FIPS_MODE=false' do
  82. let(:build_dast_api) { pipeline.builds.find_by(name: 'dast_api') }
  83. let(:build_variables) { build_dast_api.variables.pluck(:key, :value) }
  84. before do
  85. create(:ci_variable, project: project, key: 'CI_GITLAB_FIPS_MODE', value: 'false')
  86. end
  87. it 'sets DAST_API_IMAGE_SUFFIX to ""' do
  88. expect(build_variables).to be_include(['DAST_API_IMAGE_SUFFIX', ''])
  89. end
  90. end
  91. context 'when CI_GITLAB_FIPS_MODE=true' do
  92. let(:build_dast_api) { pipeline.builds.find_by(name: 'dast_api') }
  93. let(:build_variables) { build_dast_api.variables.pluck(:key, :value) }
  94. before do
  95. create(:ci_variable, project: project, key: 'CI_GITLAB_FIPS_MODE', value: 'true')
  96. end
  97. it 'sets DAST_API_IMAGE_SUFFIX to "-fips"' do
  98. expect(build_variables).to be_include(['DAST_API_IMAGE_SUFFIX', '-fips'])
  99. end
  100. end
  101. end
  102. end
  103. end
  104. end