summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/issuable_state_filter_spec.rb
blob: 0df259333e475fccf95667d2fd4753b4cfbfa86f (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'spec_helper'

describe Banzai::Filter::IssuableStateFilter, lib: true do
  include ActionView::Helpers::UrlHelper
  include FilterSpecHelper

  let(:user) { create(:user) }
  let(:context) { { current_user: user, issuable_state_filter_enabled: true } }

  def create_link(text, data)
    link_to(text, '', class: 'gfm has-tooltip', data: data)
  end

  it 'ignores non-GFM links' do
    html = %(See <a href="https://google.com/">Google</a>)
    doc = filter(html, current_user: user)

    expect(doc.css('a').last.text).to eq('Google')
  end

  it 'ignores non-issuable links' do
    project = create(:empty_project, :public)
    link = create_link('text', project: project, reference_type: 'issue')
    doc = filter(link, context)

    expect(doc.css('a').last.text).to eq('text')
  end

  it 'ignores issuable links with empty content' do
    issue = create(:issue, :closed)
    link = create_link('', issue: issue.id, reference_type: 'issue')
    doc = filter(link, context)

    expect(doc.css('a').last.text).to eq('')
  end

  it 'adds text with standard formatting' do
    issue = create(:issue, :closed)
    link = create_link(
      'something <strong>else</strong>'.html_safe,
      issue: issue.id,
      reference_type: 'issue'
    )
    doc = filter(link, context)

    expect(doc.css('a').last.inner_html).
      to eq('something <strong>else</strong> [closed]')
  end

  it 'does not append state when filter is not enabled' do
    issue = create(:issue, :closed)
    link = create_link('text', issue: issue.id, reference_type: 'issue')
    context = { current_user: user }
    doc = filter(link, context)

    expect(doc.css('a').last.text).to eq('text')
  end

  context 'for issue references' do
    it 'ignores open issue references' do
      issue = create(:issue)
      link = create_link('text', issue: issue.id, reference_type: 'issue')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text')
    end

    it 'ignores reopened issue references' do
      reopened_issue = create(:issue, :reopened)
      link = create_link('text', issue: reopened_issue.id, reference_type: 'issue')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text')
    end

    it 'appends [closed] to closed issue references' do
      closed_issue = create(:issue, :closed)
      link = create_link('text', issue: closed_issue.id, reference_type: 'issue')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text [closed]')
    end
  end

  context 'for merge request references' do
    it 'ignores open merge request references' do
      mr = create(:merge_request)
      link = create_link('text', merge_request: mr.id, reference_type: 'merge_request')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text')
    end

    it 'ignores reopened merge request references' do
      mr = create(:merge_request, :reopened)
      link = create_link('text', merge_request: mr.id, reference_type: 'merge_request')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text')
    end

    it 'ignores locked merge request references' do
      mr = create(:merge_request, :locked)
      link = create_link('text', merge_request: mr.id, reference_type: 'merge_request')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text')
    end

    it 'appends [closed] to closed merge request references' do
      mr = create(:merge_request, :closed)
      link = create_link('text', merge_request: mr.id, reference_type: 'merge_request')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text [closed]')
    end

    it 'appends [merged] to merged merge request references' do
      mr = create(:merge_request, :merged)
      link = create_link('text', merge_request: mr.id, reference_type: 'merge_request')
      doc = filter(link, context)

      expect(doc.css('a').last.text).to eq('text [merged]')
    end
  end
end