Commit 9e8a19fa authored by Florent Chehab's avatar Florent Chehab
Browse files

Bug removed in withUserTestCase and moderation testing finished !

parent a57a45ae
......@@ -68,8 +68,6 @@ class MyModelSerializer(serializers.ModelSerializer):
self.clean_validated_data()
if self.moderation_required():
print("Modération requise")
if self.instance is None: # we need to create the main model
self.instance = super(
MyModelSerializer, self).save(**kwargs)
......@@ -93,7 +91,7 @@ class MyModelSerializer(serializers.ModelSerializer):
return self.instance
else:
print("Pas besoin de modération")
moderated_and_updated = True
if self.instance is None:
self.set_model_attr_no_moder(moderated_and_updated)
......@@ -111,8 +109,8 @@ class MyModelSerializer(serializers.ModelSerializer):
objs_pending_db.delete()
self.set_model_attr_no_moder(moderated_and_updated)
return super(MyModelSerializer, self).save(**kwargs)
self.set_model_attr_no_moder(moderated_and_updated)
return super(MyModelSerializer, self).save(**kwargs)
# class MyModelVersionnedSerializer(MyModelSerializer):
......
......@@ -14,139 +14,192 @@ class ModerationTestCase(WithUserTestCase):
def test_setting_ok(self):
self.assertTrue(settings.TESTING)
#####
def _submit_post_test(self, client, data):
response = client.post(
self.api_moderation,
data,
format='json'
)
self.assertEqual(response.status_code, 201)
return response
def _submit_put_test(self, client, data, pk):
response = client.put(
self.api_moderation + str(pk) + '/',
data,
format='json'
)
self.assertEqual(response.status_code, 200)
return response
def _test_retreive_instance(self, data):
matching = ForTestingModeration.objects.filter(
aaa=data['aaa']
)
self.assertTrue(matching.exists())
return matching[0]
def _test_retreive_instance_in_moderation(self, instance, expected_fail=False):
ct = ContentType.objects.get_for_model(ForTestingModeration)
pending = PendingModeration.objects.filter(
content_type=ct, object_id=instance.pk)
if expected_fail:
self.assertFalse(pending.exists())
return None
else:
self.assertTrue(pending.exists())
self.assertEqual(len(pending), 1)
return pending[0]
def _test_val_null(self, val1, null):
if null:
self.assertIsNone(val1)
else:
self.assertIsNotNone(val1)
def _test_moderated_val(self, instance, null=True):
self._test_val_null(instance.moderated_by, null)
self._test_val_null(instance.moderated_on, null)
def _test_updated_val(self, instance, null=True):
self._test_val_null(instance.updated_on, null)
self._test_val_null(instance.updated_by, null)
def _test_new_obj_val(self, instance, data):
self.assertEqual(
instance.new_object['aaa'],
data['aaa']
)
####
@override_settings(MODERATION_ACTIVATED=True)
def test_moderation_activated(self):
"""
Simple test to check if we can create a superuser. We had issues in the past.
Test to check that when moderation IS activated,
the app behaves as expected regarding creation
and moderation of models.
"""
def _submit_post_test(client, data):
response = client.post(
self.api_moderation,
data,
format='json'
)
self.assertEqual(response.status_code, 201)
return response
def _submit_put_test(client, data, pk):
response = client.put(
self.api_moderation + str(pk) + '/',
data,
format='json'
)
self.assertEqual(response.status_code, 200)
return response
def _test_retreive_instance(data):
matching = ForTestingModeration.objects.filter(
aaa=data['aaa']
)
self.assertTrue(matching.exists())
return matching[0]
def _test_retreive_instance_in_moderation(instance, expected_fail=False):
ct = ContentType.objects.get_for_model(ForTestingModeration)
pending = PendingModeration.objects.filter(
content_type=ct, object_id=instance.pk)
if expected_fail:
self.assertFalse(pending.exists())
return None
else:
self.assertTrue(pending.exists())
self.assertEqual(len(pending), 1)
return pending[0]
def _test_val_null(val1, null):
if null:
self.assertIsNone(val1)
else:
self.assertIsNotNone(val1)
def _test_moderated_val(instance, null=True):
_test_val_null(instance.moderated_by, null)
_test_val_null(instance.moderated_on, null)
def _test_updated_val(instance, null=True):
_test_val_null(instance.updated_on, null)
_test_val_null(instance.updated_by, null)
def _test_new_obj_val(instance, data):
self.assertEqual(
instance.new_object['aaa'],
data['aaa']
)
# phase 1
# Need moderation
data_1 = {'aaa': "Test"}
_submit_post_test(self.authenticated_client, data_1)
instance = _test_retreive_instance(data_1)
self._submit_post_test(self.authenticated_client, data_1)
instance = self._test_retreive_instance(data_1)
_test_updated_val(instance, null=True)
_test_moderated_val(instance, null=True)
self._test_updated_val(instance, null=True)
self._test_moderated_val(instance, null=True)
instance_in_moderation = _test_retreive_instance_in_moderation(
instance_in_moderation = self._test_retreive_instance_in_moderation(
instance)
_test_updated_val(instance_in_moderation, null=False)
_test_new_obj_val(instance_in_moderation, data_1)
self._test_updated_val(instance_in_moderation, null=False)
self._test_new_obj_val(instance_in_moderation, data_1)
# Phase 2
# More moderation
data_2 = {'aaa': "Test 2"}
_submit_put_test(self.authenticated_client, data_2, instance.pk)
instance = _test_retreive_instance(data_1) # not data_2 !
self._submit_put_test(self.authenticated_client, data_2, instance.pk)
instance = self._test_retreive_instance(data_1) # not data_2 !
_test_updated_val(instance, null=True)
_test_moderated_val(instance, null=True)
self._test_updated_val(instance, null=True)
self._test_moderated_val(instance, null=True)
instance_in_moderation = _test_retreive_instance_in_moderation(
instance_in_moderation = self._test_retreive_instance_in_moderation(
instance)
_test_updated_val(instance_in_moderation, null=False)
self._test_updated_val(instance_in_moderation, null=False)
# here we espect data_2
_test_new_obj_val(instance_in_moderation, data_2)
self._test_new_obj_val(instance_in_moderation, data_2)
# Phase 3
# Moderation with modification
data_3 = {'aaa': "Test 3"}
_submit_put_test(self.moderator_client, data_3, instance.pk)
instance = _test_retreive_instance(data_3) # not data_2 !
self._submit_put_test(self.moderator_client, data_3, instance.pk)
instance = self._test_retreive_instance(data_3) # not data_2 !
_test_updated_val(instance, null=False)
_test_moderated_val(instance, null=False)
self._test_updated_val(instance, null=False)
self._test_moderated_val(instance, null=False)
self.assertEqual(instance.updated_by, self.moderator_user)
# (below) because the field aaa was updated by the moderator
self.assertEqual(instance.moderated_by, self.moderator_user)
instance_in_moderation = _test_retreive_instance_in_moderation(
instance_in_moderation = self._test_retreive_instance_in_moderation(
instance, True)
# Phase 4
# Put that require moderation
data_4 = {'aaa': "Test 4"}
_submit_put_test(self.authenticated_client, data_4, instance.pk)
instance = _test_retreive_instance(data_3) # not data_4 !
self._submit_put_test(self.authenticated_client, data_4, instance.pk)
instance = self._test_retreive_instance(data_3) # not data_4 !
_test_updated_val(instance, null=False) # Not True
_test_moderated_val(instance, null=False) # Not True
self._test_updated_val(instance, null=False) # Not True
self._test_moderated_val(instance, null=False) # Not True
instance_in_moderation = _test_retreive_instance_in_moderation(
instance_in_moderation = self._test_retreive_instance_in_moderation(
instance)
_test_updated_val(instance_in_moderation, null=False)
self._test_updated_val(instance_in_moderation, null=False)
# here we espect data_4
_test_new_obj_val(instance_in_moderation, data_4)
self. _test_new_obj_val(instance_in_moderation, data_4)
# Phase 5
# Moderation with no modification
_submit_put_test(self.moderator_client, data_4, instance.pk)
instance = _test_retreive_instance(data_4) # not data_2 !
self._submit_put_test(self.moderator_client, data_4, instance.pk)
instance = self._test_retreive_instance(data_4) # not data_2 !
_test_updated_val(instance, null=False)
_test_moderated_val(instance, null=False)
# (below) because the field aaa was updated by the moderator
self._test_updated_val(instance, null=False)
self._test_moderated_val(instance, null=False)
# (below) because the field aaa was NOT updated by the moderator
self.assertEqual(instance.updated_by, self.authenticated_user)
self.assertEqual(instance.moderated_by, self.moderator_user)
instance_in_moderation = _test_retreive_instance_in_moderation(
instance_in_moderation = self._test_retreive_instance_in_moderation(
instance, True)
####
@override_settings(MODERATION_ACTIVATED=True)
def test_staff_no_moderation(self):
"""
Test to check that staff member are not subject to moderation.
"""
data_1 = {'aaa': "Test sdfdf"}
self._submit_post_test(self.staff_client, data_1)
instance = self._test_retreive_instance(data_1)
self._test_updated_val(instance, null=False)
self._test_moderated_val(instance, null=False)
self.assertEqual(instance.updated_by, self.staff_user)
self.assertEqual(instance.moderated_by, self.staff_user)
self._test_retreive_instance_in_moderation(
instance, expected_fail=True
)
####
@override_settings(MODERATION_ACTIVATED=False)
def test_moderation_not_activated(self):
"""
Test to check that when moderation IS NOT activated,
the app behaves as expected regarding creation
and moderation of models.
"""
# phase 1
# Test that an authentificated user can post data
# And that there is nothing pending in moderation
data_1 = {'aaa': "Test"}
self._submit_post_test(self.authenticated_client, data_1)
instance = self._test_retreive_instance(data_1)
self._test_updated_val(instance, null=False)
self._test_moderated_val(instance, null=False)
self.assertEqual(instance.updated_by, self.authenticated_user)
self.assertEqual(instance.moderated_by, self.authenticated_user)
self._test_retreive_instance_in_moderation(
instance, expected_fail=True
)
......@@ -15,6 +15,7 @@ class WithUserTestCase(TestCase):
password=password
)
self.staff_user.is_staff = True
self.staff_user.save()
self.moderator_user = User.objects.create_user(
username='moderator_member',
......@@ -25,6 +26,7 @@ class WithUserTestCase(TestCase):
name='Moderators'
)[0]
self.moderator_group.user_set.add(self.moderator_user)
self.moderator_group.save()
self.authenticated_user = User.objects.create_user(
username='authenticated_user',
......
Markdown is supported
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