Sha256: a4f21d1a8f044f66cf381460b9ae37d494a8df2ab8feacd213b0670a7eb9a220

Contents?: true

Size: 1.9 KB

Versions: 76

Compression:

Stored size: 1.9 KB

Contents

require "cases/helper"

class SchemaThing < ActiveRecord::Base
end

class SchemaAuthorizationTest < ActiveRecord::TestCase
  self.use_transactional_fixtures = false

  TABLE_NAME = 'schema_things'
  COLUMNS = [
    'id serial primary key',
    'name character varying(50)'
  ]
  USERS = ['rails_pg_schema_user1', 'rails_pg_schema_user2']

  def setup
    @connection = ActiveRecord::Base.connection
    @connection.execute "SET search_path TO '$user',public"
    set_session_auth
    USERS.each do |u|
      @connection.execute "CREATE USER #{u}" rescue nil
      @connection.execute "CREATE SCHEMA AUTHORIZATION #{u}" rescue nil
      set_session_auth u
      @connection.execute "CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
      @connection.execute "INSERT INTO #{TABLE_NAME} (name) VALUES ('#{u}')"
      set_session_auth
    end
  end

  def teardown
    set_session_auth
    @connection.execute "RESET search_path"
    USERS.each do |u|
      @connection.execute "DROP SCHEMA #{u} CASCADE"
      @connection.execute "DROP USER #{u}"
    end
  end

  def test_schema_invisible
    assert_raise(ActiveRecord::StatementInvalid) do
      set_session_auth
      @connection.execute "SELECT * FROM #{TABLE_NAME}"
    end
  end

  def test_schema_uniqueness
    assert_nothing_raised do
      set_session_auth
      USERS.each do |u|
        set_session_auth u
        assert_equal u, @connection.select_value("SELECT name FROM #{TABLE_NAME} WHERE id = 1")
        set_session_auth
      end
    end
  end

  def test_sequence_schema_caching
    assert_nothing_raised do
      USERS.each do |u|
        set_session_auth u
        st = SchemaThing.new :name => 'TEST1'
        st.save!
        st = SchemaThing.new :id => 5, :name => 'TEST2'
        st.save!
        set_session_auth
      end
    end
  end

  private
    def set_session_auth auth = nil
       @connection.execute "SET SESSION AUTHORIZATION #{auth || 'default'}"
    end

end

Version data entries

76 entries across 73 versions & 16 rubygems

Version Path
p8-castronaut-0.6.1.1 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.6.0 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.6.1 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.7.4 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.7.5 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
activerecord-2.3.18 test/cases/schema_authorization_test_postgresql.rb
activerecord_csi-2.3.5.p8 test/cases/schema_authorization_test_postgresql.rb
activerecord-2.3.17 test/cases/schema_authorization_test_postgresql.rb
activerecord_csi-2.3.5.p7 test/cases/schema_authorization_test_postgresql.rb
activerecord_csi-2.3.5.p6 test/cases/schema_authorization_test_postgresql.rb
activerecord-2.3.16 test/cases/schema_authorization_test_postgresql.rb
activerecord-2.3.15 test/cases/schema_authorization_test_postgresql.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/activerecord-2.3.14/test/cases/schema_authorization_test_postgresql.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/activerecord-2.3.12/test/cases/schema_authorization_test_postgresql.rb
activerecord-2.3.14 test/cases/schema_authorization_test_postgresql.rb
kajam-1.0.3.rc2 vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb
activerecord-2.3.12 test/cases/schema_authorization_test_postgresql.rb
radiant-1.0.0.rc2 vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb
radiant-1.0.0.rc1 vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb
webroar-0.5.0 src/admin_panel/vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb