Commit 242318b0008c594d5fac0a97f20844c1aee8dffe

Authored by Leandro Santos
1 parent 4f321345

Avoid template definition of another environment

app/controllers/admin/templates_controller.rb
... ... @@ -41,17 +41,44 @@ class TemplatesController < AdminController
41 41 end
42 42  
43 43 def set_community_as_default
44   - set_as_default(Community.find(params[:template_id]))
  44 + begin
  45 + community = environment.communities.find(params[:template_id])
  46 + rescue ActiveRecord::RecordNotFound
  47 + message = _('Community not found. The template could no be changed.')
  48 + community = nil
  49 + end
  50 +
  51 + message = _('%s defined as default') % community.name if set_as_default(community)
  52 + session[:notice] = message
  53 +
45 54 redirect_to :action => 'index'
46 55 end
47 56  
48 57 def set_person_as_default
49   - set_as_default(Person.find(params[:template_id]))
  58 + begin
  59 + person = environment.people.find(params[:template_id])
  60 + rescue ActiveRecord::RecordNotFound
  61 + message = _('Person not found. The template could no be changed.')
  62 + person = nil
  63 + end
  64 +
  65 + message = _('%s defined as default') % person.name if set_as_default(person)
  66 + session[:notice] = message
  67 +
50 68 redirect_to :action => 'index'
51 69 end
52 70  
53 71 def set_enterprise_as_default
54   - set_as_default(Enterprise.find(params[:template_id]))
  72 + begin
  73 + enterprise = environment.enterprises.find(params[:template_id])
  74 + rescue ActiveRecord::RecordNotFound
  75 + message = _('Enterprise not found. The template could no be changed.')
  76 + enterprise = nil
  77 + end
  78 +
  79 + message = _('%s defined as default') % enterprise.name if set_as_default(enterprise)
  80 + session[:notice] = message
  81 +
55 82 redirect_to :action => 'index'
56 83 end
57 84  
... ...
test/functional/templates_controller_test.rb
... ... @@ -71,5 +71,91 @@ class TemplatesControllerTest < ActionController::TestCase
71 71 assert_equal e3, environment.enterprise_default_template
72 72 end
73 73  
  74 + should 'not allow set_community_as_default define a community template of another environment as default' do
  75 + c1= fast_create(Community, :is_template => true, :environment_id => environment.id)
  76 + environment.community_default_template= c1
  77 + environment.save
  78 +
  79 + env2 = fast_create(Environment)
  80 +
  81 + c3 = fast_create(Community, :is_template => true, :environment_id => env2.id)
  82 +
  83 + post :set_community_as_default, :template_id => c3.id
  84 + environment.reload
  85 + assert_not_equal c3, environment.community_default_template
  86 + end
  87 +
  88 + should 'not allow set_person_as_default define a person template of another environment as default' do
  89 + p1= fast_create(Person, :is_template => true, :environment_id => environment.id)
  90 + environment.person_default_template= p1
  91 + environment.save
  92 +
  93 + env2 = fast_create(Environment)
  94 + p3 = fast_create(Person, :is_template => true, :environment_id => env2.id)
  95 +
  96 + post :set_person_as_default, :template_id => p3.id
  97 + environment.reload
  98 + assert_not_equal p3, environment.person_default_template
  99 +
  100 + end
  101 +
  102 + should 'not allow set_enterprise_as_default define a enterprise of another environment as default' do
  103 + e1= fast_create(Enterprise, :is_template => true, :environment_id => environment.id)
  104 + environment.enterprise_default_template= e1
  105 + environment.save
  106 +
  107 + env2 = fast_create(Environment)
  108 + e3 = fast_create(Enterprise, :is_template => true, :environment_id => env2.id)
  109 +
  110 + post :set_enterprise_as_default, :template_id => e3.id
  111 + environment.reload
  112 + assert_not_equal e3, environment.enterprise_default_template
  113 + end
  114 +
  115 + should 'display successfully notice message after define a community template as default' do
  116 + c3 = fast_create(Community, :is_template => true, :environment_id => environment)
  117 +
  118 + post :set_community_as_default, :template_id => c3.id
  119 + assert_equal "#{c3.name} defined as default", session[:notice]
  120 + end
  121 +
  122 + should 'display successfully notice message after define a person template as default' do
  123 + p3 = fast_create(Person, :is_template => true, :environment_id => environment)
  124 +
  125 + post :set_person_as_default, :template_id => p3.id
  126 + assert_equal "#{p3.name} defined as default", session[:notice]
  127 + end
  128 +
  129 + should 'display successfully notice message after define a enterprise template as default' do
  130 + e3 = fast_create(Enterprise, :is_template => true, :environment_id => environment)
  131 +
  132 + post :set_enterprise_as_default, :template_id => e3.id
  133 + assert_equal "#{e3.name} defined as default", session[:notice]
  134 + end
  135 +
  136 + should 'display unsuccessfully notice message when a community template could not be defined as default' do
  137 + env2 = fast_create(Environment)
  138 + c3 = fast_create(Community, :is_template => true, :environment_id => env2.id)
  139 +
  140 + post :set_community_as_default, :template_id => c3.id
  141 + assert_equal "Community not found. The template could no be changed.", session[:notice]
  142 + end
  143 +
  144 + should 'display unsuccessfully notice message when a person template could not be defined as default' do
  145 + env2 = fast_create(Environment)
  146 + p3 = fast_create(Person, :is_template => true, :environment_id => env2.id)
  147 +
  148 + post :set_person_as_default, :template_id => p3.id
  149 + assert_equal "Person not found. The template could no be changed.", session[:notice]
  150 + end
  151 +
  152 + should 'display unsuccessfully notice message when a enterprise template could not be defined as default' do
  153 + env2 = fast_create(Environment)
  154 + e3 = fast_create(Community, :is_template => true, :environment_id => env2.id)
  155 +
  156 + post :set_enterprise_as_default, :template_id => e3.id
  157 + assert_equal "Enterprise not found. The template could no be changed.", session[:notice]
  158 + end
  159 +
74 160 end
75 161  
... ...