-
-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix REST registration endpoint (#8738) * Re-add html account base Fixes #8690 * fix base template * override dj-rest-auth pattern to fix fixed token model reference * pin req * fix urls.py * move definition out to separate file * fix possible issues where email is not enabled but UI shows that registration is enabled * fix import order * fix token recovery * make sure registration redirects * fix name change * fix import name * adjust description * cleanup * bum api version * add test for registration * add test for registration requirements * fix merge issues * fix merge from #8724
- Loading branch information
Showing
10 changed files
with
216 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Overrides for registration view.""" | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from allauth.account import app_settings as allauth_account_settings | ||
from dj_rest_auth.app_settings import api_settings | ||
from dj_rest_auth.registration.views import RegisterView | ||
|
||
|
||
class CustomRegisterView(RegisterView): | ||
"""Registers a new user. | ||
Accepts the following POST parameters: username, email, password1, password2. | ||
""" | ||
|
||
# Fixes https://github.com/inventree/InvenTree/issues/8707 | ||
# This contains code from dj-rest-auth 7.0 - therefore the version was pinned | ||
def get_response_data(self, user): | ||
"""Override to fix check for auth_model.""" | ||
if ( | ||
allauth_account_settings.EMAIL_VERIFICATION | ||
== allauth_account_settings.EmailVerificationMethod.MANDATORY | ||
): | ||
return {'detail': _('Verification e-mail sent.')} | ||
|
||
if api_settings.USE_JWT: | ||
data = { | ||
'user': user, | ||
'access': self.access_token, | ||
'refresh': self.refresh_token, | ||
} | ||
return api_settings.JWT_SERIALIZER( | ||
data, context=self.get_serializer_context() | ||
).data | ||
elif self.token_model: | ||
# Only change in this block is below | ||
return api_settings.TOKEN_SERIALIZER( | ||
user.api_tokens.last(), context=self.get_serializer_context() | ||
).data | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
"""Test the sso module functionality.""" | ||
"""Test the sso and auth module functionality.""" | ||
|
||
from django.conf import settings | ||
from django.contrib.auth.models import Group, User | ||
from django.core.exceptions import ValidationError | ||
from django.test import override_settings | ||
from django.test.testcases import TransactionTestCase | ||
|
||
|
@@ -9,6 +11,7 @@ | |
from common.models import InvenTreeSetting | ||
from InvenTree import sso | ||
from InvenTree.forms import RegistratonMixin | ||
from InvenTree.unit_test import InvenTreeAPITestCase | ||
|
||
|
||
class Dummy: | ||
|
@@ -119,3 +122,90 @@ def test_sso_group_created_if_not_exists(self): | |
self.assertEqual(Group.objects.filter(name='inventree_group').count(), 0) | ||
sso.ensure_sso_groups(None, self.sociallogin) | ||
self.assertEqual(Group.objects.filter(name='inventree_group').count(), 1) | ||
|
||
|
||
class EmailSettingsContext: | ||
"""Context manager to enable email settings for tests.""" | ||
|
||
def __enter__(self): | ||
"""Enable stuff.""" | ||
InvenTreeSetting.set_setting('LOGIN_ENABLE_REG', True) | ||
settings.EMAIL_HOST = 'localhost' | ||
|
||
def __exit__(self, type, value, traceback): | ||
"""Exit stuff.""" | ||
InvenTreeSetting.set_setting('LOGIN_ENABLE_REG', False) | ||
settings.EMAIL_HOST = '' | ||
|
||
|
||
class TestAuth(InvenTreeAPITestCase): | ||
"""Test authentication functionality.""" | ||
|
||
def email_args(self, user=None, email=None): | ||
"""Generate registration arguments.""" | ||
return { | ||
'username': user or 'user1', | ||
'email': email or '[email protected]', | ||
'password1': '#asdf1234', | ||
'password2': '#asdf1234', | ||
} | ||
|
||
def test_registration(self): | ||
"""Test the registration process.""" | ||
self.logout() | ||
|
||
# Duplicate username | ||
resp = self.post( | ||
'/api/auth/registration/', | ||
self.email_args(user='testuser'), | ||
expected_code=400, | ||
) | ||
self.assertIn( | ||
'A user with that username already exists.', resp.data['username'] | ||
) | ||
|
||
# Registration is disabled | ||
resp = self.post( | ||
'/api/auth/registration/', self.email_args(), expected_code=400 | ||
) | ||
self.assertIn('Registration is disabled.', resp.data['non_field_errors']) | ||
|
||
# Enable registration - now it should work | ||
with EmailSettingsContext(): | ||
resp = self.post( | ||
'/api/auth/registration/', self.email_args(), expected_code=201 | ||
) | ||
self.assertIn('key', resp.data) | ||
|
||
def test_registration_email(self): | ||
"""Test that LOGIN_SIGNUP_MAIL_RESTRICTION works.""" | ||
self.logout() | ||
|
||
# Check the setting validation is working | ||
with self.assertRaises(ValidationError): | ||
InvenTreeSetting.set_setting( | ||
'LOGIN_SIGNUP_MAIL_RESTRICTION', 'example.com,inventree.org' | ||
) | ||
|
||
# Setting setting correctly | ||
correct_setting = '@example.com,@inventree.org' | ||
InvenTreeSetting.set_setting('LOGIN_SIGNUP_MAIL_RESTRICTION', correct_setting) | ||
self.assertEqual( | ||
InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_RESTRICTION'), | ||
correct_setting, | ||
) | ||
|
||
# Wrong email format | ||
resp = self.post( | ||
'/api/auth/registration/', | ||
self.email_args(email='[email protected]'), | ||
expected_code=400, | ||
) | ||
self.assertIn('The provided email domain is not approved.', resp.data['email']) | ||
|
||
# Right format should work | ||
with EmailSettingsContext(): | ||
resp = self.post( | ||
'/api/auth/registration/', self.email_args(), expected_code=201 | ||
) | ||
self.assertIn('key', resp.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters