2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe SnippetPresenter do
|
2020-01-01 13:55:28 +05:30
|
|
|
include Gitlab::Routing.url_helpers
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:personal_snippet) { create(:personal_snippet, author: user) }
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, author: user) }
|
|
|
|
|
|
|
|
let(:project) { project_snippet.project }
|
|
|
|
let(:presenter) { described_class.new(snippet, current_user: user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#web_url' do
|
|
|
|
subject { presenter.web_url }
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
it 'returns snippet web url' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(subject).to match "/-/snippets/#{snippet.id}"
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
|
|
|
it 'returns snippet web url' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(subject).to match "/#{project.full_path}/-/snippets/#{snippet.id}"
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#raw_url' do
|
|
|
|
subject { presenter.raw_url }
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
it 'returns snippet web url' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(subject).to match "/-/snippets/#{snippet.id}/raw"
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
|
|
|
it 'returns snippet web url' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(subject).to match "/#{project.full_path}/-/snippets/#{snippet.id}/raw"
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_read_snippet?' do
|
|
|
|
subject { presenter.can_read_snippet? }
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'checks read_snippet' do
|
|
|
|
expect(presenter).to receive(:can?).with(user, :read_snippet, snippet)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'checks read_snippet ' do
|
|
|
|
expect(presenter).to receive(:can?).with(user, :read_snippet, snippet)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_update_snippet?' do
|
|
|
|
subject { presenter.can_update_snippet? }
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'checks update_snippet' do
|
|
|
|
expect(presenter).to receive(:can?).with(user, :update_snippet, snippet)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'checks update_snippet ' do
|
|
|
|
expect(presenter).to receive(:can?).with(user, :update_snippet, snippet)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_admin_snippet?' do
|
|
|
|
subject { presenter.can_admin_snippet? }
|
|
|
|
|
|
|
|
context 'with PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'checks admin_snippet' do
|
|
|
|
expect(presenter).to receive(:can?).with(user, :admin_snippet, snippet)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'checks admin_snippet ' do
|
|
|
|
expect(presenter).to receive(:can?).with(user, :admin_snippet, snippet)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_report_as_spam' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
subject { presenter.can_report_as_spam? }
|
|
|
|
|
|
|
|
it 'returns false if the user cannot submit the snippet as spam' do
|
|
|
|
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
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
describe '#blob' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
subject { presenter.blob }
|
|
|
|
|
|
|
|
context 'when snippet does not have a repository' do
|
|
|
|
it 'returns SnippetBlob' do
|
|
|
|
expect(subject).to eq snippet.blob
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet has a repository' do
|
|
|
|
let(:snippet) { create(:snippet, :repository, author: user) }
|
|
|
|
|
|
|
|
it 'returns repository first blob' do
|
|
|
|
expect(subject).to eq snippet.blobs.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
describe '#blobs' do
|
|
|
|
let(:snippet) { personal_snippet }
|
|
|
|
|
|
|
|
subject { presenter.blobs }
|
|
|
|
|
|
|
|
context 'when snippet does not have a repository' do
|
|
|
|
it 'returns an array with one SnippetBlob' do
|
|
|
|
expect(subject.size).to eq(1)
|
|
|
|
expect(subject.first).to eq(snippet.blob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet has a repository' do
|
|
|
|
let(:snippet) { create(:snippet, :repository, author: user) }
|
|
|
|
|
|
|
|
it 'returns an array with all repository blobs' do
|
|
|
|
expect(subject).to match_array(snippet.blobs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|