summaryrefslogtreecommitdiff
path: root/spec/services/search/snippet_service_spec.rb
blob: bc7885b03d947b940efb4e94e054529a6849bbbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'spec_helper'

describe Search::SnippetService do
  let(:author) { create(:author) }
  let(:project) { create(:project) }

  let!(:public_snippet)   { create(:snippet, :public, content: 'password: XXX') }
  let!(:internal_snippet) { create(:snippet, :internal, content: 'password: XXX') }
  let!(:private_snippet)  { create(:snippet, :private, content: 'password: XXX', author: author) }

  let!(:project_public_snippet)   { create(:snippet, :public, project: project, content: 'password: XXX') }
  let!(:project_internal_snippet) { create(:snippet, :internal, project: project, content: 'password: XXX') }
  let!(:project_private_snippet)  { create(:snippet, :private, project: project, content: 'password: XXX') }

  describe '#execute' do
    context 'unauthenticated' do
      it 'returns public snippets only' do
        search = described_class.new(nil, search: 'password')
        results = search.execute

        expect(results.objects('snippet_blobs')).to match_array [public_snippet, project_public_snippet]
      end
    end

    context 'authenticated' do
      it 'returns only public & internal snippets for regular users' do
        user = create(:user)
        search = described_class.new(user, search: 'password')
        results = search.execute

        expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
      end

      it 'returns public, internal snippets and project private snippets for project members' do
        member = create(:user)
        project.add_developer(member)
        search = described_class.new(member, search: 'password')
        results = search.execute

        expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
      end

      it 'returns public, internal and private snippets where user is the author' do
        search = described_class.new(author, search: 'password')
        results = search.execute

        expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet]
      end

      it 'returns all snippets when user is admin' do
        admin = create(:admin)
        search = described_class.new(admin, search: 'password')
        results = search.execute

        expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
      end
    end
  end
end