From d9a7c4df0f66fb9223d95e28aa90bdf2ae6caf4c Mon Sep 17 00:00:00 2001 From: Jens Nistler Date: Tue, 10 Sep 2024 11:00:06 +0200 Subject: [PATCH] align default settings and readme --- README.md | 33 ++++++++++++++++++--------------- django_scrubber/__init__.py | 9 ++++----- tests/test_settings.py | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 tests/test_settings.py diff --git a/README.md b/README.md index f42e5a4..7d129b7 100644 --- a/README.md +++ b/README.md @@ -251,58 +251,68 @@ SCRUBBER_GLOBAL_SCRUBBERS = { The seed used when generating random content by the Faker scrubber. Setting this to `None` means each scrubbing will generate different data. -(default: 42) +(default: `42`) ### `SCRUBBER_ENTRIES_PER_PROVIDER`: Number of entries to use as source for Faker scrubber. Increasing this value will increase the randomness of generated data, but decrease performance. -(default: 1000) +(default: `1000`) ### `SCRUBBER_SKIP_UNMANAGED`: Do not attempt to scrub models which are not managed by the ORM. -(default: True) +(default: `True`) ### `SCRUBBER_APPS_LIST`: Only scrub models belonging to these specific django apps. If unset, will scrub all installed apps. -(default: None) +(default: `None`) ### `SCRUBBER_ADDITIONAL_FAKER_PROVIDERS`: Add additional fake providers to be used by Faker. Must be noted as full dotted path to the provider class. -(default: empty list) +(default: `{*()}`, empty set) ### `SCRUBBER_FAKER_LOCALE`: Set an alternative locale for Faker used during the scrubbing process. -(default: None, falls back to Django's default locale) +(default: `None`, falls back to Django's default locale) ### `SCRUBBER_MAPPING`: Define a class and a mapper which does not have to live inside the given model. Useful, if you have no control over the models code you'd like to scrub. +````python +SCRUBBER_MAPPING = { + "auth.User": "my_app.scrubbers.UserScrubbers", +} +```` + +(default: `{}`) + ### `SCRUBBER_STRICT_MODE`: When strict mode is activated, you have to define a scrubbing policy for every field of every type defined in `SCRUBBER_REQUIRED_FIELD_TYPES`. If you have unscrubbed fields and this flag is active, you can't run `python manage.py scrub_data`. +(default: `False`) + ### `SCRUBBER_REQUIRED_FIELD_TYPES`: Defaults to all text-based Django model fields. Usually, privacy-relevant data is only stored in text-fields, numbers and booleans (usually) can't contain sensitive personal data. These fields will be checked when running `python manage.py scrub_validation`. -(default: (models.CharField, models.TextField, models.URLField, models.JSONField, models.GenericIPAddressField, - models.EmailField,)) +(default: `(models.CharField, models.TextField, models.URLField, models.JSONField, models.GenericIPAddressField, + models.EmailField,)`) ### `SCRUBBER_REQUIRED_FIELD_MODEL_WHITELIST`: @@ -314,15 +324,8 @@ against the full model name (e.g. `re.compile(auth.*)` to whitelist all auth mod (default: `('auth.Group', 'auth.Permission', 'contenttypes.ContentType', 'sessions.Session', 'sites.Site', 'django_scrubber.FakeData', 'db.TestModel',)`) -````python -SCRUBBER_MAPPING = { - "auth.User": "my_app.scrubbers.UserScrubbers", -} -```` - (default: {}) - ## Logging Scrubber uses the default django logger. The logger name is ``django_scrubber.scrubbers``. diff --git a/django_scrubber/__init__.py b/django_scrubber/__init__.py index a09ff8c..6fc3dc5 100644 --- a/django_scrubber/__init__.py +++ b/django_scrubber/__init__.py @@ -9,9 +9,9 @@ 'SCRUBBER_GLOBAL_SCRUBBERS': {}, 'SCRUBBER_SKIP_UNMANAGED': True, 'SCRUBBER_APPS_LIST': None, - 'SCRUBBER_ADDITIONAL_FAKER_PROVIDERS': [], + 'SCRUBBER_ADDITIONAL_FAKER_PROVIDERS': {*()}, 'SCRUBBER_FAKER_LOCALE': None, - 'SCRUBBER_MAPPING': dict(), + 'SCRUBBER_MAPPING': {}, 'SCRUBBER_STRICT_MODE': False, 'SCRUBBER_REQUIRED_FIELD_TYPES': ( models.CharField, @@ -21,7 +21,7 @@ models.GenericIPAddressField, models.EmailField, ), - 'SCRUBBER_REQUIRED_FIELD_MODEL_WHITELIST': [ + 'SCRUBBER_REQUIRED_FIELD_MODEL_WHITELIST': ( 'auth.Group', 'auth.Permission', 'contenttypes.ContentType', @@ -29,11 +29,10 @@ 'sessions.Session', 'sites.Site', 'django_scrubber.FakeData', - ], + ), } -# TODO: replace with ChainMap now that we only support py3 def settings_with_fallback(key): return getattr(settings, key, defaults[key]) diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 0000000..71bf493 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,18 @@ +from django.test import TestCase, override_settings + +from django_scrubber import settings_with_fallback, defaults + + +class TestScrubbers(TestCase): + def test_default(self): + self.assertEqual( + defaults["SCRUBBER_RANDOM_SEED"], + settings_with_fallback("SCRUBBER_RANDOM_SEED"), + ) + + @override_settings(SCRUBBER_RANDOM_SEED=9001) + def test_override(self): + self.assertEqual( + 9001, + settings_with_fallback("SCRUBBER_RANDOM_SEED"), + )