debian-mirror-gitlab/spec/helpers/avatars_helper_spec.rb

298 lines
8.7 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require 'rails_helper'
describe AvatarsHelper do
2018-05-09 12:01:36 +05:30
include UploadHelpers
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
2018-05-09 12:01:36 +05:30
describe '#project_icon' do
it 'returns an url for the avatar' do
project = create(:project, :public, avatar: File.open(uploaded_image_temp_path))
expect(helper.project_icon(project.full_path).to_s)
.to eq "<img data-src=\"#{project.avatar.url}\" class=\" lazy\" src=\"#{LazyImageTagHelper.placeholder_image}\" />"
end
end
describe '#avatar_icon_for' do
let!(:user) { create(:user, avatar: File.open(uploaded_image_temp_path), email: 'bar@example.com') }
let(:email) { 'foo@example.com' }
let!(:another_user) { create(:user, avatar: File.open(uploaded_image_temp_path), email: email) }
it 'prefers the user to retrieve the avatar_url' do
expect(helper.avatar_icon_for(user, email).to_s)
.to eq(user.avatar.url)
end
it 'falls back to email lookup if no user given' do
expect(helper.avatar_icon_for(nil, email).to_s)
.to eq(another_user.avatar.url)
end
end
describe '#avatar_icon_for_email' do
let(:user) { create(:user, avatar: File.open(uploaded_image_temp_path)) }
context 'using an email' do
context 'when there is a matching user' do
it 'returns a relative URL for the avatar' do
expect(helper.avatar_icon_for_email(user.email).to_s)
.to eq(user.avatar.url)
end
end
context 'when no user exists for the email' do
it 'calls gravatar_icon' do
expect(helper).to receive(:gravatar_icon).with('foo@example.com', 20, 2)
helper.avatar_icon_for_email('foo@example.com', 20, 2)
end
end
context 'without an email passed' do
it 'calls gravatar_icon' do
expect(helper).to receive(:gravatar_icon).with(nil, 20, 2)
helper.avatar_icon_for_email(nil, 20, 2)
end
end
end
end
describe '#avatar_icon_for_user' do
let(:user) { create(:user, avatar: File.open(uploaded_image_temp_path)) }
context 'with a user object passed' do
it 'returns a relative URL for the avatar' do
expect(helper.avatar_icon_for_user(user).to_s)
.to eq(user.avatar.url)
end
end
context 'without a user object passed' do
it 'calls gravatar_icon' do
expect(helper).to receive(:gravatar_icon).with(nil, 20, 2)
helper.avatar_icon_for_user(nil, 20, 2)
end
end
end
describe '#gravatar_icon' do
let(:user_email) { 'user@email.com' }
context 'with Gravatar disabled' do
before do
stub_application_setting(gravatar_enabled?: false)
end
it 'returns a generic avatar' do
expect(helper.gravatar_icon(user_email)).to match_asset_path('no_avatar.png')
end
end
context 'with Gravatar enabled' do
before do
stub_application_setting(gravatar_enabled?: true)
end
it 'returns a generic avatar when email is blank' do
expect(helper.gravatar_icon('')).to match_asset_path('no_avatar.png')
end
it 'returns a valid Gravatar URL' do
stub_config_setting(https: false)
expect(helper.gravatar_icon(user_email))
.to match('https://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118')
end
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 'returns custom gravatar path when gravatar_url is set' do
stub_gravatar_setting(plain_url: 'http://example.local/?s=%{size}&hash=%{hash}')
expect(gravatar_icon(user_email, 20))
.to eq('http://example.local/?s=40&hash=b58c6f14d292556214bd64909bcdb118')
end
it 'accepts a custom size argument' do
expect(helper.gravatar_icon(user_email, 64)).to include '?s=128'
end
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'
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
end
end
2017-08-17 22:00:37 +05:30
describe '#user_avatar' do
subject { helper.user_avatar(user: user) }
it "links to the user's profile" do
is_expected.to include("href=\"#{user_path(user)}\"")
end
it "has the user's name as title" do
is_expected.to include("title=\"#{user.name}\"")
end
it "contains the user's avatar image" do
2017-09-10 17:25:29 +05:30
is_expected.to include(CGI.escapeHTML(user.avatar_url(size: 16)))
end
end
describe '#user_avatar_without_link' do
let(:options) { { user: user } }
subject { helper.user_avatar_without_link(options) }
it 'displays user avatar' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_user(user, 16),
2018-03-17 18:26:18 +05:30
data: { container: 'body' },
class: 'avatar s16 has-tooltip',
title: user.name
2017-09-10 17:25:29 +05:30
)
end
context 'with css_class parameter' do
let(:options) { { user: user, css_class: '.cat-pics' } }
it 'uses provided css_class' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_user(user, 16),
2018-03-17 18:26:18 +05:30
data: { container: 'body' },
class: "avatar s16 #{options[:css_class]} has-tooltip",
title: user.name
2017-09-10 17:25:29 +05:30
)
end
end
context 'with size parameter' do
let(:options) { { user: user, size: 99 } }
it 'uses provided size' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_user(user, options[:size]),
2018-03-17 18:26:18 +05:30
data: { container: 'body' },
class: "avatar s#{options[:size]} has-tooltip",
title: user.name
2017-09-10 17:25:29 +05:30
)
end
end
context 'with url parameter' do
let(:options) { { user: user, url: '/over/the/rainbow.png' } }
it 'uses provided url' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-17 18:26:18 +05:30
src: options[:url],
data: { container: 'body' },
class: "avatar s16 has-tooltip",
title: user.name
)
end
end
context 'with lazy parameter' do
let(:options) { { user: user, lazy: true } }
it 'adds `lazy` class to class list, sets `data-src` with avatar URL and `src` with placeholder image' do
is_expected.to eq tag(
:img,
alt: "#{user.name}'s avatar",
src: LazyImageTagHelper.placeholder_image,
2018-03-27 19:54:05 +05:30
data: { container: 'body', src: avatar_icon_for_user(user, 16) },
2018-03-17 18:26:18 +05:30
class: "avatar s16 has-tooltip lazy",
title: user.name
2017-09-10 17:25:29 +05:30
)
end
end
context 'with has_tooltip parameter' do
context 'with has_tooltip set to true' do
let(:options) { { user: user, has_tooltip: true } }
it 'adds has-tooltip' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_user(user, 16),
2018-03-17 18:26:18 +05:30
data: { container: 'body' },
class: "avatar s16 has-tooltip",
title: user.name
2017-09-10 17:25:29 +05:30
)
end
end
context 'with has_tooltip set to false' do
let(:options) { { user: user, has_tooltip: false } }
it 'does not add has-tooltip or data container' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_user(user, 16),
2018-03-17 18:26:18 +05:30
class: "avatar s16",
title: user.name
2017-09-10 17:25:29 +05:30
)
end
end
end
context 'with user_name parameter' do
let(:options) { { user_name: 'Tinky Winky', user_email: 'no@f.un' } }
context 'with user parameter' do
let(:options) { { user: user, user_name: 'Tinky Winky' } }
it 'prefers user parameter' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{user.name}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_user(user, 16),
2018-03-17 18:26:18 +05:30
data: { container: 'body' },
class: "avatar s16 has-tooltip",
title: user.name
2017-09-10 17:25:29 +05:30
)
end
end
it 'uses user_name and user_email parameter if user is not present' do
2018-03-17 18:26:18 +05:30
is_expected.to eq tag(
:img,
2017-09-10 17:25:29 +05:30
alt: "#{options[:user_name]}'s avatar",
2018-03-27 19:54:05 +05:30
src: avatar_icon_for_email(options[:user_email], 16),
2018-03-17 18:26:18 +05:30
data: { container: 'body' },
class: "avatar s16 has-tooltip",
title: options[:user_name]
2017-09-10 17:25:29 +05:30
)
end
2017-08-17 22:00:37 +05:30
end
end
end