File size: 4,210 Bytes
ef287e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

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)