debian-mirror-gitlab/spec/models/badge_spec.rb

113 lines
2.9 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Badge do
2023-03-04 22:38:38 +05:30
let(:placeholder_url) { 'http://www.example.com/%{project_path}/%{project_id}/%{project_name}/%{default_branch}/%{commit_sha}/%{project_title}' }
2018-03-27 19:54:05 +05:30
describe 'validations' do
# Requires the let variable url_sym
shared_examples 'placeholder url' do
let(:badge) { build(:badge) }
it 'allows url with http protocol' do
badge[url_sym] = 'http://www.example.com'
expect(badge).to be_valid
end
it 'allows url with https protocol' do
badge[url_sym] = 'https://www.example.com'
expect(badge).to be_valid
end
it 'cannot be empty' do
badge[url_sym] = ''
expect(badge).not_to be_valid
end
it 'cannot be nil' do
badge[url_sym] = nil
expect(badge).not_to be_valid
end
it 'accept badges placeholders' do
badge[url_sym] = placeholder_url
expect(badge).to be_valid
end
it 'sanitize url' do
badge[url_sym] = 'javascript:alert(1)'
expect(badge).not_to be_valid
end
end
context 'link_url format' do
let(:url_sym) { :link_url }
it_behaves_like 'placeholder url'
end
context 'image_url format' do
let(:url_sym) { :image_url }
it_behaves_like 'placeholder url'
end
end
shared_examples 'rendered_links' do
2019-07-07 11:18:12 +05:30
it 'uses the project information to populate the url placeholders' do
2018-03-27 19:54:05 +05:30
stub_project_commit_info(project)
2023-03-04 22:38:38 +05:30
expect(badge.public_send("rendered_#{method}", project)).to eq "http://www.example.com/#{project.full_path}/#{project.id}/#{project.path}/master/whatever/#{project.title}"
2018-03-27 19:54:05 +05:30
end
it 'returns the url if the project used is nil' do
expect(badge.public_send("rendered_#{method}", nil)).to eq placeholder_url
end
def stub_project_commit_info(project)
allow(project).to receive(:commit).and_return(double('Commit', sha: 'whatever'))
allow(project).to receive(:default_branch).and_return('master')
end
end
context 'methods' do
let(:badge) { build(:badge, link_url: placeholder_url, image_url: placeholder_url) }
let!(:project) { create(:project) }
2020-03-13 15:44:24 +05:30
describe '#rendered_link_url' do
2018-03-27 19:54:05 +05:30
let(:method) { :link_url }
it_behaves_like 'rendered_links'
end
2020-03-13 15:44:24 +05:30
describe '#rendered_image_url' do
2018-03-27 19:54:05 +05:30
let(:method) { :image_url }
it_behaves_like 'rendered_links'
2020-03-07 23:17:34 +05:30
context 'when asset proxy is enabled' do
let(:placeholder_url) { 'http://www.example.com/image' }
before do
stub_asset_proxy_setting(
enabled: true,
url: 'https://assets.example.com',
secret_key: 'shared-secret'
)
end
it 'returns a proxied URL' do
expect(badge.rendered_image_url).to start_with('https://assets.example.com')
end
end
2018-03-27 19:54:05 +05:30
end
end
end