Spaces:
Sleeping
Sleeping
File size: 5,766 Bytes
385a349 9d855fa 385a349 9d855fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 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)
|