2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-18 10:46:31 +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)

View File

@@ -3,6 +3,7 @@ import { apiUrl } from '@lib/functions/Api';
import { t } from '@lingui/core/macro';
import { Trans } from '@lingui/react/macro';
import {
Alert,
Anchor,
Button,
Divider,
@@ -10,6 +11,7 @@ import {
Loader,
PasswordInput,
Stack,
Text,
TextInput,
VisuallyHidden
} from '@mantine/core';
@@ -30,6 +32,7 @@ import { showLoginNotification } from '../../functions/notifications';
import { useServerApiState } from '../../states/ServerApiState';
import { useUserState } from '../../states/UserState';
import { SsoButton } from '../buttons/SSOButton';
import { errorCodeLink } from '../nav/Alerts';
export function AuthenticationForm() {
const classicForm = useForm({
@@ -341,6 +344,12 @@ export function RegistrationForm() {
))}
</Group>
)}
{!registration_enabled() && !sso_registration() && (
<Alert title={t`Registration not active`} color='orange'>
<Text>{t`This might be related to missing mail settings or could be a deliberate decision.`}</Text>
{errorCodeLink('INVE-W11')}
</Alert>
)}
</>
);
}