summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/markdown/user_reference_filter_spec.rb
blob: 27fe09f443456cfc7e32098521adcbbbdbb4b970 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'spec_helper'

module Gitlab::Markdown
  describe UserReferenceFilter do
    include FilterSpecHelper

    let(:project)   { create(:empty_project) }
    let(:user)      { create(:user) }
    let(:reference) { user.to_reference }

    it 'requires project context' do
      expect { described_class.call('') }.to raise_error(ArgumentError, /:project/)
    end

    it 'ignores invalid users' do
      exp = act = "Hey #{invalidate_reference(reference)}"
      expect(filter(act).to_html).to eq(exp)
    end

    %w(pre code a style).each do |elem|
      it "ignores valid references contained inside '#{elem}' element" do
        exp = act = "<#{elem}>Hey #{reference}</#{elem}>"
        expect(filter(act).to_html).to eq exp
      end
    end

    context 'mentioning @all' do
      let(:reference) { User.reference_prefix + 'all' }

      before do
        project.team << [project.creator, :developer]
      end

      it 'supports a special @all mention' do
        doc = filter("Hey #{reference}")
        expect(doc.css('a').length).to eq 1
        expect(doc.css('a').first.attr('href'))
          .to eq urls.namespace_project_url(project.namespace, project)
      end

      it 'adds to the results hash' do
        result = pipeline_result("Hey #{reference}")
        expect(result[:references][:user]).to eq [project.creator]
      end
    end

    context 'mentioning a user' do
      it 'links to a User' do
        doc = filter("Hey #{reference}")
        expect(doc.css('a').first.attr('href')).to eq urls.user_url(user)
      end

      it 'links to a User with a period' do
        user = create(:user, name: 'alphA.Beta')

        doc = filter("Hey #{user.to_reference}")
        expect(doc.css('a').length).to eq 1
      end

      it 'links to a User with an underscore' do
        user = create(:user, name: 'ping_pong_king')

        doc = filter("Hey #{user.to_reference}")
        expect(doc.css('a').length).to eq 1
      end

      it 'includes a data-user-id attribute' do
        doc = filter("Hey #{reference}")
        link = doc.css('a').first

        expect(link).to have_attribute('data-user-id')
        expect(link.attr('data-user-id')).to eq user.namespace.owner_id.to_s
      end

      it 'adds to the results hash' do
        result = pipeline_result("Hey #{reference}")
        expect(result[:references][:user]).to eq [user]
      end
    end

    context 'mentioning a group' do
      let(:group)     { create(:group) }
      let(:user)      { create(:user) }
      let(:reference) { group.to_reference }

      context 'that the current user can read' do
        before do
          group.add_user(user, Gitlab::Access::DEVELOPER)
        end

        it 'links to the Group' do
          doc = filter("Hey #{reference}", current_user: user)
          expect(doc.css('a').first.attr('href')).to eq urls.group_url(group)
        end

        it 'includes a data-group-id attribute' do
          doc = filter("Hey #{reference}", current_user: user)
          link = doc.css('a').first

          expect(link).to have_attribute('data-group-id')
          expect(link.attr('data-group-id')).to eq group.id.to_s
        end

        it 'adds to the results hash' do
          result = pipeline_result("Hey #{reference}", current_user: user)
          expect(result[:references][:user]).to eq group.users
        end
      end

      context 'that the current user cannot read' do
        it 'ignores references to the Group' do
          doc = filter("Hey #{reference}", current_user: user)
          expect(doc.to_html).to eq "Hey #{reference}"
        end

        it 'does not add to the results hash' do
          result = pipeline_result("Hey #{reference}", current_user: user)
          expect(result[:references][:user]).to eq []
        end
      end
    end

    it 'links with adjacent text' do
      skip "TODO (rspeicher): Re-enable when usernames can't end in periods."
      doc = filter("Mention me (#{reference}.)")
      expect(doc.to_html).to match(/\(<a.+>#{reference}<\/a>\.\)/)
    end

    it 'includes default classes' do
      doc = filter("Hey #{reference}")
      expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-project_member'
    end

    it 'includes an optional custom class' do
      doc = filter("Hey #{reference}", reference_class: 'custom')
      expect(doc.css('a').first.attr('class')).to include 'custom'
    end

    it 'supports an :only_path context' do
      doc = filter("Hey #{reference}", only_path: true)
      link = doc.css('a').first.attr('href')

      expect(link).not_to match %r(https?://)
      expect(link).to eq urls.user_path(user)
    end
  end
end