2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-19 07:08:47 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Projects::BadgesController do
|
2020-08-24 08:10:17 -04:00
|
|
|
let_it_be(:project, reload: true) { create(:project, :repository) }
|
|
|
|
let_it_be(:pipeline, reload: true) { create(:ci_empty_pipeline, project: project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2017-07-19 07:08:47 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
shared_context 'renders badge irrespective of project access levels' do |badge_type|
|
|
|
|
context 'when project is public' do
|
2019-06-21 12:56:03 -04:00
|
|
|
before do
|
2022-02-17 04:15:27 -05:00
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
2019-06-21 12:56:03 -04:00
|
|
|
end
|
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
it "returns the #{badge_type} badge to unauthenticated users" do
|
|
|
|
get_badge(badge_type)
|
2019-06-21 12:56:03 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
2019-06-21 12:56:03 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
context 'when project is restricted' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
project.add_guest(user)
|
|
|
|
sign_in(user)
|
2019-06-21 12:56:03 -04:00
|
|
|
end
|
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
it "returns the #{badge_type} badge to guest users" do
|
|
|
|
get_badge(badge_type)
|
2019-06-21 12:56:03 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-21 12:56:03 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
shared_context 'when pipelines are public' do |badge_type|
|
|
|
|
before do
|
|
|
|
project.update!(public_builds: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'renders badge irrespective of project access levels', badge_type
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_context 'when pipelines are not public' do |badge_type|
|
|
|
|
before do
|
|
|
|
project.update!(public_builds: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is public' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 to unauthenticated users' do
|
|
|
|
get_badge(badge_type)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is restricted to the user' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
project.add_guest(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to project permissions' do
|
|
|
|
get_badge(badge_type)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_context 'customization' do |badge_type|
|
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key_text param is used' do
|
|
|
|
it 'sets custom key text' do
|
|
|
|
get_badge(badge_type, key_text: 'custom key text')
|
|
|
|
|
|
|
|
expect(response.body).to include('custom key text')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key_width param is used' do
|
|
|
|
it 'sets custom key width' do
|
|
|
|
get_badge(badge_type, key_width: '123')
|
|
|
|
|
|
|
|
expect(response.body).to include('123')
|
2019-06-21 12:56:03 -04:00
|
|
|
end
|
|
|
|
end
|
2022-02-17 04:15:27 -05:00
|
|
|
end
|
2017-07-19 07:08:47 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
shared_examples 'a badge resource' do |badge_type|
|
2019-06-21 12:56:03 -04:00
|
|
|
context 'format' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2017-07-19 07:08:47 -04:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
it 'renders the `flat` badge layout by default' do
|
|
|
|
get_badge(badge_type)
|
2017-07-19 07:08:47 -04:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
expect(response).to render_template('projects/badges/badge')
|
|
|
|
end
|
2017-07-19 07:08:47 -04:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
context 'when style param is set to `flat`' do
|
|
|
|
it 'renders the `flat` badge layout' do
|
2020-05-29 17:08:35 -04:00
|
|
|
get_badge(badge_type, style: 'flat')
|
2017-07-19 07:08:47 -04:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
expect(response).to render_template('projects/badges/badge')
|
|
|
|
end
|
|
|
|
end
|
2019-01-18 08:39:35 -05:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
context 'when style param is set to an invalid type' do
|
|
|
|
it 'renders the `flat` (default) badge layout' do
|
2020-05-29 17:08:35 -04:00
|
|
|
get_badge(badge_type, style: 'xxx')
|
2019-06-21 12:56:03 -04:00
|
|
|
|
|
|
|
expect(response).to render_template('projects/badges/badge')
|
|
|
|
end
|
|
|
|
end
|
2019-01-18 08:39:35 -05:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
context 'when style param is set to `flat-square`' do
|
|
|
|
it 'renders the `flat-square` badge layout' do
|
2020-05-29 17:08:35 -04:00
|
|
|
get_badge(badge_type, style: 'flat-square')
|
2019-01-18 08:39:35 -05:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
expect(response).to render_template('projects/badges/badge_flat-square')
|
|
|
|
end
|
|
|
|
end
|
2019-01-18 08:39:35 -05:00
|
|
|
end
|
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
it_behaves_like 'customization', badge_type
|
2020-05-29 17:08:35 -04:00
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
if [:pipeline, :coverage].include?(badge_type)
|
|
|
|
it_behaves_like 'when pipelines are public', badge_type
|
|
|
|
it_behaves_like 'when pipelines are not public', badge_type
|
2020-05-29 17:08:35 -04:00
|
|
|
end
|
2019-01-18 08:39:35 -05:00
|
|
|
end
|
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
describe '#pipeline' do
|
|
|
|
it_behaves_like 'a badge resource', :pipeline
|
2020-08-24 08:10:17 -04:00
|
|
|
|
|
|
|
context 'with ignore_skipped param' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
|
|
|
pipeline.update!(status: :skipped)
|
|
|
|
project.add_maintainer(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns skipped badge if set to false' do
|
|
|
|
get_badge(:pipeline, ignore_skipped: false)
|
|
|
|
expect(response.body).to include('skipped')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return skipped badge if set to true' do
|
|
|
|
get_badge(:pipeline, ignore_skipped: true)
|
|
|
|
expect(response.body).to include('unknown')
|
|
|
|
end
|
|
|
|
end
|
2019-06-21 12:56:03 -04:00
|
|
|
end
|
2019-01-18 08:39:35 -05:00
|
|
|
|
2019-06-21 12:56:03 -04:00
|
|
|
describe '#coverage' do
|
|
|
|
it_behaves_like 'a badge resource', :coverage
|
2019-01-18 08:39:35 -05:00
|
|
|
end
|
|
|
|
|
2022-02-17 04:15:27 -05:00
|
|
|
describe '#release' do
|
|
|
|
action = :release
|
|
|
|
|
|
|
|
it_behaves_like 'a badge resource', action
|
|
|
|
it_behaves_like 'renders badge irrespective of project access levels', action
|
|
|
|
end
|
|
|
|
|
2020-05-29 17:08:35 -04:00
|
|
|
def get_badge(badge, args = {})
|
2019-01-18 08:39:35 -05:00
|
|
|
params = {
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project,
|
2020-05-29 17:08:35 -04:00
|
|
|
ref: pipeline.ref
|
2020-08-24 08:10:17 -04:00
|
|
|
}.merge(args.slice(:style, :key_text, :key_width, :ignore_skipped))
|
2019-01-18 08:39:35 -05:00
|
|
|
|
|
|
|
get badge, params: params, format: :svg
|
2017-07-19 07:08:47 -04:00
|
|
|
end
|
|
|
|
end
|