2014-10-08 09:44:25 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SnippetsFinder do
|
|
|
|
let(:user) { create :user }
|
|
|
|
let(:user1) { create :user }
|
2016-03-21 19:09:20 -04:00
|
|
|
let(:group) { create :group, :public }
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2016-03-08 16:40:45 -05:00
|
|
|
let(:project1) { create(:empty_project, :public, group: group) }
|
|
|
|
let(:project2) { create(:empty_project, :private, group: group) }
|
2014-10-08 09:44:25 -04:00
|
|
|
|
|
|
|
context ':all filter' do
|
|
|
|
before do
|
2016-03-08 16:40:45 -05:00
|
|
|
@snippet1 = create(:personal_snippet, :private)
|
|
|
|
@snippet2 = create(:personal_snippet, :internal)
|
|
|
|
@snippet3 = create(:personal_snippet, :public)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all private and internal snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :all)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet2, @snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all public snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(nil, filter: :all)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1, @snippet2)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context ':by_user filter' do
|
|
|
|
before do
|
2016-03-08 16:40:45 -05:00
|
|
|
@snippet1 = create(:personal_snippet, :private, author: user)
|
|
|
|
@snippet2 = create(:personal_snippet, :internal, author: user)
|
|
|
|
@snippet3 = create(:personal_snippet, :public, author: user)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all public and internal snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user1, filter: :by_user, user: user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet2, @snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns internal snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_internal")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet2)
|
|
|
|
expect(snippets).not_to include(@snippet1, @snippet3)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns private snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_private")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet1)
|
|
|
|
expect(snippets).not_to include(@snippet2, @snippet3)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns public snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_public")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1, @snippet2)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all snippets" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet1, @snippet2, @snippet3)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
2014-10-24 12:24:49 -04:00
|
|
|
|
|
|
|
it "returns only public snippets if unauthenticated user" do
|
|
|
|
snippets = SnippetsFinder.new.execute(nil, filter: :by_user, user: user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet2, @snippet1)
|
2014-10-24 12:24:49 -04:00
|
|
|
end
|
|
|
|
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'by_project filter' do
|
|
|
|
before do
|
2016-03-08 16:40:45 -05:00
|
|
|
@snippet1 = create(:project_snippet, :private, project: project1)
|
|
|
|
@snippet2 = create(:project_snippet, :internal, project: project1)
|
|
|
|
@snippet3 = create(:project_snippet, :public, project: project1)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns public snippets for unauthorized user" do
|
|
|
|
snippets = SnippetsFinder.new.execute(nil, filter: :by_project, project: project1)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1, @snippet2)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns public and internal snippets for none project members" do
|
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet2, @snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all snippets for project members" do
|
2016-03-08 16:40:45 -05:00
|
|
|
project1.team << [user, :developer]
|
2014-10-08 09:44:25 -04:00
|
|
|
snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet1, @snippet2, @snippet3)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|