BigW Consortium Gitlab

snippet_spec.rb 7.02 KB
Newer Older
gitlabhq committed
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Snippet, models: true do
4 5 6 7 8 9 10 11
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(Gitlab::VisibilityLevel) }
    it { is_expected.to include_module(Linguist::BlobHelper) }
    it { is_expected.to include_module(Participable) }
    it { is_expected.to include_module(Referable) }
    it { is_expected.to include_module(Sortable) }
12
    it { is_expected.to include_module(Awardable) }
13 14 15
  end

  describe 'associations' do
16
    it { is_expected.to belong_to(:author).class_name('User') }
17
    it { is_expected.to belong_to(:project) }
18
    it { is_expected.to have_many(:notes).dependent(:destroy) }
19
    it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
gitlabhq committed
20 21
  end

22
  describe 'validation' do
23
    it { is_expected.to validate_presence_of(:author) }
24

25
    it { is_expected.to validate_presence_of(:title) }
26
    it { is_expected.to validate_length_of(:title).is_at_most(255) }
27

28
    it { is_expected.to validate_length_of(:file_name).is_at_most(255) }
29

30
    it { is_expected.to validate_presence_of(:content) }
31 32

    it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
gitlabhq committed
33
  end
34 35

  describe '#to_reference' do
36 37 38
    context 'when snippet belongs to a project' do
      let(:project) { build(:empty_project, name: 'sample-project') }
      let(:snippet) { build(:snippet, id: 1, project: project) }
39

40 41 42 43 44
      it 'returns a String reference to the object' do
        expect(snippet.to_reference).to eq "$1"
      end

      it 'supports a cross-project reference' do
45
        another_project = build(:empty_project, name: 'another-project', namespace: project.namespace)
46 47
        expect(snippet.to_reference(another_project)).to eq "sample-project$1"
      end
48 49
    end

50 51 52 53 54 55 56 57
    context 'when snippet does not belong to a project' do
      let(:snippet) { build(:snippet, id: 1, project: nil) }

      it 'returns a String reference to the object' do
        expect(snippet.to_reference).to eq "$1"
      end

      it 'still returns shortest reference when project arg present' do
58
        another_project = build(:empty_project, name: 'another-project')
59 60
        expect(snippet.to_reference(another_project)).to eq "$1"
      end
61 62
    end
  end
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  describe '#file_name' do
    let(:project) { create(:empty_project) }

    context 'file_name is nil' do
      let(:snippet) { create(:snippet, project: project, file_name: nil) }

      it 'returns an empty string' do
        expect(snippet.file_name).to eq ''
      end
    end

    context 'file_name is not nil' do
      let(:snippet) { create(:snippet, project: project, file_name: 'foo.txt') }

      it 'returns the file_name' do
        expect(snippet.file_name).to eq 'foo.txt'
      end
    end
  end

84 85 86 87 88 89 90
  describe "#content_html_invalidated?" do
    let(:snippet) { create(:snippet, content: "md", content_html: "html", file_name: "foo.md") }
    it "invalidates the HTML cache of content when the filename changes" do
      expect { snippet.file_name = "foo.rb" }.to change { snippet.content_html_invalidated? }.from(false).to(true)
    end
  end

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  describe '.search' do
    let(:snippet) { create(:snippet) }

    it 'returns snippets with a matching title' do
      expect(described_class.search(snippet.title)).to eq([snippet])
    end

    it 'returns snippets with a partially matching title' do
      expect(described_class.search(snippet.title[0..2])).to eq([snippet])
    end

    it 'returns snippets with a matching title regardless of the casing' do
      expect(described_class.search(snippet.title.upcase)).to eq([snippet])
    end

    it 'returns snippets with a matching file name' do
      expect(described_class.search(snippet.file_name)).to eq([snippet])
    end

    it 'returns snippets with a partially matching file name' do
      expect(described_class.search(snippet.file_name[0..2])).to eq([snippet])
    end

    it 'returns snippets with a matching file name regardless of the casing' do
      expect(described_class.search(snippet.file_name.upcase)).to eq([snippet])
    end
  end

119
  describe '.search_code' do
120 121 122 123 124 125 126 127 128 129 130 131 132 133
    let(:snippet) { create(:snippet, content: 'class Foo; end') }

    it 'returns snippets with matching content' do
      expect(described_class.search_code(snippet.content)).to eq([snippet])
    end

    it 'returns snippets with partially matching content' do
      expect(described_class.search_code('class')).to eq([snippet])
    end

    it 'returns snippets with matching content regardless of the casing' do
      expect(described_class.search_code('FOO')).to eq([snippet])
    end
  end
Yorick Peterse committed
134

135
  describe '.accessible_to' do
136 137 138
    let(:author)  { create(:author) }
    let(:project) { create(:empty_project) }

139 140 141 142
    let!(:public_snippet)   { create(:snippet, :public) }
    let!(:internal_snippet) { create(:snippet, :internal) }
    let!(:private_snippet)  { create(:snippet, :private, author: author) }

143 144 145 146 147 148 149 150 151 152 153 154
    let!(:project_public_snippet)   { create(:snippet, :public, project: project) }
    let!(:project_internal_snippet) { create(:snippet, :internal, project: project) }
    let!(:project_private_snippet)  { create(:snippet, :private, project: project) }

    it 'returns only public snippets when user is blank' do
      expect(described_class.accessible_to(nil)).to match_array [public_snippet, project_public_snippet]
    end

    it 'returns only public, and internal snippets for regular users' do
      user = create(:user)

      expect(described_class.accessible_to(user)).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
155 156
    end

157 158 159 160 161
    it 'returns public, internal snippets and project private snippets for project members' do
      member = create(:user)
      project.team << [member, :developer]

      expect(described_class.accessible_to(member)).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
162 163
    end

164 165 166 167 168 169 170 171
    it 'returns private snippets where the user is the author' do
      expect(described_class.accessible_to(author)).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet]
    end

    it 'returns all snippets when for admins' do
      admin = create(:admin)

      expect(described_class.accessible_to(admin)).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
172 173 174
    end
  end

Yorick Peterse committed
175
  describe '#participants' do
176
    let(:project) { create(:empty_project, :public) }
Yorick Peterse committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    let(:snippet) { create(:snippet, content: 'foo', project: project) }

    let!(:note1) do
      create(:note_on_project_snippet,
             noteable: snippet,
             project: project,
             note: 'a')
    end

    let!(:note2) do
      create(:note_on_project_snippet,
             noteable: snippet,
             project: project,
             note: 'b')
    end

    it 'includes the snippet author' do
      expect(snippet.participants).to include(snippet.author)
    end

    it 'includes the note authors' do
      expect(snippet.participants).to include(note1.author, note2.author)
    end
  end
gitlabhq committed
201
end