authentik.brands.tests.test_current_queries
Test brands
1"""Test brands""" 2 3from django.http import HttpRequest 4from django.test import TestCase 5 6from authentik.brands.models import Brand 7from authentik.brands.utils import _BRAND_RELATED_FK_FIELDS, get_brand_for_request 8from authentik.core.tests.utils import create_test_flow 9from authentik.flows.models import FlowDesignation 10 11 12class TestGetBrandForRequestSelectRelated(TestCase): 13 """``get_brand_for_request`` must hydrate the FK fields read on the 14 request hot path so later access doesn't trigger lazy loads.""" 15 16 def setUp(self): 17 Brand.objects.all().delete() 18 self.flow_auth = create_test_flow(designation=FlowDesignation.AUTHENTICATION) 19 self.brand = Brand.objects.create( 20 domain="select-related-test.example.com", 21 flow_authentication=self.flow_auth, 22 ) 23 24 def _make_request(self, host: str) -> HttpRequest: 25 request = HttpRequest() 26 request.META["HTTP_HOST"] = host 27 return request 28 29 def test_brand_fks_are_loaded_in_single_query(self): 30 """Brand FK access after ``get_brand_for_request`` must not trigger 31 extra queries.""" 32 request = self._make_request("select-related-test.example.com") 33 with self.assertNumQueries(1): 34 brand = get_brand_for_request(request) 35 _ = brand.flow_authentication 36 _ = brand.flow_authentication.slug if brand.flow_authentication else None 37 _ = brand.flow_invalidation 38 _ = brand.flow_recovery 39 _ = brand.flow_unenrollment 40 _ = brand.flow_user_settings 41 _ = brand.flow_device_code 42 _ = brand.flow_lockdown 43 _ = brand.flow_request 44 _ = brand.default_application 45 46 def test_brand_related_fk_list_complete(self): 47 """``_BRAND_RELATED_FK_FIELDS`` covers every Flow/Application FK on 48 Brand — fails loud when a new FK is added but not registered here.""" 49 actual_fks = { 50 f.name 51 for f in Brand._meta.get_fields() 52 if f.many_to_one and f.related_model is not None 53 } 54 relevant_fks = { 55 name for name in actual_fks if name.startswith("flow_") or name == "default_application" 56 } 57 declared = set(_BRAND_RELATED_FK_FIELDS) 58 missing = relevant_fks - declared 59 self.assertFalse( 60 missing, 61 f"Brand FK fields {missing} aren't in _BRAND_RELATED_FK_FIELDS — " 62 "add them or the request hot path will incur extra queries.", 63 ) 64 65 def test_brand_related_fks_all_exist_on_model(self): 66 """Every entry in ``_BRAND_RELATED_FK_FIELDS`` is a real FK on Brand. 67 ``select_related`` raises ``FieldError`` at first use if any entry 68 is stale, which would break every request.""" 69 actual_fks = { 70 f.name 71 for f in Brand._meta.get_fields() 72 if f.many_to_one and f.related_model is not None 73 } 74 declared = set(_BRAND_RELATED_FK_FIELDS) 75 extraneous = declared - actual_fks 76 self.assertFalse( 77 extraneous, 78 f"_BRAND_RELATED_FK_FIELDS contains {extraneous} which don't " 79 f"exist on Brand (actual FKs: {sorted(actual_fks)}).", 80 )
class
TestGetBrandForRequestSelectRelated(django.test.testcases.TestCase):
13class TestGetBrandForRequestSelectRelated(TestCase): 14 """``get_brand_for_request`` must hydrate the FK fields read on the 15 request hot path so later access doesn't trigger lazy loads.""" 16 17 def setUp(self): 18 Brand.objects.all().delete() 19 self.flow_auth = create_test_flow(designation=FlowDesignation.AUTHENTICATION) 20 self.brand = Brand.objects.create( 21 domain="select-related-test.example.com", 22 flow_authentication=self.flow_auth, 23 ) 24 25 def _make_request(self, host: str) -> HttpRequest: 26 request = HttpRequest() 27 request.META["HTTP_HOST"] = host 28 return request 29 30 def test_brand_fks_are_loaded_in_single_query(self): 31 """Brand FK access after ``get_brand_for_request`` must not trigger 32 extra queries.""" 33 request = self._make_request("select-related-test.example.com") 34 with self.assertNumQueries(1): 35 brand = get_brand_for_request(request) 36 _ = brand.flow_authentication 37 _ = brand.flow_authentication.slug if brand.flow_authentication else None 38 _ = brand.flow_invalidation 39 _ = brand.flow_recovery 40 _ = brand.flow_unenrollment 41 _ = brand.flow_user_settings 42 _ = brand.flow_device_code 43 _ = brand.flow_lockdown 44 _ = brand.flow_request 45 _ = brand.default_application 46 47 def test_brand_related_fk_list_complete(self): 48 """``_BRAND_RELATED_FK_FIELDS`` covers every Flow/Application FK on 49 Brand — fails loud when a new FK is added but not registered here.""" 50 actual_fks = { 51 f.name 52 for f in Brand._meta.get_fields() 53 if f.many_to_one and f.related_model is not None 54 } 55 relevant_fks = { 56 name for name in actual_fks if name.startswith("flow_") or name == "default_application" 57 } 58 declared = set(_BRAND_RELATED_FK_FIELDS) 59 missing = relevant_fks - declared 60 self.assertFalse( 61 missing, 62 f"Brand FK fields {missing} aren't in _BRAND_RELATED_FK_FIELDS — " 63 "add them or the request hot path will incur extra queries.", 64 ) 65 66 def test_brand_related_fks_all_exist_on_model(self): 67 """Every entry in ``_BRAND_RELATED_FK_FIELDS`` is a real FK on Brand. 68 ``select_related`` raises ``FieldError`` at first use if any entry 69 is stale, which would break every request.""" 70 actual_fks = { 71 f.name 72 for f in Brand._meta.get_fields() 73 if f.many_to_one and f.related_model is not None 74 } 75 declared = set(_BRAND_RELATED_FK_FIELDS) 76 extraneous = declared - actual_fks 77 self.assertFalse( 78 extraneous, 79 f"_BRAND_RELATED_FK_FIELDS contains {extraneous} which don't " 80 f"exist on Brand (actual FKs: {sorted(actual_fks)}).", 81 )
get_brand_for_request must hydrate the FK fields read on the
request hot path so later access doesn't trigger lazy loads.
def
setUp(self):
17 def setUp(self): 18 Brand.objects.all().delete() 19 self.flow_auth = create_test_flow(designation=FlowDesignation.AUTHENTICATION) 20 self.brand = Brand.objects.create( 21 domain="select-related-test.example.com", 22 flow_authentication=self.flow_auth, 23 )
Hook method for setting up the test fixture before exercising it.
def
test_brand_fks_are_loaded_in_single_query(self):
30 def test_brand_fks_are_loaded_in_single_query(self): 31 """Brand FK access after ``get_brand_for_request`` must not trigger 32 extra queries.""" 33 request = self._make_request("select-related-test.example.com") 34 with self.assertNumQueries(1): 35 brand = get_brand_for_request(request) 36 _ = brand.flow_authentication 37 _ = brand.flow_authentication.slug if brand.flow_authentication else None 38 _ = brand.flow_invalidation 39 _ = brand.flow_recovery 40 _ = brand.flow_unenrollment 41 _ = brand.flow_user_settings 42 _ = brand.flow_device_code 43 _ = brand.flow_lockdown 44 _ = brand.flow_request 45 _ = brand.default_application
Brand FK access after get_brand_for_request must not trigger
extra queries.