Skip to content

Commit

Permalink
Fix factory to better define a user, fix serializer (AnonymousUser do…
Browse files Browse the repository at this point in the history
…esn't have an email)
  • Loading branch information
jkachel committed Feb 4, 2025
1 parent 7021bb0 commit 6131387
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions unified_ecommerce/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
"""

import ulid
from django.contrib.auth.models import Group, User
from factory import LazyFunction, RelatedFactory
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from factory import Faker, LazyFunction, RelatedFactory
from factory.django import DjangoModelFactory
from factory.fuzzy import FuzzyText

User = get_user_model()


class UserFactory(DjangoModelFactory):
"""Factory for Users"""

global_id = Faker("uuid4")
username = LazyFunction(lambda: ulid.new().str)
email = FuzzyText(suffix="@example.com")
first_name = FuzzyText()
last_name = FuzzyText()
is_active = True

profile = RelatedFactory("users.factories.UserProfileFactory", "user")

Expand Down
16 changes: 15 additions & 1 deletion unified_ecommerce/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@
class UserSerializer(serializers.ModelSerializer):
"""Serializer for User model."""

email = serializers.SerializerMethodField()

def get_email(self, instance):
"""Return the email."""
return instance.email if not instance.is_anonymous else None

class Meta:
"""Meta class for serializer."""

model = User
fields = ["id", "username", "email", "first_name", "last_name"]
fields = [
"id",
"global_id",
"username",
"email",
"first_name",
"last_name",
"name",
]
1 change: 1 addition & 0 deletions users/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_current_user(user, client, user_client):
assert resp.json() == {
"id": None,
"username": "",
"email": None,
}

resp = user_client.get(me_api)
Expand Down

0 comments on commit 6131387

Please sign in to comment.