2019-09-30 08:06:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-06 08:33:18 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe SnippetsHelper do
|
2019-11-08 04:06:07 -05:00
|
|
|
include Gitlab::Routing
|
2018-02-06 08:33:18 -05:00
|
|
|
include IconsHelper
|
2022-01-07 07:10:47 -05:00
|
|
|
include BadgesHelper
|
2018-02-06 08:33:18 -05:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, :repository) }
|
|
|
|
let_it_be(:public_project_snippet) { create(:project_snippet, :public, :repository) }
|
2019-11-08 04:06:07 -05:00
|
|
|
|
|
|
|
describe '#embedded_raw_snippet_button' do
|
2020-07-27 08:09:50 -04:00
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
let(:ref) { blob.repository.root_ref }
|
2019-11-08 04:06:07 -05:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
subject { embedded_raw_snippet_button(snippet, blob) }
|
|
|
|
|
|
|
|
context 'for Personal Snippets' do
|
|
|
|
let(:snippet) { public_personal_snippet }
|
|
|
|
|
|
|
|
it 'returns view raw button of embedded snippets' do
|
|
|
|
expect(subject).to eq(download_link("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
|
|
|
|
end
|
2019-11-08 04:06:07 -05:00
|
|
|
end
|
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
context 'for Project Snippets' do
|
|
|
|
let(:snippet) { public_project_snippet }
|
2019-11-08 04:06:07 -05:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
it 'returns view raw button of embedded snippets' do
|
|
|
|
expect(subject).to eq(download_link("http://test.host/#{snippet.project.path_with_namespace}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
|
|
|
|
end
|
2019-11-08 04:06:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def download_link(url)
|
2021-04-09 23:09:09 -04:00
|
|
|
"<a class=\"gl-button btn btn-default\" target=\"_blank\" rel=\"noopener noreferrer\" title=\"Open raw\" href=\"#{url}\">#{external_snippet_icon('doc-code')}</a>"
|
2018-02-06 08:33:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#embedded_snippet_download_button' do
|
2020-07-27 08:09:50 -04:00
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
let(:ref) { blob.repository.root_ref }
|
|
|
|
|
|
|
|
subject { embedded_snippet_download_button(snippet, blob) }
|
2019-11-08 04:06:07 -05:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
context 'for Personal Snippets' do
|
|
|
|
let(:snippet) { public_personal_snippet }
|
2019-11-08 04:06:07 -05:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
it 'returns download button of embedded snippets' do
|
|
|
|
expect(subject).to eq(download_link("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
|
|
|
|
end
|
2019-11-08 04:06:07 -05:00
|
|
|
end
|
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
context 'for Project Snippets' do
|
|
|
|
let(:snippet) { public_project_snippet }
|
2018-02-06 08:33:18 -05:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
it 'returns download button of embedded snippets' do
|
|
|
|
expect(subject).to eq(download_link("http://test.host/#{snippet.project.path_with_namespace}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
|
|
|
|
end
|
2018-02-06 08:33:18 -05:00
|
|
|
end
|
|
|
|
|
2019-11-08 04:06:07 -05:00
|
|
|
def download_link(url)
|
2021-04-09 23:09:09 -04:00
|
|
|
"<a class=\"gl-button btn btn-default\" target=\"_blank\" title=\"Download\" rel=\"noopener noreferrer\" href=\"#{url}?inline=false\">#{external_snippet_icon('download')}</a>"
|
2019-11-08 04:06:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-06 02:09:07 -04:00
|
|
|
describe '#embedded_snippet_copy_button' do
|
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
let(:ref) { blob.repository.root_ref }
|
|
|
|
|
|
|
|
subject { embedded_copy_snippet_button(blob) }
|
|
|
|
|
|
|
|
context 'for Personal Snippets' do
|
|
|
|
let(:snippet) { public_personal_snippet }
|
|
|
|
|
|
|
|
it 'returns copy button of embedded snippets' do
|
|
|
|
expect(subject).to eq(copy_button("#{blob.id}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for Project Snippets' do
|
|
|
|
let(:snippet) { public_project_snippet }
|
|
|
|
|
|
|
|
it 'returns copy button of embedded snippets' do
|
|
|
|
expect(subject).to eq(copy_button("#{blob.id}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_button(blob_id)
|
|
|
|
"<button class=\"gl-button btn btn-default copy-to-clipboard-btn\" title=\"Copy snippet contents\" onclick=\"copyToClipboard('.blob-content[data-blob-id="#{blob_id}"] > pre')\">#{external_snippet_icon('copy-to-clipboard')}</button>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-08 04:06:07 -05:00
|
|
|
describe '#snippet_badge' do
|
|
|
|
let(:snippet) { build(:personal_snippet, visibility) }
|
|
|
|
|
|
|
|
subject { snippet_badge(snippet) }
|
|
|
|
|
|
|
|
context 'when snippet is private' do
|
|
|
|
let(:visibility) { :private }
|
|
|
|
|
|
|
|
it 'returns the snippet badge' do
|
2022-01-07 07:10:47 -05:00
|
|
|
expect(subject).to eq gl_badge_tag('private', icon: 'lock')
|
2019-11-08 04:06:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet is public' do
|
|
|
|
let(:visibility) { :public }
|
|
|
|
|
|
|
|
it 'does not return anything' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet is internal' do
|
|
|
|
let(:visibility) { :internal }
|
2018-02-06 08:33:18 -05:00
|
|
|
|
2019-11-08 04:06:07 -05:00
|
|
|
it 'does not return anything' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
2018-02-06 08:33:18 -05:00
|
|
|
end
|
|
|
|
end
|
2021-08-09 20:10:29 -04:00
|
|
|
|
|
|
|
describe '#snippet_report_abuse_path' do
|
|
|
|
let(:snippet) { public_personal_snippet }
|
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
|
|
|
|
subject { snippet_report_abuse_path(snippet) }
|
|
|
|
|
|
|
|
it 'returns false if the user cannot submit the snippet as spam' do
|
|
|
|
allow(snippet).to receive(:submittable_as_spam_by?).and_return(false)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if the user can submit the snippet as spam' do
|
|
|
|
allow(snippet).to receive(:submittable_as_spam_by?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2018-02-06 08:33:18 -05:00
|
|
|
end
|