summaryrefslogtreecommitdiff
path: root/spec/finders/snippets_finder_spec.rb
blob: 1b4ffc2d7176e1fa2ef6d896c467c747c00f1323 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'spec_helper'

describe SnippetsFinder do
  let(:user) { create :user }
  let(:user1) { create :user }
  let(:group) { create :group }

  let(:project1) { create(:empty_project, :public,   group: group) }
  let(:project2) { create(:empty_project, :private,  group: group) }
  

  context ':all filter' do
    before do
      @snippet1 = create(:personal_snippet, visibility_level: Snippet::PRIVATE)
      @snippet2 = create(:personal_snippet, visibility_level: Snippet::INTERNAL)
      @snippet3 = create(:personal_snippet, visibility_level: Snippet::PUBLIC)
    end

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

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

  context ':by_user filter' do
    before do
      @snippet1 = create(:personal_snippet, visibility_level: Snippet::PRIVATE, author: user)
      @snippet2 = create(:personal_snippet, visibility_level: Snippet::INTERNAL, author: user)
      @snippet3 = create(:personal_snippet, visibility_level: Snippet::PUBLIC, author: user)
    end

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

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

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

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

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

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

  end

  context 'by_project filter' do
    before do
      @snippet1 = create(:project_snippet, visibility_level: Snippet::PRIVATE, project: project1)
      @snippet2 = create(:project_snippet, visibility_level: Snippet::INTERNAL, project: project1)
      @snippet3 = create(:project_snippet, visibility_level: Snippet::PUBLIC, project: project1)
    end

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

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

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