application_helper_spec.rb 10.5 KB
Newer Older
1 2 3
require 'spec_helper'

describe ApplicationHelper do
4 5
  describe 'current_controller?' do
    before do
6
      allow(controller).to receive(:controller_name).and_return('foo')
7 8
    end

9
    it 'returns true when controller matches argument' do
10
      expect(current_controller?(:foo)).to be_truthy
11 12
    end

13
    it 'returns false when controller does not match argument' do
14
      expect(current_controller?(:bar)).not_to be_truthy
15
    end
16

17
    it 'should take any number of arguments' do
18 19
      expect(current_controller?(:baz, :bar)).not_to be_truthy
      expect(current_controller?(:baz, :bar, :foo)).to be_truthy
20
    end
21 22
  end

R
Robert Speicher 已提交
23 24
  describe 'current_action?' do
    before do
S
skv 已提交
25
      allow(self).to receive(:action_name).and_return('foo')
R
Robert Speicher 已提交
26 27
    end

28
    it 'returns true when action matches argument' do
29
      expect(current_action?(:foo)).to be_truthy
R
Robert Speicher 已提交
30 31
    end

32
    it 'returns false when action does not match argument' do
33
      expect(current_action?(:bar)).not_to be_truthy
R
Robert Speicher 已提交
34 35
    end

36
    it 'should take any number of arguments' do
37 38
      expect(current_action?(:baz, :bar)).not_to be_truthy
      expect(current_action?(:baz, :bar, :foo)).to be_truthy
R
Robert Speicher 已提交
39 40
    end
  end
D
Dmitriy Zaporozhets 已提交
41

42 43 44 45 46 47 48
  describe 'project_icon' do
    avatar_file_path = File.join(Rails.root, 'public', 'gitlab_logo.png')

    it 'should return an url for the avatar' do
      project = create(:project)
      project.avatar = File.open(avatar_file_path)
      project.save!
S
sue445 已提交
49
      avatar_url = "http://localhost/uploads/project/avatar/#{ project.id }/gitlab_logo.png"
V
Vinnie Okada 已提交
50
      expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to eq(
S
sue445 已提交
51
        "<img alt=\"Gitlab logo\" src=\"#{avatar_url}\" />"
52
      )
53 54
    end

55
    it 'should give uploaded icon when present' do
56 57 58
      project = create(:project)
      project.save!

59
      allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true)
60

S
sue445 已提交
61
      avatar_url = 'http://localhost' + namespace_project_avatar_path(project.namespace, project)
V
Vinnie Okada 已提交
62
      expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match(
S
sue445 已提交
63
        image_tag(avatar_url))
64 65 66
    end
  end

67
  describe 'avatar_icon' do
S
Steven Thonus 已提交
68 69
    avatar_file_path = File.join(Rails.root, 'public', 'gitlab_logo.png')

70
    it 'should return an url for the avatar' do
S
Steven Thonus 已提交
71 72 73
      user = create(:user)
      user.avatar = File.open(avatar_file_path)
      user.save!
J
Jeroen van Baarsen 已提交
74 75
      expect(avatar_icon(user.email).to_s).
        to match("/uploads/user/avatar/#{ user.id }/gitlab_logo.png")
S
Steven Thonus 已提交
76 77
    end

78 79
    it 'should return an url for the avatar with relative url' do
      Gitlab.config.gitlab.stub(relative_url_root: '/gitlab')
80 81 82 83 84
      Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))

      user = create(:user)
      user.avatar = File.open(avatar_file_path)
      user.save!
J
Jeroen van Baarsen 已提交
85 86
      expect(avatar_icon(user.email).to_s).
        to match("/gitlab/uploads/user/avatar/#{ user.id }/gitlab_logo.png")
87 88
    end

89
    it 'should call gravatar_icon when no avatar is present' do
D
Dmitriy Zaporozhets 已提交
90
      user = create(:user, email: 'test@example.com')
S
Steven Thonus 已提交
91
      user.save!
92
      expect(avatar_icon(user.email).to_s).to eq('http://www.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=40&d=identicon')
S
Steven Thonus 已提交
93 94
    end
  end
R
Robert Speicher 已提交
95

96
  describe 'gravatar_icon' do
97 98
    let(:user_email) { 'user@email.com' }

99
    it 'should return a generic avatar path when Gravatar is disabled' do
D
Dmitriy Zaporozhets 已提交
100
      ApplicationSetting.any_instance.stub(gravatar_enabled?: false)
101
      expect(gravatar_icon(user_email)).to match('no_avatar.png')
102 103
    end

104
    it 'should return a generic avatar path when email is blank' do
105
      expect(gravatar_icon('')).to match('no_avatar.png')
106 107
    end

108
    it 'should return default gravatar url' do
D
Dmitriy Zaporozhets 已提交
109
      Gitlab.config.gitlab.stub(https: false)
J
Jeroen van Baarsen 已提交
110 111
      url = 'http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118'
      expect(gravatar_icon(user_email)).to match(url)
S
Sergey Linnik 已提交
112 113
    end

114
    it 'should use SSL when appropriate' do
D
Dmitriy Zaporozhets 已提交
115
      Gitlab.config.gitlab.stub(https: true)
116
      expect(gravatar_icon(user_email)).to match('https://secure.gravatar.com')
117 118
    end

119
    it 'should return custom gravatar path when gravatar_url is set' do
S
skv 已提交
120
      allow(self).to receive(:request).and_return(double(:ssl? => false))
J
Jeroen van Baarsen 已提交
121 122 123 124 125
      allow(Gitlab.config.gravatar).
        to receive(:plain_url).
        and_return('http://example.local/?s=%{size}&hash=%{hash}')
      url = 'http://example.local/?s=20&hash=b58c6f14d292556214bd64909bcdb118'
      expect(gravatar_icon(user_email, 20)).to eq(url)
S
Sergey Linnik 已提交
126 127
    end

128
    it 'should accept a custom size' do
S
skv 已提交
129
      allow(self).to receive(:request).and_return(double(:ssl? => false))
130
      expect(gravatar_icon(user_email, 64)).to match(/\?s=64/)
131
    end
S
Sergey Linnik 已提交
132

133
    it 'should use default size when size is wrong' do
S
skv 已提交
134
      allow(self).to receive(:request).and_return(double(:ssl? => false))
135
      expect(gravatar_icon(user_email, nil)).to match(/\?s=40/)
S
Sergey Linnik 已提交
136 137
    end

138
    it 'should be case insensitive' do
S
skv 已提交
139
      allow(self).to receive(:request).and_return(double(:ssl? => false))
J
Jeroen van Baarsen 已提交
140 141
      expect(gravatar_icon(user_email)).
        to eq(gravatar_icon(user_email.upcase + ' '))
S
Sergey Linnik 已提交
142
    end
143 144
  end

145
  describe 'grouped_options_refs' do
146 147 148 149 150 151 152 153 154 155 156 157
    # Override Rails' grouped_options_for_select helper since HTML is harder to work with
    def grouped_options_for_select(options, *args)
      options
    end

    let(:options) { grouped_options_refs }

    before do
      # Must be an instance variable
      @project = create(:project)
    end

158
    it 'includes a list of branch names' do
159 160
      expect(options[0][0]).to eq('Branches')
      expect(options[0][1]).to include('master', 'feature')
161 162
    end

163
    it 'includes a list of tag names' do
164
      expect(options[1][0]).to eq('Tags')
J
Jeroen van Baarsen 已提交
165
      expect(options[1][1]).to include('v1.0.0', 'v1.1.0')
166 167
    end

168
    it 'includes a specific commit ref if defined' do
169 170
      # Must be an instance variable
      @ref = '2ed06dc41dbb5936af845b87d79e05bbf24c73b8'
S
Sergey Linnik 已提交
171

172 173
      expect(options[2][0]).to eq('Commit')
      expect(options[2][1]).to eq([@ref])
174 175
    end

176
    it 'sorts tags in a natural order' do
177
      # Stub repository.tag_names to make sure we get some valid testing data
J
Jeroen van Baarsen 已提交
178
      expect(@project.repository).to receive(:tag_names).
179 180
        and_return(['v1.0.9', 'v1.0.10', 'v2.0', 'v3.1.4.2', 'v2.0rc1¿',
                    'v1.0.9a', 'v2.0-rc1', 'v2.0rc2'])
181

J
Jeroen van Baarsen 已提交
182
      expect(options[1][1]).
183 184
        to eq(['v3.1.4.2', 'v2.0', 'v2.0rc2', 'v2.0rc1¿', 'v2.0-rc1', 'v1.0.10',
               'v1.0.9', 'v1.0.9a'])
185
    end
186
  end
187

188
  describe 'simple_sanitize' do
R
Robert Speicher 已提交
189 190
    let(:a_tag) { '<a href="#">Foo</a>' }

191
    it 'allows the a tag' do
192
      expect(simple_sanitize(a_tag)).to eq(a_tag)
R
Robert Speicher 已提交
193 194
    end

195
    it 'allows the span tag' do
R
Robert Speicher 已提交
196
      input = '<span class="foo">Bar</span>'
197
      expect(simple_sanitize(input)).to eq(input)
R
Robert Speicher 已提交
198 199
    end

200
    it 'disallows other tags' do
R
Robert Speicher 已提交
201
      input = "<strike><b>#{a_tag}</b></strike>"
202
      expect(simple_sanitize(input)).to eq(a_tag)
R
Robert Speicher 已提交
203 204
    end
  end
205

206 207
  describe 'link_to' do
    it 'should not include rel=nofollow for internal links' do
208
      expect(link_to('Home', root_path)).to eq('<a href="/">Home</a>')
209 210
    end

211
    it 'should include rel=nofollow for external links' do
212 213 214 215 216 217 218
      expect(link_to('Example', 'http://www.example.com')).
        to eq '<a href="http://www.example.com" rel="nofollow">Example</a>'
    end

    it 'should include rel=nofollow for external links and honor existing html_options' do
      expect(link_to('Example', 'http://www.example.com', class: 'toggle', data: {toggle: 'dropdown'}))
        .to eq '<a class="toggle" data-toggle="dropdown" href="http://www.example.com" rel="nofollow">Example</a>'
219 220
    end

221 222 223
    it 'should include rel=nofollow for external links and preserve other rel values' do
      expect(link_to('Example', 'http://www.example.com', rel: 'noreferrer'))
        .to eq '<a href="http://www.example.com" rel="noreferrer nofollow">Example</a>'
224 225
    end

226 227 228 229
    it 'should not include rel=nofollow for external links on the same host as GitLab' do
      expect(Gitlab.config.gitlab).to receive(:host).and_return('example.foo')
      expect(link_to('Example', 'http://example.foo/bar')).
        to eq '<a href="http://example.foo/bar">Example</a>'
230
    end
R
Robert Speicher 已提交
231 232 233 234 235 236 237 238 239 240

    it 'should not raise an error when given a bad URI' do
      expect { link_to('default', 'if real=1 RANDOM; if real>1 IDLHS; if real>500 LHS') }.
        not_to raise_error
    end

    it 'should not raise an error when given a bad mailto URL' do
      expect { link_to('email', 'mailto://foo.bar@example.es?subject=Subject%20Line') }.
        not_to raise_error
    end
241
  end
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  describe 'time_ago_with_tooltip' do
    def element(*arguments)
      time = Time.parse('2015-07-02 08:00')
      element = time_ago_with_tooltip(time, *arguments)

      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    it 'returns a time element' do
      expect(element.name).to eq 'time'
    end

    it 'includes the date string' do
      expect(element.text).to match %r{2015-07-02 \d{2}:\d{2}:\d{2}}
    end

    it 'has a datetime attribute' do
      expect(element.attr('datetime')).to eq '2015-07-02T12:00:00Z'
    end

    it 'has a formatted title attribute' do
      expect(element.attr('title')).to eq 'Jul 02, 2015 12:00pm'
    end

    it 'includes a default js-timeago class' do
      expect(element.attr('class')).to eq 'time_ago js-timeago'
    end

    it 'accepts a custom html_class' do
      expect(element(html_class: 'custom_class').attr('class')).to eq 'custom_class js-timeago'
    end

    it 'accepts a custom tooltip placement' do
      expect(element(placement: 'bottom').attr('data-placement')).to eq 'bottom'
    end

    it 're-initializes timeago Javascript' do
      el = element.next_element

      expect(el.name).to eq 'script'
      expect(el.text).to include "$('.js-timeago').timeago()"
    end

    it 'allows the script tag to be excluded' do
      expect(element(skip_js: true)).not_to include 'script'
    end
  end

291
  describe 'render_markup' do
292 293 294
    let(:content) { 'Noël' }

    it 'should preserve encoding' do
295
      expect(content.encoding.name).to eq('UTF-8')
296 297
      expect(render_markup('foo.rst', content).encoding.name).to eq('UTF-8')
    end
298

299 300 301 302 303 304 305
    it "should delegate to #markdown when file name corresponds to Markdown" do
      expect(self).to receive(:gitlab_markdown?).with('foo.md').and_return(true)
      expect(self).to receive(:markdown).and_return('NOEL')

      expect(render_markup('foo.md', content)).to eq('NOEL')
    end

306 307 308 309 310 311
    it "should delegate to #asciidoc when file name corresponds to AsciiDoc" do
      expect(self).to receive(:asciidoc?).with('foo.adoc').and_return(true)
      expect(self).to receive(:asciidoc).and_return('NOEL')

      expect(render_markup('foo.adoc', content)).to eq('NOEL')
    end
312
  end
313
end