BigW Consortium Gitlab

coverage_spec.rb 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
require 'spec_helper'

feature 'test coverage badge' do
  given!(:user) { create(:user) }
  given!(:project) { create(:project, :private) }

  context 'when user has access to view badge' do
    background do
      project.team << [user, :developer]
      login_as(user)
    end

    scenario 'user requests coverage badge image for pipeline' do
14 15 16 17
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 100, name: 'test:1')
        create_build(pipeline, coverage: 90, name: 'test:2')
      end
18 19 20 21 22 23 24

      show_test_coverage_badge

      expect_coverage_badge('95%')
    end

    scenario 'user requests coverage badge for specific job' do
25 26 27 28 29
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: 50, name: 'test:1')
        create_build(pipeline, coverage: 50, name: 'test:2')
        create_build(pipeline, coverage: 85, name: 'coverage')
      end
30 31 32 33 34 35 36

      show_test_coverage_badge(job: 'coverage')

      expect_coverage_badge('85%')
    end

    scenario 'user requests coverage badge for pipeline without coverage' do
37 38 39
      create_pipeline do |pipeline|
        create_build(pipeline, coverage: nil, name: 'test')
      end
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

      show_test_coverage_badge

      expect_coverage_badge('unknown')
    end
  end

  context 'when user does not have access to view badge' do
    background { login_as(user) }

    scenario 'user requests test coverage badge image' do
      show_test_coverage_badge

      expect(page).to have_http_status(404)
    end
  end

57 58 59 60 61
  def create_pipeline
    opts = { project: project, ref: 'master', sha: project.commit.id }

    create(:ci_pipeline, opts).tap do |pipeline|
      yield pipeline
62
      pipeline.update_status
63 64 65 66 67 68 69
    end
  end

  def create_build(pipeline, coverage:, name:)
    opts = { pipeline: pipeline, coverage: coverage, name: name }

    create(:ci_build, :success, opts)
70 71 72 73 74 75 76 77 78 79 80 81 82
  end

  def show_test_coverage_badge(job: nil)
    visit coverage_namespace_project_badges_path(
      project.namespace, project, ref: :master, job: job, format: :svg)
  end

  def expect_coverage_badge(coverage)
    svg = Nokogiri::XML.parse(page.body)
    expect(page.response_headers['Content-Type']).to include('image/svg+xml')
    expect(svg.at(%Q{text:contains("#{coverage}")})).to be_truthy
  end
end