Improve snippet model spec

parent d900a29c
...@@ -133,10 +133,10 @@ RSpec.describe Snippet do ...@@ -133,10 +133,10 @@ RSpec.describe Snippet do
end end
describe '#file_name' do describe '#file_name' do
let(:project) { create(:project) } let(:snippet) { build(:snippet, file_name: file_name) }
context 'file_name is nil' do context 'file_name is nil' do
let(:snippet) { create(:snippet, project: project, file_name: nil) } let(:file_name) { nil }
it 'returns an empty string' do it 'returns an empty string' do
expect(snippet.file_name).to eq '' expect(snippet.file_name).to eq ''
...@@ -144,10 +144,10 @@ RSpec.describe Snippet do ...@@ -144,10 +144,10 @@ RSpec.describe Snippet do
end end
context 'file_name is not nil' do context 'file_name is not nil' do
let(:snippet) { create(:snippet, project: project, file_name: 'foo.txt') } let(:file_name) { 'foo.txt' }
it 'returns the file_name' do it 'returns the file_name' do
expect(snippet.file_name).to eq 'foo.txt' expect(snippet.file_name).to eq file_name
end end
end end
end end
...@@ -161,7 +161,7 @@ RSpec.describe Snippet do ...@@ -161,7 +161,7 @@ RSpec.describe Snippet do
end end
describe '.search' do describe '.search' do
let(:snippet) { create(:snippet, title: 'test snippet', description: 'description') } let_it_be(:snippet) { create(:snippet, title: 'test snippet', description: 'description') }
it 'returns snippets with a matching title' do it 'returns snippets with a matching title' do
expect(described_class.search(snippet.title)).to eq([snippet]) expect(described_class.search(snippet.title)).to eq([snippet])
...@@ -219,25 +219,23 @@ RSpec.describe Snippet do ...@@ -219,25 +219,23 @@ RSpec.describe Snippet do
end end
describe '.with_optional_visibility' do describe '.with_optional_visibility' do
let_it_be(:public_snippet) { create(:snippet, :public) }
let_it_be(:private_snippet) { create(:snippet, :private) }
context 'when a visibility level is provided' do context 'when a visibility level is provided' do
it 'returns snippets with the given visibility' do it 'returns snippets with the given visibility' do
create(:snippet, :private)
snippet = create(:snippet, :public)
snippets = described_class snippets = described_class
.with_optional_visibility(Gitlab::VisibilityLevel::PUBLIC) .with_optional_visibility(Gitlab::VisibilityLevel::PUBLIC)
expect(snippets).to eq([snippet]) expect(snippets).to eq([public_snippet])
end end
end end
context 'when a visibility level is not provided' do context 'when a visibility level is not provided' do
it 'returns all snippets' do it 'returns all snippets' do
snippet1 = create(:snippet, :public)
snippet2 = create(:snippet, :private)
snippets = described_class.with_optional_visibility snippets = described_class.with_optional_visibility
expect(snippets).to include(snippet1, snippet2) expect(snippets).to include(public_snippet, private_snippet)
end end
end end
end end
...@@ -254,12 +252,13 @@ RSpec.describe Snippet do ...@@ -254,12 +252,13 @@ RSpec.describe Snippet do
end end
describe '.only_include_projects_visible_to' do describe '.only_include_projects_visible_to' do
let!(:project1) { create(:project, :public) } let_it_be(:author) { create(:user) }
let!(:project2) { create(:project, :internal) } let_it_be(:project1) { create(:project_empty_repo, :public, namespace: author.namespace) }
let!(:project3) { create(:project, :private) } let_it_be(:project2) { create(:project_empty_repo, :internal, namespace: author.namespace) }
let!(:snippet1) { create(:project_snippet, project: project1) } let_it_be(:project3) { create(:project_empty_repo, :private, namespace: author.namespace) }
let!(:snippet2) { create(:project_snippet, project: project2) } let_it_be(:snippet1) { create(:project_snippet, project: project1, author: author) }
let!(:snippet3) { create(:project_snippet, project: project3) } let_it_be(:snippet2) { create(:project_snippet, project: project2, author: author) }
let_it_be(:snippet3) { create(:project_snippet, project: project3, author: author) }
context 'when a user is provided' do context 'when a user is provided' do
it 'returns snippets visible to the user' do it 'returns snippets visible to the user' do
...@@ -283,55 +282,47 @@ RSpec.describe Snippet do ...@@ -283,55 +282,47 @@ RSpec.describe Snippet do
end end
describe 'only_include_projects_with_snippets_enabled' do describe 'only_include_projects_with_snippets_enabled' do
context 'when the include_private option is enabled' do let_it_be(:project, reload: true) { create(:project_empty_repo) }
it 'includes snippets for projects with snippets set to private' do let_it_be(:snippet) { create(:project_snippet, project: project) }
project = create(:project)
project.project_feature let(:access_level) { ProjectFeature::ENABLED }
.update(snippets_access_level: ProjectFeature::PRIVATE)
snippet = create(:project_snippet, project: project)
snippets = described_class
.only_include_projects_with_snippets_enabled(include_private: true)
expect(snippets).to eq([snippet]) before do
end project.project_feature.update(snippets_access_level: access_level)
end end
context 'when the include_private option is not enabled' do it 'includes snippets for projects with snippets enabled' do
it 'does not include snippets for projects that have snippets set to private' do snippets = described_class.only_include_projects_with_snippets_enabled
project = create(:project)
project.project_feature expect(snippets).to eq([snippet])
.update(snippets_access_level: ProjectFeature::PRIVATE) end
create(:project_snippet, project: project) context 'when snippet_access_level is private' do
let(:access_level) { ProjectFeature::PRIVATE }
snippets = described_class.only_include_projects_with_snippets_enabled context 'when the include_private option is enabled' do
it 'includes snippets for projects with snippets set to private' do
snippets = described_class.only_include_projects_with_snippets_enabled(include_private: true)
expect(snippets).to be_empty expect(snippets).to eq([snippet])
end
end end
end
it 'includes snippets for projects with snippets enabled' do
project = create(:project)
project.project_feature context 'when the include_private option is not enabled' do
.update(snippets_access_level: ProjectFeature::ENABLED) it 'does not include snippets for projects that have snippets set to private' do
snippets = described_class.only_include_projects_with_snippets_enabled
snippet = create(:project_snippet, project: project) expect(snippets).to be_empty
snippets = described_class.only_include_projects_with_snippets_enabled end
end
expect(snippets).to eq([snippet])
end end
end end
describe '.only_include_authorized_projects' do describe '.only_include_authorized_projects' do
it 'only includes snippets for projects the user is authorized to see' do it 'only includes snippets for projects the user is authorized to see' do
user = create(:user) user = create(:user)
project1 = create(:project, :private) project1 = create(:project_empty_repo, :private)
project2 = create(:project, :private) project2 = create(:project_empty_repo, :private)
project1.team.add_developer(user) project1.team.add_developer(user)
...@@ -345,43 +336,34 @@ RSpec.describe Snippet do ...@@ -345,43 +336,34 @@ RSpec.describe Snippet do
end end
describe '.for_project_with_user' do describe '.for_project_with_user' do
context 'when a user is provided' do let_it_be(:public_project) { create(:project_empty_repo, :public) }
it 'returns an empty collection if the user can not view the snippets' do let_it_be(:private_project) { create(:project_empty_repo, :private) }
project = create(:project, :private)
user = create(:user)
project.project_feature context 'when a user is provided' do
.update(snippets_access_level: ProjectFeature::ENABLED) let_it_be(:user) { create(:user) }
create(:project_snippet, :public, project: project) it 'returns an empty collection if the user can not view the snippets' do
create(:project_snippet, :public, project: private_project)
expect(described_class.for_project_with_user(project, user)).to be_empty expect(described_class.for_project_with_user(private_project, user)).to be_empty
end end
it 'returns the snippets if the user is a member of the project' do it 'returns the snippets if the user is a member of the project' do
project = create(:project, :private) snippet = create(:project_snippet, project: private_project)
user = create(:user)
snippet = create(:project_snippet, project: project)
project.team.add_developer(user) private_project.team.add_developer(user)
snippets = described_class.for_project_with_user(project, user) snippets = described_class.for_project_with_user(private_project, user)
expect(snippets).to eq([snippet]) expect(snippets).to eq([snippet])
end end
it 'returns public snippets for a public project the user is not a member of' do it 'returns public snippets for a public project the user is not a member of' do
project = create(:project, :public) snippet = create(:project_snippet, :public, project: public_project)
project.project_feature
.update(snippets_access_level: ProjectFeature::ENABLED)
user = create(:user)
snippet = create(:project_snippet, :public, project: project)
create(:project_snippet, :private, project: project) create(:project_snippet, :private, project: public_project)
snippets = described_class.for_project_with_user(project, user) snippets = described_class.for_project_with_user(public_project, user)
expect(snippets).to eq([snippet]) expect(snippets).to eq([snippet])
end end
...@@ -389,26 +371,17 @@ RSpec.describe Snippet do ...@@ -389,26 +371,17 @@ RSpec.describe Snippet do
context 'when a user is not provided' do context 'when a user is not provided' do
it 'returns an empty collection for a private project' do it 'returns an empty collection for a private project' do
project = create(:project, :private) create(:project_snippet, :public, project: private_project)
project.project_feature expect(described_class.for_project_with_user(private_project)).to be_empty
.update(snippets_access_level: ProjectFeature::ENABLED)
create(:project_snippet, :public, project: project)
expect(described_class.for_project_with_user(project)).to be_empty
end end
it 'returns public snippets for a public project' do it 'returns public snippets for a public project' do
project = create(:project, :public) snippet = create(:project_snippet, :public, project: public_project)
snippet = create(:project_snippet, :public, project: project)
project.project_feature
.update(snippets_access_level: ProjectFeature::PUBLIC)
create(:project_snippet, :private, project: project) create(:project_snippet, :private, project: public_project)
snippets = described_class.for_project_with_user(project) snippets = described_class.for_project_with_user(public_project)
expect(snippets).to eq([snippet]) expect(snippets).to eq([snippet])
end end
...@@ -430,34 +403,30 @@ RSpec.describe Snippet do ...@@ -430,34 +403,30 @@ RSpec.describe Snippet do
end end
describe '#participants' do describe '#participants' do
let(:project) { create(:project, :public) } let_it_be(:project) { create(:project, :public) }
let(:snippet) { create(:snippet, content: 'foo', project: project) } let_it_be(:snippet) { create(:snippet, content: 'foo', project: project) }
let!(:note1) do let_it_be(:note1) do
create(:note_on_project_snippet, create(:note_on_project_snippet,
noteable: snippet, noteable: snippet,
project: project, project: project,
note: 'a') note: 'a')
end end
let!(:note2) do let_it_be(:note2) do
create(:note_on_project_snippet, create(:note_on_project_snippet,
noteable: snippet, noteable: snippet,
project: project, project: project,
note: 'b') note: 'b')
end end
it 'includes the snippet author' do it 'includes the snippet author and note authors' do
expect(snippet.participants).to include(snippet.author) expect(snippet.participants).to include(snippet.author, note1.author, note2.author)
end
it 'includes the note authors' do
expect(snippet.participants).to include(note1.author, note2.author)
end end
end end
describe '#check_for_spam' do describe '#check_for_spam' do
let(:snippet) { create :snippet, visibility_level: visibility_level } let(:snippet) { create(:snippet, visibility_level: visibility_level) }
subject do subject do
snippet.assign_attributes(title: title) snippet.assign_attributes(title: title)
...@@ -500,7 +469,7 @@ RSpec.describe Snippet do ...@@ -500,7 +469,7 @@ RSpec.describe Snippet do
end end
describe '#blob' do describe '#blob' do
let(:snippet) { create(:snippet) } let(:snippet) { build(:snippet) }
it 'returns a blob representing the snippet data' do it 'returns a blob representing the snippet data' do
blob = snippet.blob blob = snippet.blob
...@@ -514,6 +483,14 @@ RSpec.describe Snippet do ...@@ -514,6 +483,14 @@ RSpec.describe Snippet do
describe '#blobs' do describe '#blobs' do
let(:snippet) { create(:snippet) } 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
context 'when repository does not exist' do context 'when repository does not exist' do
it 'returns empty array' do it 'returns empty array' do
expect(snippet.blobs).to be_empty expect(snippet.blobs).to be_empty
...@@ -527,14 +504,6 @@ RSpec.describe Snippet do ...@@ -527,14 +504,6 @@ RSpec.describe Snippet do
expect(snippet.blobs).to all(be_a(Blob)) expect(snippet.blobs).to all(be_a(Blob))
end end
end end
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 end
describe '#to_json' do describe '#to_json' do
...@@ -554,7 +523,7 @@ RSpec.describe Snippet do ...@@ -554,7 +523,7 @@ RSpec.describe Snippet do
end end
describe '#storage' do describe '#storage' do
let(:snippet) { create(:snippet) } let(:snippet) { build(:snippet, id: 1) }
it "stores snippet in #{Storage::Hashed::SNIPPET_REPOSITORY_PATH_PREFIX} dir" do it "stores snippet in #{Storage::Hashed::SNIPPET_REPOSITORY_PATH_PREFIX} dir" do
expect(snippet.storage.disk_path).to start_with Storage::Hashed::SNIPPET_REPOSITORY_PATH_PREFIX expect(snippet.storage.disk_path).to start_with Storage::Hashed::SNIPPET_REPOSITORY_PATH_PREFIX
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment