BigW Consortium Gitlab

snippets_finder_spec.rb 7.48 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 snippets visible to a user' 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
    let!(:project_snippet1) { create(:project_snippet, :private) }
    let!(:project_snippet2) { create(:project_snippet, :internal) }
    let!(:project_snippet3) { create(:project_snippet, :public) }
18 19

    it "returns all private and internal snippets" do
20 21 22
      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)
23 24 25
    end

    it "returns all public snippets" do
26 27 28 29 30 31
      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
32
      snippets = described_class.new(user).execute
33 34 35 36 37 38

      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
39
      snippets = described_class.new(nil).execute
40 41 42 43 44 45 46 47 48

      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)

49
      snippets = described_class.new(external_user).execute
50 51 52

      expect(snippets).to include(snippet3, project_snippet3, authored_snippet)
      expect(snippets).not_to include(snippet1, snippet2, project_snippet1, project_snippet2)
53 54 55
    end
  end

56
  context 'filter by visibility' do
57 58 59 60
    let!(:snippet1) { create(:personal_snippet, :private) }
    let!(:snippet2) { create(:personal_snippet, :internal) }
    let!(:snippet3) { create(:personal_snippet, :public) }

61
    it "returns public snippets when visibility is PUBLIC" do
62
      snippets = described_class.new(nil, visibility: Snippet::PUBLIC).execute
63 64 65

      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet1, snippet2)
66
    end
67 68
  end

69 70 71 72 73 74
  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
75
      snippets = described_class.new(user, scope: :all).execute
76 77 78 79 80

      expect(snippets).to include(snippet1, snippet2, snippet3)
    end

    it "returns all snippets for 'are_private' scope" do
81
      snippets = described_class.new(user, scope: :are_private).execute
82 83 84 85 86 87

      expect(snippets).to include(snippet1)
      expect(snippets).not_to include(snippet2, snippet3)
    end

    it "returns all snippets for 'are_interna;' scope" do
88
      snippets = described_class.new(user, scope: :are_internal).execute
89 90 91 92 93 94

      expect(snippets).to include(snippet2)
      expect(snippets).not_to include(snippet1, snippet3)
    end

    it "returns all snippets for 'are_private' scope" do
95
      snippets = described_class.new(user, scope: :are_public).execute
96 97 98 99 100 101 102

      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet1, snippet2)
    end
  end

  context 'filter by author' do
103 104 105
    let!(:snippet1) { create(:personal_snippet, :private, author: user) }
    let!(:snippet2) { create(:personal_snippet, :internal, author: user) }
    let!(:snippet3) { create(:personal_snippet, :public, author: user) }
106 107

    it "returns all public and internal snippets" do
108
      snippets = described_class.new(user1, author: user).execute
109

110 111
      expect(snippets).to include(snippet2, snippet3)
      expect(snippets).not_to include(snippet1)
112 113 114
    end

    it "returns internal snippets" do
115
      snippets = described_class.new(user, author: user, visibility: Snippet::INTERNAL).execute
116

117 118
      expect(snippets).to include(snippet2)
      expect(snippets).not_to include(snippet1, snippet3)
119 120 121
    end

    it "returns private snippets" do
122
      snippets = described_class.new(user, author: user, visibility: Snippet::PRIVATE).execute
123

124 125
      expect(snippets).to include(snippet1)
      expect(snippets).not_to include(snippet2, snippet3)
126 127 128
    end

    it "returns public snippets" do
129
      snippets = described_class.new(user, author: user, visibility: Snippet::PUBLIC).execute
130

131 132
      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet1, snippet2)
133 134 135
    end

    it "returns all snippets" do
136
      snippets = described_class.new(user, author: user).execute
137

138
      expect(snippets).to include(snippet1, snippet2, snippet3)
139
    end
140 141

    it "returns only public snippets if unauthenticated user" do
142
      snippets = described_class.new(nil, author: user).execute
143

144 145
      expect(snippets).to include(snippet3)
      expect(snippets).not_to include(snippet2, snippet1)
146
    end
147 148
  end

149
  context 'filter by project' do
150
    before do
151 152 153
      @snippet1 = create(:project_snippet, :private,  project: project1)
      @snippet2 = create(:project_snippet, :internal, project: project1)
      @snippet3 = create(:project_snippet, :public,   project: project1)
154 155 156
    end

    it "returns public snippets for unauthorized user" do
157
      snippets = described_class.new(nil, project: project1).execute
158

159 160
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
161 162
    end

163
    it "returns public and internal snippets for non project members" do
164
      snippets = described_class.new(user, project: project1).execute
165

166 167
      expect(snippets).to include(@snippet2, @snippet3)
      expect(snippets).not_to include(@snippet1)
168 169
    end

170
    it "returns public snippets for non project members" do
171
      snippets = described_class.new(user, project: project1, visibility: Snippet::PUBLIC).execute
172

173 174 175 176 177
      expect(snippets).to include(@snippet3)
      expect(snippets).not_to include(@snippet1, @snippet2)
    end

    it "returns internal snippets for non project members" do
178
      snippets = described_class.new(user, project: project1, visibility: Snippet::INTERNAL).execute
179

180 181 182 183 184
      expect(snippets).to include(@snippet2)
      expect(snippets).not_to include(@snippet1, @snippet3)
    end

    it "does not return private snippets for non project members" do
185
      snippets = described_class.new(user, project: project1, visibility: Snippet::PRIVATE).execute
186

187 188 189
      expect(snippets).not_to include(@snippet1, @snippet2, @snippet3)
    end

190
    it "returns all snippets for project members" do
191
      project1.team << [user, :developer]
192

193
      snippets = described_class.new(user, project: project1).execute
194

195
      expect(snippets).to include(@snippet1, @snippet2, @snippet3)
196
    end
197 198 199

    it "returns private snippets for project members" do
      project1.team << [user, :developer]
200

201
      snippets = described_class.new(user, project: project1, visibility: Snippet::PRIVATE).execute
202

203 204
      expect(snippets).to include(@snippet1)
    end
205 206
  end
end