From adaad90e4aa51b6783a35994a5a697596b6197de Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Mon, 19 Jul 2021 15:37:17 +0200 Subject: [PATCH] api: allow changing profile image (#52949) --- src/authentic2/attribute_kinds.py | 5 +++-- tests/test_attribute_kinds.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/authentic2/attribute_kinds.py b/src/authentic2/attribute_kinds.py index 2987fa22..f55a8bc5 100644 --- a/src/authentic2/attribute_kinds.py +++ b/src/authentic2/attribute_kinds.py @@ -322,10 +322,11 @@ DEFAULT_ATTRIBUTE_KINDS = [ 'field_class': fields.ProfileImageField, 'serialize': profile_image_serialize, 'deserialize': profile_image_deserialize, - 'rest_framework_field_class': serializers.FileField, + 'rest_framework_field_class': serializers.ImageField, 'rest_framework_field_kwargs': { - 'read_only': True, 'use_url': True, + 'allow_empty_file': True, + '_DjangoImageField': fields.ProfileImageField, }, 'html_value': profile_image_html_value, 'attributes_ng_serialize': profile_attributes_ng_serialize, diff --git a/tests/test_attribute_kinds.py b/tests/test_attribute_kinds.py index 43d6527e..6ca72280 100644 --- a/tests/test_attribute_kinds.py +++ b/tests/test_attribute_kinds.py @@ -440,6 +440,27 @@ def test_profile_image(db, app, admin, mailoutbox): form = response.form assert form['cityscape_image'].attrs['accept'] == 'image/*' + # clear image using API + response = app.put('/api/users/%s/' % john().uuid, params={'cityscape_image': ''}) + assert john().attributes.cityscape_image == None + + # put back first image using API + response = app.put( + '/api/users/%s/' % john().uuid, params={'cityscape_image': Upload('tests/200x200.jpg')} + ) + assert john().attributes.cityscape_image + profile_filename = john().attributes.cityscape_image.name + assert profile_filename.endswith('.jpeg') + + # verify 201x201 image is accepted and resized by API + response = app.put( + '/api/users/%s/' % john().uuid, params={'cityscape_image': Upload('tests/201x201.jpg')} + ) + with PIL.Image.open(os.path.join(settings.MEDIA_ROOT, john().attributes.cityscape_image.name)) as image: + assert image.width == 200 + assert image.height == 200 + assert john().attributes.cityscape_image.name != profile_filename + def test_multiple_attribute_setter(db, app, simple_user): nicks = Attribute.objects.create( -- 2.20.1