From 3adf5209d56d96eb8cbf0085d29b17a45c85f94d Mon Sep 17 00:00:00 2001 From: MrJmad Date: Tue, 30 Nov 2021 00:14:35 +0100 Subject: [PATCH] Add datamigration to populate database with venv --- .../migrations/0079_populate_database.py | 19 +++++ aidants_connect_web/tests/test_utilities.py | 73 +++++++++++++++++++ aidants_connect_web/utilities.py | 35 +++++++++ 3 files changed, 127 insertions(+) create mode 100644 aidants_connect_web/migrations/0079_populate_database.py diff --git a/aidants_connect_web/migrations/0079_populate_database.py b/aidants_connect_web/migrations/0079_populate_database.py new file mode 100644 index 000000000..3aa778671 --- /dev/null +++ b/aidants_connect_web/migrations/0079_populate_database.py @@ -0,0 +1,19 @@ +from django.db import migrations + +from aidants_connect_web.utilities import create_first_user_organisation_and_token + + +def populate_database(apps, _): + create_first_user_organisation_and_token() + + +class Migration(migrations.Migration): + + dependencies = [ + ("aidants_connect_web", "0078_auto_20211122_1813"), + ("otp_static", "__latest__"), + ] + + operations = [ + migrations.RunPython(populate_database, reverse_code=migrations.RunPython.noop), + ] diff --git a/aidants_connect_web/tests/test_utilities.py b/aidants_connect_web/tests/test_utilities.py index 9475736e8..6aa328562 100644 --- a/aidants_connect_web/tests/test_utilities.py +++ b/aidants_connect_web/tests/test_utilities.py @@ -1,7 +1,18 @@ +import os + from django.test import tag, TestCase +from aidants_connect_web.models import Aidant, Organisation + +from aidants_connect_web.tests.factories import AidantFactory, OrganisationFactory + +from aidants_connect_web.utilities import create_first_user_organisation_and_token from aidants_connect_web.utilities import generate_sha256_hash +from django_otp.plugins.otp_static.models import StaticDevice, StaticToken + +from unittest import mock + @tag("utilities") class UtilitiesTests(TestCase): @@ -18,3 +29,65 @@ def test_generate_sha256_hash(self): ) self.assertEqual(generate_sha256_hash("123salt".encode()), hash_123salt) self.assertEqual(len(generate_sha256_hash("123salt".encode())), 64) + + +@tag("utilities") +class CreateFirstUserTests(TestCase): + def test_dont_create_if_user_exists(self): + AidantFactory() + self.assertEqual(1, len(Aidant.objects.all())) + self.assertIsNone(create_first_user_organisation_and_token()) + self.assertEqual(1, len(Aidant.objects.all())) + + def test_dont_create_if_organisation_exists(self): + OrganisationFactory() + self.assertEqual(1, len(Organisation.objects.all())) + self.assertIsNone(create_first_user_organisation_and_token()) + self.assertEqual(1, len(Organisation.objects.all())) + + def test_dont_create_without_all_venv(self): + self.assertEqual(0, len(Aidant.objects.all())) + self.assertEqual(0, len(Organisation.objects.all())) + self.assertIsNone(create_first_user_organisation_and_token()) + self.assertEqual(0, len(Organisation.objects.all())) + self.assertEqual(0, len(Aidant.objects.all())) + + @mock.patch.dict(os.environ, {"INIT_ORGA_NAME": "Donjons et Siphons"}) + @mock.patch.dict(os.environ, {"INIT_ADMIN_USERNAME": "mario.brossse@world.fr"}) + @mock.patch.dict(os.environ, {"INIT_ADMIN_PASSWORD": "PEACHforEVER"}) + def test_dont_create_without_one_venv(self): + self.assertEqual(0, len(Aidant.objects.all())) + self.assertEqual(0, len(Organisation.objects.all())) + self.assertIsNone(create_first_user_organisation_and_token()) + self.assertEqual(0, len(Organisation.objects.all())) + self.assertEqual(0, len(Aidant.objects.all())) + + @mock.patch.dict(os.environ, {"INIT_ORGA_NAME": "Donjons et Siphons"}) + @mock.patch.dict(os.environ, {"INIT_ADMIN_USERNAME": "mario.brossse@world.fr"}) + @mock.patch.dict(os.environ, {"INIT_ADMIN_PASSWORD": "PEACHforEVER"}) + @mock.patch.dict(os.environ, {"INIT_TOKEN": "12345"}) + def test_dont_create_without_with_a_invalid_token(self): + self.assertEqual(0, len(Aidant.objects.all())) + self.assertEqual(0, len(Organisation.objects.all())) + self.assertIsNone(create_first_user_organisation_and_token()) + self.assertEqual(0, len(Organisation.objects.all())) + self.assertEqual(0, len(Aidant.objects.all())) + + @mock.patch.dict(os.environ, {"INIT_ORGA_NAME": "Donjons et Siphons"}) + @mock.patch.dict(os.environ, {"INIT_ADMIN_USERNAME": "mario.brossse@world.fr"}) + @mock.patch.dict(os.environ, {"INIT_ADMIN_PASSWORD": "PEACHforEVER"}) + @mock.patch.dict(os.environ, {"INIT_TOKEN": "123456"}) + def test_create_all_object(self): + self.assertEqual(0, len(Aidant.objects.all())) + self.assertEqual(0, len(Organisation.objects.all())) + user = create_first_user_organisation_and_token() + self.assertIsNotNone(user) + self.assertEqual(1, len(Organisation.objects.all())) + self.assertEqual(1, len(Aidant.objects.all())) + self.assertEqual(1, len(StaticToken.objects.all())) + self.assertEqual(1, len(StaticDevice.objects.all())) + + self.assertEqual(user.username, "mario.brossse@world.fr") + self.assertEqual(Organisation.objects.first().name, "Donjons et Siphons") + + self.assertEqual(StaticToken.objects.first().token, "123456") diff --git a/aidants_connect_web/utilities.py b/aidants_connect_web/utilities.py index ba14bddf3..2017962fb 100644 --- a/aidants_connect_web/utilities.py +++ b/aidants_connect_web/utilities.py @@ -1,11 +1,13 @@ import io import hashlib +import os from datetime import date, datetime from urllib.parse import urlencode, quote import qrcode from pathlib import Path +from django.apps import apps from django.conf import settings from typing import TYPE_CHECKING, Optional, Union @@ -101,3 +103,36 @@ def generate_mailto_link(recipient: str, subject: str, body: str): def mandate_template_path(): return settings.MANDAT_TEMPLATE_PATH + + +def create_first_user_organisation_and_token(): + Aidant = apps.get_model("aidants_connect_web", "Aidant") + Organisation = apps.get_model("aidants_connect_web", "Organisation") + StaticDevice = apps.get_model("otp_static", "StaticDevice") + StaticToken = apps.get_model("otp_static", "StaticToken") + + init_orga_name = os.environ.get("INIT_ORGA_NAME", None) + init_admin_username = os.environ.get("INIT_ADMIN_USERNAME", None) + init_admin_password = os.environ.get("INIT_ADMIN_PASSWORD", None) + init_token = os.environ.get("INIT_TOKEN", "") + + models = [Aidant, Organisation, StaticToken, StaticDevice] + if any([model.objects.exists() for model in models]): + return None + + if len(init_token) != 6: + return None + + if all([init_token, init_orga_name, init_admin_username, init_admin_password]): + orga = Organisation.objects.create(name=init_orga_name) + user = Aidant.objects.create_superuser( + username=init_admin_username, + password=init_admin_password, + organisation=orga, + ) + device = StaticDevice.objects.create(user=user, name="Init Code") + StaticToken.objects.create(token=init_token, device=device) + + return user + + return None