2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-09-14 06:31:27 +00:00

feat(backend): improve comms around registration (#9932)

* use better email check

* add error code for missing email settings

* Add note when not registration is enabled

* fix wrong call

* fix tests - might be worth checking if the blank check for testing is a good idea

* capture issues and print to find out what the duck is going on

* exend log

* fix context check
This commit is contained in:
Matthias Mair
2025-07-13 01:20:45 +02:00
committed by GitHub
parent a2e06d1fee
commit b351fb7483
6 changed files with 37 additions and 6 deletions

View File

@@ -20,6 +20,8 @@ from common.settings import get_global_setting
from InvenTree.exceptions import log_error
from users.models import ApiToken
from .helpers_email import is_email_configured
logger = structlog.get_logger('inventree')
@@ -91,11 +93,11 @@ def registration_enabled():
get_global_setting('LOGIN_ENABLE_REG')
or InvenTree.sso.sso_registration_enabled()
):
if settings.EMAIL_HOST:
if is_email_configured():
return True
else:
logger.warning(
'Registration cannot be enabled, because EMAIL_HOST is not configured.'
'INVE-W11: Registration cannot be enabled, because EMAIL_HOST is not configured.'
)
return False

View File

@@ -14,16 +14,18 @@ from common.models import Priority, issue_mail
logger = structlog.get_logger('inventree')
def is_email_configured():
def is_email_configured() -> bool:
"""Check if email backend is configured.
Fails on tests nominally, if no bypassed via settings.TESTING_BYPASS_MAILCHECK.
NOTE: This does not check if the configuration is valid!
"""
configured = True
testing = settings.TESTING
if InvenTree.ready.isInTestMode():
return False
return settings.TESTING_BYPASS_MAILCHECK
if InvenTree.ready.isImportingData():
return False

View File

@@ -62,6 +62,8 @@ if TESTING:
# Are environment variables manipulated by tests? Needs to be set by testing code
TESTING_ENV = False
# Are we bypassing exceptions?
TESTING_BYPASS_MAILCHECK = False # Bypass email disablement for tests
# New requirement for django 3.2+
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

View File

@@ -182,7 +182,10 @@ class TestAuth(InvenTreeAPITestCase):
self.post(self.reg_url, self.email_args(), expected_code=403)
# Enable registration - now it should work
with self.settings(EMAIL_HOST='localhost') as _, EmailSettingsContext() as _:
with (
self.settings(EMAIL_HOST='localhost', TESTING_BYPASS_MAILCHECK=True) as _,
EmailSettingsContext() as _,
):
resp = self.post(self.reg_url, self.email_args(), expected_code=200)
self.assertEqual(resp.json()['data']['user']['email'], self.test_email)
@@ -213,6 +216,9 @@ class TestAuth(InvenTreeAPITestCase):
self.assertIn('The provided email domain is not approved.', str(resp.json()))
# Right format should work
with self.settings(EMAIL_HOST='localhost') as _, EmailSettingsContext() as _:
with (
self.settings(EMAIL_HOST='localhost', TESTING_BYPASS_MAILCHECK=True) as _,
EmailSettingsContext() as _,
):
resp = self.post(self.reg_url, self.email_args(), expected_code=200)
self.assertEqual(resp.json()['data']['user']['email'], self.test_email)