Skip to content

Commit

Permalink
Merge pull request #4521 from maarcingebala/fix-updating-the-customer
Browse files Browse the repository at this point in the history
Fix generating random avatars when updating staff accounts
  • Loading branch information
maarcingebala committed Jul 25, 2019
2 parents 7307320 + 6171e5b commit 7b3de41
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ All notable, unreleased changes to this project will be documented in this file.
- Create general abstraction for object metadata - #4447 by @salwator
- Contrast improvements - #4508 by @benekex2
- Allow selecting the number of rows displayed in dashboard's list views - #4414 by @benekex2
- Fix generating random avatars when updating staff accounts - #4521 by @maarcingebala
- Changed license for artwork to CC-BY 4.0


## 2.8.0

### Core
Expand Down
7 changes: 5 additions & 2 deletions saleor/graphql/account/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ def clean_input(cls, info, instance, data):

@classmethod
def save(cls, info, user, cleaned_input):
user.avatar = get_random_avatar()
create_avatar = not user.avatar
if create_avatar:
user.avatar = get_random_avatar()
user.save()
create_user_avatar_thumbnails.delay(user_id=user.pk)
if create_avatar:
create_user_avatar_thumbnails.delay(user_id=user.pk)
if cleaned_input.get("send_password_email"):
send_set_password_staff_email.delay(user.pk)

Expand Down
45 changes: 45 additions & 0 deletions tests/api/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from saleor.account import events as account_events
from saleor.account.models import Address, User
from saleor.account.utils import get_random_avatar
from saleor.checkout import AddressType
from saleor.graphql.account.mutations import (
CustomerDelete,
Expand Down Expand Up @@ -1131,6 +1132,50 @@ def test_staff_update(staff_api_client, permission_manage_staff, media_root):
assert not data["user"]["isActive"]


@patch("saleor.graphql.account.mutations.get_random_avatar")
def test_staff_update_doesnt_change_existing_avatar(
mock_get_random_avatar, staff_api_client, permission_manage_staff, media_root
):
query = """
mutation UpdateStaff(
$id: ID!, $permissions: [PermissionEnum], $is_active: Boolean) {
staffUpdate(
id: $id,
input: {permissions: $permissions, isActive: $is_active}) {
errors {
field
message
}
}
}
"""

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(
query, variables, permissions=[permission_manage_staff]
)
content = get_graphql_content(response)
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


def test_staff_delete(staff_api_client, permission_manage_staff):
query = """
mutation DeleteStaff($id: ID!) {
Expand Down

0 comments on commit 7b3de41

Please sign in to comment.