Skip to content

Instantly share code, notes, and snippets.

@ephes
Created August 16, 2019 09:38
Show Gist options
  • Save ephes/f3b43c18ea26d49670baf866d49df889 to your computer and use it in GitHub Desktop.
Save ephes/f3b43c18ea26d49670baf866d49df889 to your computer and use it in GitHub Desktop.
import pytest
from django.conf import settings
from python_podcast.users.tests.factories import UserFactory
@pytest.fixture
def user() -> settings.AUTH_USER_MODEL:
password = "password"
user = UserFactory(password=password)
user._password = password
return user
from typing import Any, Sequence
from django.contrib.auth import get_user_model
from factory import DjangoModelFactory, Faker, post_generation
class UserFactory(DjangoModelFactory):
username = Faker("user_name")
email = Faker("email")
name = Faker("name")
@post_generation
def password(self, create: bool, extracted: Sequence[Any], **kwargs):
print("args: ", self, create, extracted, kwargs)
password = extracted if extracted is not None else self.get_fake_password()
if extracted is not None:
password = extracted
else:
password = Faker(
"password",
length=42,
special_chars=True,
digits=True,
upper_case=True,
lower_case=True,
).generate(
extra_kwargs={}
)
self.set_password(password)
class Meta:
model = get_user_model()
django_get_or_create = ["username"]
import pytest
@pytest.mark.django_db
def test_list_archive_owner_with_authorization(self, client, user):
# login
r = client.login(username=user.username, password=user._password)
test_url = reverse("appname:list")
r = client.get(test_url)
assert r.status_code == 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment