AkomptaBackend / api /tests_new.py
rinogeek's picture
Update
9d855fa
Raw
History Blame Contribute Delete
5.77 kB
from django.urls import reverse
from rest_framework.test import APITestCase, APIClient
from rest_framework import status
from django.contrib.auth import get_user_model
from .models import Notification, SupportTicket
import zipfile
import io
User = get_user_model()
class NotificationTests(APITestCase):
"""Tests pour les notifications"""
def setUp(self):
self.user = User.objects.create_user(
email='test@example.com',
password='TestPass123!',
first_name='John',
last_name='Doe'
)
self.client = APIClient()
self.client.force_authenticate(user=self.user)
self.notifications_url = reverse('notification-list')
def test_create_notification(self):
"""Test création de notification"""
data = {
'title': 'Test Notification',
'message': 'This is a test message',
'type': 'system'
}
response = self.client.post(self.notifications_url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Notification.objects.count(), 1)
self.assertEqual(Notification.objects.first().user, self.user)
def test_list_notifications(self):
"""Test récupération de la liste des notifications"""
Notification.objects.create(
user=self.user,
title='Test Notification',
message='This is a test message',
type='system'
)
response = self.client.get(self.notifications_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data['results']), 1)
def test_mark_read(self):
"""Test marquer une notification comme lue"""
notification = Notification.objects.create(
user=self.user,
title='Test Notification',
message='This is a test message',
type='system'
)
url = reverse('notification-mark-read', args=[notification.id])
response = self.client.patch(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
notification.refresh_from_db()
self.assertTrue(notification.is_read)
def test_mark_all_read(self):
"""Test marquer toutes les notifications comme lues"""
Notification.objects.create(
user=self.user,
title='Test Notification 1',
message='Message 1',
type='system'
)
Notification.objects.create(
user=self.user,
title='Test Notification 2',
message='Message 2',
type='system'
)
url = reverse('notification-mark-all-read')
response = self.client.patch(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Notification.objects.filter(is_read=True).count(), 2)
class SupportTicketTests(APITestCase):
"""Tests pour les tickets support"""
def setUp(self):
self.user = User.objects.create_user(
email='test@example.com',
password='TestPass123!',
first_name='John',
last_name='Doe'
)
self.client = APIClient()
self.client.force_authenticate(user=self.user)
self.support_url = reverse('support-list')
def test_create_ticket(self):
"""Test création de ticket"""
data = {
'subject': 'Help me',
'message': 'I need help'
}
response = self.client.post(self.support_url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(SupportTicket.objects.count(), 1)
self.assertEqual(SupportTicket.objects.first().user, self.user)
def test_list_tickets(self):
"""Test récupération de la liste des tickets"""
SupportTicket.objects.create(
user=self.user,
subject='Help me',
message='I need help'
)
response = self.client.get(self.support_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data['results']), 1)
class SyscohadaReportsTests(APITestCase):
"""Tests pour l'export SYSCOHADA (ZIP)"""
def setUp(self):
self.user = User.objects.create_user(
email='syscohada@example.com',
password='TestPass123!',
first_name='John',
last_name='Doe'
)
self.client = APIClient()
self.client.force_authenticate(user=self.user)
self.url = reverse('syscohada-download')
def test_download_zip_contains_two_files(self):
response = self.client.get(self.url, {'year': 2026})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response['Content-Type'], 'application/zip')
zf = zipfile.ZipFile(io.BytesIO(response.content))
names = sorted(zf.namelist())
self.assertEqual(len(names), 2)
self.assertTrue(any(name.startswith('compte_resultat_syscohada_2026') for name in names))
self.assertTrue(any(name.startswith('bilan_syscohada_2026') for name in names))
# Basic content sanity
cr_name = next(name for name in names if name.startswith('compte_resultat_syscohada_2026'))
cr_csv = zf.read(cr_name).decode('utf-8')
self.assertIn('REF,LIBELLES,NUMERO DE COMPTES,MONTANT_N,MONTANT_N_1', cr_csv)
bilan_name = next(name for name in names if name.startswith('bilan_syscohada_2026'))
bilan_csv = zf.read(bilan_name).decode('utf-8')
self.assertIn('SECTION,REF,LIBELLE,NOTE,BRUT,AMORT/DEPREC,NET_N,NET_N_1', bilan_csv)