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

describe ApplicationHelper do
4
  describe 'current_controller?' do
5
    it 'returns true when controller matches argument' do
R
Robert Speicher 已提交
6 7 8
      stub_controller_name('foo')

      expect(helper.current_controller?(:foo)).to eq true
9 10
    end

11
    it 'returns false when controller does not match argument' do
R
Robert Speicher 已提交
12 13 14
      stub_controller_name('foo')

      expect(helper.current_controller?(:bar)).to eq false
15
    end
16

R
Robert Speicher 已提交
17 18 19 20 21 22 23 24 25
    it 'takes any number of arguments' do
      stub_controller_name('foo')

      expect(helper.current_controller?(:baz, :bar)).to eq false
      expect(helper.current_controller?(:baz, :bar, :foo)).to eq true
    end

    def stub_controller_name(value)
      allow(helper.controller).to receive(:controller_name).and_return(value)
26
    end
27 28
  end

R
Robert Speicher 已提交
29
  describe 'current_action?' do
R
Robert Speicher 已提交
30 31 32 33
    it 'returns true when action matches' do
      stub_action_name('foo')

      expect(helper.current_action?(:foo)).to eq true
R
Robert Speicher 已提交
34 35
    end

R
Robert Speicher 已提交
36 37 38 39
    it 'returns false when action does not match' do
      stub_action_name('foo')

      expect(helper.current_action?(:bar)).to eq false
R
Robert Speicher 已提交
40 41
    end

R
Robert Speicher 已提交
42 43 44 45 46
    it 'takes any number of arguments' do
      stub_action_name('foo')

      expect(helper.current_action?(:baz, :bar)).to eq false
      expect(helper.current_action?(:baz, :bar, :foo)).to eq true
R
Robert Speicher 已提交
47 48
    end

R
Robert Speicher 已提交
49 50
    def stub_action_name(value)
      allow(helper).to receive(:action_name).and_return(value)
R
Robert Speicher 已提交
51 52
    end
  end
D
Dmitriy Zaporozhets 已提交
53

54
  describe 'project_icon' do
R
Robert Speicher 已提交
55
    let(:avatar_file_path) { File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif') }
56 57

    it 'should return an url for the avatar' do
R
Robert Speicher 已提交
58 59 60 61
      project = create(:project, avatar: File.open(avatar_file_path))

      avatar_url = "http://localhost/uploads/project/avatar/#{project.id}/banana_sample.gif"
      expect(helper.project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).
V
Valery Sizov 已提交
62
        to eq "<img src=\"#{avatar_url}\" alt=\"Banana sample\" />"
63 64
    end

65
    it 'should give uploaded icon when present' do
66 67
      project = create(:project)

68
      allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true)
69

S
sue445 已提交
70
      avatar_url = 'http://localhost' + namespace_project_avatar_path(project.namespace, project)
R
Robert Speicher 已提交
71
      expect(helper.project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match(
S
sue445 已提交
72
        image_tag(avatar_url))
73 74 75
    end
  end

76
  describe 'avatar_icon' do
R
Robert Speicher 已提交
77
    let(:avatar_file_path) { File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif') }
S
Steven Thonus 已提交
78

79
    it 'should return an url for the avatar' do
R
Robert Speicher 已提交
80 81 82 83
      user = create(:user, avatar: File.open(avatar_file_path))

      expect(helper.avatar_icon(user.email).to_s).
        to match("/uploads/user/avatar/#{user.id}/banana_sample.gif")
S
Steven Thonus 已提交
84 85
    end

86
    it 'should return an url for the avatar with relative url' do
R
Robert Speicher 已提交
87 88 89
      stub_config_setting(relative_url_root: '/gitlab')
      # Must be stubbed after the stub above, and separately
      stub_config_setting(url: Settings.send(:build_gitlab_url))
90

R
Robert Speicher 已提交
91 92 93 94
      user = create(:user, avatar: File.open(avatar_file_path))

      expect(helper.avatar_icon(user.email).to_s).
        to match("/gitlab/uploads/user/avatar/#{user.id}/banana_sample.gif")
95 96
    end

R
Robert Speicher 已提交
97
    it 'should call gravatar_icon when no User exists with the given email' do
J
Jan-Gerd Tenberge 已提交
98
      expect(helper).to receive(:gravatar_icon).with('foo@example.com', 20, 2)
R
Robert Speicher 已提交
99

J
Jan-Gerd Tenberge 已提交
100
      helper.avatar_icon('foo@example.com', 20, 2)
S
Steven Thonus 已提交
101
    end
102 103 104 105 106 107 108 109 110

    describe 'using a User' do
      it 'should return an URL for the avatar' do
        user = create(:user, avatar: File.open(avatar_file_path))

        expect(helper.avatar_icon(user).to_s).
          to match("/uploads/user/avatar/#{user.id}/banana_sample.gif")
      end
    end
S
Steven Thonus 已提交
111
  end
R
Robert Speicher 已提交
112

113
  describe 'gravatar_icon' do
114 115
    let(:user_email) { 'user@email.com' }

R
Robert Speicher 已提交
116 117 118 119
    context 'with Gravatar disabled' do
      before do
        stub_application_setting(gravatar_enabled?: false)
      end
120

R
Robert Speicher 已提交
121 122 123
      it 'returns a generic avatar' do
        expect(helper.gravatar_icon(user_email)).to match('no_avatar.png')
      end
124 125
    end

R
Robert Speicher 已提交
126 127 128 129
    context 'with Gravatar enabled' do
      before do
        stub_application_setting(gravatar_enabled?: true)
      end
S
Sergey Linnik 已提交
130

R
Robert Speicher 已提交
131 132 133
      it 'returns a generic avatar when email is blank' do
        expect(helper.gravatar_icon('')).to match('no_avatar.png')
      end
134

R
Robert Speicher 已提交
135 136
      it 'returns a valid Gravatar URL' do
        stub_config_setting(https: false)
S
Sergey Linnik 已提交
137

R
Robert Speicher 已提交
138 139 140
        expect(helper.gravatar_icon(user_email)).
          to match('http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118')
      end
S
Sergey Linnik 已提交
141

R
Robert Speicher 已提交
142 143 144 145 146 147 148 149 150
      it 'uses HTTPs when configured' do
        stub_config_setting(https: true)

        expect(helper.gravatar_icon(user_email)).
          to match('https://secure.gravatar.com')
      end

      it 'should return custom gravatar path when gravatar_url is set' do
        stub_gravatar_setting(plain_url: 'http://example.local/?s=%{size}&hash=%{hash}')
S
Sergey Linnik 已提交
151

R
Robert Speicher 已提交
152
        expect(gravatar_icon(user_email, 20)).
153
          to eq('http://example.local/?s=40&hash=b58c6f14d292556214bd64909bcdb118')
R
Robert Speicher 已提交
154 155 156
      end

      it 'accepts a custom size argument' do
157
        expect(helper.gravatar_icon(user_email, 64)).to include '?s=128'
R
Robert Speicher 已提交
158 159
      end

160 161 162 163 164 165
      it 'defaults size to 40@2x when given an invalid size' do
        expect(helper.gravatar_icon(user_email, nil)).to include '?s=80'
      end

      it 'accepts a scaling factor' do
        expect(helper.gravatar_icon(user_email, 40, 3)).to include '?s=120'
R
Robert Speicher 已提交
166 167 168 169 170 171 172 173
      end

      it 'ignores case and surrounding whitespace' do
        normal = helper.gravatar_icon('foo@example.com')
        upcase = helper.gravatar_icon(' FOO@EXAMPLE.COM ')

        expect(normal).to eq upcase
      end
S
Sergey Linnik 已提交
174
    end
175 176
  end

177
  describe 'grouped_options_refs' do
R
Robert Speicher 已提交
178 179
    let(:options) { helper.grouped_options_refs }
    let(:project) { create(:project) }
180 181

    before do
R
Robert Speicher 已提交
182 183 184 185 186 187 188
      assign(:project, project)

      # Override Rails' grouped_options_for_select helper to just return the
      # first argument (`options`), since it's easier to work with than the
      # generated HTML.
      allow(helper).to receive(:grouped_options_for_select).
        and_wrap_original { |_, *args| args.first }
189 190
    end

191
    it 'includes a list of branch names' do
192 193
      expect(options[0][0]).to eq('Branches')
      expect(options[0][1]).to include('master', 'feature')
194 195
    end

196
    it 'includes a list of tag names' do
197
      expect(options[1][0]).to eq('Tags')
J
Jeroen van Baarsen 已提交
198
      expect(options[1][1]).to include('v1.0.0', 'v1.1.0')
199 200
    end

201
    it 'includes a specific commit ref if defined' do
202
      # Must be an instance variable
R
Robert Speicher 已提交
203 204
      ref = '2ed06dc41dbb5936af845b87d79e05bbf24c73b8'
      assign(:ref, ref)
S
Sergey Linnik 已提交
205

206
      expect(options[2][0]).to eq('Commit')
R
Robert Speicher 已提交
207
      expect(options[2][1]).to eq([ref])
208 209
    end

210
    it 'sorts tags in a natural order' do
211
      # Stub repository.tag_names to make sure we get some valid testing data
R
Robert Speicher 已提交
212
      expect(project.repository).to receive(:tag_names).
213 214
        and_return(['v1.0.9', 'v1.0.10', 'v2.0', 'v3.1.4.2', 'v2.0rc1¿',
                    'v1.0.9a', 'v2.0-rc1', 'v2.0rc2'])
215

J
Jeroen van Baarsen 已提交
216
      expect(options[1][1]).
217 218
        to eq(['v3.1.4.2', 'v2.0', 'v2.0rc2', 'v2.0rc1¿', 'v2.0-rc1', 'v1.0.10',
               'v1.0.9', 'v1.0.9a'])
219
    end
220
  end
221

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

225
    it 'allows the a tag' do
R
Robert Speicher 已提交
226
      expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
R
Robert Speicher 已提交
227 228
    end

229
    it 'allows the span tag' do
R
Robert Speicher 已提交
230
      input = '<span class="foo">Bar</span>'
R
Robert Speicher 已提交
231
      expect(helper.simple_sanitize(input)).to eq(input)
R
Robert Speicher 已提交
232 233
    end

234
    it 'disallows other tags' do
R
Robert Speicher 已提交
235
      input = "<strike><b>#{a_tag}</b></strike>"
R
Robert Speicher 已提交
236
      expect(helper.simple_sanitize(input)).to eq(a_tag)
R
Robert Speicher 已提交
237 238
    end
  end
239

240 241
  describe 'time_ago_with_tooltip' do
    def element(*arguments)
242
      Time.zone = 'UTC'
243
      time = Time.zone.parse('2015-07-02 08:23')
R
Robert Speicher 已提交
244
      element = helper.time_ago_with_tooltip(time, *arguments)
245 246 247 248 249 250 251 252 253

      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
254
      expect(element.text).to eq '2015-07-02 08:23:00 UTC'
255 256 257
    end

    it 'has a datetime attribute' do
258
      expect(element.attr('datetime')).to eq '2015-07-02T08:23:00Z'
259 260 261
    end

    it 'has a formatted title attribute' do
262
      expect(element.attr('title')).to eq 'Jul 2, 2015 8:23am'
263 264 265
    end

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

    it 'accepts a custom html_class' do
270 271
      expect(element(html_class: 'custom_class').attr('class')).
        to eq 'custom_class js-timeago js-timeago-pending'
272 273 274 275 276 277 278 279 280 281
    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'
282
      expect(el.text).to include "$('.js-timeago-pending').removeClass('js-timeago-pending').timeago()"
283 284 285 286 287
    end

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

    it 'converts to Time' do
      expect { helper.time_ago_with_tooltip(Date.today) }.not_to raise_error
    end
292 293
  end

294
  describe 'render_markup' do
295
    let(:content) { 'Noël' }
296 297 298 299
    let(:user) { create(:user) }
    before do
      allow(helper).to receive(:current_user).and_return(user)
    end
300 301

    it 'should preserve encoding' do
302
      expect(content.encoding.name).to eq('UTF-8')
R
Robert Speicher 已提交
303
      expect(helper.render_markup('foo.rst', content).encoding.name).to eq('UTF-8')
304
    end
305

306
    it "should delegate to #markdown when file name corresponds to Markdown" do
R
Robert Speicher 已提交
307 308
      expect(helper).to receive(:gitlab_markdown?).with('foo.md').and_return(true)
      expect(helper).to receive(:markdown).and_return('NOEL')
309

R
Robert Speicher 已提交
310
      expect(helper.render_markup('foo.md', content)).to eq('NOEL')
311 312
    end

313
    it "should delegate to #asciidoc when file name corresponds to AsciiDoc" do
R
Robert Speicher 已提交
314 315
      expect(helper).to receive(:asciidoc?).with('foo.adoc').and_return(true)
      expect(helper).to receive(:asciidoc).and_return('NOEL')
316

R
Robert Speicher 已提交
317
      expect(helper.render_markup('foo.adoc', content)).to eq('NOEL')
318
    end
319
  end
320
end