From a3c24add466eeea2cb2ba6c828298f9cee197805 Mon Sep 17 00:00:00 2001 From: Caio SBA Date: Wed, 27 Apr 2016 13:47:33 -0300 Subject: [PATCH] Ticket #262: Fixed link to profile image --- app/controllers/public/profile_controller.rb | 11 +++++++++++ config/routes.rb | 3 +++ test/functional/profile_controller_test.rb | 33 +++++++++++++++++++++++++++++++++ test/integration/routing_test.rb | 7 +++++++ 4 files changed, 54 insertions(+), 0 deletions(-) diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 0cfaeb2..b709b5e 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -395,6 +395,17 @@ class ProfileController < PublicController end end + def icon + size = params[:size] || :portrait + image, mime = profile_icon(profile, size.to_sym, true) + + unless image.match(/^\/\/www\.gravatar\.com/).nil? + redirect_to 'https:' + image + else + @file = File.join(Rails.root, 'public', image) + send_file @file, type: mime, disposition: 'inline' + end + end protected diff --git a/config/routes.rb b/config/routes.rb index 21c706d..076ed0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -86,6 +86,9 @@ Noosfero::Application.routes.draw do # comments match 'profile/:profile/comment/:action/:id', controller: 'comment', profile: /#{Noosfero.identifier_format_in_url}/i, via: :all + + # icon + match 'profile/:profile/icon(/:size)', controller: 'profile', action: 'icon', size: /(big|minor|thumb|portrait|icon)/, profile: /#{Noosfero.identifier_format_in_url}/i, via: :get # public profile information match 'profile/:profile(/:action(/:id))', controller: 'profile', action: 'index', id: /[^\/]*/, profile: /#{Noosfero.identifier_format_in_url}/i, as: :profile, via: :all diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 57da6e0..fee1442 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -1932,4 +1932,37 @@ class ProfileControllerTest < ActionController::TestCase assert_redirected_to :controller => 'account', :action => 'login' end + should 'return portrait icon if size is not provided and there is a profile image' do + img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')) + profile = fast_create(Person, image_id: img.id) + + get :icon, profile: profile.identifier, size: nil + assert_response :success + assert_equal 'image/png', @response.header['Content-Type'] + assert File.exists?(assigns(:file)) + end + + should 'return icon in provided size if there is a profile image' do + img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')) + profile = fast_create(Person, image_id: img.id) + + get :icon, profile: profile.identifier, size: :big + assert_response :success + assert_equal 'image/png', @response.header['Content-Type'] + assert File.exists?(assigns(:file)) + end + + should 'return icon from gravatar without size if there is no profile image' do + profile = fast_create(Person) + + get :icon, profile: profile.identifier + assert_redirected_to /^https:\/\/www\.gravatar\.com\/.*/ + end + + should 'return icon from gravatar with size if there is no profile image' do + profile = fast_create(Person) + + get :icon, profile: profile.identifier, size: :thumb + assert_redirected_to /^https:\/\/www\.gravatar\.com\/.*/ + end end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 9a2cd19..c02e35e 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -276,4 +276,11 @@ class RoutingTest < ActionDispatch::IntegrationTest assert_routing('/profile/~', :controller => 'profile', :profile => '~', :action => 'index') end + should 'have route to profile icon without size' do + assert_routing('/profile/ze/icon', controller: 'profile', profile: 'ze', action: 'icon') + end + + should 'have route to profile icon with supported size' do + assert_routing('/profile/ze/icon/big', controller: 'profile', profile: 'ze', action: 'icon', size: 'big') + end end -- libgit2 0.21.2