Skip to content

Commit

Permalink
Migrate remaining tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Aug 31, 2024
1 parent b0b14ac commit 7c677bf
Show file tree
Hide file tree
Showing 51 changed files with 2,976 additions and 2,641 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ exclude = [
]

[tool.isort]
profile = "black"
line_length = 88
skip = ["migrations", "examples", ".venv", ".tox"]
known_third_party = "django_dynamic_fixture"
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements-ci.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ flake8-comprehensions
flake8-debugger
flake8-isort
flake8-pyproject
flake8-pytest-style
flake8-print
flake8-tuple
isort[pyproject]
Expand Down
4 changes: 4 additions & 0 deletions requirements/requirements-ci.lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ flake8-debugger==4.1.2
# via -r requirements/requirements-ci.in
flake8-isort==6.1.1
# via -r requirements/requirements-ci.in
flake8-plugin-utils==1.3.3
# via flake8-pytest-style
flake8-print==5.0.0
# via -r requirements/requirements-ci.in
flake8-pyproject==1.2.3
# via -r requirements/requirements-ci.in
flake8-pytest-style==2.0.0
# via -r requirements/requirements-ci.in
flake8-tuple==0.4.1
# via -r requirements/requirements-ci.in
h11==0.14.0
Expand Down
6 changes: 6 additions & 0 deletions requirements/requirements-dev.lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,16 @@ flake8-debugger==4.1.2
# via -r requirements/requirements-ci.lock.txt
flake8-isort==6.1.1
# via -r requirements/requirements-ci.lock.txt
flake8-plugin-utils==1.3.3
# via
# -r requirements/requirements-ci.lock.txt
# flake8-pytest-style
flake8-print==5.0.0
# via -r requirements/requirements-ci.lock.txt
flake8-pyproject==1.2.3
# via -r requirements/requirements-ci.lock.txt
flake8-pytest-style==2.0.0
# via -r requirements/requirements-ci.lock.txt
flake8-tuple==0.4.1
# via -r requirements/requirements-ci.lock.txt
h11==0.14.0
Expand Down
2 changes: 1 addition & 1 deletion rest_registration/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_registration.utils.validation import (
run_validators,
validate_user_password,
validate_user_password_confirm
validate_user_password_confirm,
)


Expand Down
2 changes: 1 addition & 1 deletion rest_registration/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
reset_password,
send_reset_password_link,
verify_email,
verify_registration
verify_registration,
)

app_name = 'rest_registration'
Expand Down
2 changes: 1 addition & 1 deletion rest_registration/api/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from rest_registration.utils.users import (
get_user_by_verification_id,
get_user_email_field_name,
get_user_setting
get_user_setting,
)
from rest_registration.utils.verification import verify_signer_or_bad_request

Expand Down
2 changes: 1 addition & 1 deletion rest_registration/api/views/register_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_user_by_verification_id,
get_user_email_field_name,
is_user_email_field_unique,
user_with_email_exists
user_with_email_exists,
)
from rest_registration.utils.verification import verify_signer_or_bad_request

Expand Down
2 changes: 1 addition & 1 deletion rest_registration/api/views/reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from rest_registration.utils.validation import (
run_validators,
validate_password_with_user_id,
validate_user_password_confirm
validate_user_password_confirm,
)
from rest_registration.utils.verification import verify_signer_or_bad_request

Expand Down
10 changes: 6 additions & 4 deletions rest_registration/auth_token_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ def get_app_names(self) -> Sequence[str]:
]

def provide_token(self, user: 'AbstractBaseUser') -> AuthToken:
from rest_framework.authtoken.models import \
Token # noqa: E501 pylint: disable=import-outside-toplevel
from rest_framework.authtoken.models import ( # noqa: E501 pylint: disable=import-outside-toplevel
Token,
)

token_obj, _ = Token.objects.get_or_create(user=user)
return AuthToken(token_obj.key)

def revoke_token(
self, user: 'AbstractBaseUser', *,
token: Optional[AuthToken] = None) -> None:
from rest_framework.authtoken.models import \
Token # noqa: E501 pylint: disable=import-outside-toplevel
from rest_framework.authtoken.models import ( # noqa: E501 pylint: disable=import-outside-toplevel
Token,
)

try:
token_obj: Token = Token.objects.get(user_id=user.pk)
Expand Down
2 changes: 1 addition & 1 deletion rest_registration/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_user_email_field_name,
get_user_login_field_names,
get_user_setting,
is_model_field_unique
is_model_field_unique,
)

if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion rest_registration/contrib/verification_redirects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_registration.api.views.register_email import process_verify_email_data
from rest_registration.api.views.reset_password import process_reset_password_data
from rest_registration.contrib.verification_redirects.settings import (
verification_redirects_settings
verification_redirects_settings,
)


Expand Down
5 changes: 3 additions & 2 deletions rest_registration/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
class _BaseCheckCodeMixin(CheckCode): # noqa: E501 pylint: disable=abstract-method

def get_app_name(self) -> str:
from rest_registration.apps import \
RestRegistrationConfig # noqa: E501 pylint: disable=import-outside-toplevel, cyclic-import
from rest_registration.apps import ( # noqa: E501 pylint: disable=import-outside-toplevel, cyclic-import
RestRegistrationConfig,
)

return RestRegistrationConfig.name

Expand Down
2 changes: 1 addition & 1 deletion rest_registration/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .email import ( # noqa: F401
create_verification_notification,
send_notification,
send_verification_notification
send_verification_notification,
)
8 changes: 6 additions & 2 deletions rest_registration/utils/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def convert_html_to_text(value: str, preserve_urls: bool = False) -> str:
return stripper.get_data()


class MLStripperParseFailed(ValueError):
pass


class MLStripper(HTMLParser):

def __init__(self, preserve_urls: bool = False) -> None:
Expand All @@ -29,7 +33,7 @@ def parse_marked_section(self, i, report=1):
except AssertionError as exc:
# rephrase confusing assertion error introduced by:
# https://bugs.python.org/issue38573
raise ValueError(str(exc)) from exc
raise MLStripperParseFailed(str(exc)) from exc

def handle_starttag(self, tag: str, attrs: List[Tuple[str, Optional[str]]]) -> None:
self._tag_info_stack.append(TagInfo(tag, dict(attrs)))
Expand Down Expand Up @@ -58,7 +62,7 @@ def handle_data(self, data: str) -> None:
self._append_segment(data)

def error(self, message: str) -> None:
raise ValueError(f"HTML parse error: {message}")
raise MLStripperParseFailed(f"HTML parse error: {message}")

def get_data(self) -> str:
paragraph_texts = []
Expand Down
1 change: 1 addition & 0 deletions rest_registration/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

if TYPE_CHECKING:
from django_stubs_ext import StrOrPromise

# mypy uses typing_extensions by default (Py 3.8); when importing from typing
# one will get the following error message in mypy:
#
Expand Down
2 changes: 1 addition & 1 deletion rest_registration/utils/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
List,
Tuple,
TypeVar,
Union
Union,
)

from django.contrib import auth
Expand Down
2 changes: 1 addition & 1 deletion rest_registration/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rest_registration.utils.users import (
UserAttrsProxy,
build_initial_user,
get_user_by_verification_id
get_user_by_verification_id,
)

if TYPE_CHECKING:
Expand Down
119 changes: 0 additions & 119 deletions tests/helpers/testcases.py

This file was deleted.

32 changes: 21 additions & 11 deletions tests/unit_tests/api/serializers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_registration.settings import registration_settings


@pytest.fixture()
@pytest.fixture
def serializer():
serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS
return serializer_class(data={})
Expand All @@ -12,25 +12,35 @@ def serializer():
def test_generated_fields(settings_with_register_verification, serializer):
field_names = set(serializer.get_fields())
assert field_names == {
'id', 'username', 'first_name', 'last_name', 'email',
'password', 'password_confirm',
"id",
"username",
"first_name",
"last_name",
"email",
"password",
"password_confirm",
}


def test_simple_email_based_user_generated_fields(
settings_with_register_verification,
settings_with_simple_email_based_user,
serializer):
settings_with_register_verification,
settings_with_simple_email_based_user,
serializer,
):
field_names = set(serializer.get_fields())

assert field_names == {'id', 'email', 'password', 'password_confirm'}
assert field_names == {"id", "email", "password", "password_confirm"}


def test_no_password_generated_fields(
settings_with_register_verification,
settings_with_register_no_confirm,
serializer):
settings_with_register_verification, settings_with_register_no_confirm, serializer
):
field_names = set(serializer.get_fields())
assert field_names == {
'id', 'username', 'first_name', 'last_name', 'email', 'password',
"id",
"username",
"first_name",
"last_name",
"email",
"password",
}
Loading

0 comments on commit 7c677bf

Please sign in to comment.