issues_finder_spec.rb 14.5 KB
Newer Older
1 2
require 'spec_helper'

3
describe IssuesFinder do
4 5 6 7 8 9
  set(:user) { create(:user) }
  set(:user2) { create(:user) }
  set(:project1) { create(:empty_project) }
  set(:project2) { create(:empty_project) }
  set(:milestone) { create(:milestone, project: project1) }
  set(:label) { create(:label, project: project2) }
10
  set(:issue1) { create(:issue, author: user, assignees: [user], project: project1, milestone: milestone, title: 'gitlab', created_at: 1.week.ago) }
11
  set(:issue2) { create(:issue, author: user, assignees: [user], project: project2, description: 'gitlab') }
12
  set(:issue3) { create(:issue, author: user2, assignees: [user2], project: project2, title: 'tanuki', description: 'tanuki', created_at: 1.week.from_now) }
13

Sean McGivern's avatar
Sean McGivern committed
14
  describe '#execute' do
15 16
    set(:closed_issue) { create(:issue, author: user2, assignees: [user2], project: project2, state: 'closed') }
    set(:label_link) { create(:label_link, label: label, target: issue2) }
Sean McGivern's avatar
Sean McGivern committed
17 18
    let(:search_user) { user }
    let(:params) { {} }
19
    let(:issues) { described_class.new(search_user, params.reverse_merge(scope: scope, state: 'opened')).execute }
20

21
    before(:context) do
22 23 24 25 26 27 28 29 30
      project1.team << [user, :master]
      project2.team << [user, :developer]
      project2.team << [user2, :developer]

      issue1
      issue2
      issue3
    end

31
    context 'scope: all' do
Sean McGivern's avatar
Sean McGivern committed
32
      let(:scope) { 'all' }
33

Sean McGivern's avatar
Sean McGivern committed
34 35
      it 'returns all issues' do
        expect(issues).to contain_exactly(issue1, issue2, issue3)
36
      end
37

Sean McGivern's avatar
Sean McGivern committed
38 39 40 41 42 43
      context 'filtering by assignee ID' do
        let(:params) { { assignee_id: user.id } }

        it 'returns issues assigned to that user' do
          expect(issues).to contain_exactly(issue1, issue2)
        end
44
      end
45

Sean McGivern's avatar
Sean McGivern committed
46 47 48 49 50 51
      context 'filtering by author ID' do
        let(:params) { { author_id: user2.id } }

        it 'returns issues created by that user' do
          expect(issues).to contain_exactly(issue3)
        end
52 53
      end

Sean McGivern's avatar
Sean McGivern committed
54 55 56 57 58 59
      context 'filtering by milestone' do
        let(:params) { { milestone_title: milestone.title } }

        it 'returns issues assigned to that milestone' do
          expect(issues).to contain_exactly(issue1)
        end
60 61
      end

Felipe Artur's avatar
Felipe Artur committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      context 'filtering by group milestone' do
        let!(:group) { create(:group, :public) }
        let(:group_milestone) { create(:milestone, group: group) }
        let!(:group_member) { create(:group_member, group: group, user: user) }
        let(:params) { { milestone_title: group_milestone.title } }

        before do
          project2.update(namespace: group)
          issue2.update(milestone: group_milestone)
          issue3.update(milestone: group_milestone)
        end

        it 'returns issues assigned to that group milestone' do
          expect(issues).to contain_exactly(issue2, issue3)
        end
      end

Sean McGivern's avatar
Sean McGivern committed
79 80 81 82 83 84
      context 'filtering by no milestone' do
        let(:params) { { milestone_title: Milestone::None.title } }

        it 'returns issues with no milestone' do
          expect(issues).to contain_exactly(issue2, issue3)
        end
85 86
      end

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      context 'filtering by upcoming milestone' do
        let(:params) { { milestone_title: Milestone::Upcoming.name } }

        let(:project_no_upcoming_milestones) { create(:empty_project, :public) }
        let(:project_next_1_1) { create(:empty_project, :public) }
        let(:project_next_8_8) { create(:empty_project, :public) }

        let(:yesterday) { Date.today - 1.day }
        let(:tomorrow) { Date.today + 1.day }
        let(:two_days_from_now) { Date.today + 2.days }
        let(:ten_days_from_now) { Date.today + 10.days }

        let(:milestones) do
          [
            create(:milestone, :closed, project: project_no_upcoming_milestones),
            create(:milestone, project: project_next_1_1, title: '1.1', due_date: two_days_from_now),
            create(:milestone, project: project_next_1_1, title: '8.8', due_date: ten_days_from_now),
            create(:milestone, project: project_next_8_8, title: '1.1', due_date: yesterday),
            create(:milestone, project: project_next_8_8, title: '8.8', due_date: tomorrow)
          ]
        end

        before do
          milestones.each do |milestone|
111
            create(:issue, project: milestone.project, milestone: milestone, author: user, assignees: [user])
112 113 114 115 116 117 118 119 120
          end
        end

        it 'returns issues in the upcoming milestone for each project' do
          expect(issues.map { |issue| issue.milestone.title }).to contain_exactly('1.1', '8.8')
          expect(issues.map { |issue| issue.milestone.due_date }).to contain_exactly(tomorrow, two_days_from_now)
        end
      end

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      context 'filtering by started milestone' do
        let(:params) { { milestone_title: Milestone::Started.name } }

        let(:project_no_started_milestones) { create(:empty_project, :public) }
        let(:project_started_1_and_2) { create(:empty_project, :public) }
        let(:project_started_8) { create(:empty_project, :public) }

        let(:yesterday) { Date.today - 1.day }
        let(:tomorrow) { Date.today + 1.day }
        let(:two_days_ago) { Date.today - 2.days }

        let(:milestones) do
          [
            create(:milestone, project: project_no_started_milestones, start_date: tomorrow),
            create(:milestone, project: project_started_1_and_2, title: '1.0', start_date: two_days_ago),
            create(:milestone, project: project_started_1_and_2, title: '2.0', start_date: yesterday),
            create(:milestone, project: project_started_1_and_2, title: '3.0', start_date: tomorrow),
            create(:milestone, project: project_started_8, title: '7.0'),
            create(:milestone, project: project_started_8, title: '8.0', start_date: yesterday),
            create(:milestone, project: project_started_8, title: '9.0', start_date: tomorrow)
          ]
        end

        before do
          milestones.each do |milestone|
146
            create(:issue, project: milestone.project, milestone: milestone, author: user, assignees: [user])
147 148 149 150 151 152 153 154 155
          end
        end

        it 'returns issues in the started milestones for each project' do
          expect(issues.map { |issue| issue.milestone.title }).to contain_exactly('1.0', '2.0', '8.0')
          expect(issues.map { |issue| issue.milestone.start_date }).to contain_exactly(two_days_ago, yesterday, yesterday)
        end
      end

Sean McGivern's avatar
Sean McGivern committed
156 157
      context 'filtering by label' do
        let(:params) { { label_name: label.title } }
158

Sean McGivern's avatar
Sean McGivern committed
159 160 161 162
        it 'returns issues with that label' do
          expect(issues).to contain_exactly(issue2)
        end
      end
163

Sean McGivern's avatar
Sean McGivern committed
164 165 166
      context 'filtering by multiple labels' do
        let(:params) { { label_name: [label.title, label2.title].join(',') } }
        let(:label2) { create(:label, project: project2) }
167

168 169 170
        before do
          create(:label_link, label: label2, target: issue2)
        end
171

Sean McGivern's avatar
Sean McGivern committed
172 173 174
        it 'returns the unique issues with any of those labels' do
          expect(issues).to contain_exactly(issue2)
        end
175 176
      end

Sean McGivern's avatar
Sean McGivern committed
177 178 179 180 181 182
      context 'filtering by no label' do
        let(:params) { { label_name: Label::None.title } }

        it 'returns issues with no labels' do
          expect(issues).to contain_exactly(issue1, issue3)
        end
183 184
      end

barthc's avatar
barthc committed
185 186 187 188 189 190 191 192
      context 'filtering by issue term' do
        let(:params) { { search: 'git' } }

        it 'returns issues with title and description match for search term' do
          expect(issues).to contain_exactly(issue1, issue2)
        end
      end

193 194
      context 'filtering by issues iids' do
        let(:params) { { iids: issue3.iid } }
barthc's avatar
barthc committed
195

196
        it 'returns issues with iids match' do
barthc's avatar
barthc committed
197 198 199 200
          expect(issues).to contain_exactly(issue3)
        end
      end

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      context 'filtering by state' do
        context 'with opened' do
          let(:params) { { state: 'opened' } }

          it 'returns only opened issues' do
            expect(issues).to contain_exactly(issue1, issue2, issue3)
          end
        end

        context 'with closed' do
          let(:params) { { state: 'closed' } }

          it 'returns only closed issues' do
            expect(issues).to contain_exactly(closed_issue)
          end
        end

        context 'with all' do
          let(:params) { { state: 'all' } }

          it 'returns all issues' do
            expect(issues).to contain_exactly(issue1, issue2, issue3, closed_issue)
          end
        end

        context 'with invalid state' do
          let(:params) { { state: 'invalid_state' } }

          it 'returns all issues' do
            expect(issues).to contain_exactly(issue1, issue2, issue3, closed_issue)
          end
        end
      end

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      context 'filtering by created_at' do
        context 'through created_after' do
          let(:params) { { created_after: issue3.created_at } }

          it 'returns issues created on or after the given date' do
            expect(issues).to contain_exactly(issue3)
          end
        end

        context 'through created_before' do
          let(:params) { { created_before: issue1.created_at + 1.second } }

          it 'returns issues created on or before the given date' do
            expect(issues).to contain_exactly(issue1)
          end
        end
      end

Sean McGivern's avatar
Sean McGivern committed
253 254 255 256 257 258
      context 'when the user is unauthorized' do
        let(:search_user) { nil }

        it 'returns no results' do
          expect(issues).to be_empty
        end
259 260
      end

Sean McGivern's avatar
Sean McGivern committed
261 262 263 264 265 266
      context 'when the user can see some, but not all, issues' do
        let(:search_user) { user2 }

        it 'returns only issues they can see' do
          expect(issues).to contain_exactly(issue2, issue3)
        end
267
      end
268 269 270 271 272 273 274 275 276

      it 'finds issues user can access due to group' do
        group = create(:group)
        project = create(:empty_project, group: group)
        issue = create(:issue, project: project)
        group.add_user(user, :owner)

        expect(issues).to include(issue)
      end
277 278
    end

279
    context 'personal scope' do
Sean McGivern's avatar
Sean McGivern committed
280 281 282 283
      let(:scope) { 'assigned-to-me' }

      it 'returns issue assigned to the user' do
        expect(issues).to contain_exactly(issue1, issue2)
284 285
      end

Sean McGivern's avatar
Sean McGivern committed
286 287 288 289 290 291
      context 'filtering by project' do
        let(:params) { { project_id: project1.id } }

        it 'returns issues assigned to the user in that project' do
          expect(issues).to contain_exactly(issue1)
        end
292
      end
293
    end
294 295 296 297 298

    context 'when project restricts issues' do
      let(:scope) { nil }

      it "doesn't return team-only issues to non team members" do
299
        project = create(:empty_project, :public, :issues_private)
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
        issue = create(:issue, project: project)

        expect(issues).not_to include(issue)
      end

      it "doesn't return issues if feature disabled" do
        [project1, project2].each do |project|
          project.project_feature.update!(issues_access_level: ProjectFeature::DISABLED)
        end

        expect(issues.count).to eq 0
      end
    end
  end

315
  describe '#with_confidentiality_access_check' do
316 317 318 319 320 321 322 323 324 325
    let(:guest) { create(:user) }
    set(:authorized_user) { create(:user) }
    set(:project) { create(:empty_project, namespace: authorized_user.namespace) }
    set(:public_issue) { create(:issue, project: project) }
    set(:confidential_issue) { create(:issue, project: project, confidential: true) }

    context 'when no project filter is given' do
      let(:params) { {} }

      context 'for an anonymous user' do
326
        subject { described_class.new(nil, params).with_confidentiality_access_check }
327 328 329 330 331 332 333 334

        it 'returns only public issues' do
          expect(subject).to include(public_issue)
          expect(subject).not_to include(confidential_issue)
        end
      end

      context 'for a user without project membership' do
335
        subject { described_class.new(user, params).with_confidentiality_access_check }
336 337 338 339 340 341 342 343

        it 'returns only public issues' do
          expect(subject).to include(public_issue)
          expect(subject).not_to include(confidential_issue)
        end
      end

      context 'for a guest user' do
344
        subject { described_class.new(guest, params).with_confidentiality_access_check }
345 346 347 348 349 350 351 352 353 354 355 356

        before do
          project.add_guest(guest)
        end

        it 'returns only public issues' do
          expect(subject).to include(public_issue)
          expect(subject).not_to include(confidential_issue)
        end
      end

      context 'for a project member with access to view confidential issues' do
357
        subject { described_class.new(authorized_user, params).with_confidentiality_access_check }
358

359 360 361 362
        it 'returns all issues' do
          expect(subject).to include(public_issue, confidential_issue)
        end
      end
363 364
    end

365 366 367 368
    context 'when searching within a specific project' do
      let(:params) { { project_id: project.id } }

      context 'for an anonymous user' do
369
        subject { described_class.new(nil, params).with_confidentiality_access_check }
370 371 372 373 374 375 376 377 378 379 380 381 382 383

        it 'returns only public issues' do
          expect(subject).to include(public_issue)
          expect(subject).not_to include(confidential_issue)
        end

        it 'does not filter by confidentiality' do
          expect(Issue).not_to receive(:where).with(a_string_matching('confidential'), anything)

          subject
        end
      end

      context 'for a user without project membership' do
384
        subject { described_class.new(user, params).with_confidentiality_access_check }
385 386 387 388 389 390 391 392 393 394 395 396 397 398

        it 'returns only public issues' do
          expect(subject).to include(public_issue)
          expect(subject).not_to include(confidential_issue)
        end

        it 'filters by confidentiality' do
          expect(Issue).to receive(:where).with(a_string_matching('confidential'), anything)

          subject
        end
      end

      context 'for a guest user' do
399
        subject { described_class.new(guest, params).with_confidentiality_access_check }
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

        before do
          project.add_guest(guest)
        end

        it 'returns only public issues' do
          expect(subject).to include(public_issue)
          expect(subject).not_to include(confidential_issue)
        end

        it 'filters by confidentiality' do
          expect(Issue).to receive(:where).with(a_string_matching('confidential'), anything)

          subject
        end
      end

      context 'for a project member with access to view confidential issues' do
418
        subject { described_class.new(authorized_user, params).with_confidentiality_access_check }
419 420 421 422 423 424 425 426 427 428 429

        it 'returns all issues' do
          expect(subject).to include(public_issue, confidential_issue)
        end

        it 'does not filter by confidentiality' do
          expect(Issue).not_to receive(:where).with(a_string_matching('confidential'), anything)

          subject
        end
      end
430
    end
431 432
  end
end