2011-10-16 21:07:10 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 09:50:51 +00:00
|
|
|
describe Snippet, models: true do
|
2015-05-03 03:11:21 +00:00
|
|
|
describe 'modules' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(Gitlab::VisibilityLevel) }
|
|
|
|
it { is_expected.to include_module(Linguist::BlobHelper) }
|
|
|
|
it { is_expected.to include_module(Participable) }
|
|
|
|
it { is_expected.to include_module(Referable) }
|
|
|
|
it { is_expected.to include_module(Sortable) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'associations' do
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to belong_to(:author).class_name('User') }
|
2015-05-03 03:14:31 +00:00
|
|
|
it { is_expected.to belong_to(:project) }
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to have_many(:notes).dependent(:destroy) }
|
2011-10-16 21:07:10 +00:00
|
|
|
end
|
|
|
|
|
2015-05-03 03:14:31 +00:00
|
|
|
describe 'validation' do
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to validate_presence_of(:author) }
|
2012-08-29 15:36:02 +00:00
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to validate_presence_of(:title) }
|
2015-05-18 20:40:10 +00:00
|
|
|
it { is_expected.to validate_length_of(:title).is_within(0..255) }
|
2012-08-29 15:36:02 +00:00
|
|
|
|
2015-05-18 20:40:10 +00:00
|
|
|
it { is_expected.to validate_length_of(:file_name).is_within(0..255) }
|
2012-08-29 15:36:02 +00:00
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to validate_presence_of(:content) }
|
2015-05-03 03:14:31 +00:00
|
|
|
|
|
|
|
it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
|
2011-10-16 21:07:10 +00:00
|
|
|
end
|
2015-05-03 03:11:21 +00:00
|
|
|
|
|
|
|
describe '#to_reference' do
|
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
let(:snippet) { create(:snippet, project: project) }
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(snippet.to_reference).to eq "$#{snippet.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a cross-project reference' do
|
|
|
|
cross = double('project')
|
|
|
|
expect(snippet.to_reference(cross)).to eq "#{project.to_reference}$#{snippet.id}"
|
|
|
|
end
|
|
|
|
end
|
2016-03-01 11:51:01 +00:00
|
|
|
|
|
|
|
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-06-22 20:44:24 +00:00
|
|
|
describe '.search_code' do
|
2016-03-01 11:51:01 +00:00
|
|
|
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-05-26 11:38:28 +00:00
|
|
|
|
2016-06-22 20:44:24 +00:00
|
|
|
describe '.accessible_to' do
|
|
|
|
let(:author) { create(:author) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let!(:public_snippet) { create(:snippet, :public) }
|
|
|
|
let!(:internal_snippet) { create(:snippet, :internal) }
|
|
|
|
let!(:private_snippet) { create(:snippet, :private, author: author) }
|
|
|
|
|
|
|
|
it 'returns only public snippets when user is nil' do
|
|
|
|
expect(described_class.accessible_to(nil)).to eq [public_snippet]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only public, and internal snippets when user is not nil' do
|
|
|
|
expect(described_class.accessible_to(user)).to match_array [public_snippet, internal_snippet]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets where the user is the author' do
|
|
|
|
expect(described_class.accessible_to(author)).to match_array [public_snippet, internal_snippet, private_snippet]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
describe '#participants' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
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
|
2011-10-16 21:07:10 +00:00
|
|
|
end
|