BigW Consortium Gitlab

snippets_finder_spec.rb 5.14 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

  context ':all filter' do
12 13 14
    let!(:snippet1) { create(:personal_snippet, :private) }
    let!(:snippet2) { create(:personal_snippet, :internal) }
    let!(:snippet3) { create(:personal_snippet, :public) }
15 16 17

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

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

29 30 31 32 33 34 35 36 37 38
  context ':public filter' do
    let!(:snippet1) { create(:personal_snippet, :private) }
    let!(:snippet2) { create(:personal_snippet, :internal) }
    let!(:snippet3) { create(:personal_snippet, :public) }

    it "returns public public snippets" do
      snippets = SnippetsFinder.new.execute(nil, filter: :public)

      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet1, snippet2)
39
    end
40 41 42 43 44 45
  end

  context ':by_user filter' 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) }
46 47 48

    it "returns all public and internal snippets" do
      snippets = SnippetsFinder.new.execute(user1, filter: :by_user, user: user)
49 50
      expect(snippets).to include(snippet2, snippet3)
      expect(snippets).not_to include(snippet1)
51 52 53 54
    end

    it "returns internal snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_internal")
55 56
      expect(snippets).to include(snippet2)
      expect(snippets).not_to include(snippet1, snippet3)
57 58 59 60
    end

    it "returns private snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_private")
61 62
      expect(snippets).to include(snippet1)
      expect(snippets).not_to include(snippet2, snippet3)
63 64 65 66
    end

    it "returns public snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user, scope: "are_public")
67 68
      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet1, snippet2)
69 70 71 72
    end

    it "returns all snippets" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_user, user: user)
73
      expect(snippets).to include(snippet1, snippet2, snippet3)
74
    end
75 76 77

    it "returns only public snippets if unauthenticated user" do
      snippets = SnippetsFinder.new.execute(nil, filter: :by_user, user: user)
78 79
      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet2, snippet1)
80
    end
81 82 83 84
  end

  context 'by_project filter' do
    before do
85 86 87
      @snippet1 = create(:project_snippet, :private,  project: project1)
      @snippet2 = create(:project_snippet, :internal, project: project1)
      @snippet3 = create(:project_snippet, :public,   project: project1)
88 89 90 91
    end

    it "returns public snippets for unauthorized user" do
      snippets = SnippetsFinder.new.execute(nil, filter: :by_project, project: project1)
92 93
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
94 95
    end

96
    it "returns public and internal snippets for non project members" do
97
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1)
98 99
      expect(snippets).to include(@snippet2, @snippet3)
      expect(snippets).not_to include(@snippet1)
100 101
    end

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    it "returns public snippets for non project members" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1, scope: "are_public")
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
    end

    it "returns internal snippets for non project members" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1, scope: "are_internal")
      expect(snippets).to include(@snippet2)
      expect(snippets).not_to include(@snippet1, @snippet3)
    end

    it "does not return private snippets for non project members" do
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1, scope: "are_private")
      expect(snippets).not_to include(@snippet1, @snippet2, @snippet3)
    end

119
    it "returns all snippets for project members" do
120
      project1.team << [user, :developer]
121
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1)
122
      expect(snippets).to include(@snippet1, @snippet2, @snippet3)
123
    end
124 125 126 127 128 129

    it "returns private snippets for project members" do
      project1.team << [user, :developer]
      snippets = SnippetsFinder.new.execute(user, filter: :by_project, project: project1, scope: "are_private")
      expect(snippets).to include(@snippet1)
    end
130 131
  end
end