Last active
May 24, 2021 06:45
-
-
Save rririanto/ba31e58a5ae03268ee4acda3bd4fa6d3 to your computer and use it in GitHub Desktop.
Pytest for user models
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
import pytest | |
import copy | |
from apps.user.models import User | |
from django.forms.models import model_to_dict | |
from typing import Any, Dict | |
pytestmark = pytest.mark.django_db | |
TEST_USERS = { | |
"admin": dict( | |
first_name="Admin", | |
password="admin", | |
is_superuser=True, | |
is_staff=True, | |
email="[email protected]" | |
), | |
"member1": dict( | |
first_name="Member1", | |
password="member1", | |
email="[email protected]", | |
is_staff=False), | |
"member2": dict( | |
first_name="Member2", | |
password="member2", | |
email="[email protected]", | |
is_staff=False), | |
"member3": dict( | |
first_name="Member3", | |
password="member3", | |
email="[email protected]", | |
is_staff=False) | |
} | |
def _get_user(username) -> Dict[Any, Any]: | |
user_dict = copy.deepcopy(TEST_USERS[username]) | |
user_dict.update({"user": _require_user(username=username, **user_dict)}) | |
return user_dict | |
@pytest.fixture(params=["member1", "member2", "member3"]) | |
def request_users(request) -> Dict [Any, Any]: | |
return _get_user(request.param) | |
@pytest.fixture(params=["admin"]) | |
def request_super_users(request) -> Dict [Any, Any]: | |
return _get_user(request.param) | |
@pytest.fixture(params=TEST_USERS.keys()) | |
def site_users(request) -> Dict [Any, Any]: | |
return _get_user(request.param) | |
def _require_user( | |
username, | |
first_name, | |
password=None, | |
is_superuser=False, | |
email=None, | |
is_staff=False, | |
) -> Any: | |
"""Helper to get/create a new user.""" | |
email = email if email is not None else "%[email protected]" % username | |
criteria = { | |
"username": username, | |
"first_name": first_name, | |
"email": email, | |
"is_active": True, | |
"is_superuser": is_superuser, | |
"is_staff": is_staff | |
} | |
user, created = User.objects.get_or_create(**criteria) | |
if created: | |
if password is None: | |
user.set_unusable_password() | |
else: | |
user.set_password(password) | |
user.save() | |
return user | |
def test_create_user(request_users) -> Any: | |
user_dict = request_users | |
user_created = user_dict.get('user') | |
assert user_created.email == user_dict.get('email') | |
assert user_created.is_active is True | |
assert user_created.is_staff is False | |
assert user_created.is_superuser is False | |
assert str(user_created) == user_dict.get('first_name').lower() | |
def test_create_superuser(request_super_users) -> Any: | |
user_dict = request_super_users | |
user_created = user_dict.get('user') | |
assert user_created.email == user_dict.get('email') | |
assert user_created.is_active is True | |
assert user_created.is_staff is True | |
assert user_created.is_superuser is True | |
assert str(user_created) == user_dict.get('first_name').lower() | |
def test_attribute_count() -> None: | |
user_dict = model_to_dict(User(), fields=[field.name for field in User._meta.fields]) | |
assert len(user_dict.keys()) == 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment