kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
6.8 KiB
209 lines
6.8 KiB
7 years ago
|
require 'spec_helper'
|
||
|
|
||
|
describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
|
||
7 years ago
|
let(:user) { FactoryBot.build_stubbed(:user) }
|
||
7 years ago
|
let(:mail_digest) { Digest::MD5.hexdigest(user.mail) }
|
||
7 years ago
|
let(:avatar_stub) { FactoryBot.build_stubbed(:avatar_attachment) }
|
||
7 years ago
|
|
||
|
let(:enable_gravatars) { false }
|
||
|
let(:gravatar_default) { '' }
|
||
|
let(:enable_local_avatars) { false }
|
||
|
let(:plugin_settings) do
|
||
|
{
|
||
|
'enable_gravatars' => enable_gravatars,
|
||
|
'enable_local_avatars' => enable_local_avatars,
|
||
|
'gravatar_default' => gravatar_default
|
||
|
}
|
||
|
end
|
||
|
|
||
|
before do
|
||
|
allow(Setting)
|
||
|
.to receive(:plugin_openproject_avatars)
|
||
|
.and_return(plugin_settings)
|
||
|
|
||
|
allow(user).to receive(:local_avatar_attachment).and_return avatar_stub
|
||
|
end
|
||
|
|
||
|
def local_expected_image_tag(user)
|
||
|
tag_options = { title: user.name,
|
||
|
alt: 'Avatar',
|
||
|
class: 'avatar' }
|
||
|
|
||
|
image_tag local_expected_url(user), tag_options
|
||
|
end
|
||
|
|
||
|
def local_expected_url(user)
|
||
|
user_avatar_url(user)
|
||
|
end
|
||
|
|
||
|
def gravatar_expected_image_tag(digest, options = {})
|
||
|
tag_options = options.reverse_merge(title: user.name,
|
||
|
alt: 'Gravatar',
|
||
6 years ago
|
class: 'avatar avatar--gravatar-image avatar--fallback').delete_if { |key, value| value.nil? || key == :ssl }
|
||
7 years ago
|
|
||
|
image_tag gravatar_expected_url(digest, options), tag_options
|
||
|
end
|
||
|
|
||
|
def gravatar_expected_url(digest, options = {})
|
||
|
ssl = !!options[:ssl]
|
||
|
|
||
|
host =
|
||
|
if ssl
|
||
|
'https://secure.gravatar.com'
|
||
|
else
|
||
|
'http://gravatar.com'
|
||
|
end
|
||
|
|
||
|
"#{host}/avatar/#{digest}?secure=#{ssl}"
|
||
|
end
|
||
|
|
||
|
describe 'gravatar and local' do
|
||
|
context 'when enabled' do
|
||
|
let(:enable_gravatars) { true }
|
||
|
let(:enable_local_avatars) { true }
|
||
|
it "should return the image attached to the user" do
|
||
|
expect(helper.avatar(user)).to be_html_eql(local_expected_image_tag(user))
|
||
|
end
|
||
|
|
||
|
it "should return the gravatar image if no image uploaded for the user" do
|
||
|
allow(user).to receive(:local_avatar_attachment).and_return nil
|
||
|
|
||
|
expect(helper.avatar(user)).to be_html_eql(gravatar_expected_image_tag(mail_digest))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'when gravatar disabled' do
|
||
|
let(:enable_gravatars) { false }
|
||
|
let(:enable_local_avatars) { true }
|
||
|
it "should return blank if image attached to the user but gravatars disabled" do
|
||
|
expect(helper.avatar(user)).to be_html_eql(local_expected_image_tag(user))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'when all disabled' do
|
||
|
let(:enable_gravatars) { false }
|
||
|
let(:enable_local_avatars) { false }
|
||
|
|
||
|
it "should return blank" do
|
||
|
expect(helper.avatar(user)).to eq ''
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe '#avatar_url' do
|
||
|
context 'when enabled' do
|
||
|
let(:enable_gravatars) { true }
|
||
|
let(:enable_local_avatars) { true }
|
||
|
it "should return the url to the image attached to the user" do
|
||
|
expect(helper.avatar_url(user)).to eq(local_expected_url(user))
|
||
|
end
|
||
|
|
||
|
it "should return the gravatar url if no image uploaded for the user" do
|
||
|
allow(user).to receive(:local_avatar_attachment).and_return nil
|
||
|
|
||
|
expect(helper.avatar_url(user)).to eq(gravatar_expected_url(mail_digest))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'when gravatar disabled' do
|
||
|
let(:enable_gravatars) { false }
|
||
|
let(:enable_local_avatars) { true }
|
||
|
it "should return the url if image attached to the user but gravatars disabled" do
|
||
|
expect(helper.avatar_url(user)).to eq(local_expected_url(user))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'when all disabled' do
|
||
|
let(:enable_gravatars) { false }
|
||
|
let(:enable_local_avatars) { false }
|
||
|
|
||
|
it "should return blank" do
|
||
|
expect(helper.avatar_url(user)).to eq ''
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe 'gravatar' do
|
||
|
context 'when enabled' do
|
||
|
let(:enable_gravatars) { true }
|
||
|
let(:enable_local_avatars) { false }
|
||
|
describe 'ssl dependent on protocol settings' do
|
||
|
context 'with https protocol', with_settings: { protocol: 'https' } do
|
||
|
it "should be set to secure if protocol is 'https'" do
|
||
|
expect(helper.default_gravatar_options[:secure]).to be true
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'with http protocol', with_settings: { protocol: 'http' } do
|
||
|
it "should be set to unsecure if protocol is 'http'" do
|
||
|
expect(helper.default_gravatar_options[:secure]).to be false
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'with http', with_settings: { protocol: 'http' } do
|
||
|
it 'should return a gravatar image tag if a user is provided' do
|
||
|
expect(helper.avatar(user)).to be_html_eql(gravatar_expected_image_tag(mail_digest))
|
||
|
end
|
||
|
|
||
|
it 'should return a gravatar url if a user is provided' do
|
||
|
expect(helper.avatar_url(user)).to eq(gravatar_expected_url(mail_digest))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'with https', with_settings: { protocol: 'https' } do
|
||
|
it 'should return a gravatar image tag with ssl if the request was ssl required' do
|
||
|
expect(helper.avatar(user)).to be_html_eql(gravatar_expected_image_tag(mail_digest, ssl: true))
|
||
|
end
|
||
|
|
||
|
it 'should return a gravatar image tag with ssl if the request was ssl required' do
|
||
|
expect(helper.avatar_url(user)).to eq(gravatar_expected_url(mail_digest, ssl: true))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
it 'should return an empty string if a non parsable (e-mail) string is provided' do
|
||
|
expect(helper.avatar('just the name')).to eq('')
|
||
|
end
|
||
|
|
||
|
it 'should return an empty string if nil is provided' do
|
||
|
expect(helper.avatar(nil)).to eq('')
|
||
|
end
|
||
|
|
||
|
it 'should return a gravatar image tag if a parsable e-mail string is provided' do
|
||
|
mail = '<e-mail@mail.de>'
|
||
|
digest = Digest::MD5.hexdigest('e-mail@mail.de')
|
||
|
|
||
|
expect(helper.avatar(mail)).to be_html_eql(gravatar_expected_image_tag(digest, title: nil))
|
||
|
end
|
||
|
|
||
|
it 'should return a gravatar image tag if a parsable e-mail string is provided' do
|
||
|
mail = '<e-mail@mail.de>'
|
||
|
digest = Digest::MD5.hexdigest('e-mail@mail.de')
|
||
|
|
||
|
expect(helper.avatar_url(mail)).to eq(gravatar_expected_url(digest))
|
||
|
end
|
||
|
|
||
|
it 'should return an empty string if a non parsable (e-mail) string is provided' do
|
||
|
expect(helper.avatar_url('just the name')).to eq('')
|
||
|
end
|
||
|
|
||
|
it 'should return an empty string if nil is provided' do
|
||
|
expect(helper.avatar_url(nil)).to eq('')
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context 'when all disabled' do
|
||
|
let(:enable_gravatars) { false }
|
||
|
let(:enable_local_avatars) { false }
|
||
|
|
||
|
it 'should return an empty string if gravatar is disabled' do
|
||
|
expect(helper.avatar(user)).to eq('')
|
||
|
end
|
||
|
|
||
|
it 'should return an empty string if gravatar is disabled' do
|
||
|
expect(helper.avatar_url(user)).to eq('')
|
||
|
end
|
||
|
end
|
||
|
end
|