From ae226fa402a69691d1b859ac82b06ad882627dfb Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Tue, 21 Oct 2008 18:17:58 -0300 Subject: [PATCH] ActionItem795: making profiles available in only its environments --- app/models/profile.rb | 2 +- lib/needs_profile.rb | 2 +- test/functional/content_viewer_controller_test.rb | 13 ++++++++++++- test/functional/memberships_controller_test.rb | 3 +++ test/unit/profile_test.rb | 21 +++++++++++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/models/profile.rb b/app/models/profile.rb index bab7c8c..5a091ea 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -160,7 +160,7 @@ class Profile < ActiveRecord::Base validates_presence_of :identifier, :name validates_format_of :identifier, :with => IDENTIFIER_FORMAT validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS - validates_uniqueness_of :identifier + validates_uniqueness_of :identifier, :scope => :environment_id validates_length_of :nickname, :maximum => 16, :allow_nil => true diff --git a/lib/needs_profile.rb b/lib/needs_profile.rb index 31d6588..b3b2c51 100644 --- a/lib/needs_profile.rb +++ b/lib/needs_profile.rb @@ -21,7 +21,7 @@ module NeedsProfile end def load_profile - @profile ||= Profile.find_by_identifier(params[:profile]) + @profile ||= environment.profiles.find_by_identifier(params[:profile]) render_not_found unless @profile end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 6b54e41..2aa1300 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -31,7 +31,7 @@ class ContentViewerControllerTest < Test::Unit::TestCase page = profile.articles.build(:name => 'test') page.save! - uses_host 'anhetegua.net' + uses_host 'colivre.net' get :view_page, :profile => profile.identifier, :page => [ 'test' ] assert_response :success assert_equal page, assigns(:page) @@ -517,4 +517,15 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_response :missing end + should 'not show a profile in an environment that is not its home environment' do + p = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => Environment.default) + + current = Environment.create!(:name => 'test environment') + current.domains.create!(:name => 'example.com') + uses_host 'www.example.com' + + get :view_page, :profile => 'mytestprofile', :page => [] + assert_response :missing + end + end diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 902fe80..edcdbbc 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -222,10 +222,13 @@ class MembershipsControllerTest < Test::Unit::TestCase template.boxes << Box.new template.boxes[0].blocks << Block.new template.save! + env = Environment.create!(:name => 'test_env') env.settings[:community_template_id] = template.id env.save! + profile.environment = env + profile.save! @controller.stubs(:environment).returns(env) post :new_community, :profile => profile.identifier, :community => { :name => 'test community', :description => 'a test community'} diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index d1c60d6..80aa6c9 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -946,6 +946,27 @@ class ProfileTest < Test::Unit::TestCase end end + should 'not be possible to have different profiles with the same identifier in the same environment' do + env = Environment.create!(:name => 'My test environment') + + p1 = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => env) + + p2 = Profile.new(:identifier => 'mytestprofile', :name => 'My test profile', :environment => env) + assert !p2.valid? + + assert p2.errors.on(:identifier) + assert_equal p1.environment, p2.environment + end + + should 'be possible to have different profiles with the same identifier in different environments' do + p1 = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile') + + env = Environment.create!(:name => 'My test environment') + p2 = Profile.create!(:identifier => 'mytestprofile', :name => 'My test profile', :environment => env) + + assert_not_equal p1.environment, p2.environment + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2