BigW Consortium Gitlab

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

3
describe IssuesFinder do
4 5 6 7
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let(:project1) { create(:empty_project) }
  let(:project2) { create(:empty_project) }
8
  let(:milestone) { create(:milestone, project: project1) }
9
  let(:label) { create(:label, project: project2) }
10 11 12
  let(:issue1) { create(:issue, author: user, assignee: user, project: project1, milestone: milestone) }
  let(:issue2) { create(:issue, author: user, assignee: user, project: project2) }
  let(:issue3) { create(:issue, author: user2, assignee: user2, project: project2) }
13
  let!(:label_link) { create(:label_link, label: label, target: issue2) }
14 15 16 17

  before do
    project1.team << [user, :master]
    project2.team << [user, :developer]
18
    project2.team << [user2, :developer]
19 20 21 22

    issue1
    issue2
    issue3
23 24
  end

25 26 27 28
  describe '#execute' do
    let(:search_user) { user }
    let(:params) { {} }
    let(:issues) { IssuesFinder.new(search_user, params.merge(scope: scope, state: 'opened')).execute }
29

30
    context 'scope: all' do
31
      let(:scope) { 'all' }
32

33 34
      it 'returns all issues' do
        expect(issues).to contain_exactly(issue1, issue2, issue3)
35
      end
36

37 38 39 40 41 42
      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
43
      end
44

45 46 47 48 49 50
      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
51 52
      end

53 54 55 56 57 58
      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
59 60
      end

61 62 63 64 65 66
      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
67 68
      end

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
      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|
            create(:issue, project: milestone.project, milestone: milestone, author: user, assignee: user)
          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

103 104
      context 'filtering by label' do
        let(:params) { { label_name: label.title } }
105

106 107 108 109
        it 'returns issues with that label' do
          expect(issues).to contain_exactly(issue2)
        end
      end
110

111 112 113
      context 'filtering by multiple labels' do
        let(:params) { { label_name: [label.title, label2.title].join(',') } }
        let(:label2) { create(:label, project: project2) }
114

115
        before { create(:label_link, label: label2, target: issue2) }
116

117 118 119
        it 'returns the unique issues with any of those labels' do
          expect(issues).to contain_exactly(issue2)
        end
120 121
      end

122 123 124 125 126 127
      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
128 129
      end

130 131 132 133 134 135
      context 'when the user is unauthorized' do
        let(:search_user) { nil }

        it 'returns no results' do
          expect(issues).to be_empty
        end
136 137
      end

138 139 140 141 142 143
      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
144
      end
145 146
    end

147
    context 'personal scope' do
148 149 150 151
      let(:scope) { 'assigned-to-me' }

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

154 155 156 157 158 159
      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
160
      end
161
    end
162 163
  end
end