Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Add test for user manager, causes an error if you pass an empty email…
Browse files Browse the repository at this point in the history
… to the model
  • Loading branch information
aapetrushkin committed Sep 16, 2023
1 parent 8878dc5 commit 790d856
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
27 changes: 26 additions & 1 deletion tests/plugins/identity/user.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import datetime as dt
from typing import Callable, Protocol, TypeAlias, TypedDict, Unpack, cast, final
from collections.abc import Callable
from random import SystemRandom
from typing import Protocol, TypeAlias, TypedDict, Unpack, cast, final

import pytest
from django.utils.crypto import RANDOM_STRING_CHARS, get_random_string
from mimesis import BaseProvider, Field, Locale
from mimesis.schema import Schema

from server.apps.identity.models import User

UserAssertion: TypeAlias = Callable[[str, 'UserData'], None]

min_len, max_len = 10, 20


class FakeProvider(BaseProvider):
"""Represents a fake test data provider."""
Expand Down Expand Up @@ -44,6 +49,20 @@ def user_factory(
**fields,
}

def random_string(
self,
min_default_len: int = min_len,
max_default_len: int = max_len,
) -> str:
"""Create a random string."""
return get_random_string(
length=SystemRandom().randrange(
start=min_default_len,
stop=max_default_len,
),
allowed_chars=RANDOM_STRING_CHARS,
)


class UserData(TypedDict, total=False):
"""
Expand Down Expand Up @@ -134,3 +153,9 @@ def factory(email: str, expected: UserData) -> None:
assert getattr(user, field_name) == data_value

return factory


@pytest.fixture()
def random_string() -> Callable[..., str]:
"""Give a fixture that can generate a string of a given length."""
return FakeProvider().random_string
17 changes: 0 additions & 17 deletions tests/test_apps/test_identity/test_model.py

This file was deleted.

14 changes: 14 additions & 0 deletions tests/test_apps/test_identity/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,17 @@ def test_bad_format_email_required_field(
)
assert response.status_code == HTTPStatus.OK
assert not User.objects.filter(email=post_data['email'])


@pytest.mark.django_db()
def test_user_manager_create_error(random_string):
"""If email is missing, error is called."""
random_user = {
'email': None,
'password': random_string(),
'first_name': random_string(),
'last_name': random_string(),
'phone': random_string(),
}
with pytest.raises(ValueError, match='Users must have an email address'):
User.objects.create_user(**random_user)

0 comments on commit 790d856

Please sign in to comment.