BigW Consortium Gitlab

search_helper_spec.rb 1.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
require 'spec_helper'

describe SearchHelper do
  # Override simple_sanitize for our testing purposes
  def simple_sanitize(str)
    str
  end

  describe 'search_autocomplete_source' do
    context "with no current user" do
skv committed
11 12 13
      before do
        allow(self).to receive(:current_user).and_return(nil)
      end
14 15

      it "it returns nil" do
16
        expect(search_autocomplete_opts("q")).to be_nil
17 18 19 20 21 22 23
      end
    end

    context "with a user" do
      let(:user)   { create(:user) }

      before do
skv committed
24
        allow(self).to receive(:current_user).and_return(user)
25 26 27
      end

      it "includes Help sections" do
28
        expect(search_autocomplete_opts("hel").size).to eq(9)
29 30 31
      end

      it "includes default sections" do
32
        expect(search_autocomplete_opts("adm").size).to eq(1)
33 34
      end

35 36 37 38
      it "does not allow regular expression in search term" do
        expect(search_autocomplete_opts("(webhooks|api)").size).to eq(0)
      end

39 40
      it "includes the user's groups" do
        create(:group).add_owner(user)
41
        expect(search_autocomplete_opts("gro").size).to eq(1)
42 43
      end

44 45 46 47 48
      it "includes nested group" do
        create(:group, :nested, name: 'foo').add_owner(user)
        expect(search_autocomplete_opts('foo').size).to eq(1)
      end

49
      it "includes the user's projects" do
50
        project = create(:empty_project, namespace: create(:namespace, owner: user))
51
        expect(search_autocomplete_opts(project.name).size).to eq(1)
52 53
      end

54
      it "does not include the public group" do
55
        group = create(:group)
Dmitriy Zaporozhets committed
56
        expect(search_autocomplete_opts(group.name).size).to eq(0)
57 58
      end

59
      context "with a current project" do
60 61 62
        before do
          @project = create(:project, :repository)
        end
63 64

        it "includes project-specific sections" do
65 66
          expect(search_autocomplete_opts("Files").size).to eq(1)
          expect(search_autocomplete_opts("Commits").size).to eq(1)
67 68 69 70 71
        end
      end
    end
  end
end