Coverage for utilities/security.py: 62%

14 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.core.exceptions import ImproperlyConfigured 

2 

3__all__ = ( 

4 'validate_peppers', 

5) 

6 

7 

8def validate_peppers(peppers): 

9 """ 

10 Validate the given dictionary of cryptographic peppers for type & sufficient length. 

11 """ 

12 if not isinstance(peppers, dict): 12 ↛ 13line 12 didn't jump to line 13 because the condition on line 12 was never true

13 raise ImproperlyConfigured("API_TOKEN_PEPPERS must be a dictionary.") 

14 for key, pepper in peppers.items(): 

15 if type(key) is not int: 15 ↛ 16line 15 didn't jump to line 16 because the condition on line 15 was never true

16 raise ImproperlyConfigured(f"Invalid API_TOKEN_PEPPERS key: {key}. All keys must be integers.") 

17 if not 0 <= key <= 32767: 17 ↛ 18line 17 didn't jump to line 18 because the condition on line 17 was never true

18 raise ImproperlyConfigured( 

19 f"Invalid API_TOKEN_PEPPERS key: {key}. Key values must be between 0 and 32767, inclusive." 

20 ) 

21 if type(pepper) is not str: 21 ↛ 22line 21 didn't jump to line 22 because the condition on line 21 was never true

22 raise ImproperlyConfigured(f"Invalid pepper {key}: Pepper value must be a string.") 

23 if len(pepper) < 50: 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true

24 raise ImproperlyConfigured(f"Invalid pepper {key}: Pepper must be at least 50 characters in length.")