BigW Consortium Gitlab

project_search_results_spec.rb 11.9 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::ProjectSearchResults, lib: true do
4
  let(:user) { create(:user) }
5
  let(:project) { create(:empty_project) }
6 7 8
  let(:query) { 'hello world' }

  describe 'initialize with empty ref' do
9
    let(:results) { described_class.new(user, project, query, '') }
10 11

    it { expect(results.project).to eq(project) }
12
    it { expect(results.query).to eq('hello world') }
13 14 15 16
  end

  describe 'initialize with ref' do
    let(:ref) { 'refs/heads/test' }
17
    let(:results) { described_class.new(user, project, query, ref) }
18 19 20

    it { expect(results.project).to eq(project) }
    it { expect(results.repository_ref).to eq(ref) }
21
    it { expect(results.query).to eq('hello world') }
22
  end
23

24
  describe 'blob search' do
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
    let(:project) { create(:project, :public, :repository) }

    subject(:results) { described_class.new(user, project, 'files').objects('blobs') }

    context 'when repository is disabled' do
      let(:project) { create(:project, :public, :repository, :repository_disabled) }

      it 'hides blobs from members' do
        project.add_reporter(user)

        is_expected.to be_empty
      end

      it 'hides blobs from non-members' do
        is_expected.to be_empty
      end
    end

    context 'when repository is internal' do
      let(:project) { create(:project, :public, :repository, :repository_private) }

      it 'finds blobs for members' do
        project.add_reporter(user)

        is_expected.not_to be_empty
      end

      it 'hides blobs from non-members' do
        is_expected.to be_empty
      end
    end
56 57

    it 'finds by name' do
58
      expect(results.map(&:first)).to include('files/images/wm.svg')
59 60 61 62 63 64 65 66 67 68 69 70 71 72
    end

    it 'finds by content' do
      blob = results.select { |result| result.first == "CHANGELOG" }.flatten.last

      expect(blob.filename).to eq("CHANGELOG")
    end

    describe 'parsing results' do
      let(:results) { project.repository.search_files_by_content('feature', 'master') }
      let(:search_result) { results.first }

      subject { described_class.parse_search_result(search_result) }

73 74 75 76
      it "returns a valid FoundBlob" do
        is_expected.to be_an Gitlab::SearchResults::FoundBlob
        expect(subject.id).to be_nil
        expect(subject.path).to eq('CHANGELOG')
77 78 79 80 81 82 83 84 85 86
        expect(subject.filename).to eq('CHANGELOG')
        expect(subject.basename).to eq('CHANGELOG')
        expect(subject.ref).to eq('master')
        expect(subject.startline).to eq(188)
        expect(subject.data.lines[2]).to eq("  - Feature: Replace teams with group membership\n")
      end

      context "when filename has extension" do
        let(:search_result) { "master:CONTRIBUTE.md:5:- [Contribute to GitLab](#contribute-to-gitlab)\n" }

87
        it { expect(subject.path).to eq('CONTRIBUTE.md') }
88 89 90 91 92 93 94
        it { expect(subject.filename).to eq('CONTRIBUTE.md') }
        it { expect(subject.basename).to eq('CONTRIBUTE') }
      end

      context "when file under directory" do
        let(:search_result) { "master:a/b/c.md:5:a b c\n" }

95
        it { expect(subject.path).to eq('a/b/c.md') }
96 97 98 99 100 101
        it { expect(subject.filename).to eq('a/b/c.md') }
        it { expect(subject.basename).to eq('a/b/c') }
      end
    end
  end

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  describe 'wiki search' do
    let(:project) { create(:project, :public) }
    let(:wiki) { build(:project_wiki, project: project) }
    let!(:wiki_page) { wiki.create_page('Title', 'Content') }

    subject(:results) { described_class.new(user, project, 'Content').objects('wiki_blobs') }

    context 'when wiki is disabled' do
      let(:project) { create(:project, :public, :wiki_disabled) }

      it 'hides wiki blobs from members' do
        project.add_reporter(user)

        is_expected.to be_empty
      end

      it 'hides wiki blobs from non-members' do
        is_expected.to be_empty
      end
    end

    context 'when wiki is internal' do
      let(:project) { create(:project, :public, :wiki_private) }

      it 'finds wiki blobs for members' do
        project.add_reporter(user)

        is_expected.not_to be_empty
      end

      it 'hides wiki blobs from non-members' do
        is_expected.to be_empty
      end
    end

    it 'finds by content' do
      expect(results).to include("master:Title.md:1:Content\n")
    end
  end

142 143 144 145 146 147 148 149
  it 'does not list issues on private projects' do
    issue = create(:issue, project: project)

    results = described_class.new(user, project, issue.title)

    expect(results.objects('issues')).not_to include issue
  end

150 151 152 153 154 155 156
  describe 'confidential issues' do
    let(:query) { 'issue' }
    let(:author) { create(:user) }
    let(:assignee) { create(:user) }
    let(:non_member) { create(:user) }
    let(:member) { create(:user) }
    let(:admin) { create(:admin) }
157
    let(:project) { create(:empty_project, :internal) }
158 159
    let!(:issue) { create(:issue, project: project, title: 'Issue 1') }
    let!(:security_issue_1) { create(:issue, :confidential, project: project, title: 'Security issue 1', author: author) }
160
    let!(:security_issue_2) { create(:issue, :confidential, title: 'Security issue 2', project: project, assignees: [assignee]) }
161

162
    it 'does not list project confidential issues for non project members' do
163 164 165 166 167 168 169 170 171
      results = described_class.new(non_member, project, query)
      issues = results.objects('issues')

      expect(issues).to include issue
      expect(issues).not_to include security_issue_1
      expect(issues).not_to include security_issue_2
      expect(results.issues_count).to eq 1
    end

172
    it 'does not list project confidential issues for project members with guest role' do
173 174 175 176 177 178 179 180 181 182 183
      project.team << [member, :guest]

      results = described_class.new(member, project, query)
      issues = results.objects('issues')

      expect(issues).to include issue
      expect(issues).not_to include security_issue_1
      expect(issues).not_to include security_issue_2
      expect(results.issues_count).to eq 1
    end

184
    it 'lists project confidential issues for author' do
185 186 187 188 189 190 191 192 193
      results = described_class.new(author, project, query)
      issues = results.objects('issues')

      expect(issues).to include issue
      expect(issues).to include security_issue_1
      expect(issues).not_to include security_issue_2
      expect(results.issues_count).to eq 2
    end

194
    it 'lists project confidential issues for assignee' do
195
      results = described_class.new(assignee, project, query)
196 197 198 199 200 201 202 203
      issues = results.objects('issues')

      expect(issues).to include issue
      expect(issues).not_to include security_issue_1
      expect(issues).to include security_issue_2
      expect(results.issues_count).to eq 2
    end

204
    it 'lists project confidential issues for project members' do
205 206 207 208 209 210 211 212 213 214 215
      project.team << [member, :developer]

      results = described_class.new(member, project, query)
      issues = results.objects('issues')

      expect(issues).to include issue
      expect(issues).to include security_issue_1
      expect(issues).to include security_issue_2
      expect(results.issues_count).to eq 3
    end

216
    it 'lists all project issues for admin' do
217 218 219 220 221 222 223 224 225
      results = described_class.new(admin, project, query)
      issues = results.objects('issues')

      expect(issues).to include issue
      expect(issues).to include security_issue_1
      expect(issues).to include security_issue_2
      expect(results.issues_count).to eq 3
    end
  end
226 227 228 229 230 231 232 233 234 235 236 237

  describe 'notes search' do
    it 'lists notes' do
      project = create(:empty_project, :public)
      note = create(:note, project: project)

      results = described_class.new(user, project, note.note)

      expect(results.objects('notes')).to include note
    end

    it "doesn't list issue notes when access is restricted" do
238
      project = create(:empty_project, :public, :issues_private)
239 240 241 242 243 244 245 246
      note = create(:note_on_issue, project: project)

      results = described_class.new(user, project, note.note)

      expect(results.objects('notes')).not_to include note
    end

    it "doesn't list merge_request notes when access is restricted" do
247
      project = create(:empty_project, :public, :merge_requests_private)
248 249 250 251 252 253 254
      note = create(:note_on_merge_request, project: project)

      results = described_class.new(user, project, note.note)

      expect(results.objects('notes')).not_to include note
    end
  end
255 256 257 258 259 260 261 262 263

  # Examples for commit access level test
  #
  # params:
  # * search_phrase
  # * commit
  #
  shared_examples 'access restricted commits' do
    context 'when project is internal' do
264
      let(:project) { create(:project, :internal, :repository) }
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

      it 'does not search if user is not authenticated' do
        commits = described_class.new(nil, project, search_phrase).objects('commits')

        expect(commits).to be_empty
      end

      it 'searches if user is authenticated' do
        commits = described_class.new(user, project, search_phrase).objects('commits')

        expect(commits).to contain_exactly commit
      end
    end

    context 'when project is private' do
      let!(:creator) { create(:user, username: 'private-project-author') }
281
      let!(:private_project) { create(:project, :private, :repository, creator: creator, namespace: creator.namespace) }
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
      let(:team_master) do
        user = create(:user, username: 'private-project-master')
        private_project.team << [user, :master]
        user
      end
      let(:team_reporter) do
        user = create(:user, username: 'private-project-reporter')
        private_project.team << [user, :reporter]
        user
      end

      it 'does not show commit to stranger' do
        commits = described_class.new(nil, private_project, search_phrase).objects('commits')

        expect(commits).to be_empty
      end

      context 'team access' do
        it 'shows commit to creator' do
          commits = described_class.new(creator, private_project, search_phrase).objects('commits')

          expect(commits).to contain_exactly commit
        end

        it 'shows commit to master' do
          commits = described_class.new(team_master, private_project, search_phrase).objects('commits')

          expect(commits).to contain_exactly commit
        end

        it 'shows commit to reporter' do
          commits = described_class.new(team_reporter, private_project, search_phrase).objects('commits')

          expect(commits).to contain_exactly commit
        end
      end
    end
  end

  describe 'commit search' do
    context 'by commit message' do
323
      let(:project) { create(:project, :public, :repository) }
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
      let(:commit) { project.repository.commit('59e29889be61e6e0e5e223bfa9ac2721d31605b8') }
      let(:message) { 'Sorry, I did a mistake' }

      it 'finds commit by message' do
        commits = described_class.new(user, project, message).objects('commits')

        expect(commits).to contain_exactly commit
      end

      it 'handles when no commit match' do
        commits = described_class.new(user, project, 'not really an existing description').objects('commits')

        expect(commits).to be_empty
      end

      it_behaves_like 'access restricted commits' do
        let(:search_phrase) { message }
        let(:commit) { project.repository.commit('59e29889be61e6e0e5e223bfa9ac2721d31605b8') }
      end
    end

    context 'by commit hash' do
346
      let(:project) { create(:project, :public, :repository) }
347
      let(:commit) { project.repository.commit('0b4bc9a') }
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
      commit_hashes = { short: '0b4bc9a', full: '0b4bc9a49b562e85de7cc9e834518ea6828729b9' }

      commit_hashes.each do |type, commit_hash|
        it "shows commit by #{type} hash id" do
          commits = described_class.new(user, project, commit_hash).objects('commits')

          expect(commits).to contain_exactly commit
        end
      end

      it 'handles not existing commit hash correctly' do
        commits = described_class.new(user, project, 'deadbeef').objects('commits')

        expect(commits).to be_empty
      end

      it_behaves_like 'access restricted commits' do
        let(:search_phrase) { '0b4bc9a49' }
        let(:commit) { project.repository.commit('0b4bc9a') }
      end
    end
  end
371
end