Coverage for django_napse/auth/db_essentials.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1import contextlib 

2import json 

3 

4from django.apps import apps 

5from django.db.models.signals import post_migrate 

6from django.dispatch import receiver 

7 

8from django_napse.core.settings import napse_settings 

9from django_napse.utils.errors import NapseKeyError 

10 

11 

12@receiver(post_migrate) 

13def create_master_key(sender, **kwargs): 

14 NapseAPIKey = apps.get_model("django_napse_auth", "NapseAPIKey") 

15 with contextlib.suppress(NapseKeyError.DuplicateMasterkey): 

16 _, key = NapseAPIKey.objects.create_key(name="", is_master_key=True) 

17 print(f"Master key created: {key}") 

18 with open(napse_settings.NAPSE_SECRETS_FILE_PATH, "r") as f: 

19 secrets = json.load(f) 

20 secrets["master_key"] = key 

21 with open(napse_settings.NAPSE_SECRETS_FILE_PATH, "w") as f: 

22 json.dump(secrets, f, indent=4)