Read about our upcoming Code of Conduct on this issue

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

describe ApplicationHelper do
4
5
  include UploadHelpers

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

8
  describe 'current_controller?' do
9
    it 'returns true when controller matches argument' do
Robert Speicher's avatar
Robert Speicher committed
10
11
12
      stub_controller_name('foo')

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

15
    it 'returns false when controller does not match argument' do
Robert Speicher's avatar
Robert Speicher committed
16
17
18
      stub_controller_name('foo')

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

Robert Speicher's avatar
Robert Speicher committed
21
22
23
24
25
26
27
28
29
    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)
30
    end
31
32
  end

Robert Speicher's avatar
Robert Speicher committed
33
  describe 'current_action?' do
Robert Speicher's avatar
Robert Speicher committed
34
35
36
37
    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
38
39
    end

Robert Speicher's avatar
Robert Speicher committed
40
41
42
43
    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
44
45
    end

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

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

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

      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)
      avatar_url = "#{gitlab_host}/uploads/project/avatar/#{project.id}/banana_sample.gif"
Robert Speicher's avatar
Robert Speicher committed
68

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

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

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

78
79
      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))
80
81
82
    end
  end

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

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

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

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

      expect(helper.avatar_icon(user.email).to_s).to match(avatar_url)
Steven Thonus's avatar
Steven Thonus committed
95
96
    end

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

102
      user = create(:user, avatar: File.open(uploaded_image_temp_path))
Robert Speicher's avatar
Robert Speicher committed
103
104
105

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

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

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

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

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

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

Robert Speicher's avatar
Robert Speicher committed
127
128
129
130
    context 'with Gravatar disabled' do
      before do
        stub_application_setting(gravatar_enabled?: false)
      end
131

Robert Speicher's avatar
Robert Speicher committed
132
133
134
      it 'returns a generic avatar' do
        expect(helper.gravatar_icon(user_email)).to match('no_avatar.png')
      end
135
136
    end

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

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

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

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

Robert Speicher's avatar
Robert Speicher committed
153
154
155
156
157
158
159
      it 'uses HTTPs when configured' do
        stub_config_setting(https: true)

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

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

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

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

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

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

191
    it 'allows the a tag' do
Robert Speicher's avatar
Robert Speicher committed
192
      expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
193
194
    end

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

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

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

      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
220
      expect(element.text).to eq @time.strftime("%b %d, %Y")
221
222
223
    end

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

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

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

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

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

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

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

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