1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-05 01:26:54 +00:00

Fully test logs app

This commit is contained in:
Yohann D'ANELLO 2020-09-24 14:30:24 +02:00
parent 1979d33314
commit a2ce495a6b
4 changed files with 34 additions and 7 deletions

View File

@ -1,3 +1,6 @@
from unittest.case import skipIf
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
@ -14,3 +17,8 @@ class TestAPIPages(TestCase):
def test_user_page(self):
response = self.client.get("/api/user/")
self.assertEqual(response.status_code, 200)
@skipIf("logs" not in settings.INSTALLED_APPS, reason="logs app is not used")
def test_logs_page(self):
response = self.client.get("/api/logs/")
self.assertEqual(response.status_code, 200)

View File

@ -10,8 +10,8 @@ router = routers.DefaultRouter()
router.register('user', UserViewSet)
if "logs" in settings.INSTALLED_APPS:
from logs.api.views import ChangelogViewSet
router.register('logs', ChangelogViewSet)
from logs.api.urls import register_logs_urls
register_logs_urls(router, "logs")
app_name = 'api'

View File

@ -54,8 +54,7 @@ def save_object(sender, instance, **kwargs):
# On récupère alors l'utilisateur·trice connecté·e à la VM, et on récupère la note associée
ip = "127.0.0.1"
username = getpass.getuser()
if User.objects.filter(username=username).exists():
user = User.objects.get(username=username)
user = User.objects.get(username=username) if User.objects.filter(username=username).exists() else None
# On n'enregistre pas les connexions
# noinspection PyProtectedMember
@ -113,8 +112,7 @@ def delete_object(sender, instance, **kwargs):
# On récupère alors l'utilisateur·trice connecté·e à la VM, et on récupère la note associée
ip = "127.0.0.1"
username = getpass.getuser()
if User.objects.filter(username=username).exists():
user = User.objects.get(username=username)
user = User.objects.get(username=username) if User.objects.filter(username=username).exists() else None
# On crée notre propre sérialiseur JSON pour pouvoir sauvegarder les modèles
class CustomSerializer(ModelSerializer):
@ -131,4 +129,4 @@ def delete_object(sender, instance, **kwargs):
previous=instance_json,
data="",
action="delete"
).save()
)

21
apps/logs/tests.py Normal file
View File

@ -0,0 +1,21 @@
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.test import TestCase
from .models import Changelog
class TestChangelog(TestCase):
def test_logs(self):
user = User.objects.create(email="admin@example.com")
self.assertTrue(Changelog.objects.filter(action="create", instance_pk=user.pk,
model=ContentType.objects.get_for_model(User)).exists())
old_user_pk = user.pk
user.delete()
self.assertTrue(Changelog.objects.filter(action="delete", instance_pk=old_user_pk,
model=ContentType.objects.get_for_model(User)).exists())
changelog = Changelog.objects.first()
self.assertRaises(ValidationError, changelog.delete)
str(Changelog.objects.all())