2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Snippet do
|
2015-09-11 14:41:01 +05:30
|
|
|
describe 'modules' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(Gitlab::VisibilityLevel) }
|
|
|
|
it { is_expected.to include_module(Participable) }
|
|
|
|
it { is_expected.to include_module(Referable) }
|
|
|
|
it { is_expected.to include_module(Sortable) }
|
2016-09-29 09:46:39 +05:30
|
|
|
it { is_expected.to include_module(Awardable) }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:author).class_name('User') }
|
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
it { is_expected.to have_many(:notes).dependent(:destroy) }
|
2016-09-29 09:46:39 +05:30
|
|
|
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
describe 'validation' do
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.to validate_presence_of(:author) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.to validate_presence_of(:title) }
|
2017-08-17 22:00:37 +05:30
|
|
|
it { is_expected.to validate_length_of(:title).is_at_most(255) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it { is_expected.to validate_length_of(:file_name).is_at_most(255) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.to validate_presence_of(:content) }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#to_reference' do
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when snippet belongs to a project' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { build(:project, name: 'sample-project') }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:snippet) { build(:snippet, id: 1, project: project) }
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(snippet.to_reference).to eq "$1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a cross-project reference' do
|
2017-09-10 17:25:29 +05:30
|
|
|
another_project = build(:project, name: 'another-project', namespace: project.namespace)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(snippet.to_reference(another_project)).to eq "sample-project$1"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet does not belong to a project' do
|
|
|
|
let(:snippet) { build(:snippet, id: 1, project: nil) }
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(snippet.to_reference).to eq "$1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'still returns shortest reference when project arg present' do
|
2017-09-10 17:25:29 +05:30
|
|
|
another_project = build(:project, name: 'another-project')
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(snippet.to_reference(another_project)).to eq "$1"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#file_name' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'file_name is nil' do
|
|
|
|
let(:snippet) { create(:snippet, project: project, file_name: nil) }
|
|
|
|
|
|
|
|
it 'returns an empty string' do
|
|
|
|
expect(snippet.file_name).to eq ''
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'file_name is not nil' do
|
|
|
|
let(:snippet) { create(:snippet, project: project, file_name: 'foo.txt') }
|
|
|
|
|
|
|
|
it 'returns the file_name' do
|
|
|
|
expect(snippet.file_name).to eq 'foo.txt'
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
describe "#content_html_invalidated?" do
|
|
|
|
let(:snippet) { create(:snippet, content: "md", content_html: "html", file_name: "foo.md") }
|
|
|
|
it "invalidates the HTML cache of content when the filename changes" do
|
|
|
|
expect { snippet.file_name = "foo.rb" }.to change { snippet.content_html_invalidated? }.from(false).to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
describe '.search' do
|
|
|
|
let(:snippet) { create(:snippet) }
|
|
|
|
|
|
|
|
it 'returns snippets with a matching title' do
|
|
|
|
expect(described_class.search(snippet.title)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a partially matching title' do
|
|
|
|
expect(described_class.search(snippet.title[0..2])).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching title regardless of the casing' do
|
|
|
|
expect(described_class.search(snippet.title.upcase)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching file name' do
|
|
|
|
expect(described_class.search(snippet.file_name)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a partially matching file name' do
|
|
|
|
expect(described_class.search(snippet.file_name[0..2])).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching file name regardless of the casing' do
|
|
|
|
expect(described_class.search(snippet.file_name.upcase)).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
describe '.search_code' do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:snippet) { create(:snippet, content: 'class Foo; end') }
|
|
|
|
|
|
|
|
it 'returns snippets with matching content' do
|
|
|
|
expect(described_class.search_code(snippet.content)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with partially matching content' do
|
|
|
|
expect(described_class.search_code('class')).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with matching content regardless of the casing' do
|
|
|
|
expect(described_class.search_code('FOO')).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
describe '#participants' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, :public) }
|
2016-06-16 23:09:34 +05:30
|
|
|
let(:snippet) { create(:snippet, content: 'foo', project: project) }
|
|
|
|
|
|
|
|
let!(:note1) do
|
|
|
|
create(:note_on_project_snippet,
|
|
|
|
noteable: snippet,
|
|
|
|
project: project,
|
|
|
|
note: 'a')
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:note2) do
|
|
|
|
create(:note_on_project_snippet,
|
|
|
|
noteable: snippet,
|
|
|
|
project: project,
|
|
|
|
note: 'b')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the snippet author' do
|
|
|
|
expect(snippet.participants).to include(snippet.author)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the note authors' do
|
|
|
|
expect(snippet.participants).to include(note1.author, note2.author)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe '#check_for_spam' do
|
|
|
|
let(:snippet) { create :snippet, visibility_level: visibility_level }
|
|
|
|
|
|
|
|
subject do
|
|
|
|
snippet.assign_attributes(title: title)
|
|
|
|
snippet.check_for_spam?
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when public and spammable attributes changed' do
|
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
let(:title) { 'woo' }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when private' do
|
|
|
|
let(:visibility_level) { Snippet::PRIVATE }
|
|
|
|
let(:title) { snippet.title }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when switching to public' do
|
|
|
|
snippet.save!
|
|
|
|
snippet.visibility_level = Snippet::PUBLIC
|
|
|
|
|
|
|
|
expect(snippet.check_for_spam?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when spammable attributes have not changed' do
|
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
let(:title) { snippet.title }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#blob' do
|
|
|
|
let(:snippet) { create(:snippet) }
|
|
|
|
|
|
|
|
it 'returns a blob representing the snippet data' do
|
|
|
|
blob = snippet.blob
|
|
|
|
|
|
|
|
expect(blob).to be_a(Blob)
|
|
|
|
expect(blob.path).to eq(snippet.file_name)
|
|
|
|
expect(blob.data).to eq(snippet.content)
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|