Commit 218cf0a55a36f477d65d498202d1d60efa39ae5f

Authored by Rafael Reggiani Manzo
Committed by Joenio Costa
1 parent 01fecce0

Set feed configurations as Environment attributes

As ENV vars are not the settings implementation choice through the,
project, this is should turn this aspect of the feed fetching proxy
functionality compliant with Noosfero's design choices.
app/models/environment.rb
... ... @@ -13,7 +13,9 @@ class Environment < ActiveRecord::Base
13 13 :reports_lower_bound, :noreply_email,
14 14 :signup_welcome_screen_body, :members_whitelist_enabled,
15 15 :members_whitelist, :highlighted_news_amount,
16   - :portal_news_amount, :date_format, :signup_intro
  16 + :portal_news_amount, :date_format, :signup_intro,
  17 + :enable_feed_proxy, :http_feed_proxy, :https_feed_proxy,
  18 + :disable_feed_ssl
17 19  
18 20 has_many :users
19 21  
... ...
app/views/features/index.html.erb
... ... @@ -49,6 +49,25 @@ Check all the features you want to enable for your environment, uncheck all the
49 49 </div>
50 50 <hr/>
51 51  
  52 +<h3><%= _('Feed') %></h3>
  53 + <div class="option">
  54 + <%= check_box :environment, :enable_feed_proxy %>
  55 + <label><%= _('Enable feed proxy') %></label>
  56 + </div>
  57 + <div class="input">
  58 + <div class="info"><%= _('HTTP feed proxy address:') %></div>
  59 + <%= text_field :environment, :http_feed_proxy %>
  60 + </div>
  61 + <div class="input">
  62 + <div class="info"><%= _('HTTPS feed proxy address:') %></div>
  63 + <%= text_field :environment, :https_feed_proxy %>
  64 + </div>
  65 + <div class="option">
  66 + <%= check_box :environment, :disable_feed_ssl %>
  67 + <label><%= _('Disable feed SSL') %></label>
  68 + </div>
  69 +<hr/>
  70 +
52 71 <div>
53 72 <% button_bar do %>
54 73 <%= submit_button('save', _('Save changes')) %>
... ...
db/migrate/20160408011124_add_enable_feed_proxy_to_environments.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class AddEnableFeedProxyToEnvironments < ActiveRecord::Migration
  2 + def change
  3 + add_column :environments, :enable_feed_proxy, :boolean, default: false
  4 + end
  5 +end
... ...
db/migrate/20160408011622_add_http_feed_proxy_to_environments.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class AddHttpFeedProxyToEnvironments < ActiveRecord::Migration
  2 + def change
  3 + add_column :environments, :http_feed_proxy, :string
  4 + end
  5 +end
... ...
db/migrate/20160408011635_add_https_feed_proxy_to_environments.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class AddHttpsFeedProxyToEnvironments < ActiveRecord::Migration
  2 + def change
  3 + add_column :environments, :https_feed_proxy, :string
  4 + end
  5 +end
... ...
db/migrate/20160408011720_add_disable_feed_ssl_to_environments.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class AddDisableFeedSslToEnvironments < ActiveRecord::Migration
  2 + def change
  3 + add_column :environments, :disable_feed_ssl, :boolean, default: false
  4 + end
  5 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended that you check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(version: 20160324132518) do
  14 +ActiveRecord::Schema.define(version: 20160408011720) do
15 15  
16 16 # These are extensions that must be enabled in order to support this database
17 17 enable_extension "plpgsql"
... ... @@ -390,6 +390,10 @@ ActiveRecord::Schema.define(version: 20160324132518) do
390 390 t.string "noreply_email"
391 391 t.string "redirection_after_signup", default: "keep_on_same_page"
392 392 t.string "date_format", default: "month_name_with_year"
  393 + t.boolean "enable_feed_proxy", default: false
  394 + t.string "http_feed_proxy"
  395 + t.string "https_feed_proxy"
  396 + t.boolean "disable_feed_ssl", default: false
393 397 end
394 398  
395 399 create_table "external_feeds", force: :cascade do |t|
... ...
lib/feed_handler.rb
... ... @@ -42,13 +42,21 @@ class FeedHandler
42 42 if !valid_url?(address)
43 43 raise InvalidUrl.new("\"%s\" is not a valid URL" % address)
44 44 end
  45 +
45 46 header = {"User-Agent" => "Noosfero/#{Noosfero::VERSION}"}
46   - if address.starts_with?("https://")
47   - header.merge!(:proxy => ENV['FEED_HTTPS_PROXY']) if ENV['FEED_HTTPS_PROXY']
48   - else
49   - header.merge!(:proxy => ENV['FEED_HTTP_PROXY']) if ENV['FEED_HTTP_PROXY']
  47 +
  48 + environment = Environment.default
  49 +
  50 + if environment.enable_feed_proxy
  51 + if address.starts_with?("https://")
  52 + header.merge!(:proxy => environment.https_feed_proxy) if environment.https_feed_proxy
  53 + else
  54 + header.merge!(:proxy => environment.http_feed_proxy) if environment.http_feed_proxy
  55 + end
50 56 end
51   - header.merge!(:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) if ENV['SSL_VERIFY_NONE']
  57 +
  58 + header.merge!(:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) if environment.disable_feed_ssl
  59 +
52 60 open(address, header, &block)
53 61 end
54 62 return content
... ...
test/unit/environment_test.rb
... ... @@ -1723,4 +1723,42 @@ class EnvironmentTest &lt; ActiveSupport::TestCase
1723 1723 refute environment.errors[:date_format.to_s].present?
1724 1724 end
1725 1725  
  1726 + should 'respond to enable_feed_proxy' do
  1727 + assert_respond_to Environment.new, :enable_feed_proxy
  1728 + end
  1729 +
  1730 + should 'set enable_feed_proxy on environment' do
  1731 + e = fast_create(Environment, :name => 'Enterprise test')
  1732 + e.enable_feed_proxy = true
  1733 + e.save
  1734 + assert_equal true, e.enable_feed_proxy
  1735 + end
  1736 +
  1737 + should 'not enable feed proxy when enable by default' do
  1738 + assert_equal false, Environment.new.enable_feed_proxy
  1739 + end
  1740 +
  1741 + should 'respond to disable_feed_ssl' do
  1742 + assert_respond_to Environment.new, :disable_feed_ssl
  1743 + end
  1744 +
  1745 + should 'set disable_feed_ssl on environment' do
  1746 + e = fast_create(Environment, :name => 'Enterprise test')
  1747 + e.disable_feed_ssl = true
  1748 + e.save
  1749 + assert_equal true, e.disable_feed_ssl
  1750 + end
  1751 +
  1752 + should 'not disable feed ssl when enable by default' do
  1753 + assert_equal false, Environment.new.disable_feed_ssl
  1754 + end
  1755 +
  1756 + should 'respond to http_feed_proxy' do
  1757 + assert_respond_to Environment.new, :http_feed_proxy
  1758 + end
  1759 +
  1760 + should 'respond to https_feed_proxy' do
  1761 + assert_respond_to Environment.new, :https_feed_proxy
  1762 + end
  1763 +
1726 1764 end
... ...
test/unit/feed_handler_test.rb
... ... @@ -5,14 +5,13 @@ class FeedHandlerTest &lt; ActiveSupport::TestCase
5 5 def setup
6 6 @handler = FeedHandler.new
7 7 @container = nil
8   - ENV.stubs('[]').with(anything)
9 8 end
10 9 attr_reader :handler
11 10 def container
12 11 @container ||= create(:feed_reader_block)
13 12 end
14 13  
15   - should 'fetch feed content' do
  14 + should 'fetch feed content with proxy disabled and SSL enabled' do
16 15 content = handler.fetch(container.address)
17 16 assert_match /<description>Feed content<\/description>/, content
18 17 assert_match /<title>Feed for unit tests<\/title>/, content
... ... @@ -84,7 +83,11 @@ class FeedHandlerTest &lt; ActiveSupport::TestCase
84 83 end
85 84  
86 85 should 'identifies itself as noosfero user agent' do
  86 + Environment.any_instance.expects(:enable_feed_proxy).returns(false)
  87 + Environment.any_instance.expects(:disable_feed_ssl).returns(false)
  88 +
87 89 handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}"}, anything).returns('bli content')
  90 +
88 91 assert_equal 'bli content', handler.fetch('http://site.org/feed.xml')
89 92 end
90 93  
... ... @@ -150,35 +153,52 @@ class FeedHandlerTest &lt; ActiveSupport::TestCase
150 153 end
151 154 end
152 155  
153   - should 'set proxy when FEED_HTTP_PROXY is setted from env' do
154   - ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128')
  156 + should 'set proxy when http_feed_proxy is setted from env' do
  157 + Environment.any_instance.expects(:enable_feed_proxy).returns(true)
  158 + Environment.any_instance.expects(:disable_feed_ssl).returns(false)
  159 + Environment.any_instance.expects(:http_feed_proxy).twice.returns('http://127.0.0.1:3128')
  160 +
155 161 handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.1:3128'}, anything).returns('bli content')
  162 +
156 163 assert_equal 'bli content', handler.fetch('http://site.org/feed.xml')
157 164 end
158 165  
159   - should 'set proxy when FEED_HTTPS_PROXY is setted from env' do
160   - ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.1:3128')
  166 + should 'set proxy when https_feed_proxy is setted from env' do
  167 + Environment.any_instance.expects(:enable_feed_proxy).returns(true)
  168 + Environment.any_instance.expects(:disable_feed_ssl).returns(false)
  169 + Environment.any_instance.expects(:https_feed_proxy).twice.returns('http://127.0.0.1:3128')
  170 +
161 171 handler.expects(:open).with('https://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.1:3128'}, anything).returns('bli content')
  172 +
162 173 assert_equal 'bli content', handler.fetch('https://site.org/feed.xml')
163 174 end
164 175  
165 176 should 'use https proxy for https address when both env variables were defined' do
166   - ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.2:3128')
167   - ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128')
  177 + Environment.any_instance.expects(:enable_feed_proxy).returns(true)
  178 + Environment.any_instance.expects(:disable_feed_ssl).returns(false)
  179 + Environment.any_instance.expects(:https_feed_proxy).twice.returns('http://127.0.0.2:3128')
  180 +
168 181 handler.expects(:open).with('https://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.2:3128'}, anything).returns('bli content')
  182 +
169 183 assert_equal 'bli content', handler.fetch('https://site.org/feed.xml')
170 184 end
171 185  
172 186 should 'use http proxy for http address when both env variables were defined' do
173   - ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.2:3128')
174   - ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128')
  187 + Environment.any_instance.expects(:enable_feed_proxy).returns(true)
  188 + Environment.any_instance.expects(:disable_feed_ssl).returns(false)
  189 + Environment.any_instance.expects(:http_feed_proxy).twice.returns('http://127.0.0.1:3128')
  190 +
175 191 handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.1:3128'}, anything).returns('bli content')
  192 +
176 193 assert_equal 'bli content', handler.fetch('http://site.org/feed.xml')
177 194 end
178 195  
179 196 should 'not verify ssl when define env parameter SSL_VERIFY_NONE' do
180   - ENV.stubs('[]').with('SSL_VERIFY_NONE').returns(true)
  197 + Environment.any_instance.expects(:enable_feed_proxy).returns(false)
  198 + Environment.any_instance.expects(:disable_feed_ssl).returns(true)
  199 +
181 200 handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE}, anything)
  201 +
182 202 handler.fetch('http://site.org/feed.xml')
183 203 end
184 204  
... ...