Skip to content

Commit

Permalink
Add datamigration to populate database with venv
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjmad committed Dec 3, 2021
1 parent 3d47df5 commit 3adf520
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
19 changes: 19 additions & 0 deletions aidants_connect_web/migrations/0079_populate_database.py
Original file line number Diff line number Diff line change
@@ -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),
]
73 changes: 73 additions & 0 deletions aidants_connect_web/tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
35 changes: 35 additions & 0 deletions aidants_connect_web/utilities.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3adf520

Please sign in to comment.