BigW Consortium Gitlab

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

describe ApplicationHelper do
4 5
  include UploadHelpers

6
  describe 'current_controller?' do
7
    it 'returns true when controller matches argument' do
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
14 15 16
      stub_controller_name('foo')

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

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

31
  describe 'current_action?' do
32 33 34 35
    it 'returns true when action matches' do
      stub_action_name('foo')

      expect(helper.current_action?(:foo)).to eq true
36 37
    end

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
42 43
    end

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
49 50
    end

51 52
    def stub_action_name(value)
      allow(helper).to receive(:action_name).and_return(value)
53 54
    end
  end
Dmitriy Zaporozhets committed
55

56
  describe 'project_icon' do
57
    it 'returns an url for the avatar' do
58
      project = create(:project, avatar: File.open(uploaded_image_temp_path))
59

60
      avatar_url = "http://#{Gitlab.config.gitlab.host}/uploads/project/avatar/#{project.id}/banana_sample.gif"
61
      expect(helper.project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).
Valery Sizov committed
62
        to eq "<img src=\"#{avatar_url}\" alt=\"Banana sample\" />"
63 64
    end

65
    it 'gives 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

70
      avatar_url = "http://#{Gitlab.config.gitlab.host}#{namespace_project_avatar_path(project.namespace, project)}"
71
      expect(helper.project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match(
72
        image_tag(avatar_url))
73 74 75
    end
  end

76
  describe 'avatar_icon' do
77
    it 'returns an url for the avatar' do
78
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
79 80 81

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

84
    it 'returns an url for the avatar with relative url' do
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

89
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
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
Jan-Gerd Tenberge committed
96
      expect(helper).to receive(:gravatar_icon).with('foo@example.com', 20, 2)
97

Jan-Gerd Tenberge committed
98
      helper.avatar_icon('foo@example.com', 20, 2)
99
    end
100 101

    describe 'using a User' do
102
      it 'returns an URL for the avatar' do
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
109
  end
110

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

114 115 116 117
    context 'with Gravatar disabled' do
      before do
        stub_application_setting(gravatar_enabled?: false)
      end
118

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

124 125 126 127
    context 'with Gravatar enabled' do
      before do
        stub_application_setting(gravatar_enabled?: true)
      end
128

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

133 134
      it 'returns a valid Gravatar URL' do
        stub_config_setting(https: false)
135

136 137 138
        expect(helper.gravatar_icon(user_email)).
          to match('http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118')
      end
139

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
148
        stub_gravatar_setting(plain_url: 'http://example.local/?s=%{size}&hash=%{hash}')
149

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

      it 'accepts a custom size argument' do
155
        expect(helper.gravatar_icon(user_email, 64)).to include '?s=128'
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'
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
172
    end
173
  end
174

175
  describe 'simple_sanitize' do
176 177
    let(:a_tag) { '<a href="#">Foo</a>' }

178
    it 'allows the a tag' do
179
      expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
180 181
    end

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

187
    it 'disallows other tags' do
188
      input = "<strike><b>#{a_tag}</b></strike>"
189
      expect(helper.simple_sanitize(input)).to eq(a_tag)
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')
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')
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
255 256
      expect(helper).to receive(:gitlab_markdown?).with('foo.md').and_return(true)
      expect(helper).to receive(:markdown).and_return('NOEL')
257

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
262 263
      expect(helper).to receive(:asciidoc?).with('foo.adoc').and_return(true)
      expect(helper).to receive(:asciidoc).and_return('NOEL')
264

265
      expect(helper.render_markup('foo.adoc', content)).to eq('NOEL')
266
    end
267
  end
268
end