Fakultas Ilmu Komputer UI

Verified Commit dc96d1cb authored by Muhammad Ariq Basyar's avatar Muhammad Ariq Basyar
Browse files

[CHORE] fix now image will not be deleted if FE send an empty string

parent 4f2fb5de
Pipeline #80485 passed with stages
in 5 minutes and 25 seconds
# Generated by Django 3.1.7 on 2021-05-31 15:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('informasi_fasilitas', '0020_merge_20210531_1241'),
]
operations = [
migrations.AlterField(
model_name='kegiatan',
name='zona_waktu',
field=models.CharField(choices=[('WIB', 'WIB'), ('WITA', 'WITA'), ('WIT', 'WIT')], default='WIB', max_length=4),
),
]
......@@ -107,7 +107,6 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'creator_email': self.mock_user_test['email'],
'time_start': self.kegiatan_time_start,
'time_end': self.kegiatan_time_end})
})
self.assertEqual(response.status_code, HTTPStatus.CREATED)
self.assertDictEqual(data, expected_json)
count = Kegiatan.objects.all().count()
......@@ -163,7 +162,7 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'id': self.kegiatan.id,
'place_id': self.kegiatan.lokasi.place_id,
'creator': self.kegiatan.user.last_name,
'creator_email': kegiatan_wita.user.email,
'creator_email': self.kegiatan.user.email,
'nama_kegiatan': self.kegiatan.nama_kegiatan,
'penyelenggara': self.kegiatan.penyelenggara,
'deskripsi': self.kegiatan.deskripsi,
......@@ -277,7 +276,6 @@ class KegiatanRelatedViewTest(InformasiFasilitasViewTest):
'creator_email': self.mock_user_test['email'],
'time_start': self.kegiatan_time_start,
'time_end': self.kegiatan_time_end})
})
send_data.pop("images")
expected_json.update(send_data)
......
......@@ -59,6 +59,14 @@ class BisaGoUserSerializer(serializers.ModelSerializer):
representation[hidden_field] = self.Meta.hidden_replacement_char
return representation
def to_internal_value(self, data):
copied_data = data.copy()
if copied_data.get('foto') == '':
if self.instance.foto is not None:
copied_data['foto'] = self.instance.foto
internal_value = super().to_internal_value(copied_data)
return internal_value
def _update_instance(self, instance, validated_data, fields, mapper):
for field in fields:
mapped = mapper.get(field) or field
......
......@@ -23,6 +23,20 @@ class MockRequest:
return 'http:/dummy%s' % path
class MockEmptyImage:
def __init__(self, name, size=1):
self.name = name
self.size = size
# https://stackoverflow.com/a/26896684/11485041
self.content = b"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"
self._committed = True
def __eq__(self, obj):
if isinstance(obj, str):
if obj == '':
return True
class BaseTestSerializer(TestCase):
mock_user = {
'username': 'dummy username',
......@@ -36,8 +50,9 @@ class BaseTestSerializer(TestCase):
}
def setUp(self):
self.image = SimpleUploadedFile("test1.jpg",
content=open("test_file/test1.jpg", 'rb').read(), content_type='image/jpeg')
self.image = open("test_file/test1.jpg", 'rb')
self.upload_image = SimpleUploadedFile("test1.jpg",
content=self.image.read(), content_type='upload_image/jpeg')
self.hidden_fields = ['nomor telepon','email','alamat','tanggal lahir']
class TestBisaGoUserSerializer(BaseTestSerializer):
......@@ -147,7 +162,7 @@ class TestBisaGoUserSerializer(BaseTestSerializer):
update = {
'name': 'another dummy name', 'phone_number': '99999999',
'seen': False, 'alamat': 'dif dummy st', 'foto': self.image,
'seen': False, 'alamat': 'dif dummy st', 'foto': self.upload_image,
'jenis_kelamin': 'laki-laki', 'tanggal_lahir': '2000-10-02',
'disabilitas': 'tanpa batas', 'pekerjaan': 'makan',
'organisasi_komunitas': 'komunitas tanpa batas'
......@@ -173,6 +188,39 @@ class TestBisaGoUserSerializer(BaseTestSerializer):
self.assertEqual(data, expected)
def test_can_update_should_not_change_when_sending_empty_string_image(self):
user = User.objects.create_user(**self.mock_user)
bisagouser = BisaGoUser.objects.create(user=user,
**self.mock_bisagouser, foto=self.upload_image)
update = {
'name': 'another dummy name', 'phone_number': '99999999',
'seen': False, 'alamat': 'dif dummy st', 'foto': MockEmptyImage('pic.jpg'),
'jenis_kelamin': 'laki-laki', 'tanggal_lahir': '2000-10-02',
'disabilitas': 'tanpa batas', 'pekerjaan': 'makan',
'organisasi_komunitas': 'komunitas tanpa batas'
}
request = MockRequest(user_id=user.id)
serializer = BisaGoUserSerializer(bisagouser, data=update,
context={'request':request})
serializer.is_valid(raise_exception=True)
serializer.save()
expected = {
'phone_number': '99999999', 'tanggal_lahir': '2000-10-02',
'jenis_kelamin': 'laki-laki', 'disabilitas': 'tanpa batas',
'pekerjaan': 'makan', 'alamat': 'dif dummy st', 'seen': False,
'username': 'dummy username', 'name': 'another dummy name',
'email': 'test@email.com', 'hidden_fields': self.hidden_fields,
'organisasi_komunitas': 'komunitas tanpa batas',
'foto': MockRequest().build_absolute_uri(bisagouser.foto.url),
}
data = serializer.data
self.assertEqual(data, expected)
class TestRegisterUserSerializer(BaseTestSerializer):
def test_meta_model(self):
......@@ -254,7 +302,7 @@ class TestRegisterUserSerializer(BaseTestSerializer):
'phone_number': '000011112222',
'tanggal_lahir': '2000-01-01',
'jenis_kelamin': 'laki-laki',
'foto': self.image,
'foto': self.upload_image,
'organisasi_komunitas': 'komunitas lain',
}
......@@ -278,7 +326,7 @@ class TestRegisterUserSerializer(BaseTestSerializer):
'phone_number': '000011112222',
'tanggal_lahir': '2000-01-01',
'jenis_kelamin': 'laki-laki',
'foto': self.image,
'foto': self.upload_image,
'organisasi_komunitas': 'komunitas lain',
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment