2020-06-07 17:30:56 +00:00
|
|
|
"""passbook e2e testing utilities"""
|
|
|
|
|
|
|
|
from glob import glob
|
2020-06-19 17:34:27 +00:00
|
|
|
from importlib.util import module_from_spec, spec_from_file_location
|
2020-06-07 17:30:56 +00:00
|
|
|
from inspect import getmembers, isfunction
|
2020-06-19 17:34:27 +00:00
|
|
|
|
|
|
|
from Cryptodome.PublicKey import RSA
|
2020-06-07 17:30:56 +00:00
|
|
|
from django.apps import apps
|
|
|
|
from django.db import connection, transaction
|
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
|
2020-06-07 17:44:28 +00:00
|
|
|
|
2020-06-07 17:30:56 +00:00
|
|
|
def apply_default_data():
|
|
|
|
"""apply objects created by migrations after tables have been truncated"""
|
|
|
|
# Find all migration files
|
|
|
|
# load all functions
|
|
|
|
migration_files = glob("**/migrations/*.py", recursive=True)
|
|
|
|
matches = []
|
|
|
|
for migration in migration_files:
|
2020-06-19 17:34:27 +00:00
|
|
|
with open(migration, "r+") as migration_file:
|
2020-06-07 17:30:56 +00:00
|
|
|
# Check if they have a `RunPython`
|
|
|
|
if "RunPython" in migration_file.read():
|
|
|
|
matches.append(migration)
|
|
|
|
|
|
|
|
with connection.schema_editor() as schema_editor:
|
|
|
|
for match in matches:
|
|
|
|
# Load module from file path
|
|
|
|
spec = spec_from_file_location("", match)
|
|
|
|
migration_module = module_from_spec(spec)
|
|
|
|
# pyright: reportGeneralTypeIssues=false
|
|
|
|
spec.loader.exec_module(migration_module)
|
|
|
|
# Call all functions from module
|
|
|
|
for _, func in getmembers(migration_module, isfunction):
|
|
|
|
with transaction.atomic():
|
|
|
|
try:
|
|
|
|
func(apps, schema_editor)
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|
2020-06-19 17:34:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ensure_rsa_key():
|
|
|
|
"""Ensure that at least one RSAKey Object exists, create one if none exist"""
|
|
|
|
from oidc_provider.models import RSAKey
|
|
|
|
|
|
|
|
if not RSAKey.objects.exists():
|
|
|
|
key = RSA.generate(2048)
|
|
|
|
rsakey = RSAKey(key=key.exportKey("PEM").decode("utf8"))
|
|
|
|
rsakey.save()
|