add testss for user creation

This commit is contained in:
Matthias 2022-02-28 20:29:04 +01:00
parent 7d9edaea8b
commit ad81439140
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
2 changed files with 51 additions and 0 deletions

View File

@ -30,6 +30,7 @@ class InvenTreeConfig(AppConfig):
if not isInTestMode():
self.update_exchange_rates()
if canAppAccessDatabase() or settings.TESTING:
self.add_user_on_startup()
def remove_obsolete_tasks(self):
@ -189,6 +190,8 @@ class InvenTreeConfig(AppConfig):
logger.info(f'User {str(new_user)} was created!')
except IntegrityError as _e:
logger.warning(f'The user "{add_user}" could not be created due to the following error:\n{str(_e)}')
if settings.TESTING:
raise _e
except Exception as _e:
raise _e

View File

@ -1,9 +1,13 @@
import json
from test.support import EnvironmentVarGuard
from django.test import TestCase
import django.core.exceptions as django_exceptions
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
from django.conf import settings
from django.db.utils import IntegrityError
from djmoney.money import Money
from djmoney.contrib.exchange.models import Rate, convert_money
@ -407,3 +411,47 @@ class TestStatus(TestCase):
def test_Importing(self):
self.assertEqual(ready.isImportingData(), False)
class TestSettings(TestCase):
"""
Unit tests for settings
"""
def setUp(self) -> None:
self.user_mdl = get_user_model()
self.env = EnvironmentVarGuard()
def run_reload(self):
from plugin import registry
with self.env:
settings.USER_ADDED = False
registry.reload_plugins()
def test_set_user_to_few(self):
# add shortcut
user_count = self.user_mdl.objects.count
# nothing set
self.assertEqual(user_count(), 0)
# not enough set
self.env.set('INVENTREE_SET_USER', 'admin') # set username
self.run_reload()
self.assertEqual(user_count(), 0)
# enough set
self.env.set('INVENTREE_SET_USER', 'admin') # set username
self.env.set('INVENTREE_SET_EMAIL', 'info@example.com') # set email
self.env.set('INVENTREE_SET_PASSWORD', 'password123') # set password
self.run_reload()
self.assertEqual(user_count(), 1)
# double adding should not work
self.env.set('INVENTREE_SET_USER', 'admin') # set username
self.env.set('INVENTREE_SET_EMAIL', 'info@example.com') # set email
self.env.set('INVENTREE_SET_PASSWORD', 'password123') # set password
with self.assertRaises(IntegrityError):
self.run_reload()
self.assertEqual(user_count(), 1)