[Git][noosfero/noosfero][master] 3 commits: profile: be case insensitive on urls

Bráulio Bhavamitra gitlab at gitlab.com
Mon Aug 10 20:43:44 BRT 2015


Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero


Commits:
a2e3c514 by Braulio Bhavamitra at 2015-08-10T20:05:57Z
profile: be case insensitive on urls

- - - - -
e22aa922 by Braulio Bhavamitra at 2015-08-10T20:43:07Z
Fix mock abused test

- - - - -
088c792e by Braulio Bhavamitra at 2015-08-10T20:43:26Z
Merge branch 'case-insensitive-profile' into noosfero

- - - - -


7 changed files:

- app/controllers/my_profile/enterprise_validation_controller.rb
- app/models/validation_info.rb
- config/routes.rb
- lib/needs_profile.rb
- lib/noosfero/plugin/routes.rb
- test/functional/enterprise_validation_controller_test.rb
- test/integration/routing_test.rb


Changes:

=====================================
app/controllers/my_profile/enterprise_validation_controller.rb
=====================================
--- a/app/controllers/my_profile/enterprise_validation_controller.rb
+++ b/app/controllers/my_profile/enterprise_validation_controller.rb
@@ -1,7 +1,7 @@
 class EnterpriseValidationController < MyProfileController
 
   protect 'validate_enterprise', :profile
-  
+
   def index
     @pending_validations = profile.pending_validations
   end
@@ -27,7 +27,7 @@ class EnterpriseValidationController < MyProfileController
   post_only :reject
   def reject
     @pending = profile.find_pending_validation(params[:id])
-    if @pending 
+    if @pending
       @pending.reject_explanation = params[:reject_explanation]
       begin
         @pending.reject


=====================================
app/models/validation_info.rb
=====================================
--- a/app/models/validation_info.rb
+++ b/app/models/validation_info.rb
@@ -2,9 +2,10 @@ class ValidationInfo < ActiveRecord::Base
 
   attr_accessible :validation_methodology, :restrictions, :organization
 
-  validates_presence_of :validation_methodology
-
   belongs_to :organization
 
+  validates_presence_of :organization
+  validates_presence_of :validation_methodology
+
   xss_terminate :only => [ :validation_methodology, :restrictions ], :on => 'validation'
 end


=====================================
config/routes.rb
=====================================
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -56,37 +56,37 @@ Noosfero::Application.routes.draw do
   match 'search(/:action(/*category_path))', :controller => 'search'
 
   # events
-  match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format_in_url}/
+  match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format_in_url}/i
 
   # catalog
-  match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/, :as => :catalog
+  match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/i, :as => :catalog
 
   # invite
-  match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format_in_url}/
+  match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format_in_url}/i
 
   # feeds per tag
-  match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :tag_feed
+  match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/i, :as => :tag_feed
 
   # profile tags
-  match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format_in_url}/
+  match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format_in_url}/i
 
   # profile search
-  match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/
+  match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/i
 
   # comments
-  match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format_in_url}/
+  match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format_in_url}/i
 
   # public profile information
-  match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :profile
+  match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format_in_url}/i, :as => :profile
 
   # contact
-  match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format_in_url}/
+  match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format_in_url}/i
 
   # map balloon
   match 'map_balloon/:action/:id', :controller => 'map_balloon', :id => /.*/
@@ -98,8 +98,8 @@ Noosfero::Application.routes.draw do
   ## Controllers that are profile-specific (for profile admins )
   ######################################################
   # profile customization - "My profile"
-  match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/
-  match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format_in_url}/, :as => :myprofile
+  match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/i
+  match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format_in_url}/i, :as => :myprofile
 
 
   ######################################################
@@ -127,14 +127,14 @@ Noosfero::Application.routes.draw do
   # cache stuff - hack
   match 'public/:action/:id', :controller => 'public'
 
-  match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new
+  match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format_in_url}/i, :constraints => EnvironmentDomainConstraint.new
   match '*page/versions', :controller => 'content_viewer', :action => 'article_versions'
 
-  match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new
+  match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format_in_url}/i, :constraints => EnvironmentDomainConstraint.new
   match '*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff'
 
   # match requests for profiles that don't have a custom domain
-  match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new
+  match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format_in_url}/i, :constraints => EnvironmentDomainConstraint.new
 
   # match requests for content in domains hosted for profiles
   match '/(*page)', :controller => 'content_viewer', :action => 'view_page'


=====================================
lib/needs_profile.rb
=====================================
--- a/lib/needs_profile.rb
+++ b/lib/needs_profile.rb
@@ -21,7 +21,11 @@ module NeedsProfile
   protected
 
   def load_profile
-    @profile ||= environment.profiles.find_by_identifier(params[:profile])
+    if params[:profile]
+      params[:profile].downcase!
+      @profile ||= environment.profiles.where(identifier: params[:profile]).first
+    end
+
     if @profile
       profile_hostname = @profile.hostname
       if profile_hostname && profile_hostname != request.host


=====================================
lib/noosfero/plugin/routes.rb
=====================================
--- a/lib/noosfero/plugin/routes.rb
+++ b/lib/noosfero/plugin/routes.rb
@@ -19,15 +19,15 @@ Dir.glob(Rails.root.join(plugins_root, '*', 'controllers')) do |controllers_dir|
     controllers.each do |controller|
       controller_name = controller.gsub("#{plugin_name}_plugin_",'')
       if %w[profile myprofile].include?(folder.to_s)
-        match "#{prefixes_by_folder[folder]}/#{plugin_name}/#{controller_name}(/:action(/:id))", :controller => controller, :profile => /#{Noosfero.identifier_format}/
+        match "#{prefixes_by_folder[folder]}/#{plugin_name}/#{controller_name}(/:action(/:id))", controller: controller, profile: /#{Noosfero.identifier_format_in_url}/i
       else
-        match "#{prefixes_by_folder[folder]}/#{plugin_name}/#{controller_name}(/:action(/:id))", :controller => controller
+        match "#{prefixes_by_folder[folder]}/#{plugin_name}/#{controller_name}(/:action(/:id))", controller: controller
       end
     end
   end
 
-  match 'plugin/' + plugin_name + '(/:action(/:id))', :controller => plugin_name + '_plugin'
-  match 'profile/:profile/plugin/' + plugin_name + '(/:action(/:id))', :controller => plugin_name + '_plugin_profile', :profile => /#{Noosfero.identifier_format}/
-  match 'myprofile/:profile/plugin/' + plugin_name + '(/:action(/:id))', :controller => plugin_name + '_plugin_myprofile', :profile => /#{Noosfero.identifier_format}/
-  match 'admin/plugin/' + plugin_name + '(/:action(/:id))', :controller => plugin_name + '_plugin_admin'
+  match 'plugin/' + plugin_name + '(/:action(/:id))', controller: plugin_name + '_plugin'
+  match 'profile/:profile/plugin/' + plugin_name + '(/:action(/:id))', controller: plugin_name + '_plugin_profile', profile: /#{Noosfero.identifier_format_in_url}/i
+  match 'myprofile/:profile/plugin/' + plugin_name + '(/:action(/:id))', controller: plugin_name + '_plugin_myprofile', profile: /#{Noosfero.identifier_format_in_url}/i
+  match 'admin/plugin/' + plugin_name + '(/:action(/:id))', controller: plugin_name + '_plugin_admin'
 end


=====================================
test/functional/enterprise_validation_controller_test.rb
=====================================
--- a/test/functional/enterprise_validation_controller_test.rb
+++ b/test/functional/enterprise_validation_controller_test.rb
@@ -12,126 +12,109 @@ class EnterpriseValidationControllerTest < ActionController::TestCase
     @controller = EnterpriseValidationController.new
     @request    = ActionController::TestRequest.new
     @response   = ActionController::TestResponse.new
-    
+
     login_as 'ze'
-    @org = Organization.create!(:identifier => 'myorg', :name => "My Org")
+    @user = Profile['ze']
+    @org = Organization.create!(identifier: 'myorg', name: "My Org")
     give_permission('ze', 'validate_enterprise', @org)
-    Profile.expects(:find_by_identifier).with('myorg').returns(@org).at_least_once
   end
 
   should 'list pending validations on index' do
-    empty = []
-    @org.expects(:pending_validations).returns(empty)
-    get :index, :profile => 'myorg'
-    assert_equal empty, assigns(:pending_validations)
+    get :index, profile: 'myorg'
+    assert_equal [], assigns(:pending_validations)
     assert_template 'index'
   end
 
   should 'display details and prompt for needed data when approving or rejecting enterprise' do
-    validating = CreateEnterprise.new
-    @org.expects(:find_pending_validation).with('kakakaka').returns(validating)
-
-    get :details, :profile => 'myorg', :id => 'kakakaka'
-    assert_same validating, assigns(:pending)
+    code = 'kakakaka'
+    @org.validations.create! code: code, name: 'test', identifier: 'test', requestor: @user, target: @org
+    get :details, profile: 'myorg', id: code
+    assert_equal @org.find_pending_validation(code), assigns(:pending)
   end
 
   should 'refuse to validate unexisting request' do
-    @org.expects(:find_pending_validation).with('kakakaka').returns(nil)
-    get :details , :profile => 'myorg', :id => 'kakakaka'
+    get :details, profile: 'myorg', id: 'kakakaka'
     assert_response 404
   end
 
   should 'be able to actually validate enterprise on request' do
-    validation = CreateEnterprise.new
-    @org.expects(:find_pending_validation).with('kakakaka').returns(validation)
-    validation.expects(:approve)
-    validation.expects(:code).returns('kakakaka')
-    post :approve, :profile => 'myorg', :id => 'kakakaka'
-    assert_redirected_to :action => 'view_processed', :id => 'kakakaka'
+    code = 'kakakaka'
+    @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
+    post :approve, profile: 'myorg', id: code
+    assert_redirected_to action: 'view_processed', id: code
   end
 
   should 'be able to reject an enterprise' do
-    validation = CreateEnterprise.new
-    @org.expects(:find_pending_validation).with('kakakaka').returns(validation)
-    validation.expects(:reject)
-    validation.expects(:code).returns('kakakaka')
-    post :reject, :profile => 'myorg', :id => 'kakakaka', :reject_explanation => 'this is not a solidarity economy enterprise'
-    assert_redirected_to :action => 'view_processed', :id => 'kakakaka'
+    code = 'kakakaka'
+    @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
+    post :reject, profile: 'myorg', id: code, reject_explanation: 'this is not a solidarity economy enterprise'
+    assert_redirected_to action: 'view_processed', id: code
   end
 
   should 'require the user to fill in the explanation for an rejection' do
-    validation = CreateEnterprise.new
-    validation.stubs(:environment).returns(Environment.default)
-    @org.expects(:find_pending_validation).with('kakakaka').returns(validation)
-
-    # FIXME: this is not working, but should. Anyway the assert_response and
-    # assert_template below in some test some things we need. But the
-    # expectation below must be put to work.
-    #
-    #validation.expects(:reject).raises(ActiveRecord::RecordInvalid)
+    code = 'kakakaka'
+    @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
 
-    post :reject, :profile => 'myorg', :id => 'kakakaka'
+    post :reject, profile: 'myorg', id: code
     assert_response :success
     assert_template 'details'
   end
 
   should 'list validations already processed' do
-    processed_validations = [CreateEnterprise.new]
-    @org.expects(:processed_validations).returns(processed_validations)
-    
-    get :list_processed, :profile => 'myorg'
+    v = @org.validations.create! code: 'kakakaka', name: 'test2', identifier: 'test2', requestor: @user, target: @org
+    v.perform
 
-    assert_equal processed_validations, assigns(:processed_validations)
+    get :list_processed, profile: 'myorg'
+
+    assert_equal @org.processed_validations, assigns(:processed_validations)
 
     assert_response :success
     assert_template 'list_processed'
   end
-  
+
   should 'be able to display a validation that was already processed' do
-    validation = CreateEnterprise.new
-    @org.expects(:find_processed_validation).with('kakakaka').returns(validation)
-    get :view_processed, :profile => 'myorg', :id => 'kakakaka'
-    assert_same validation, assigns(:processed)
+    code = 'kakakaka'
+    v = @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
+    v.perform
+
+    get :view_processed, profile: 'myorg', id: code
+    assert_same @org.processed_validations.first, assigns(:processed)
   end
 
   should 'display a form for editing the validation info' do
-    info = ValidationInfo.new(:validation_methodology => 'none')
-    @org.expects(:validation_info).returns(info)
-    get :edit_validation_info, :profile => 'myorg'
+    info = @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
+    get :edit_validation_info, profile: 'myorg'
     assert_response :success
     assert_equal info, assigns(:info)
   end
 
   should 'save an alteration of the validation info' do
-    info = ValidationInfo.new(:validation_methodology => 'none')
-    @org.expects(:validation_info).returns(info)
-    post :edit_validation_info, :profile => 'myorg', :info => {:validation_methodology => 'new methodology'}
-    
+    info = @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
+    post :edit_validation_info, profile: 'myorg', info: {validation_methodology: 'new methodology'}
+
     assert_response :redirect
-    assert_redirected_to :action => 'index'
-    assert_equal info, assigns(:info)
+    assert_redirected_to action: 'index'
+    info.reload
+    assert_equal info.reload, assigns(:info)
   end
 
   should 'not save an empaty validation mthodology' do
-    info = ValidationInfo.new(:validation_methodology => 'none')
-    @org.expects(:validation_info).returns(info)
-    post :edit_validation_info, :profile => 'myorg', :info => {:validation_methodology => ''}
-    
+    info = @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
+    post :edit_validation_info, profile: 'myorg', info: {validation_methodology: ''}
+
     assert_response :success
     assert_equal info, assigns(:info)
   end
 
   should 'filter html from methodology of the validation info' do
-    info = ValidationInfo.new(:validation_methodology => 'none')
-    @org.expects(:validation_info).returns(info)
-    post :edit_validation_info, :profile => 'myorg', :info => {:validation_methodology => 'new <b>methodology</b>'}
+    @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
+    post :edit_validation_info, profile: 'myorg', info: {validation_methodology: 'new <b>methodology</b>'}
     assert_sanitized assigns(:info).validation_methodology
   end
 
   should 'filter html from restrictions of the validation info' do
-    info = ValidationInfo.new(:validation_methodology => 'none')
-    @org.expects(:validation_info).returns(info)
-    post :edit_validation_info, :profile => 'myorg', :info => {:restrictions => 'new <b>methodology</b>'}
+    @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
+    post :edit_validation_info, profile: 'myorg', info: {restrictions: 'new <b>methodology</b>'}
     assert_sanitized assigns(:info).restrictions
   end
 


=====================================
test/integration/routing_test.rb
=====================================
--- a/test/integration/routing_test.rb
+++ b/test/integration/routing_test.rb
@@ -27,6 +27,10 @@ class RoutingTest < ActionController::IntegrationTest
     assert_routing('/account/new_password/90dfhga7sadgd0as6saas', :controller => 'account', :action => 'new_password', :code => '90dfhga7sadgd0as6saas')
   end
 
+  should 'ignore case for profiles' do
+    assert_routing '/myprofile/ZE/cms', profile: 'ZE', controller: 'cms', action: 'index'
+  end
+
   def test_cms
     assert_routing('/myprofile/ze/cms', :profile => 'ze', :controller => 'cms', :action => 'index')
   end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/918d7d3baa92ed908fd0d490741a093fdee579b6...088c792e86414670f5dad67dad6def42add07acc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150810/fc071f65/attachment-0001.html>


More information about the Noosfero-dev mailing list