Skip to content

Commit

Permalink
Remove deprecated behavior (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Mar 29, 2023
1 parent d646b80 commit 121c9d4
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 231 deletions.
9 changes: 1 addition & 8 deletions rest_registration/api/views/register_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ def post(self, request: Request) -> Response:
user = request.user
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)

# TODO: Issue #114 - remove code supporting deprecated behavior
get_email = getattr(serializer, 'get_email', None)
if callable(get_email):
email = get_email()
else:
email = serializer.validated_data['email']

email = serializer.validated_data['email']
email_already_used = (
is_user_email_field_unique()
and user_with_email_exists(email))
Expand Down
47 changes: 0 additions & 47 deletions rest_registration/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,53 +320,6 @@ def valid_login_authentication_backend_check() -> None:
get_login_authentication_backend()


# TODO: Issue #114 - remove deprecation check
@register()
@predicate_check(
'LOGIN_SERIALIZER_CLASS contains deprecated get_authenticated_user method,'
' which will be removed in version 0.7.0;'
' for a replacement, please refer to LOGIN_AUTHENTICATOR setting',
WarningCode.DEPRECATION
)
def deprecated_login_serializer_check() -> bool:
serializer_class = registration_settings.LOGIN_SERIALIZER_CLASS
deprecated = (
hasattr(serializer_class, 'get_authenticated_user')
and callable(serializer_class.get_authenticated_user))
return not deprecated


# TODO: Issue #114 - remove deprecation check
@register()
@predicate_check(
'SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS contains deprecated'
' get_user_or_none method, which will be removed in version 0.7.0;'
' for a replacement, please refer to SEND_RESET_PASSWORD_LINK_USER_FINDER setting',
WarningCode.DEPRECATION
)
def deprecated_send_reset_password_link_serializer_check() -> bool:
serializer_class = registration_settings.SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS
deprecated = (
hasattr(serializer_class, 'get_user_or_none')
and callable(serializer_class.get_user_or_none))
return not deprecated


# TODO: Issue #114 - remove deprecation check
@register()
@predicate_check(
'REGISTER_EMAIL_SERIALIZER_CLASS contains deprecated get_email method,'
' which will be removed in version 0.7.0',
WarningCode.DEPRECATION
)
def deprecated_register_email_serializer_check() -> bool:
serializer_class = registration_settings.REGISTER_EMAIL_SERIALIZER_CLASS
deprecated = (
hasattr(serializer_class, 'get_email')
and callable(serializer_class.get_email))
return not deprecated


@register()
@predicate_check(
'REGISTER_EMAIL_SERIALIZER_CLASS does not contain email field',
Expand Down
20 changes: 0 additions & 20 deletions rest_registration/utils/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@

def authenticate_by_login_data(
data: Dict[str, Any], **kwargs: Any) -> 'AbstractBaseUser':
serializer = kwargs.get('serializer')
if serializer:
# TODO: Issue #114 - remove code supporting deprecated behavior
get_authenticated_user = getattr(
serializer, 'get_authenticated_user', None)
if callable(get_authenticated_user):
user = get_authenticated_user() # type: Optional[AbstractBaseUser]
if not user:
raise UserNotFound()
return user

login_field_names = get_user_login_field_names()
username_field_name = get_username_field_name()
password = data.get('password')
Expand Down Expand Up @@ -115,15 +104,6 @@ def find_user_by_by_send_reset_password_link_data(
Return user if matching given criteria (login fields / e-mail).
Return ``None`` otherwise.
"""
serializer = kwargs.get('serializer')
if serializer:
# TODO: Issue #114 - remove code supporting deprecated behavior
get_user_or_none = getattr(serializer, 'get_user_or_none', None)
if callable(get_user_or_none):
user = get_user_or_none() # type: Optional[AbstractBaseUser]
if not user:
raise UserNotFound()
return user
login_field_names = get_user_login_field_names()
finder_tests = [('login', login_field_names)]
finder_tests.extend((f, [f]) for f in login_field_names)
Expand Down
31 changes: 0 additions & 31 deletions tests/api/views/login/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,6 @@ def test_invalid_non_field_errors(
assert_response_is_bad_request(response)


@override_rest_registration_settings({
'LOGIN_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedLoginSerializer', # noqa: E501
})
def test_when_deprecated_login_serializer_then_success(
settings_minimal, user,
api_view_provider, api_factory):
request = api_factory.create_post_request({
'login': 'abra',
'password': 'cadabra',
})
api_factory.add_session_to_request(request)
response = api_view_provider.view_func(request)
assert_response_is_ok(response)


# TODO: Issue #114 - remove code testing support of deprecated behavior
@override_rest_registration_settings({
'LOGIN_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedLoginSerializer', # noqa: E501
})
def test_when_deprecated_login_serializer_and_invalid_creds_then_failure(
settings_minimal, user,
api_view_provider, api_factory):
request = api_factory.create_post_request({
'login': 'abra',
'password': 'blabla',
})
api_factory.add_session_to_request(request)
response = api_view_provider.view_func(request)
assert_response_is_bad_request(response)


@override_rest_registration_settings({
'USER_LOGIN_FIELDS': ['username', 'email'],
})
Expand Down
18 changes: 0 additions & 18 deletions tests/api/views/register_email/test_register_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,24 +298,6 @@ def test_register_email_fail_email_already_used(
assert "detail" in response.data


@override_rest_registration_settings({
'REGISTER_EMAIL_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedRegisterEmailSerializer', # noqa: E501
})
def test_ok_when_deprecated_register_email_serializer(
user,
api_view_provider, api_factory):
request = api_factory.create_post_request({
'email': 'not@used.com',
})
force_authenticate(request, user=user)
with capture_sent_emails() as sent_emails:
response = api_view_provider.view_func(request)
assert_response_is_ok(response)
assert_no_email_sent(sent_emails)
user.refresh_from_db()
assert user.email == 'abra@cadabra.com'


@override_rest_registration_settings({
'VERIFICATION_TEMPLATES_SELECTOR': 'tests.testapps.custom_templates.utils.select_verification_templates', # noqa E501
})
Expand Down
13 changes: 0 additions & 13 deletions tests/api/views/reset_password/test_send_reset_password_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,6 @@ def test_ok_when_custom_verification_templates_selector(
assert_valid_send_link_email(sent_email, user, timer)


@override_rest_registration_settings({
'SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedSendResetPasswordLinkSerializer', # noqa: E501
})
def test_when_deprecated_send_reset_password_link_serializer_then_success(
settings_with_reset_password_verification, user,
api_view_provider, api_factory):
request = api_factory.create_post_request({
'login': 'abra',
})
response = api_view_provider.view_func(request)
assert_response_is_ok(response)


def assert_valid_send_link_email(sent_email, user, timer):
assert sent_email.from_email == VERIFICATION_FROM_EMAIL
assert sent_email.to == [user.email]
Expand Down
45 changes: 0 additions & 45 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,51 +405,6 @@ def test_fail_when_login_auth_backend_not_in_multiple_auth_backends():
assert {e.msg for e in errors} == expected_messages


# TODO: Issue #114 - remove code testing the deprecation check
@override_rest_registration_settings({
'LOGIN_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedLoginSerializer', # noqa: E501
})
def test_deprecated_login_serializer_check_fails():
errors = simulate_checks()
assert_error_codes_match(errors, [
WarningCode.DEPRECATION,
])
expected_messages = {
"LOGIN_SERIALIZER_CLASS contains deprecated get_authenticated_user method, which will be removed in version 0.7.0; for a replacement, please refer to LOGIN_AUTHENTICATOR setting", # noqa: E501
}
assert {e.msg for e in errors} == expected_messages


# TODO: Issue #114 - remove code testing the deprecation check
@override_rest_registration_settings({
'SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedSendResetPasswordLinkSerializer', # noqa: E501
})
def test_deprecated_send_reset_password_link_serializer_check_fails():
errors = simulate_checks()
assert_error_codes_match(errors, [
WarningCode.DEPRECATION,
])
expected_messages = {
"SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS contains deprecated get_user_or_none method, which will be removed in version 0.7.0; for a replacement, please refer to SEND_RESET_PASSWORD_LINK_USER_FINDER setting", # noqa: E501
}
assert {e.msg for e in errors} == expected_messages


# TODO: Issue #114 - remove code testing the deprecation check
@override_rest_registration_settings({
'REGISTER_EMAIL_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.DefaultDeprecatedRegisterEmailSerializer', # noqa: E501
})
def test_deprecated_register_email_serializer_check_fails():
errors = simulate_checks()
assert_error_codes_match(errors, [
WarningCode.DEPRECATION,
])
expected_messages = {
"REGISTER_EMAIL_SERIALIZER_CLASS contains deprecated get_email method, which will be removed in version 0.7.0", # noqa: E501
}
assert {e.msg for e in errors} == expected_messages


@override_rest_registration_settings({
'REGISTER_EMAIL_SERIALIZER_CLASS': 'tests.testapps.custom_serializers.serializers.InvalidRegisterEmailSerializer', # noqa: E501
})
Expand Down
49 changes: 0 additions & 49 deletions tests/testapps/custom_serializers/serializers.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,6 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers


class DefaultDeprecatedLoginSerializer(serializers.Serializer):
login = serializers.CharField()
password = serializers.CharField()

def get_authenticated_user(self):
data = self.validated_data
if data['login'] == 'abra' and data['password'] == 'cadabra':
user_model = get_user_model()
return user_model.objects.get()
return None

def create(self, validated_data):
pass

def update(self, instance, validated_data):
pass


class DefaultDeprecatedSendResetPasswordLinkSerializer(serializers.Serializer):
login = serializers.CharField(required=True)

def get_user_or_none(self):
data = self.validated_data
if data['login'] == 'abra':
user_model = get_user_model()
return user_model.objects.get()
return None

def create(self, validated_data):
pass

def update(self, instance, validated_data):
pass


class DefaultDeprecatedRegisterEmailSerializer(serializers.Serializer):
email = serializers.EmailField(required=True)

def get_email(self):
return 'abra@cadabra.com'

def create(self, validated_data):
pass

def update(self, instance, validated_data):
pass


class InvalidRegisterEmailSerializer(serializers.Serializer):
definitely_not_email = serializers.EmailField(required=True)

Expand Down

0 comments on commit 121c9d4

Please sign in to comment.