BigW Consortium Gitlab

snippets_finder_spec.rb 3.67 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe SnippetsFinder do
  let(:user) { create :user }
  let(:user1) { create :user }
Douwe Maan committed
6
  let(:group) { create :group, :public }
7

8 9
  let(:project1) { create(:empty_project, :public,  group: group) }
  let(:project2) { create(:empty_project, :private, group: group) }
10 11 12

  context ':all filter' do
    before do
13 14 15
      @snippet1 = create(:personal_snippet, :private)
      @snippet2 = create(:personal_snippet, :internal)
      @snippet3 = create(:personal_snippet, :public)
16 17 18 19
    end

    it "returns all private and internal snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :all)
20 21
      expect(snippets).to include(@snippet2, @snippet3)
      expect(snippets).not_to include(@snippet1)
22 23 24 25
    end

    it "returns all public snippets" do
      snippets = SnippetsFinder.new.execute(nil, filter: :all)
26 27
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
28 29 30 31 32
    end
  end

  context ':by_user filter' do
    before do
33 34 35
      @snippet1 = create(:personal_snippet, :private,  author: user)
      @snippet2 = create(:personal_snippet, :internal, author: user)
      @snippet3 = create(:personal_snippet, :public,   author: user)
36 37 38 39
    end

    it "returns all public and internal snippets" do
      snippets = SnippetsFinder.new.execute(user1, filter: :by_user, user: user)
40 41
      expect(snippets).to include(@snippet2, @snippet3)
      expect(snippets).not_to include(@snippet1)
42 43 44 45
    end

    it "returns internal snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_internal")
46 47
      expect(snippets).to include(@snippet2)
      expect(snippets).not_to include(@snippet1, @snippet3)
48 49 50 51
    end

    it "returns private snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_private")
52 53
      expect(snippets).to include(@snippet1)
      expect(snippets).not_to include(@snippet2, @snippet3)
54 55 56 57
    end

    it "returns public snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_public")
58 59
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
60 61 62 63
    end

    it "returns all snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user)
64
      expect(snippets).to include(@snippet1, @snippet2, @snippet3)
65
    end
66 67 68

    it "returns only public snippets if unauthenticated user" do
      snippets = SnippetsFinder.new.execute(nil, filter: :by_user, user: user)
69 70
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet2, @snippet1)
71 72
    end

73 74 75 76
  end

  context 'by_project filter' do
    before do
77 78 79
      @snippet1 = create(:project_snippet, :private,  project: project1)
      @snippet2 = create(:project_snippet, :internal, project: project1)
      @snippet3 = create(:project_snippet, :public,   project: project1)
80 81 82 83
    end

    it "returns public snippets for unauthorized user" do
      snippets = SnippetsFinder.new.execute(nil, filter: :by_project, project: project1)
84 85
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
86 87 88 89
    end

    it "returns public and internal snippets for none project members" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1)
90 91
      expect(snippets).to include(@snippet2, @snippet3)
      expect(snippets).not_to include(@snippet1)
92 93 94
    end

    it "returns all snippets for project members" do
95
      project1.team << [user, :developer]
96
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1)
97
      expect(snippets).to include(@snippet1, @snippet2, @snippet3)
98 99 100
    end
  end
end