mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-04 01:51:28 +00:00
feat: Securisation du Backend
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.1.3 on 2025-11-30 11:02
|
||||
# Generated by Django 5.1.3 on 2026-03-14 13:23
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
|
||||
59
Back-End/GestionNotification/tests.py
Normal file
59
Back-End/GestionNotification/tests.py
Normal file
@ -0,0 +1,59 @@
|
||||
"""
|
||||
Tests unitaires pour le module GestionNotification.
|
||||
Vérifie que les endpoints requièrent une authentification JWT.
|
||||
"""
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
from Auth.models import Profile
|
||||
|
||||
|
||||
def create_user(email="notif_test@example.com", password="testpassword123"):
|
||||
return Profile.objects.create_user(username=email, email=email, password=password)
|
||||
|
||||
|
||||
def get_jwt_token(user):
|
||||
refresh = RefreshToken.for_user(user)
|
||||
return str(refresh.access_token)
|
||||
|
||||
|
||||
TEST_REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
),
|
||||
'DEFAULT_PERMISSION_CLASSES': (
|
||||
'rest_framework.permissions.IsAuthenticated',
|
||||
),
|
||||
}
|
||||
|
||||
TEST_CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}
|
||||
|
||||
|
||||
@override_settings(
|
||||
CACHES=TEST_CACHES,
|
||||
SESSION_ENGINE='django.contrib.sessions.backends.db',
|
||||
REST_FRAMEWORK=TEST_REST_FRAMEWORK,
|
||||
)
|
||||
class NotificationEndpointAuthTest(TestCase):
|
||||
"""Tests d'authentification sur les endpoints Notification."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse("GestionNotification:notifications")
|
||||
self.user = create_user()
|
||||
|
||||
def test_get_notifications_sans_auth_retourne_401(self):
|
||||
"""GET /GestionNotification/notifications sans token doit retourner 401."""
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
def test_get_notifications_avec_auth_retourne_200(self):
|
||||
"""GET /GestionNotification/notifications avec token valide doit retourner 200."""
|
||||
token = get_jwt_token(self.user)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
115
Back-End/GestionNotification/tests_security.py
Normal file
115
Back-End/GestionNotification/tests_security.py
Normal file
@ -0,0 +1,115 @@
|
||||
"""
|
||||
Tests de sécurité — GestionNotification
|
||||
Vérifie :
|
||||
- Les notifications sont filtrées par utilisateur (plus d'accès global)
|
||||
- Authentification requise
|
||||
"""
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
from Auth.models import Profile, ProfileRole
|
||||
from Establishment.models import Establishment
|
||||
from GestionNotification.models import Notification
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def create_user_with_role(email, name="Ecole Test"):
|
||||
user = Profile.objects.create_user(
|
||||
username=email, email=email, password="TestPass!123"
|
||||
)
|
||||
est = Establishment.objects.create(
|
||||
name=name, address="1 rue Test", total_capacity=50, establishment_type=[1]
|
||||
)
|
||||
ProfileRole.objects.create(
|
||||
profile=user, role_type=ProfileRole.RoleType.PROFIL_ECOLE,
|
||||
establishment=est, is_active=True
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
OVERRIDE = dict(
|
||||
CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}},
|
||||
SESSION_ENGINE='django.contrib.sessions.backends.db',
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@override_settings(**OVERRIDE)
|
||||
class NotificationAuthTest(TestCase):
|
||||
"""Authentification requise sur l'endpoint notifications."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse('GestionNotification:notifications')
|
||||
|
||||
def test_sans_auth_retourne_401(self):
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
|
||||
@override_settings(**OVERRIDE)
|
||||
class NotificationFilterTest(TestCase):
|
||||
"""
|
||||
Chaque utilisateur ne voit que ses propres notifications.
|
||||
Avant la correction, toutes les notifications étaient retournées
|
||||
à n'importe quel utilisateur authentifié (IDOR).
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse('GestionNotification:notifications')
|
||||
self.alice = create_user_with_role('alice_notif@test.com', 'Ecole Alice')
|
||||
self.bob = create_user_with_role('bob_notif@test.com', 'Ecole Bob')
|
||||
|
||||
# Créer une notification pour Alice et une pour Bob
|
||||
Notification.objects.create(
|
||||
user=self.alice, message='Message pour Alice', typeNotification=0
|
||||
)
|
||||
Notification.objects.create(
|
||||
user=self.bob, message='Message pour Bob', typeNotification=0
|
||||
)
|
||||
|
||||
def test_alice_voit_uniquement_ses_notifications(self):
|
||||
"""Alice ne doit voir que sa propre notification, pas celle de Bob."""
|
||||
token = str(RefreshToken.for_user(self.alice).access_token)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}')
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
data = response.json()
|
||||
self.assertEqual(len(data), 1, "Alice doit voir uniquement ses propres notifications")
|
||||
self.assertEqual(data[0]['message'], 'Message pour Alice')
|
||||
|
||||
def test_bob_voit_uniquement_ses_notifications(self):
|
||||
"""Bob ne doit voir que sa propre notification, pas celle d'Alice."""
|
||||
token = str(RefreshToken.for_user(self.bob).access_token)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}')
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
data = response.json()
|
||||
self.assertEqual(len(data), 1, "Bob doit voir uniquement ses propres notifications")
|
||||
self.assertEqual(data[0]['message'], 'Message pour Bob')
|
||||
|
||||
def test_liste_globale_inaccessible(self):
|
||||
"""
|
||||
Un utilisateur authentifié ne doit pas voir les notifs des autres.
|
||||
Vérification croisée : nombre de notifs retournées == 1.
|
||||
"""
|
||||
carol = create_user_with_role('carol_notif@test.com', 'Ecole Carol')
|
||||
token = str(RefreshToken.for_user(carol).access_token)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}')
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
data = response.json()
|
||||
# Carol n'a aucune notification
|
||||
self.assertEqual(len(data), 0,
|
||||
"Un utilisateur sans notification ne doit pas voir celles des autres (IDOR)")
|
||||
@ -1,5 +1,6 @@
|
||||
from django.http.response import JsonResponse
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from .models import *
|
||||
|
||||
@ -8,8 +9,11 @@ from Subscriptions.serializers import NotificationSerializer
|
||||
from N3wtSchool import bdd
|
||||
|
||||
class NotificationView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
notifsList=bdd.getAllObjects(Notification)
|
||||
notifs_serializer=NotificationSerializer(notifsList, many=True)
|
||||
# Filtrer les notifications de l'utilisateur authentifié uniquement (protection IDOR)
|
||||
notifsList = Notification.objects.filter(user=request.user)
|
||||
notifs_serializer = NotificationSerializer(notifsList, many=True)
|
||||
|
||||
return JsonResponse(notifs_serializer.data, safe=False)
|
||||
Reference in New Issue
Block a user