application_helper_spec.rb 7.94 KB
Newer Older
1
# coding: utf-8
2 3 4
require 'spec_helper'

describe ApplicationHelper do
5 6
  include UploadHelpers

7 8
  let(:gitlab_host) { "http://#{Gitlab.config.gitlab.host}" }

9
  describe 'current_controller?' do
10
    it 'returns true when controller matches argument' do
11 12 13
      stub_controller_name('foo')

      expect(helper.current_controller?(:foo)).to eq true
14 15
    end

16
    it 'returns false when controller does not match argument' do
17 18 19
      stub_controller_name('foo')

      expect(helper.current_controller?(:bar)).to eq false
20
    end
21

22 23 24 25 26 27 28 29 30
    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)
31
    end
32 33
  end

Robert Speicher's avatar
Robert Speicher committed
34
  describe 'current_action?' do
35 36 37 38
    it 'returns true when action matches' do
      stub_action_name('foo')

      expect(helper.current_action?(:foo)).to eq true
Robert Speicher's avatar
Robert Speicher committed
39 40
    end

41 42 43 44
    it 'returns false when action does not match' do
      stub_action_name('foo')

      expect(helper.current_action?(:bar)).to eq false
Robert Speicher's avatar
Robert Speicher committed
45 46
    end

47 48 49 50 51
    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
Robert Speicher's avatar
Robert Speicher committed
52 53
    end

54 55
    def stub_action_name(value)
      allow(helper).to receive(:action_name).and_return(value)
Robert Speicher's avatar
Robert Speicher committed
56 57
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
58

59
  describe 'project_icon' do
60
    it 'returns an url for the avatar' do
61
      project = create(:empty_project, avatar: File.open(uploaded_image_temp_path))
62
      avatar_url = "/uploads/system/project/avatar/#{project.id}/banana_sample.gif"
63 64 65 66 67

      expect(helper.project_icon(project.full_path).to_s).
        to eq "<img src=\"#{avatar_url}\" alt=\"Banana sample\" />"

      allow(ActionController::Base).to receive(:asset_host).and_return(gitlab_host)
68
      avatar_url = "#{gitlab_host}/uploads/system/project/avatar/#{project.id}/banana_sample.gif"
69

70
      expect(helper.project_icon(project.full_path).to_s).
Valery Sizov's avatar
Valery Sizov committed
71
        to eq "<img src=\"#{avatar_url}\" alt=\"Banana sample\" />"
72 73
    end

74
    it 'gives uploaded icon when present' do
75
      project = create(:empty_project)
76

77
      allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true)
78

79 80
      avatar_url = "#{gitlab_host}#{namespace_project_avatar_path(project.namespace, project)}"
      expect(helper.project_icon(project.full_path).to_s).to match(image_tag(avatar_url))
81 82 83
    end
  end

84
  describe 'avatar_icon' do
85
    it 'returns an url for the avatar' do
86
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
87

88
      avatar_url = "/uploads/system/user/avatar/#{user.id}/banana_sample.gif"
89 90 91 92

      expect(helper.avatar_icon(user.email).to_s).to match(avatar_url)

      allow(ActionController::Base).to receive(:asset_host).and_return(gitlab_host)
93
      avatar_url = "#{gitlab_host}/uploads/system/user/avatar/#{user.id}/banana_sample.gif"
94 95

      expect(helper.avatar_icon(user.email).to_s).to match(avatar_url)
96 97
    end

98
    it 'returns an url for the avatar with relative url' do
99 100 101
      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))
102

103
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
104 105

      expect(helper.avatar_icon(user.email).to_s).
106
        to match("/gitlab/uploads/system/user/avatar/#{user.id}/banana_sample.gif")
107 108
    end

109
    it 'calls gravatar_icon when no User exists with the given email' do
Jan-Gerd Tenberge's avatar
Jan-Gerd Tenberge committed
110
      expect(helper).to receive(:gravatar_icon).with('foo@example.com', 20, 2)
111

Jan-Gerd Tenberge's avatar
Jan-Gerd Tenberge committed
112
      helper.avatar_icon('foo@example.com', 20, 2)
113
    end
114 115

    describe 'using a User' do
116
      it 'returns an URL for the avatar' do
117
        user = create(:user, avatar: File.open(uploaded_image_temp_path))
118 119

        expect(helper.avatar_icon(user).to_s).
120
          to match("/uploads/system/user/avatar/#{user.id}/banana_sample.gif")
121 122
      end
    end
123
  end
Robert Speicher's avatar
Robert Speicher committed
124

125
  describe 'gravatar_icon' do
126 127
    let(:user_email) { 'user@email.com' }

128 129 130 131
    context 'with Gravatar disabled' do
      before do
        stub_application_setting(gravatar_enabled?: false)
      end
132

133 134 135
      it 'returns a generic avatar' do
        expect(helper.gravatar_icon(user_email)).to match('no_avatar.png')
      end
136 137
    end

138 139 140 141
    context 'with Gravatar enabled' do
      before do
        stub_application_setting(gravatar_enabled?: true)
      end
Sergey Linnik's avatar
Sergey Linnik committed
142

143 144 145
      it 'returns a generic avatar when email is blank' do
        expect(helper.gravatar_icon('')).to match('no_avatar.png')
      end
146

147 148
      it 'returns a valid Gravatar URL' do
        stub_config_setting(https: false)
Sergey Linnik's avatar
Sergey Linnik committed
149

150 151 152
        expect(helper.gravatar_icon(user_email)).
          to match('http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118')
      end
Sergey Linnik's avatar
Sergey Linnik committed
153

154 155 156 157 158 159 160
      it 'uses HTTPs when configured' do
        stub_config_setting(https: true)

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

161
      it 'returns custom gravatar path when gravatar_url is set' do
162
        stub_gravatar_setting(plain_url: 'http://example.local/?s=%{size}&hash=%{hash}')
Sergey Linnik's avatar
Sergey Linnik committed
163

164
        expect(gravatar_icon(user_email, 20)).
165
          to eq('http://example.local/?s=40&hash=b58c6f14d292556214bd64909bcdb118')
166 167 168
      end

      it 'accepts a custom size argument' do
169
        expect(helper.gravatar_icon(user_email, 64)).to include '?s=128'
170 171
      end

172 173 174 175 176 177
      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'
178 179 180 181 182 183 184 185
      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
Sergey Linnik's avatar
Sergey Linnik committed
186
    end
187
  end
188

189
  describe 'simple_sanitize' do
190 191
    let(:a_tag) { '<a href="#">Foo</a>' }

192
    it 'allows the a tag' do
193
      expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
194 195
    end

196
    it 'allows the span tag' do
197
      input = '<span class="foo">Bar</span>'
198
      expect(helper.simple_sanitize(input)).to eq(input)
199 200
    end

201
    it 'disallows other tags' do
202
      input = "<strike><b>#{a_tag}</b></strike>"
203
      expect(helper.simple_sanitize(input)).to eq(a_tag)
204 205
    end
  end
206

207 208
  describe 'time_ago_with_tooltip' do
    def element(*arguments)
209
      Time.zone = 'UTC'
Phil Hughes's avatar
Phil Hughes committed
210 211
      @time = Time.zone.parse('2015-07-02 08:23')
      element = helper.time_ago_with_tooltip(@time, *arguments)
212 213 214 215 216 217 218 219 220

      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
Phil Hughes's avatar
Phil Hughes committed
221
      expect(element.text).to eq @time.strftime("%b %d, %Y")
222 223 224
    end

    it 'has a datetime attribute' do
225
      expect(element.attr('datetime')).to eq '2015-07-02T08:23:00Z'
226 227 228
    end

    it 'has a formatted title attribute' do
229
      expect(element.attr('title')).to eq 'Jul 2, 2015 8:23am'
230 231 232
    end

    it 'includes a default js-timeago class' do
233
      expect(element.attr('class')).to eq 'js-timeago'
234 235 236
    end

    it 'accepts a custom html_class' do
237
      expect(element(html_class: 'custom_class').attr('class')).
238
        to eq 'js-timeago custom_class'
239 240 241 242 243 244
    end

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

245 246 247
    it 'converts to Time' do
      expect { helper.time_ago_with_tooltip(Date.today) }.not_to raise_error
    end
248

249
    it 'add class for the short format' do
250 251 252 253
      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
254 255
  end

Semyon Pupkov's avatar
Semyon Pupkov committed
256 257 258 259
  describe '#active_when' do
    it { expect(helper.active_when(true)).to eq('active') }
    it { expect(helper.active_when(false)).to eq(nil) }
  end
260
end