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

describe ApplicationHelper do
J
Jacob Vosmaer 已提交
4 5
  include UploadHelpers

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

      expect(helper.current_controller?(:foo)).to eq true
11 12
    end

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

      expect(helper.current_controller?(:bar)).to eq false
17
    end
18

R
Robert Speicher 已提交
19 20 21 22 23 24 25 26 27
    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)
28
    end
29 30
  end

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

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

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

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

R
Robert Speicher 已提交
44 45 46 47 48
    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 已提交
49 50
    end

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

56
  describe 'project_icon' do
57
    it 'returns an url for the avatar' do
58
      project = create(:empty_project, avatar: File.open(uploaded_image_temp_path))
R
Robert Speicher 已提交
59

60
      avatar_url = "http://#{Gitlab.config.gitlab.host}/uploads/project/avatar/#{project.id}/banana_sample.gif"
R
Robert Speicher 已提交
61
      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 'gives uploaded icon when present' do
66
      project = create(:empty_project)
67

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

70
      avatar_url = "http://#{Gitlab.config.gitlab.host}#{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
77
    it 'returns an url for the avatar' do
J
Jacob Vosmaer 已提交
78
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
R
Robert Speicher 已提交
79 80 81

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

84
    it 'returns an url for the avatar with relative url' do
R
Robert Speicher 已提交
85 86 87
      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))
88

J
Jacob Vosmaer 已提交
89
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
R
Robert Speicher 已提交
90 91 92

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

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

J
Jan-Gerd Tenberge 已提交
98
      helper.avatar_icon('foo@example.com', 20, 2)
S
Steven Thonus 已提交
99
    end
100 101

    describe 'using a User' do
102
      it 'returns an URL for the avatar' do
J
Jacob Vosmaer 已提交
103
        user = create(:user, avatar: File.open(uploaded_image_temp_path))
104 105 106 107 108

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

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

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

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

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

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

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

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

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

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

147
      it 'returns custom gravatar path when gravatar_url is set' do
R
Robert Speicher 已提交
148
        stub_gravatar_setting(plain_url: 'http://example.local/?s=%{size}&hash=%{hash}')
S
Sergey Linnik 已提交
149

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

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

158 159 160 161 162 163
      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 已提交
164 165 166 167 168 169 170 171
      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 已提交
172
    end
173
  end
174

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

178
    it 'allows the a tag' do
R
Robert Speicher 已提交
179
      expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
R
Robert Speicher 已提交
180 181
    end

182
    it 'allows the span tag' do
R
Robert Speicher 已提交
183
      input = '<span class="foo">Bar</span>'
R
Robert Speicher 已提交
184
      expect(helper.simple_sanitize(input)).to eq(input)
R
Robert Speicher 已提交
185 186
    end

187
    it 'disallows other tags' do
R
Robert Speicher 已提交
188
      input = "<strike><b>#{a_tag}</b></strike>"
R
Robert Speicher 已提交
189
      expect(helper.simple_sanitize(input)).to eq(a_tag)
R
Robert Speicher 已提交
190 191
    end
  end
192

193 194
  describe 'time_ago_with_tooltip' do
    def element(*arguments)
195
      Time.zone = 'UTC'
196
      time = Time.zone.parse('2015-07-02 08:23')
R
Robert Speicher 已提交
197
      element = helper.time_ago_with_tooltip(time, *arguments)
198 199 200 201 202 203 204 205 206

      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
207
      expect(element.text).to eq '2015-07-02 08:23:00 UTC'
208 209 210
    end

    it 'has a datetime attribute' do
211
      expect(element.attr('datetime')).to eq '2015-07-02T08:23:00Z'
212 213 214
    end

    it 'has a formatted title attribute' do
215
      expect(element.attr('title')).to eq 'Jul 2, 2015 8:23am'
216 217 218
    end

    it 'includes a default js-timeago class' do
219
      expect(element.attr('class')).to eq 'js-timeago'
220 221 222
    end

    it 'accepts a custom html_class' do
223
      expect(element(html_class: 'custom_class').attr('class')).
224
        to eq 'js-timeago custom_class'
225 226 227 228 229 230
    end

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

231 232 233
    it 'converts to Time' do
      expect { helper.time_ago_with_tooltip(Date.today) }.not_to raise_error
    end
234

235
    it 'add class for the short format' do
236 237 238 239
      timeago_element = element(short_format: 'short')
      expect(timeago_element.attr('class')).to eq 'js-short-timeago'
      expect(timeago_element.next_element).to eq nil
    end
240 241
  end

242
  describe 'render_markup' do
243
    let(:content) { 'Noël' }
244 245 246 247
    let(:user) { create(:user) }
    before do
      allow(helper).to receive(:current_user).and_return(user)
    end
248

249
    it 'preserves encoding' do
250
      expect(content.encoding.name).to eq('UTF-8')
R
Robert Speicher 已提交
251
      expect(helper.render_markup('foo.rst', content).encoding.name).to eq('UTF-8')
252
    end
253

254
    it "delegates to #markdown when file name corresponds to Markdown" do
R
Robert Speicher 已提交
255 256
      expect(helper).to receive(:gitlab_markdown?).with('foo.md').and_return(true)
      expect(helper).to receive(:markdown).and_return('NOEL')
257

R
Robert Speicher 已提交
258
      expect(helper.render_markup('foo.md', content)).to eq('NOEL')
259 260
    end

261
    it "delegates to #asciidoc when file name corresponds to AsciiDoc" do
R
Robert Speicher 已提交
262 263
      expect(helper).to receive(:asciidoc?).with('foo.adoc').and_return(true)
      expect(helper).to receive(:asciidoc).and_return('NOEL')
264

R
Robert Speicher 已提交
265
      expect(helper.render_markup('foo.adoc', content)).to eq('NOEL')
266
    end
267
  end
S
Semyon Pupkov 已提交
268 269 270 271 272

  describe '#active_when' do
    it { expect(helper.active_when(true)).to eq('active') }
    it { expect(helper.active_when(false)).to eq(nil) }
  end
273
end