From ed5c23fcead62b114a12f55740f7530760087338 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 18:50:16 +0100 Subject: [PATCH 01/30] Add template defaults --- InvenTree/config_template.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index b14c1224ce..153cf2f16b 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -154,6 +154,11 @@ static_root: '/home/inventree/data/static' # Use environment variable INVENTREE_LOGIN_ATTEMPTS #login_attempts: 5 +# Add new user on first startup +#set_user: admin +#set_email: info@example.com +#set_password: inventree + # Permit custom authentication backends #authentication_backends: # - 'django.contrib.auth.backends.ModelBackend' From a42674256738acd06f4cb196fa1ec8ac39f49095 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:08:52 +0100 Subject: [PATCH 02/30] [FR] Non-interactive, environment variable based set-up in Docker deployments Fixes #2501 --- InvenTree/InvenTree/apps.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 76b918459c..667da9b185 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -4,8 +4,11 @@ import logging from django.apps import AppConfig from django.core.exceptions import AppRegistryNotReady +from django.conf import settings +from django.contrib.auth import get_user_model from InvenTree.ready import isInTestMode, canAppAccessDatabase +from .config import get_setting import InvenTree.tasks @@ -26,6 +29,8 @@ class InvenTreeConfig(AppConfig): if not isInTestMode(): self.update_exchange_rates() + self.add_user_on_startup() + def remove_obsolete_tasks(self): """ Delete any obsolete scheduled tasks in the database @@ -138,3 +143,33 @@ class InvenTreeConfig(AppConfig): update_exchange_rates() except Exception as e: logger.error(f"Error updating exchange rates: {e}") + + def add_user_on_startup(self): + """Add a user on startup""" + + # get values + add_user = get_setting( + 'INVENTREE_SET_USER', + settings.CONFIG.get('set_user', False) + ) + add_email = get_setting( + 'INVENTREE_SET_EMAIL', + settings.CONFIG.get('set_email', False) + ) + add_password = get_setting( + 'INVENTREE_SET_PASSWORD', + settings.CONFIG.get('set_password', False) + ) + + # check if all values are present + if not (add_user and add_email and add_password): + logger.warn('Not all required settings for adding a user on startup are present:\nINVENTREE_SET_USER, INVENTREE_SET_EMAIL, INVENTREE_SET_PASSWORD') + return + + # create user + user = get_user_model() + try: + new_user = user.objects.create_user(add_user, add_email, add_password) + logger.info(f'User {str(new_user)} was created!') + except Exception as _e: + print(_e) From 28012a3ceadc563d176a04d271bfdff6db86ac40 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:19:10 +0100 Subject: [PATCH 03/30] only add once --- InvenTree/InvenTree/apps.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 667da9b185..e984e4f178 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -146,6 +146,9 @@ class InvenTreeConfig(AppConfig): def add_user_on_startup(self): """Add a user on startup""" + # stop if already created + if hasattr(settings, 'USER_ADDED') and settings.USER_ADDED: + return # get values add_user = get_setting( @@ -171,5 +174,6 @@ class InvenTreeConfig(AppConfig): try: new_user = user.objects.create_user(add_user, add_email, add_password) logger.info(f'User {str(new_user)} was created!') + settings.USER_ADDED = True except Exception as _e: print(_e) From ac2d54a1501420815d32bec43c0c03e91ae634ff Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:29:54 +0100 Subject: [PATCH 04/30] catch uniqueness --- InvenTree/InvenTree/apps.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index e984e4f178..c15a5c4d0a 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -6,6 +6,7 @@ from django.apps import AppConfig from django.core.exceptions import AppRegistryNotReady from django.conf import settings from django.contrib.auth import get_user_model +from django.db.utils import IntegrityError from InvenTree.ready import isInTestMode, canAppAccessDatabase from .config import get_setting @@ -174,6 +175,10 @@ class InvenTreeConfig(AppConfig): try: new_user = user.objects.create_user(add_user, add_email, add_password) logger.info(f'User {str(new_user)} was created!') - settings.USER_ADDED = True + except IntegrityError as _e: + logger.warning(f'The user "{add_user}" could not be created due to the following error:\n{str(_e)}') except Exception as _e: - print(_e) + raise _e + + # do not try again this round + settings.USER_ADDED = True From 9552482471136c1244c882786b00db0a95edfcbf Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:38:14 +0100 Subject: [PATCH 05/30] fix initial checks --- InvenTree/InvenTree/apps.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index c15a5c4d0a..fad6b2a9a5 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -166,7 +166,15 @@ class InvenTreeConfig(AppConfig): ) # check if all values are present - if not (add_user and add_email and add_password): + set_variables = 0 + for tested_var in [add_user, add_email, add_password]: + if tested_var: + set_variables += 1 + + if set_variables == 0: + return + + if set_variables < 3: logger.warn('Not all required settings for adding a user on startup are present:\nINVENTREE_SET_USER, INVENTREE_SET_EMAIL, INVENTREE_SET_PASSWORD') return From 63977d47ce684a039d514c806c04b4e7e89dc0b3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:41:23 +0100 Subject: [PATCH 06/30] only test once --- InvenTree/InvenTree/apps.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index fad6b2a9a5..c209ac4203 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -172,10 +172,12 @@ class InvenTreeConfig(AppConfig): set_variables += 1 if set_variables == 0: + settings.USER_ADDED = True return if set_variables < 3: logger.warn('Not all required settings for adding a user on startup are present:\nINVENTREE_SET_USER, INVENTREE_SET_EMAIL, INVENTREE_SET_PASSWORD') + settings.USER_ADDED = True return # create user From 7d9edaea8bd87c5e7bf446e3db27ddb05797aece Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:43:00 +0100 Subject: [PATCH 07/30] add docstrings --- InvenTree/InvenTree/apps.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index c209ac4203..c970315dbd 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -171,16 +171,18 @@ class InvenTreeConfig(AppConfig): if tested_var: set_variables += 1 + # no variable set -> do not try anything if set_variables == 0: settings.USER_ADDED = True return + # not all needed variables set if set_variables < 3: logger.warn('Not all required settings for adding a user on startup are present:\nINVENTREE_SET_USER, INVENTREE_SET_EMAIL, INVENTREE_SET_PASSWORD') settings.USER_ADDED = True return - # create user + # good to go -> create user user = get_user_model() try: new_user = user.objects.create_user(add_user, add_email, add_password) From ad814391404233cee0a3649fd10479f21057aaf2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 20:29:04 +0100 Subject: [PATCH 08/30] add testss for user creation --- InvenTree/InvenTree/apps.py | 3 +++ InvenTree/InvenTree/tests.py | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index c970315dbd..a2757c6f58 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -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 diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 205231eb7b..67b087f201 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -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) From 23531e0a5d2fdccfefd06d0ac6382e00cbb3d5ee Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 20:29:45 +0100 Subject: [PATCH 09/30] remove dead code --- InvenTree/InvenTree/apps.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index a2757c6f58..62afa9f3f2 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -192,8 +192,6 @@ class InvenTreeConfig(AppConfig): 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 # do not try again this round settings.USER_ADDED = True From d6a42d64d8f26d9eb0f8c9494565796b89446bcb Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 20:33:10 +0100 Subject: [PATCH 10/30] make docs clearer --- InvenTree/InvenTree/apps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 62afa9f3f2..295ab1b9f4 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -148,7 +148,7 @@ class InvenTreeConfig(AppConfig): def add_user_on_startup(self): """Add a user on startup""" - # stop if already created + # stop if checks were already created if hasattr(settings, 'USER_ADDED') and settings.USER_ADDED: return @@ -193,5 +193,5 @@ class InvenTreeConfig(AppConfig): if settings.TESTING: raise _e - # do not try again this round + # do not try again settings.USER_ADDED = True From 3b1bfddd8b85ed61d29b7ed7dae10a27d33b2f43 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 20:33:36 +0100 Subject: [PATCH 11/30] also expect IntegrationsPluginError --- InvenTree/InvenTree/tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 67b087f201..66178e7322 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -25,6 +25,7 @@ import InvenTree.tasks from stock.models import StockLocation from common.settings import currency_codes +from plugin.helpers import IntegrationPluginError class ValidatorTest(TestCase): @@ -452,6 +453,6 @@ class TestSettings(TestCase): 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): + with self.assertRaises(IntegrityError, IntegrationPluginError): self.run_reload() self.assertEqual(user_count(), 1) From 4e898d5eac38d51b5d0eadf993b7d237e2e855fa Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 20:42:20 +0100 Subject: [PATCH 12/30] fix test transactions --- InvenTree/InvenTree/apps.py | 4 +++- InvenTree/InvenTree/tests.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 295ab1b9f4..a290811b97 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -6,6 +6,7 @@ from django.apps import AppConfig from django.core.exceptions import AppRegistryNotReady from django.conf import settings from django.contrib.auth import get_user_model +from django.db import transaction from django.db.utils import IntegrityError from InvenTree.ready import isInTestMode, canAppAccessDatabase @@ -186,7 +187,8 @@ class InvenTreeConfig(AppConfig): # good to go -> create user user = get_user_model() try: - new_user = user.objects.create_user(add_user, add_email, add_password) + with transaction.atomic(): + new_user = user.objects.create_user(add_user, add_email, add_password) 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)}') diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 66178e7322..107a6fa317 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -453,6 +453,6 @@ class TestSettings(TestCase): 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, IntegrationPluginError): + with self.assertRaises(IntegrationPluginError): self.run_reload() self.assertEqual(user_count(), 1) From 737d3977054d22fa125b5e7793d68a44da85fcb5 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 20:44:13 +0100 Subject: [PATCH 13/30] PEP fix --- InvenTree/InvenTree/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 107a6fa317..e9d40f5297 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -7,7 +7,6 @@ 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 From 12c42962612bd846d732eb7f6470aa6b9bb8f0ce Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 21:08:27 +0100 Subject: [PATCH 14/30] add special env testing flag --- InvenTree/InvenTree/apps.py | 4 ++-- InvenTree/InvenTree/settings.py | 1 + InvenTree/InvenTree/tests.py | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index a290811b97..a6116f0ec2 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -31,7 +31,7 @@ class InvenTreeConfig(AppConfig): if not isInTestMode(): self.update_exchange_rates() - if canAppAccessDatabase() or settings.TESTING: + if canAppAccessDatabase() or settings.TESTING_ENV: self.add_user_on_startup() def remove_obsolete_tasks(self): @@ -192,7 +192,7 @@ 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: + if settings.TESTING_ENV: raise _e # do not try again diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index c45be06f4b..7291178e4a 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -37,6 +37,7 @@ def _is_true(x): # Determine if we are running in "test" mode e.g. "manage.py test" TESTING = 'test' in sys.argv +TESTING_ENV = False # New requirement for django 3.2+ DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index e9d40f5297..9014ee1591 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -432,6 +432,8 @@ class TestSettings(TestCase): def test_set_user_to_few(self): # add shortcut user_count = self.user_mdl.objects.count + # enable testing mode + settings.TESTING_ENV = True # nothing set self.assertEqual(user_count(), 0) @@ -455,3 +457,6 @@ class TestSettings(TestCase): with self.assertRaises(IntegrationPluginError): self.run_reload() self.assertEqual(user_count(), 1) + + # make sure to clean up + settings.TESTING_ENV = False From 5cc214dfbccf682ee4bc3c06835b23344cce563a Mon Sep 17 00:00:00 2001 From: Matthias Mair <66015116+matmair@users.noreply.github.com> Date: Tue, 1 Mar 2022 12:23:17 +0100 Subject: [PATCH 15/30] Update settings.py Add comment to explain usage --- InvenTree/InvenTree/settings.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 7291178e4a..069d0d3363 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -36,7 +36,8 @@ def _is_true(x): # Determine if we are running in "test" mode e.g. "manage.py test" -TESTING = 'test' in sys.argv +TESTING = 'test' in sys. +# Are enviroment variables manipulated by tests? Needs to be set by testing code TESTING_ENV = False # New requirement for django 3.2+ From 2f4cae4c9dccb3a9ce954c01c6642bb206b2d927 Mon Sep 17 00:00:00 2001 From: Matthias Mair <66015116+matmair@users.noreply.github.com> Date: Tue, 1 Mar 2022 12:25:04 +0100 Subject: [PATCH 16/30] Update config_template.yaml Change name for config name --- InvenTree/config_template.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 153cf2f16b..65dd20d3e8 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -155,9 +155,9 @@ static_root: '/home/inventree/data/static' #login_attempts: 5 # Add new user on first startup -#set_user: admin -#set_email: info@example.com -#set_password: inventree +#admin_user: admin +#admin_email: info@example.com +#admin_password: inventree # Permit custom authentication backends #authentication_backends: From 484a53ff8e8c0850794203bef5e0c58a74ef07de Mon Sep 17 00:00:00 2001 From: Matthias Mair <66015116+matmair@users.noreply.github.com> Date: Tue, 1 Mar 2022 12:26:16 +0100 Subject: [PATCH 17/30] Update apps.py Update env names --- InvenTree/InvenTree/apps.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index a6116f0ec2..ff95d74345 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -155,16 +155,16 @@ class InvenTreeConfig(AppConfig): # get values add_user = get_setting( - 'INVENTREE_SET_USER', - settings.CONFIG.get('set_user', False) + 'INVENTREE_ADMIN_USER', + settings.CONFIG.get('admin_user', False) ) add_email = get_setting( - 'INVENTREE_SET_EMAIL', - settings.CONFIG.get('set_email', False) + 'INVENTREE_ADMIN_EMAIL', + settings.CONFIG.get('admin_email', False) ) add_password = get_setting( - 'INVENTREE_SET_PASSWORD', - settings.CONFIG.get('set_password', False) + 'INVENTREE_ADMIN_PASSWORD', + settings.CONFIG.get('admin_password', False) ) # check if all values are present From a500e8cf542a71a73ee2bba2cf2114c89676fd4d Mon Sep 17 00:00:00 2001 From: Matthias Mair <66015116+matmair@users.noreply.github.com> Date: Tue, 1 Mar 2022 12:28:31 +0100 Subject: [PATCH 18/30] Fix typo --- InvenTree/InvenTree/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 069d0d3363..d820dfb716 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -36,7 +36,7 @@ def _is_true(x): # Determine if we are running in "test" mode e.g. "manage.py test" -TESTING = 'test' in sys. +TESTING = 'test' in sys.argv # Are enviroment variables manipulated by tests? Needs to be set by testing code TESTING_ENV = False From 56e0b5f98dfb3b00d72e86cdf1820522cd9a9794 Mon Sep 17 00:00:00 2001 From: Matthias Mair <66015116+matmair@users.noreply.github.com> Date: Tue, 1 Mar 2022 12:31:56 +0100 Subject: [PATCH 19/30] Add as superuser --- InvenTree/InvenTree/apps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index ff95d74345..80389bda95 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -188,7 +188,7 @@ class InvenTreeConfig(AppConfig): user = get_user_model() try: with transaction.atomic(): - new_user = user.objects.create_user(add_user, add_email, add_password) + new_user = user.objects.create_superuser(add_user, add_email, add_password) 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)}') From 0957feafa148ef2caccf64c081e72234316cb52f Mon Sep 17 00:00:00 2001 From: Matthias Mair <66015116+matmair@users.noreply.github.com> Date: Tue, 1 Mar 2022 20:12:38 +0100 Subject: [PATCH 20/30] fix test --- InvenTree/InvenTree/tests.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 9014ee1591..cd6986e8d0 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -439,21 +439,21 @@ class TestSettings(TestCase): self.assertEqual(user_count(), 0) # not enough set - self.env.set('INVENTREE_SET_USER', 'admin') # set username + self.env.set('INVENTREE_ADMIN_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.env.set('INVENTREE_ADMIN_USER', 'admin') # set username + self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email + self.env.set('INVENTREE_ADMIN_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 + self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username + self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email + self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password with self.assertRaises(IntegrationPluginError): self.run_reload() self.assertEqual(user_count(), 1) From d018654c9ce53c0db4b28fa71c8396f3b0bdfd73 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 00:17:00 +0100 Subject: [PATCH 21/30] fix settings override? --- InvenTree/InvenTree/tests.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index cd6986e8d0..c31dad4d95 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -2,7 +2,7 @@ import json from test.support import EnvironmentVarGuard -from django.test import TestCase +from django.test import TestCase, override_settings import django.core.exceptions as django_exceptions from django.core.exceptions import ValidationError from django.contrib.auth import get_user_model @@ -429,11 +429,10 @@ class TestSettings(TestCase): settings.USER_ADDED = False registry.reload_plugins() + @override_settings(TESTING_ENV=True) def test_set_user_to_few(self): # add shortcut user_count = self.user_mdl.objects.count - # enable testing mode - settings.TESTING_ENV = True # nothing set self.assertEqual(user_count(), 0) @@ -457,6 +456,3 @@ class TestSettings(TestCase): with self.assertRaises(IntegrationPluginError): self.run_reload() self.assertEqual(user_count(), 1) - - # make sure to clean up - settings.TESTING_ENV = False From 48583f470c607b2cdb5d0c6b4e44bebe58e26cb7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 19:54:40 +0100 Subject: [PATCH 22/30] remove testing code --- InvenTree/InvenTree/tests.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index c31dad4d95..0b50404396 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -24,7 +24,6 @@ import InvenTree.tasks from stock.models import StockLocation from common.settings import currency_codes -from plugin.helpers import IntegrationPluginError class ValidatorTest(TestCase): @@ -436,23 +435,3 @@ class TestSettings(TestCase): # nothing set self.assertEqual(user_count(), 0) - - # not enough set - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.run_reload() - self.assertEqual(user_count(), 0) - - # enough set - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email - self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password - self.run_reload() - self.assertEqual(user_count(), 1) - - # double adding should not work - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email - self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password - with self.assertRaises(IntegrationPluginError): - self.run_reload() - self.assertEqual(user_count(), 1) From 2c6731df0cd6899f7b2159e955d73adc242b76dc Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 22:49:05 +0100 Subject: [PATCH 23/30] add ENVs back in --- InvenTree/InvenTree/tests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 0b50404396..f65f4c3952 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -432,6 +432,11 @@ class TestSettings(TestCase): def test_set_user_to_few(self): # add shortcut user_count = self.user_mdl.objects.count + # enable testing mode + settings.TESTING_ENV = True # nothing set self.assertEqual(user_count(), 0) + + # make sure to clean up + settings.TESTING_ENV = False From d03af8f5b015c26f5360826e0f898491b8a1b1c2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 22:50:36 +0100 Subject: [PATCH 24/30] nothing test --- InvenTree/InvenTree/tests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index f65f4c3952..5a21cbc87b 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -438,5 +438,10 @@ class TestSettings(TestCase): # nothing set self.assertEqual(user_count(), 0) + # not enough set + self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username + self.run_reload() + self.assertEqual(user_count(), 0) + # make sure to clean up settings.TESTING_ENV = False From 7d58db4336b29b7def1f02aeb75262ad836bb558 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 22:52:14 +0100 Subject: [PATCH 25/30] full set again --- InvenTree/InvenTree/tests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 5a21cbc87b..53e8bdb618 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -24,6 +24,7 @@ import InvenTree.tasks from stock.models import StockLocation from common.settings import currency_codes +from plugin.helpers import IntegrationPluginError class ValidatorTest(TestCase): @@ -443,5 +444,20 @@ class TestSettings(TestCase): self.run_reload() self.assertEqual(user_count(), 0) + # enough set + self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username + self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email + self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password + self.run_reload() + self.assertEqual(user_count(), 1) + + # double adding should not work + self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username + self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email + self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password + with self.assertRaises(IntegrationPluginError): + self.run_reload() + self.assertEqual(user_count(), 1) + # make sure to clean up settings.TESTING_ENV = False From c893b81314e210f1fdfaaf397bf7ef7c9f9c025c Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 23:34:44 +0100 Subject: [PATCH 26/30] remove the wrong setting --- InvenTree/InvenTree/tests.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 53e8bdb618..46f23578ba 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -451,13 +451,5 @@ class TestSettings(TestCase): self.run_reload() self.assertEqual(user_count(), 1) - # double adding should not work - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email - self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password - with self.assertRaises(IntegrationPluginError): - self.run_reload() - self.assertEqual(user_count(), 1) - # make sure to clean up settings.TESTING_ENV = False From 941efd08d13154f4168939a3587a9808c70caf41 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 23:36:53 +0100 Subject: [PATCH 27/30] change type of error --- InvenTree/InvenTree/tests.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 46f23578ba..9747a6741f 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -7,6 +7,7 @@ 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 @@ -451,5 +452,13 @@ class TestSettings(TestCase): self.run_reload() self.assertEqual(user_count(), 1) + # double adding should not work + self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username + self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email + self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password + with self.assertRaises(IntegrityError): + self.run_reload() + self.assertEqual(user_count(), 1) + # make sure to clean up settings.TESTING_ENV = False From 771d82bdc20bb03d74b660395147bcba95ec8bcb Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 23:40:46 +0100 Subject: [PATCH 28/30] style fix --- InvenTree/InvenTree/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 9747a6741f..eb6f3e6605 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -25,7 +25,6 @@ import InvenTree.tasks from stock.models import StockLocation from common.settings import currency_codes -from plugin.helpers import IntegrationPluginError class ValidatorTest(TestCase): From a158ecf4016d0ef7963f39ba0a57a7a256e03039 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Mar 2022 23:41:41 +0100 Subject: [PATCH 29/30] reset to previous --- InvenTree/InvenTree/tests.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index eb6f3e6605..571a4c5012 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -7,7 +7,6 @@ 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 @@ -451,13 +450,5 @@ class TestSettings(TestCase): self.run_reload() self.assertEqual(user_count(), 1) - # double adding should not work - self.env.set('INVENTREE_ADMIN_USER', 'admin') # set username - self.env.set('INVENTREE_ADMIN_EMAIL', 'info@example.com') # set email - self.env.set('INVENTREE_ADMIN_PASSWORD', 'password123') # set password - with self.assertRaises(IntegrityError): - self.run_reload() - self.assertEqual(user_count(), 1) - # make sure to clean up settings.TESTING_ENV = False From e11fa338b67032a62bb131dd5ada1498cecbc30a Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 3 Mar 2022 00:52:10 +0100 Subject: [PATCH 30/30] also test empty --- InvenTree/InvenTree/tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 571a4c5012..f89a8b073d 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -436,6 +436,7 @@ class TestSettings(TestCase): settings.TESTING_ENV = True # nothing set + self.run_reload() self.assertEqual(user_count(), 0) # not enough set