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
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project1) { create(:project, :public, group: group) }
|
|
|
|
let(:project2) { create(:project, :private, group: group) }
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
context 'all snippets visible to a user' do
|
2016-11-26 10:37:26 -05:00
|
|
|
let!(:snippet1) { create(:personal_snippet, :private) }
|
|
|
|
let!(:snippet2) { create(:personal_snippet, :internal) }
|
|
|
|
let!(:snippet3) { create(:personal_snippet, :public) }
|
2017-04-28 18:06:27 -04:00
|
|
|
let!(:project_snippet1) { create(:project_snippet, :private) }
|
|
|
|
let!(:project_snippet2) { create(:project_snippet, :internal) }
|
|
|
|
let!(:project_snippet3) { create(:project_snippet, :public) }
|
2014-10-08 09:44:25 -04:00
|
|
|
|
|
|
|
it "returns all private and internal snippets" do
|
2017-04-28 18:06:27 -04:00
|
|
|
snippets = described_class.new(user, scope: :all).execute
|
|
|
|
expect(snippets).to include(snippet2, snippet3, project_snippet2, project_snippet3)
|
|
|
|
expect(snippets).not_to include(snippet1, project_snippet1)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all public snippets" do
|
2017-04-28 18:06:27 -04:00
|
|
|
snippets = described_class.new(nil, scope: :all).execute
|
|
|
|
expect(snippets).to include(snippet3, project_snippet3)
|
|
|
|
expect(snippets).not_to include(snippet1, snippet2, project_snippet1, project_snippet2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all public and internal snippets for normal user" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet2, snippet3, project_snippet2, project_snippet3)
|
|
|
|
expect(snippets).not_to include(snippet1, project_snippet1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all public snippets for non authorized user" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(nil).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet3, project_snippet3)
|
|
|
|
expect(snippets).not_to include(snippet1, snippet2, project_snippet1, project_snippet2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all public and authored snippets for external user" do
|
|
|
|
external_user = create(:user, :external)
|
|
|
|
authored_snippet = create(:personal_snippet, :internal, author: external_user)
|
|
|
|
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(external_user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet3, project_snippet3, authored_snippet)
|
|
|
|
expect(snippets).not_to include(snippet1, snippet2, project_snippet1, project_snippet2)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
context 'filter by visibility' do
|
2016-11-26 10:37:26 -05:00
|
|
|
let!(:snippet1) { create(:personal_snippet, :private) }
|
|
|
|
let!(:snippet2) { create(:personal_snippet, :internal) }
|
|
|
|
let!(:snippet3) { create(:personal_snippet, :public) }
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
it "returns public snippets when visibility is PUBLIC" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(nil, visibility: Snippet::PUBLIC).execute
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet3)
|
|
|
|
expect(snippets).not_to include(snippet1, snippet2)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
2016-11-26 10:37:26 -05:00
|
|
|
end
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
context 'filter by scope' do
|
|
|
|
let!(:snippet1) { create(:personal_snippet, :private, author: user) }
|
|
|
|
let!(:snippet2) { create(:personal_snippet, :internal, author: user) }
|
|
|
|
let!(:snippet3) { create(:personal_snippet, :public, author: user) }
|
|
|
|
|
|
|
|
it "returns all snippets for 'all' scope" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, scope: :all).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet1, snippet2, snippet3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all snippets for 'are_private' scope" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, scope: :are_private).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet1)
|
|
|
|
expect(snippets).not_to include(snippet2, snippet3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all snippets for 'are_interna;' scope" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, scope: :are_internal).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet2)
|
|
|
|
expect(snippets).not_to include(snippet1, snippet3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all snippets for 'are_private' scope" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, scope: :are_public).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
expect(snippets).to include(snippet3)
|
|
|
|
expect(snippets).not_to include(snippet1, snippet2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filter by author' do
|
2016-11-26 10:37:26 -05:00
|
|
|
let!(:snippet1) { create(:personal_snippet, :private, author: user) }
|
|
|
|
let!(:snippet2) { create(:personal_snippet, :internal, author: user) }
|
|
|
|
let!(:snippet3) { create(:personal_snippet, :public, author: user) }
|
2014-10-08 09:44:25 -04:00
|
|
|
|
|
|
|
it "returns all public and internal snippets" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user1, author: user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-11-26 10:37:26 -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
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, author: user, visibility: Snippet::INTERNAL).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-11-26 10:37:26 -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
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, author: user, visibility: Snippet::PRIVATE).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-11-26 10:37:26 -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
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, author: user, visibility: Snippet::PUBLIC).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-11-26 10:37:26 -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
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, author: user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-11-26 10:37:26 -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
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(nil, author: user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-11-26 10:37:26 -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
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
context 'filter by project' do
|
2014-10-08 09:44:25 -04:00
|
|
|
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
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(nil, project: project1).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
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
|
|
|
|
|
2016-12-09 17:01:08 -05:00
|
|
|
it "returns public and internal snippets for non project members" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, project: project1).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
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
|
|
|
|
|
2016-12-09 17:01:08 -05:00
|
|
|
it "returns public snippets for non project members" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, project: project1, visibility: Snippet::PUBLIC).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-12-09 17:01:08 -05:00
|
|
|
expect(snippets).to include(@snippet3)
|
|
|
|
expect(snippets).not_to include(@snippet1, @snippet2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns internal snippets for non project members" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, project: project1, visibility: Snippet::INTERNAL).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-12-09 17:01:08 -05:00
|
|
|
expect(snippets).to include(@snippet2)
|
|
|
|
expect(snippets).not_to include(@snippet1, @snippet3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return private snippets for non project members" do
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, project: project1, visibility: Snippet::PRIVATE).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-12-09 17:01:08 -05:00
|
|
|
expect(snippets).not_to include(@snippet1, @snippet2, @snippet3)
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:44:25 -04:00
|
|
|
it "returns all snippets for project members" do
|
2016-03-08 16:40:45 -05:00
|
|
|
project1.team << [user, :developer]
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, project: project1).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(snippets).to include(@snippet1, @snippet2, @snippet3)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
2016-12-09 17:01:08 -05:00
|
|
|
|
|
|
|
it "returns private snippets for project members" do
|
|
|
|
project1.team << [user, :developer]
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2017-05-10 05:21:33 -04:00
|
|
|
snippets = described_class.new(user, project: project1, visibility: Snippet::PRIVATE).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2016-12-09 17:01:08 -05:00
|
|
|
expect(snippets).to include(@snippet1)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
end
|