summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/reference_parser/project_parser_spec.rb
blob: e4936aa9e5752db8d6c957722c4eb9c55acb9f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# frozen_string_literal: true

require 'spec_helper'

describe Banzai::ReferenceParser::ProjectParser do
  include ReferenceParserHelpers

  let(:project) { create(:project, :public) }
  let(:user) { create(:user) }
  subject { described_class.new(Banzai::RenderContext.new(project, user)) }
  let(:link) { empty_html_link }

  describe '#referenced_by' do
    describe 'when the link has a data-project attribute' do
      context 'using an existing project ID' do
        it 'returns an Array of projects' do
          link['data-project'] = project.id.to_s

          expect(subject.gather_references([link])).to eq([project])
        end
      end

      context 'using a non-existing project ID' do
        it 'returns an empty Array' do
          link['data-project'] = ''

          expect(subject.gather_references([link])).to eq([])
        end
      end

      context 'using a private project ID' do
        it 'returns an empty Array when unauthorized' do
          private_project = create(:project, :private)

          link['data-project'] = private_project.id.to_s

          expect(subject.gather_references([link])).to eq([])
        end

        it 'returns an Array when authorized' do
          private_project = create(:project, :private, namespace: user.namespace)

          link['data-project'] = private_project.id.to_s

          expect(subject.gather_references([link])).to eq([private_project])
        end
      end
    end
  end
end