Commit 77048105 authored by Florent Chehab's avatar Florent Chehab

Test of moderation feature ok.

Need to clean it and to add test for when moderation is not activated.
parent 04274a84
Pipeline #26538 passed with stages
in 2 minutes and 1 second
......@@ -47,7 +47,7 @@ from backend.models.user import PreviousDeparture
from backend.models.user import PreviousDepartureFeedback
from backend.models.user import UserData
from backend.models.tools import PendingModeration
from backend.models.moderation import PendingModeration
CLASSIC_MODELS = [
Country,
......
# Generated by Django 2.0.3 on 2018-08-25 17:38
from django.conf import settings
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('backend', '0011_auto_20180825_1849'),
]
operations = [
migrations.RenameModel(
old_name='TestingModeration',
new_name='ForTestingModeration',
),
]
# Generated by Django 2.0.3 on 2018-08-25 17:44
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('backend', '0012_auto_20180825_1938'),
]
operations = [
migrations.RenameField(
model_name='fortestingmoderation',
old_name='do_not_touch_this_stuff',
new_name='aaa',
),
]
from .myModel import MyModel # noqa: F401
from .myModelSerializer import MyModelSerializer # noqa: F401
from .myModelViewSet import MyModelViewSet # noqa: F401
from .pendingModeration import PendingModeration, PendingModerationSerializer, PendingModerationViewSet # noqa: F401
from .forTestingModeration import ForTestingModeration, ForTestingModerationSerializer, ForTestingModerationViewSet # noqa: F401
from .myModel import MyModel
from .myModelSerializer import MyModelSerializer
from .myModelViewSet import MyModelViewSet
from django.db import models
class ForTestingModeration(MyModel):
"""
Simple model for testing purposes
"""
aaa = models.CharField(max_length=100)
class ForTestingModerationSerializer(MyModelSerializer):
"""
Same as above
"""
class Meta:
model = ForTestingModeration
fields = '__all__'
class ForTestingModerationViewSet(MyModelViewSet):
"""
Same as above
"""
serializer_class = ForTestingModerationSerializer
queryset = ForTestingModeration.objects.all()
......@@ -4,7 +4,6 @@ from .pendingModeration import PendingModeration
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from backend.utils import is_member
from django.db import models
from .myModel import MyModel
......
from rest_framework import viewsets, permissions
from .myModelSerializer import MyModelSerializer
from backend.models.tools import NoDeleteIfNotAdmin
class MyModelViewSet(viewsets.ModelViewSet): # TODO switch to dict
serializer_class = MyModelSerializer
permission_classes = (
permissions.IsAuthenticated,
NoDeleteIfNotAdmin,
)
from .DictModeViewSet import DictModeViewSet # noqa: F401
from .usefullLinksField import UsefullLinksField # noqa: F401
from .validateWithRestFramework import validate_with_rest_framework # noqa: F401
from .pendingModeration import PendingModeration, PendingModerationSerializer, PendingModerationViewSet # noqa: F401
from .noDeleteIfNotAdmin import NoDeleteIfNotAdmin # noqa: F401
......@@ -14,10 +14,11 @@ class UniversitySerializer(MyModelSerializer):
class UniversityViewSet(viewsets.ModelViewSet): # TODO switch to dict
serializer_class = UniversitySerializer
permission_classes = (
permissions.IsAuthenticated,
NoDeleteIfNotAdmin,
)
queryset = University.objects.all()\
.prefetch_related('univ_campus', 'univ_campus__updated_by') # pylint: disable=E1101
serializer_class = UniversitySerializer
from .withUserTestCase import WithUserTestCase # noqa: F401
from django.test import override_settings
from .withUserTestCase import WithUserTestCase
from backend.models.moderation import ForTestingModeration
from backend.models.moderation import PendingModeration
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
class ModerationTestCase(WithUserTestCase):
def test_setting_ok(self):
self.assertTrue(settings.TESTING)
def moreSetUp(self):
self.api_moderation = '/api/test/moderation/'
def test_read(self):
"""
TODO à déplacer ailleur
Basic tests to check read access rights
"""
response = self.staff_client.get(self.api_moderation)
self.assertEqual(response.status_code, 200)
response = self.authenticated_client.get(self.api_moderation)
self.assertEqual(response.status_code, 200)
response = self.unauthenticated_client.get(self.api_moderation)
self.assertEqual(response.status_code, 403)
@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.
"""
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)
_test_updated_val(instance, null=True)
_test_moderated_val(instance, null=True)
instance_in_moderation = _test_retreive_instance_in_moderation(
instance)
_test_updated_val(instance_in_moderation, null=False)
_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 !
_test_updated_val(instance, null=True)
_test_moderated_val(instance, null=True)
instance_in_moderation = _test_retreive_instance_in_moderation(
instance)
_test_updated_val(instance_in_moderation, null=False)
# here we espect data_2
_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 !
_test_updated_val(instance, null=False)
_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, 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 !
_test_updated_val(instance, null=False) # Not True
_test_moderated_val(instance, null=False) # Not True
instance_in_moderation = _test_retreive_instance_in_moderation(
instance)
_test_updated_val(instance_in_moderation, null=False)
# here we espect data_4
_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 !
_test_updated_val(instance, null=False)
_test_moderated_val(instance, null=False)
# (below) because the field aaa was 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, True)
from rest_framework.test import APIClient
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.test import TestCase
class WithUserTestCase(TestCase):
def setUp(self):
password = '123456'
self.staff_user = User.objects.create_user(
username='staff_member',
email='master@master.fr',
password=password
)
self.staff_user.is_staff = True
self.moderator_user = User.objects.create_user(
username='moderator_member',
email='moderator@moderator.fr',
password=password
)
self.moderator_group = Group.objects.get_or_create(
name='Moderators'
)[0]
self.moderator_group.user_set.add(self.moderator_user)
self.authenticated_user = User.objects.create_user(
username='authenticated_user',
email='authenticated@authenticated.fr',
password=password
)
self.staff_client = APIClient()
self.staff_client.login(
username=self.staff_user.username,
password=password
)
self.moderator_client = APIClient()
self.moderator_client.login(
username=self.moderator_user.username,
password=password
)
self.authenticated_client = APIClient()
self.authenticated_client.login(
username=self.authenticated_user.username,
password=password
)
self.unauthenticated_client = APIClient()
self.moreSetUp()
def moreSetUp(self):
pass
from django.conf.urls import url, include
from django.conf import settings
from rest_framework import routers
from backend.models.location import CountryViewSet
from backend.models.location import CityViewSet
......@@ -50,7 +52,7 @@ from backend.models.user import PreviousDepartureViewSet
from backend.models.user import PreviousDepartureFeedbackViewSet
from backend.models.user import UserDataViewSet
from backend.models.tools import PendingModerationViewSet
from backend.models.moderation import PendingModerationViewSet
from rest_framework.documentation import include_docs_urls
......@@ -62,6 +64,11 @@ urlpatterns = [
router = routers.DefaultRouter()
if settings.TESTING:
from backend.models.moderation import ForTestingModerationViewSet
router.register(r'test/moderation', ForTestingModerationViewSet)
router.register(r'country', CountryViewSet)
router.register(r'city', CityViewSet)
router.register(r'campus', CampusViewSet)
......@@ -87,7 +94,8 @@ router.register(r'city_more/other_stuff', CityOtherStuffViewSet)
router.register(r'city_more/photos', CityPhotoViewSet)
router.register(r'university_more/all/(?P<univ_id>[0-9]+)', UniversityModulesViewSet, "university_details")
router.register(
r'university_more/all/(?P<univ_id>[0-9]+)', UniversityModulesViewSet, "university_details")
router.register(r'university_more/scholarship', UniversityScholarshipViewSet)
router.register(r'university_more/info', UniversityInfoViewSet)
router.register(r'university_more/insurance', UniversityInsuranceViewSet)
......@@ -114,8 +122,10 @@ router.register(r'other/department', DepartmentViewSet)
router.register(r'other/specialty', SpecialtyViewSet)
router.register(r'other/offer', OfferViewSet)
router.register(r'university_more/previous_departure', PreviousDepartureViewSet)
router.register(r'university_more/previous_departure_feedback', PreviousDepartureFeedbackViewSet)
router.register(r'university_more/previous_departure',
PreviousDepartureViewSet)
router.register(r'university_more/previous_departure_feedback',
PreviousDepartureFeedbackViewSet)
router.register(r'user/recommendation', RecommendationViewSet)
router.register(r'user/recommendations_list', RecommendationListViewSet)
router.register(r'user/data', UserDataViewSet)
......
from .is_member import is_member # noqa: F401
def is_member(group_name, user):
"""
Function to know if a user is part of a specific group.
TODO unit test
"""
return user.groups.filter(name=group_name).exists()
......@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
import sys
import django_heroku
import dj_database_url
......@@ -50,6 +51,7 @@ MIDDLEWARE = [
# If we want to activate the moderation functionnalities
MODERATION_ACTIVATED = True
TESTING = "pytest" in sys.modules
INTERNAL_IPS = ['127.0.0.1']
......
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