Sha256: ecbde0dc301f1b37358152766b8ccaf16a5718f4a3e50e921cd7ce243e578d21

Contents?: true

Size: 1.88 KB

Versions: 25

Compression:

Stored size: 1.88 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}"
      @connection.execute "CREATE SCHEMA AUTHORIZATION #{u}"
      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

25 entries across 25 versions & 7 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
masover-castronaut-0.4.4.4 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
masover-castronaut-0.4.4.5 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
masover-castronaut-0.5.0.1 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.4.1 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.4.2 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.4.3 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.4.4 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.4.5 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.4.6 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.5.0 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.5.1 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.5.2 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.5.3 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
relevance-castronaut-0.5.4 vendor/activerecord/test/cases/schema_authorization_test_postgresql.rb
radiant-0.7.2 vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb
vibes-bj-1.2.2 spec/rails_root/vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/activerecord/test/cases/schema_authorization_test_postgresql.rb
activerecord-2.1.1 test/cases/schema_authorization_test_postgresql.rb
activerecord-2.1.0 test/cases/schema_authorization_test_postgresql.rb