Repository: saleor/saleor

Issue:
Drop creating an avatar in create and update staff mutations.
### What I'm trying to achieve
Drop creating an avatar in creating and update staff mutations.

### Steps to reproduce the problem
1. Create a staff user without avatar or update user without avatar
2. Avatar is created

### What I expected to happend
Don't create an avatar when the user doesn't have one.

### Screenshots
<!-- If applicable, add screenshots to help explain your problem. -->

**System information**
Operating system:



Discussion:

Resolving patch:
diff --git a/saleor/account/utils.py b/saleor/account/utils.py
--- a/saleor/account/utils.py
+++ b/saleor/account/utils.py
@@ -1,10 +1,6 @@
-import os
-import random
-
 import jwt
 from django.conf import settings
 from django.core.exceptions import ValidationError
-from django.core.files import File
 from django.utils import timezone
 
 from ..account.error_codes import AccountErrorCode
@@ -13,10 +9,6 @@
 from ..plugins.manager import get_plugins_manager
 from .models import User
 
-AVATARS_PATH = os.path.join(
-    settings.PROJECT_ROOT, "saleor", "static", "images", "avatars"
-)
-
 
 def store_user_address(user, address, address_type):
     """Add address to user address book and set as default one."""
@@ -64,7 +56,6 @@ def create_superuser(credentials):
         defaults={"is_active": True, "is_staff": True, "is_superuser": True},
     )
     if created:
-        user.avatar = get_random_avatar()
         user.set_password(credentials["password"])
         user.save()
         create_thumbnails(
@@ -76,13 +67,6 @@ def create_superuser(credentials):
     return msg
 
 
-def get_random_avatar():
-    """Return random avatar picked from a pool of static avatars."""
-    avatar_name = random.choice(os.listdir(AVATARS_PATH))
-    avatar_path = os.path.join(AVATARS_PATH, avatar_name)
-    return File(open(avatar_path, "rb"), name=avatar_name)
-
-
 def remove_staff_member(staff):
     """Remove staff member account only if it has no orders placed.
 
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -9,7 +9,7 @@
 from ....account.emails import send_set_password_email_with_url
 from ....account.error_codes import AccountErrorCode
 from ....account.thumbnails import create_user_avatar_thumbnails
-from ....account.utils import get_random_avatar, remove_staff_member
+from ....account.utils import remove_staff_member
 from ....checkout import AddressType
 from ....core.permissions import AccountPermissions, get_permissions
 from ....core.utils.url import validate_storefront_url
@@ -182,12 +182,7 @@ def clean_input(cls, info, instance, data):
 
     @classmethod
     def save(cls, info, user, cleaned_input):
-        create_avatar = not user.avatar
-        if create_avatar:
-            user.avatar = get_random_avatar()
         user.save()
-        if create_avatar:
-            create_user_avatar_thumbnails.delay(user_id=user.pk)
         if cleaned_input.get("redirect_url"):
             send_set_password_email_with_url(
                 redirect_url=cleaned_input.get("redirect_url"), user=user, staff=True


Test patch:
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -16,7 +16,7 @@
 from saleor.account import events as account_events
 from saleor.account.error_codes import AccountErrorCode
 from saleor.account.models import Address, User
-from saleor.account.utils import create_jwt_token, get_random_avatar
+from saleor.account.utils import create_jwt_token
 from saleor.checkout import AddressType
 from saleor.graphql.account.mutations.base import INVALID_TOKEN
 from saleor.graphql.account.mutations.staff import (
@@ -1389,10 +1389,6 @@ def test_staff_create(
     assert data["user"]["email"] == email
     assert data["user"]["isStaff"]
     assert data["user"]["isActive"]
-    assert re.match(
-        r"http://testserver/media/user-avatars/avatar\d+.*",
-        data["user"]["avatar"]["url"],
-    )
     permissions = data["user"]["permissions"]
     assert permissions[0]["code"] == "MANAGE_PRODUCTS"
 
@@ -1515,9 +1511,8 @@ def test_staff_update(staff_api_client, permission_manage_staff, media_root):
     assert not data["user"]["isActive"]
 
 
-@patch("saleor.graphql.account.mutations.staff.get_random_avatar")
 def test_staff_update_doesnt_change_existing_avatar(
-    mock_get_random_avatar, staff_api_client, permission_manage_staff, media_root
+    staff_api_client, permission_manage_staff, media_root
 ):
     query = """
     mutation UpdateStaff(
@@ -1539,15 +1534,9 @@ def test_staff_update_doesnt_change_existing_avatar(
 
     mock_file = MagicMock(spec=File)
     mock_file.name = "image.jpg"
-    mock_get_random_avatar.return_value = mock_file
 
     staff_user = User.objects.create(email="staffuser@example.com", is_staff=True)
 
-    # Create random avatar
-    staff_user.avatar = get_random_avatar()
-    staff_user.save()
-    original_path = staff_user.avatar.path
-
     id = graphene.Node.to_global_id("User", staff_user.id)
     variables = {"id": id, "permissions": [], "is_active": False}
     response = staff_api_client.post_graphql(
@@ -1557,10 +1546,8 @@ def test_staff_update_doesnt_change_existing_avatar(
     data = content["data"]["staffUpdate"]
     assert data["errors"] == []
 
-    # Make sure that random avatar isn't recreated when there is one already set.
-    mock_get_random_avatar.assert_not_called()
     staff_user.refresh_from_db()
-    assert staff_user.avatar.path == original_path
+    assert not staff_user.avatar
 
 
 def test_staff_delete(staff_api_client, permission_manage_staff):
diff --git a/tests/test_account.py b/tests/test_account.py
--- a/tests/test_account.py
+++ b/tests/test_account.py
@@ -1,10 +1,8 @@
-import re
 from urllib.parse import urlencode
 
 import i18naddress
 import pytest
 from django.core.exceptions import ValidationError
-from django.core.files import File
 from django.http import QueryDict
 from django.template import Context, Template
 from django_countries.fields import Country
@@ -13,7 +11,7 @@
 from saleor.account.i18n import AddressForm
 from saleor.account.models import User
 from saleor.account.templatetags.i18n_address_tags import format_address
-from saleor.account.utils import get_random_avatar, remove_staff_member
+from saleor.account.utils import remove_staff_member
 from saleor.account.validators import validate_possible_number
 
 
@@ -293,12 +291,6 @@ def test_get_full_name(email, first_name, last_name, full_name, address):
     assert user.get_full_name() == full_name
 
 
-def test_get_random_avatar():
-    avatar = get_random_avatar()
-    assert isinstance(avatar, File)
-    assert re.match(r"avatar\d+.png", avatar.name)
-
-
 def test_remove_staff_member_with_orders(staff_user, permission_manage_products, order):
     order.user = staff_user
     order.save()
diff --git a/tests/test_core.py b/tests/test_core.py
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -99,7 +99,7 @@ def test_create_superuser(db, client, media_root):
     assert User.objects.all().count() == 1
     admin = User.objects.all().first()
     assert admin.is_superuser
-    assert admin.avatar
+    assert not admin.avatar
     # Test duplicating
     create_superuser(credentials)
     assert User.objects.all().count() == 1