Skip to content

Commit

Permalink
[frontend] Move more tests around for icon route drop
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Jul 12, 2018
1 parent eab5062 commit 1144389
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
5 changes: 2 additions & 3 deletions src/api/app/helpers/webui/user_helper.rb
Expand Up @@ -5,8 +5,7 @@ def user_image_tag(user, opt = {})
alt = user.try(:login) if alt.empty?
size = opt[:size] || 20
if user && ::Configuration.gravatar
hash = Digest::MD5.hexdigest(user.email.downcase)
url = "http://www.gravatar.com/avatar/#{hash}?s=#{size}&d=wavatar"
url = "http://www.gravatar.com/avatar/#{user.gravatar_hash}?s=#{size}&d=wavatar"
else
url = 'default_face.png'
end
Expand Down Expand Up @@ -68,7 +67,7 @@ def user_with_realname_and_icon(user, opts = {})
printed_name = "#{realname} (#{user.login})"
end

user_icon(user) + ' ' + link_to(printed_name, user_show_path(user))
user_image_tag(user) + ' ' + link_to(printed_name, user_show_path(user))
end
end

Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/user.rb
Expand Up @@ -838,6 +838,10 @@ def mark_login!
update_attributes(last_logged_in_at: Time.now, login_failure_count: 0)
end

def gravatar_hash
Digest::MD5.hexdigest(email.downcase)
end

private

def password_validation
Expand Down
23 changes: 22 additions & 1 deletion src/api/spec/helpers/webui/user_helper_spec.rb
Expand Up @@ -39,13 +39,15 @@

context 'for users that are not logged in' do
before do
user.email = 'greatguy@nowhere.fi'
user.save
User.current = anonymous_user
end

it 'does not link to user profiles' do
expect(user_and_role(user.login)).to eq(
"<img width=\"20\" height=\"20\" alt=\"#{CGI.escapeHTML(user.realname)}\" " \
"src=\"/user/#{user.login}/icon?size=20\" />#{CGI.escapeHTML(user.realname)} (#{user.login})"
"src=\"http://www.gravatar.com/avatar/803d88429659fa6549ee1a10ccdfbd47?s=20&amp;d=wavatar\" />#{CGI.escapeHTML(user.realname)} (#{user.login})"
)
end
end
Expand All @@ -71,4 +73,23 @@
expect(requester_str(creator.login, nil, 'ana-team')).to include('group', 'ana-team')
end
end

describe '#user_image_tag' do
let(:user) { create(:user, realname: 'Digger', email: 'gordo@example.com') }
context 'with gravatar configuration disabled' do
before do
allow(Configuration).to receive(:gravatar).and_return(false)
end

it 'returns default face' do
expect(user_image_tag(user)).to eq('<img width="20" height="20" alt="Digger" src="/images/default_face.png" />')
end
end

context 'with gravatar configuration enabled' do
it 'returns gravatar url' do
expect(user_image_tag(user)).to eq('<img width="20" height="20" alt="Digger" src="http://www.gravatar.com/avatar/66ada5090a2f94d4cfec83801081f3a2?s=20&amp;d=wavatar" />')
end
end
end
end
26 changes: 0 additions & 26 deletions src/api/spec/models/user_spec.rb
Expand Up @@ -551,30 +551,4 @@
expect(user.can_create_project?('foo:bar')).to be true
end
end

describe '#gravatar_image' do
context 'gravatar configuration disabled' do
before do
allow(Configuration).to receive(:gravatar).and_return(false)
end

it { expect(user.gravatar_image(0)).to eq(:none) }
end

context 'gravatar configuration enabled' do
context 'problems loading the image' do
before do
allow(ActiveXML.backend).to receive(:load_external_url).and_raise(ActiveXML::Transport::Error)
end

it { expect(user.gravatar_image(0)).to eq(:none) }
end

before do
allow(ActiveXML.backend).to receive(:load_external_url).with(anything).and_return('some_content')
end

it { expect(user.gravatar_image(0)).to eq('some_content') }
end
end
end

0 comments on commit 1144389

Please sign in to comment.