BigW Consortium Gitlab

search_helper_spec.rb 3.43 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
      end
    end

20
    context "with a standard user" do
21 22 23
      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 33 34 35 36
        expect(search_autocomplete_opts("dash").size).to eq(1)
      end

      it "does not include admin sections" do
        expect(search_autocomplete_opts("admin").size).to eq(0)
37 38
      end

39 40 41 42
      it "does not allow regular expression in search term" do
        expect(search_autocomplete_opts("(webhooks|api)").size).to eq(0)
      end

43 44
      it "includes the user's groups" do
        create(:group).add_owner(user)
45
        expect(search_autocomplete_opts("gro").size).to eq(1)
46 47
      end

48 49 50 51 52
      it "includes nested group" do
        create(:group, :nested, name: 'foo').add_owner(user)
        expect(search_autocomplete_opts('foo').size).to eq(1)
      end

53
      it "includes the user's projects" do
54
        project = create(:project, namespace: create(:namespace, owner: user))
55
        expect(search_autocomplete_opts(project.name).size).to eq(1)
56 57
      end

58
      it "does not include the public group" do
59
        group = create(:group)
Dmitriy Zaporozhets committed
60
        expect(search_autocomplete_opts(group.name).size).to eq(0)
61 62
      end

63
      context "with a current project" do
64 65 66
        before do
          @project = create(:project, :repository)
        end
67 68

        it "includes project-specific sections" do
69 70
          expect(search_autocomplete_opts("Files").size).to eq(1)
          expect(search_autocomplete_opts("Commits").size).to eq(1)
71 72 73
        end
      end
    end
74 75 76 77 78 79 80 81 82 83 84 85

    context 'with an admin user' do
      let(:admin) { create(:admin) }

      before do
        allow(self).to receive(:current_user).and_return(admin)
      end

      it "includes admin sections" do
        expect(search_autocomplete_opts("admin").size).to eq(1)
      end
    end
86
  end
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  describe 'search_filter_input_options' do
    context 'project' do
      before do
        @project = create(:project, :repository)
      end

      it 'includes id with type' do
        expect(search_filter_input_options('type')[:id]).to eq('filtered-search-type')
      end

      it 'includes project-id' do
        expect(search_filter_input_options('')[:data]['project-id']).to eq(@project.id)
      end

      it 'includes project base-endpoint' do
        expect(search_filter_input_options('')[:data]['base-endpoint']).to eq(project_path(@project))
      end
105 106 107 108

      it 'includes autocomplete=off flag' do
        expect(search_filter_input_options('')[:autocomplete]).to eq('off')
      end
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    end

    context 'group' do
      before do
        @group = create(:group, name: 'group')
      end

      it 'does not includes project-id' do
        expect(search_filter_input_options('')[:data]['project-id']).to eq(nil)
      end

      it 'includes group base-endpoint' do
        expect(search_filter_input_options('')[:data]['base-endpoint']).to eq("/groups#{group_path(@group)}")
      end
    end
  end
125
end