Commit 2c1493ff8c707e121e6512973e2973ba4aeaa53d

Authored by MoisesMachado
1 parent d25daf44

ActionItem114: fixed some tests

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@818 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
@@ -34,13 +34,17 @@ class ApplicationController < ActionController::Base @@ -34,13 +34,17 @@ class ApplicationController < ActionController::Base
34 34
35 def self.needs_profile 35 def self.needs_profile
36 before_filter :load_profile 36 before_filter :load_profile
37 - design :holder => 'profile' 37 + design :holder => 'profile'
38 end 38 end
39 39
40 def load_profile 40 def load_profile
41 - @profile = Profile.find_by_identifier(params[:profile])  
42 - @profile ||= Profile.find(:first) #FIXME This is not correct it was made to the system don't crash  
43 - raise "The profile must be loaded %s" % params[:profile].to_s if @profile.nil? 41 + @profile = Profile.find_by_identifier(params[:profile])
  42 + render_not_found(request.path) unless @profile
  43 + end
  44 +
  45 + def render_not_found(path)
  46 + @path = path
  47 + render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'not_found.rhtml'), :layout => 'not_found', :status => 404
44 end 48 end
45 49
46 def self.acts_as_environment_admin_controller 50 def self.acts_as_environment_admin_controller
app/controllers/profile_admin/profile_editor_controller.rb
@@ -5,7 +5,7 @@ class ProfileEditorController < ProfileAdminController @@ -5,7 +5,7 @@ class ProfileEditorController < ProfileAdminController
5 5
6 design_editor :holder => 'profile', :autosave => true, :block_types => :block_types 6 design_editor :holder => 'profile', :autosave => true, :block_types => :block_types
7 7
8 - protect [:edit], 'edit_profile', :profile 8 + protect [:index, :edit], 'edit_profile', :profile
9 9
10 def block_types 10 def block_types
11 { 11 {
app/controllers/profile_admin_controller.rb
@@ -13,7 +13,7 @@ class ProfileAdminController < ApplicationController @@ -13,7 +13,7 @@ class ProfileAdminController < ApplicationController
13 before_filter do |controller| 13 before_filter do |controller|
14 unless controller.profile.kind_of?(some_class) 14 unless controller.profile.kind_of?(some_class)
15 controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.profile.name) 15 controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.profile.name)
16 - controller.render :file => 'app/views/shared/access_denied.rhtml' , :layout => true, :status => 403 16 + controller.render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'access_denied.rhtml'), :layout => true, :status => 403
17 end 17 end
18 end 18 end
19 end 19 end
app/controllers/public/content_viewer_controller.rb
@@ -8,7 +8,7 @@ class ContentViewerController < PublicController @@ -8,7 +8,7 @@ class ContentViewerController < PublicController
8 @path = path.join('/') 8 @path = path.join('/')
9 @page = Article.find_by_path(@path) 9 @page = Article.find_by_path(@path)
10 if @page.nil? 10 if @page.nil?
11 - render :action => 'not_found', :status => 404 11 + render_not_found(@path)
12 end 12 end
13 end 13 end
14 14
app/helpers/application_helper.rb
@@ -139,7 +139,7 @@ module ApplicationHelper @@ -139,7 +139,7 @@ module ApplicationHelper
139 [ accessibility_link, 139 [ accessibility_link,
140 ( link_to content_tag('span', _('Login')), :controller => 'account', :action => 'login'), 140 ( link_to content_tag('span', _('Login')), :controller => 'account', :action => 'login'),
141 ] 141 ]
142 - end 142 + end.join(" ")
143 end 143 end
144 144
145 def header 145 def header
app/models/person.rb
@@ -8,12 +8,6 @@ class Person < Profile @@ -8,12 +8,6 @@ class Person < Profile
8 # has_many :people, :through => :person_friendships, :foreign_key => 'friend_id' 8 # has_many :people, :through => :person_friendships, :foreign_key => 'friend_id'
9 9
10 has_one :person_info 10 has_one :person_info
11 -# has_many :role_assignments, :as => :accessor, :class_name => 'RoleAssignment'  
12 -  
13 -# def has_permission?(perm, res=nil)  
14 -# return true if res == self && PERMISSIONS[:profile].keys.include?(perm)  
15 -# role_assignments.any? {|ra| ra.has_permission?(perm, res)}  
16 -# end  
17 11
18 def self.conditions_for_profiles(conditions, person) 12 def self.conditions_for_profiles(conditions, person)
19 new_conditions = sanitize_sql(['role_assignments.accessor_id = ?', person]) 13 new_conditions = sanitize_sql(['role_assignments.accessor_id = ?', person])
app/models/profile.rb
@@ -43,6 +43,7 @@ class Profile < ActiveRecord::Base @@ -43,6 +43,7 @@ class Profile < ActiveRecord::Base
43 community 43 community
44 test 44 test
45 search 45 search
  46 + not_found
46 ] 47 ]
47 48
48 acts_as_taggable 49 acts_as_taggable
@@ -132,15 +133,4 @@ class Profile < ActiveRecord::Base @@ -132,15 +133,4 @@ class Profile < ActiveRecord::Base
132 def superior_instance 133 def superior_instance
133 environment 134 environment
134 end 135 end
135 -  
136 -# def affiliate(person, roles)  
137 -# roles = [roles] unless roles.kind_of?(Array)  
138 -# roles.map do |role|  
139 -# unless RoleAssignment.find(:first, :conditions => {:person_id => person, :role_id => role, :resource_id => self, :resource_type => self.class.base_class.name})  
140 -# RoleAssignment.new(:person => person, :role => role, :resource => self).save  
141 -# else  
142 -# false  
143 -# end  
144 -# end.any?  
145 -# end  
146 end 136 end
app/views/layouts/not_found.rhtml 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +<html>
  2 + <head>
  3 + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4 + <title>
  5 + <%= _('Page not found') %>
  6 + </title>
  7 + </head>
  8 + <body>
  9 + <%= yield %>
  10 + </body>
  11 +</html>
lib/authenticated_test_helper.rb
1 module AuthenticatedTestHelper 1 module AuthenticatedTestHelper
2 # Sets the current user in the session from the user fixtures. 2 # Sets the current user in the session from the user fixtures.
3 def login_as(user) 3 def login_as(user)
4 - @request.session[:user] = user ? users(user).id : nil 4 + @request.session[:user] = User.find_by_login(user.to_s)
5 end 5 end
6 6
7 def content_type(type) 7 def content_type(type)
test/functional/categories_controller_test.rb
@@ -15,13 +15,17 @@ class CategoriesControllerTest &lt; Test::Unit::TestCase @@ -15,13 +15,17 @@ class CategoriesControllerTest &lt; Test::Unit::TestCase
15 Environment.stubs(:default).returns(env) 15 Environment.stubs(:default).returns(env)
16 assert (@cat1 = env.categories.create(:name => 'a test category')) 16 assert (@cat1 = env.categories.create(:name => 'a test category'))
17 assert (@cat1 = env.categories.create(:name => 'another category')) 17 assert (@cat1 = env.categories.create(:name => 'another category'))
18 - login_as(:ze) 18 + login_as(create_admin_user(@env))
19 end 19 end
20 all_fixtures 20 all_fixtures
21 attr_reader :env, :cat1, :cat2 21 attr_reader :env, :cat1, :cat2
22 22
23 def test_index 23 def test_index
  24 + assert user = login_as(create_admin_user(Environment.default))
  25 + assert user.person.has_permission?('manage_environment_categories',Environment.default ), "#{user.login} don't have permission to manage_environment_categories in #{Environment.default.name}"
24 get :index 26 get :index
  27 + assert_response :success
  28 + assert_template 'index'
25 assert_kind_of Array, assigns(:categories) 29 assert_kind_of Array, assigns(:categories)
26 assert_tag :tag => 'a', :attributes => { :href => '/admin/categories/new'} 30 assert_tag :tag => 'a', :attributes => { :href => '/admin/categories/new'}
27 end 31 end
test/functional/content_viewer_controller_test.rb
@@ -28,16 +28,21 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -28,16 +28,21 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
28 def test_should_get_not_found_error_for_unexisting_page 28 def test_should_get_not_found_error_for_unexisting_page
29 uses_host 'anhetegua.net' 29 uses_host 'anhetegua.net'
30 get :view_page, :profile => 'aprofile', :page => ['some_unexisting_page'] 30 get :view_page, :profile => 'aprofile', :page => ['some_unexisting_page']
31 - assert_response :redirect  
32 - assert_redirected_to :controller => 'search', :action => 'index' 31 + assert_response :missing
  32 + # This is an idea of instead of give an error search for the term
  33 +# assert_response :redirect
  34 +# assert_redirected_to :controller => 'search', :action => 'index'
33 end 35 end
34 36
35 def test_should_get_not_found_error_for_unexisting_profile 37 def test_should_get_not_found_error_for_unexisting_profile
36 Profile.delete_all 38 Profile.delete_all
37 uses_host 'anhetegua' 39 uses_host 'anhetegua'
38 get :view_page, :profile => 'some_unexisting_profile', :page => [] 40 get :view_page, :profile => 'some_unexisting_profile', :page => []
39 - assert_response :redirect  
40 - assert_redirected_to :controller => 'search', :action => 'index' 41 + assert_response :missing
  42 +
  43 + # This is an idea of instead of give an error search for the term
  44 +# assert_response :redirect
  45 +# assert_redirected_to :controller => 'search', :action => 'index'
41 end 46 end
42 47
43 end 48 end
test/functional/features_controller_test.rb
@@ -6,14 +6,14 @@ class FeaturesController; def rescue_action(e) raise e end; end @@ -6,14 +6,14 @@ class FeaturesController; def rescue_action(e) raise e end; end
6 6
7 class FeaturesControllerTest < Test::Unit::TestCase 7 class FeaturesControllerTest < Test::Unit::TestCase
8 8
9 - fixtures :environments, :domains  
10 - 9 + all_fixtures
11 def setup 10 def setup
12 @controller = FeaturesController.new 11 @controller = FeaturesController.new
13 @request = ActionController::TestRequest.new 12 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new 13 @response = ActionController::TestResponse.new
  14 + login_as(create_admin_user(Environment.find(2)))
15 end 15 end
16 - 16 +
17 def test_listing_features 17 def test_listing_features
18 uses_host 'anhetegua.net' 18 uses_host 'anhetegua.net'
19 get :index 19 get :index
test/functional/membership_editor_controller_test.rb
@@ -21,8 +21,6 @@ class MembershipEditorControllerTest &lt; Test::Unit::TestCase @@ -21,8 +21,6 @@ class MembershipEditorControllerTest &lt; Test::Unit::TestCase
21 21
22 should 'prompt for new enterprise data' do 22 should 'prompt for new enterprise data' do
23 get :new_enterprise, :profile => 'ze' 23 get :new_enterprise, :profile => 'ze'
24 - assert assigns(:virtual_communities)  
25 - assert_kind_of Array, assigns(:virtual_communities)  
26 assert assigns(:validation_entities) 24 assert assigns(:validation_entities)
27 assert_kind_of Array, assigns(:validation_entities) 25 assert_kind_of Array, assigns(:validation_entities)
28 end 26 end
test/test_helper.rb
@@ -64,6 +64,13 @@ class Test::Unit::TestCase @@ -64,6 +64,13 @@ class Test::Unit::TestCase
64 raise "profile_identifier must be set!" unless extra_parameters[:profile] 64 raise "profile_identifier must be set!" unless extra_parameters[:profile]
65 end 65 end
66 66
  67 + def create_admin_user(env)
  68 + admin_user = User.find_by_login('root') || User.create!(:login => 'root', :email => 'root@noosfero.org', :password => 'root', :password_confirmation => 'root')
  69 + admin_role = Role.find_by_name('admin_role') || Role.create!(:name => 'admin_role', :permissions => ['view_environment_admin_panel','edit_environment_features', 'edit_environment_design', 'manage_environment_categories', 'manage_environment_roles', 'manage_environment_validators'])
  70 + RoleAssignment.create!(:accessor => admin_user.person, :role => admin_role, :resource => env) unless admin_user.person.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([admin_role, admin_user, env])
  71 + admin_user.login
  72 + end
  73 +
67 private 74 private
68 75
69 def uses_host(name) 76 def uses_host(name)