authentik.root.tests.test_db_schema_editor
1from django.db import ProgrammingError, connection, transaction 2from django.test import TestCase 3from pgtrigger.compiler import Trigger, UpsertTriggerSql 4 5from authentik.lib.config import CONFIG 6 7 8class TestDatabaseSchemaEditor(TestCase): 9 def setUp(self): 10 self.trigger = Trigger( 11 name="test", 12 sql=UpsertTriggerSql( 13 func="RETURN NEW;", 14 operation="INSERT", 15 pgid="pgtrigger_test_8f3a1", 16 table="test_table", 17 when="BEFORE", 18 ), 19 ) 20 21 @CONFIG.patch("postgresql.default_schema", "public") 22 def test_public_schema(self): 23 """With the public schema, trigger SQL is executed unchanged""" 24 with connection.schema_editor(collect_sql=True) as editor: 25 editor.execute(self.trigger.install_sql, params=None) 26 self.assertEqual(editor.collected_sql, [self.trigger.install_sql]) 27 28 @CONFIG.patch("postgresql.default_schema", "authentik") 29 def test_custom_schema(self): 30 """With a custom schema, trigger SQL creates and calls the helper function in that schema""" 31 with connection.schema_editor(collect_sql=True) as editor: 32 editor.execute(self.trigger.install_sql, params=None) 33 sql = editor.collected_sql[0] 34 self.assertNotIn('"public"._pgtrigger_should_ignore', sql) 35 self.assertEqual(sql.count('"authentik"._pgtrigger_should_ignore'), 2) 36 37 @CONFIG.patch("postgresql.default_schema", "test_e3de7180") 38 def test_install_without_privileges_on_public(self): 39 """A role that can't create objects in `public` can install triggers into its own schema""" 40 with connection.cursor() as cursor: 41 cursor.execute("CREATE ROLE test_e3de7180") 42 cursor.execute("CREATE SCHEMA test_e3de7180 AUTHORIZATION test_e3de7180") 43 cursor.execute("REVOKE ALL ON SCHEMA public FROM PUBLIC") 44 cursor.execute("SET LOCAL ROLE test_e3de7180") 45 cursor.execute("SET LOCAL search_path = test_e3de7180") 46 cursor.execute("CREATE TABLE test_table (id integer)") 47 48 with ( 49 self.assertRaisesMessage(ProgrammingError, "permission denied for schema public"), 50 transaction.atomic(), 51 ): 52 cursor.execute(self.trigger.install_sql, params=None) 53 with connection.schema_editor() as editor: 54 editor.execute(self.trigger.install_sql, params=None) 55 cursor.execute("INSERT INTO test_table VALUES (1)") 56 cursor.execute( 57 "SELECT n.nspname FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace " 58 "WHERE p.proname = '_pgtrigger_should_ignore'" 59 ) 60 self.assertIn(("test_e3de7180",), cursor.fetchall())
class
TestDatabaseSchemaEditor(django.test.testcases.TestCase):
9class TestDatabaseSchemaEditor(TestCase): 10 def setUp(self): 11 self.trigger = Trigger( 12 name="test", 13 sql=UpsertTriggerSql( 14 func="RETURN NEW;", 15 operation="INSERT", 16 pgid="pgtrigger_test_8f3a1", 17 table="test_table", 18 when="BEFORE", 19 ), 20 ) 21 22 @CONFIG.patch("postgresql.default_schema", "public") 23 def test_public_schema(self): 24 """With the public schema, trigger SQL is executed unchanged""" 25 with connection.schema_editor(collect_sql=True) as editor: 26 editor.execute(self.trigger.install_sql, params=None) 27 self.assertEqual(editor.collected_sql, [self.trigger.install_sql]) 28 29 @CONFIG.patch("postgresql.default_schema", "authentik") 30 def test_custom_schema(self): 31 """With a custom schema, trigger SQL creates and calls the helper function in that schema""" 32 with connection.schema_editor(collect_sql=True) as editor: 33 editor.execute(self.trigger.install_sql, params=None) 34 sql = editor.collected_sql[0] 35 self.assertNotIn('"public"._pgtrigger_should_ignore', sql) 36 self.assertEqual(sql.count('"authentik"._pgtrigger_should_ignore'), 2) 37 38 @CONFIG.patch("postgresql.default_schema", "test_e3de7180") 39 def test_install_without_privileges_on_public(self): 40 """A role that can't create objects in `public` can install triggers into its own schema""" 41 with connection.cursor() as cursor: 42 cursor.execute("CREATE ROLE test_e3de7180") 43 cursor.execute("CREATE SCHEMA test_e3de7180 AUTHORIZATION test_e3de7180") 44 cursor.execute("REVOKE ALL ON SCHEMA public FROM PUBLIC") 45 cursor.execute("SET LOCAL ROLE test_e3de7180") 46 cursor.execute("SET LOCAL search_path = test_e3de7180") 47 cursor.execute("CREATE TABLE test_table (id integer)") 48 49 with ( 50 self.assertRaisesMessage(ProgrammingError, "permission denied for schema public"), 51 transaction.atomic(), 52 ): 53 cursor.execute(self.trigger.install_sql, params=None) 54 with connection.schema_editor() as editor: 55 editor.execute(self.trigger.install_sql, params=None) 56 cursor.execute("INSERT INTO test_table VALUES (1)") 57 cursor.execute( 58 "SELECT n.nspname FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace " 59 "WHERE p.proname = '_pgtrigger_should_ignore'" 60 ) 61 self.assertIn(("test_e3de7180",), cursor.fetchall())
Similar to TransactionTestCase, but use transaction.atomic() to achieve
test isolation.
In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).
On database backends with no transaction support, TestCase behaves as TransactionTestCase.
def
setUp(self):
10 def setUp(self): 11 self.trigger = Trigger( 12 name="test", 13 sql=UpsertTriggerSql( 14 func="RETURN NEW;", 15 operation="INSERT", 16 pgid="pgtrigger_test_8f3a1", 17 table="test_table", 18 when="BEFORE", 19 ), 20 )
Hook method for setting up the test fixture before exercising it.
@CONFIG.patch('postgresql.default_schema', 'public')
def
test_public_schema(self):
22 @CONFIG.patch("postgresql.default_schema", "public") 23 def test_public_schema(self): 24 """With the public schema, trigger SQL is executed unchanged""" 25 with connection.schema_editor(collect_sql=True) as editor: 26 editor.execute(self.trigger.install_sql, params=None) 27 self.assertEqual(editor.collected_sql, [self.trigger.install_sql])
With the public schema, trigger SQL is executed unchanged
@CONFIG.patch('postgresql.default_schema', 'authentik')
def
test_custom_schema(self):
29 @CONFIG.patch("postgresql.default_schema", "authentik") 30 def test_custom_schema(self): 31 """With a custom schema, trigger SQL creates and calls the helper function in that schema""" 32 with connection.schema_editor(collect_sql=True) as editor: 33 editor.execute(self.trigger.install_sql, params=None) 34 sql = editor.collected_sql[0] 35 self.assertNotIn('"public"._pgtrigger_should_ignore', sql) 36 self.assertEqual(sql.count('"authentik"._pgtrigger_should_ignore'), 2)
With a custom schema, trigger SQL creates and calls the helper function in that schema
@CONFIG.patch('postgresql.default_schema', 'test_e3de7180')
def
test_install_without_privileges_on_public(self):
38 @CONFIG.patch("postgresql.default_schema", "test_e3de7180") 39 def test_install_without_privileges_on_public(self): 40 """A role that can't create objects in `public` can install triggers into its own schema""" 41 with connection.cursor() as cursor: 42 cursor.execute("CREATE ROLE test_e3de7180") 43 cursor.execute("CREATE SCHEMA test_e3de7180 AUTHORIZATION test_e3de7180") 44 cursor.execute("REVOKE ALL ON SCHEMA public FROM PUBLIC") 45 cursor.execute("SET LOCAL ROLE test_e3de7180") 46 cursor.execute("SET LOCAL search_path = test_e3de7180") 47 cursor.execute("CREATE TABLE test_table (id integer)") 48 49 with ( 50 self.assertRaisesMessage(ProgrammingError, "permission denied for schema public"), 51 transaction.atomic(), 52 ): 53 cursor.execute(self.trigger.install_sql, params=None) 54 with connection.schema_editor() as editor: 55 editor.execute(self.trigger.install_sql, params=None) 56 cursor.execute("INSERT INTO test_table VALUES (1)") 57 cursor.execute( 58 "SELECT n.nspname FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace " 59 "WHERE p.proname = '_pgtrigger_should_ignore'" 60 ) 61 self.assertIn(("test_e3de7180",), cursor.fetchall())
A role that can't create objects in public can install triggers into its own schema