From c96b9562a2b4e31353947e887ad0d85ed2164a15 Mon Sep 17 00:00:00 2001 From: Luc SORIGNET Date: Sun, 15 Mar 2026 12:09:31 +0100 Subject: [PATCH] chore(ci): ajout test_settings.py et SKILL run-tests --- .github/copilot-instructions.md | 1 + .github/instructions/run-tests.instruction.md | 53 +++++++++++++++ Back-End/N3wtSchool/test_settings.py | 66 +++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 .github/instructions/run-tests.instruction.md create mode 100644 Back-End/N3wtSchool/test_settings.py diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 031bc7d..01d4331 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -56,3 +56,4 @@ Pour le front-end, les exigences de qualité sont les suivantes : - **Tickets** : [issues guidelines](./instructions/issues.instruction.md) - **Commits** : [commit guidelines](./instructions/general-commit.instruction.md) +- **Tests** : [run tests](./instructions/run-tests.instruction.md) diff --git a/.github/instructions/run-tests.instruction.md b/.github/instructions/run-tests.instruction.md new file mode 100644 index 0000000..571ff41 --- /dev/null +++ b/.github/instructions/run-tests.instruction.md @@ -0,0 +1,53 @@ +--- +applyTo: "**" +--- + +# Lancer les tests – N3WT-SCHOOL + +## Tests backend (Django) + +Les tests backend tournent dans le conteneur Docker. Toujours utiliser `--settings=N3wtSchool.test_settings`. + +```powershell +# Tous les tests +docker exec -it n3wt-school-backend-1 python3 manage.py test --settings=N3wtSchool.test_settings --verbosity=2 + +# Un module spécifique +docker exec -it n3wt-school-backend-1 python3 manage.py test --settings=N3wtSchool.test_settings Auth.tests --verbosity=2 +``` + +### Points importants + +- Le fichier `Back-End/N3wtSchool/test_settings.py` configure l'environnement de test : + - Base PostgreSQL dédiée `school_test` (SQLite incompatible avec `ArrayField`) + - Cache en mémoire locale (pas de Redis) + - Channels en mémoire (`InMemoryChannelLayer`) + - Throttling désactivé + - Hashage MD5 (plus rapide) + - Email en mode `locmem` +- Si le conteneur n'est pas démarré : `docker compose up -d` depuis la racine du projet +- Les logs `WARNING` dans la sortie des tests sont normaux (endpoints qui retournent 400/401 intentionnellement) + +## Tests frontend (Jest) + +```powershell +# Depuis le dossier Front-End +cd Front-End +npm test -- --watchAll=false + +# Avec couverture +npm test -- --watchAll=false --coverage +``` + +### Points importants + +- Les tests sont dans `Front-End/src/test/` +- Les warnings `ReactDOMTestUtils.act is deprecated` sont non bloquants (dépendance `@testing-library/react`) +- Config Jest : `Front-End/jest.config.js` + +## Résultats attendus + +| Périmètre | Nb tests | Statut | +| -------------- | -------- | ------ | +| Backend Django | 121 | ✅ OK | +| Frontend Jest | 24 | ✅ OK | diff --git a/Back-End/N3wtSchool/test_settings.py b/Back-End/N3wtSchool/test_settings.py new file mode 100644 index 0000000..de2d288 --- /dev/null +++ b/Back-End/N3wtSchool/test_settings.py @@ -0,0 +1,66 @@ +""" +Settings de test pour l'exécution des tests unitaires Django. +Utilise la base PostgreSQL du docker-compose (ArrayField non supporté par SQLite). +Redis et Celery sont désactivés. +""" +import os +os.environ.setdefault('SECRET_KEY', 'django-insecure-test-secret-key-for-unit-tests-only') +os.environ.setdefault('WEBHOOK_API_KEY', 'test-webhook-api-key-for-unit-tests-only') +os.environ.setdefault('DJANGO_DEBUG', 'True') + +from N3wtSchool.settings import * # noqa: F401, F403 + +# Base de données PostgreSQL dédiée aux tests (isolée de la base de prod) +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'school_test', + 'USER': os.environ.get('DB_USER', 'postgres'), + 'PASSWORD': os.environ.get('DB_PASSWORD', 'postgres'), + 'HOST': os.environ.get('DB_HOST', 'database'), + 'PORT': os.environ.get('DB_PORT', '5432'), + 'TEST': { + 'NAME': 'school_test', + }, + } +} + +# Cache en mémoire locale (pas de Redis) +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + } +} + +# Sessions en base de données (plus simple que le cache pour les tests) +SESSION_ENGINE = 'django.contrib.sessions.backends.db' + +# Django Channels en mémoire (pas de Redis) +CHANNEL_LAYERS = { + 'default': { + 'BACKEND': 'channels.layers.InMemoryChannelLayer', + } +} + +# Désactiver Celery pendant les tests +CELERY_TASK_ALWAYS_EAGER = True +CELERY_TASK_EAGER_PROPAGATES = True + +# Email en mode console (pas d'envoi réel) +EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' + +# Clé secrète fixe pour les tests +SECRET_KEY = 'django-insecure-test-secret-key-for-unit-tests-only' +SIMPLE_JWT['SIGNING_KEY'] = SECRET_KEY # noqa: F405 + +# Désactiver le throttling pendant les tests +REST_FRAMEWORK['DEFAULT_THROTTLE_CLASSES'] = [] # noqa: F405 +REST_FRAMEWORK['DEFAULT_THROTTLE_RATES'] = {} # noqa: F405 + +# Accélérer le hashage des mots de passe pour les tests +PASSWORD_HASHERS = [ + 'django.contrib.auth.hashers.MD5PasswordHasher', +] + +# Désactiver les logs verbeux pendant les tests +LOGGING['root']['level'] = 'CRITICAL' # noqa: F405