Coverage for authentication\test\test_login.py: 100%
41 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-13 15:18 +0200
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-13 15:18 +0200
1from rest_framework.test import APITestCase
2from django.urls import reverse
3from users.models.base_user import User
4from rest_framework import status
5from rest_framework.response import Response
6from typing import cast
8class LoginTests(APITestCase):
9 def setUp(self):
10 # Création des utilisateurs avec différents rôles
11 self.admin = User.objects.create_user(email='admin@example.com', password='adminpass123', role='admin', is_staff=True)
12 self.employe = User.objects.create_user(email='employe@example.com', password='employeepass123', role='employe')
13 self.client_user = User.objects.create_user(email='client@example.com', password='clientpass123', role='client')
15 self.url = reverse('login')
17 def test_admin_login_success(self):
18 data = {'email': 'admin@example.com', 'password': 'adminpass123'}
19 response = cast(Response, self.client.post(self.url, data))
20 self.assertEqual(response.status_code, status.HTTP_200_OK)
21 self.assertIn('access', response.data)
22 self.assertIn('refresh', response.data)
23 self.assertEqual(response.data['role'], 'admin')
25 def test_employe_login_success(self):
26 data = {'email': 'employe@example.com', 'password': 'employeepass123'}
27 response = cast(Response, self.client.post(self.url, data))
28 self.assertEqual(response.status_code, status.HTTP_200_OK)
29 self.assertIn('access', response.data)
30 self.assertIn('refresh', response.data)
31 self.assertEqual(response.data['role'], 'employe')
33 def test_client_login_success(self):
34 data = {'email': 'client@example.com', 'password': 'clientpass123'}
35 response = cast(Response, self.client.post(self.url, data))
36 self.assertEqual(response.status_code, status.HTTP_200_OK)
37 self.assertIn('access', response.data)
38 self.assertIn('refresh', response.data)
39 self.assertEqual(response.data['role'], 'client')
41 def test_login_failure_wrong_password(self):
42 data = {'email': 'admin@example.com', 'password': 'wrongpassword'}
43 response = cast(Response, self.client.post(self.url, data))
44 self.assertIn(response.status_code, [status.HTTP_401_UNAUTHORIZED, status.HTTP_400_BAD_REQUEST])
46 def test_login_failure_nonexistent_user(self):
47 data = {'email': 'nouser@example.com', 'password': 'somepassword'}
48 response = cast(Response, self.client.post(self.url, data))
49 self.assertIn(response.status_code, [status.HTTP_401_UNAUTHORIZED, status.HTTP_400_BAD_REQUEST])