issue_parser_spec.rb 3.89 KB
Newer Older
1 2
require 'spec_helper'

3
describe Banzai::ReferenceParser::IssueParser do
4 5
  include ReferenceParserHelpers

6
  let(:project) { create(:project, :public) }
7 8 9 10
  let(:user)    { create(:user) }
  let(:issue)   { create(:issue, project: project) }
  let(:link)    { empty_html_link }
  subject       { described_class.new(project, user) }
11 12 13 14 15 16 17

  describe '#nodes_visible_to_user' do
    context 'when the link has a data-issue attribute' do
      before do
        link['data-issue'] = issue.id.to_s
      end

18 19
      it_behaves_like "referenced feature visibility", "issues"

20
      it 'returns the nodes when the user can read the issue' do
21
        expect(Ability).to receive(:issues_readable_by_user)
22 23
                             .with([issue], user)
                             .and_return([issue])
24 25 26 27 28

        expect(subject.nodes_visible_to_user(user, [link])).to eq([link])
      end

      it 'returns an empty Array when the user can not read the issue' do
29
        expect(Ability).to receive(:issues_readable_by_user)
30 31
                             .with([issue], user)
                             .and_return([])
32 33 34

        expect(subject.nodes_visible_to_user(user, [link])).to eq([])
      end
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

      context 'when the user cannot read cross project' do
        let(:issue) { create(:issue) }

        before do
          allow(Ability).to receive(:allowed?).with(user, :read_cross_project) { false }
          allow(Ability).to receive(:allowed?).with(user, :read_cross_project, :global) { false }
        end

        it 'returns the nodes when the user can read the issue' do
          expect(Ability).to receive(:allowed?)
                               .with(user, :read_issue_iid, issue)
                               .and_return(true)

          expect(subject.nodes_visible_to_user(user, [link])).to eq([link])
        end

        it 'returns an empty Array when the user can not read the issue' do
          expect(Ability).to receive(:allowed?)
                               .with(user, :read_issue_iid, issue)
                               .and_return(false)

          expect(subject.nodes_visible_to_user(user, [link])).to eq([])
        end

        context 'when the issue is not cross project' do
          let(:issue) { create(:issue, project: project) }

          it 'does not check `can_read_reference` if the issue is not cross project' do
            expect(Ability).to receive(:issues_readable_by_user)
                                 .with([issue], user)
                                 .and_return([])

            expect(subject).not_to receive(:can_read_reference?).with(user, issue)

            expect(subject.nodes_visible_to_user(user, [link])).to eq([])
          end
        end
      end
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
    end

    context 'when the link does not have a data-issue attribute' do
      it 'returns an empty Array' do
        expect(subject.nodes_visible_to_user(user, [link])).to eq([])
      end
    end
  end

  describe '#referenced_by' do
    context 'when the link has a data-issue attribute' do
      context 'using an existing issue ID' do
        before do
          link['data-issue'] = issue.id.to_s
        end

        it 'returns an Array of issues' do
          expect(subject.referenced_by([link])).to eq([issue])
        end

        it 'returns an empty Array when the list of nodes is empty' do
          expect(subject.referenced_by([link])).to eq([issue])
          expect(subject.referenced_by([])).to eq([])
        end
      end
99 100 101 102 103 104 105 106 107 108

      context 'when issue with given ID does not exist' do
        before do
          link['data-issue'] = '-1'
        end

        it 'returns an empty Array' do
          expect(subject.referenced_by([link])).to eq([])
        end
      end
109 110 111
    end
  end

Jarka Kadlecova's avatar
Jarka Kadlecova committed
112
  describe '#records_for_nodes' do
113 114 115 116
    it 'returns a Hash containing the issues for a list of nodes' do
      link['data-issue'] = issue.id.to_s
      nodes = [link]

Jarka Kadlecova's avatar
Jarka Kadlecova committed
117
      expect(subject.records_for_nodes(nodes)).to eq({ link => issue })
118 119 120
    end
  end
end