Tesis 1.0.0
Loading...
Searching...
No Matches
test_auth_views.py
Go to the documentation of this file.
1from django.test import TestCase, Client
2from django.urls import reverse
3from rest_framework.test import APIClient
4from rest_framework import status
5from appUSERS.models import Usuario
6import json
7
8class RegistroLoginTests(TestCase):
9 """
10 Pruebas para las vistas de registro y login
11 """
12
13 def setUp(self):
14 self.client = APIClient()
15 self.register_url = reverse('appUSERS:register')
16 self.login_url = reverse('appUSERS:login')
17
18 # Datos para registro y login
20 'email': 'test@example.com',
21 'password': 'testpassword123',
22 'nombre': 'Usuario',
23 'apellido': 'Test',
24 'telefono': '1234567890'
25 }
26
27 # Usuario pre-creado para pruebas de login
28 self.test_user = Usuario.objects.create_user(
29 email='existing@example.com',
30 password='existingpassword123',
31 nombre='Existing',
32 apellido='User',
33 telefono='9876543210'
34 )
35
37 """Verifica que el registro de usuario funciona correctamente"""
38 response = self.client.post(
39 self.register_url,
40 data=json.dumps(self.valid_user_data),
41 content_type='application/json'
42 )
43
44 self.assertEqual(response.status_code, status.HTTP_201_CREATED)
45 self.assertTrue(Usuario.objects.filter(email='test@example.com').exists())
46
48 """Verifica que el login funciona correctamente con credenciales válidas"""
49 login_data = {
50 'email': 'existing@example.com',
51 'password': 'existingpassword123'
52 }
53
54 response = self.client.post(
55 self.login_url,
56 data=json.dumps(login_data),
57 content_type='application/json'
58 )
59
60 self.assertEqual(response.status_code, status.HTTP_200_OK)
61 self.assertIn('access', response.data)
62 self.assertIn('refresh', response.data)
63 self.assertEqual(response.data['email'], 'existing@example.com')
64
66 """Verifica que el login falla con credenciales inválidas"""
67 login_data = {
68 'email': 'existing@example.com',
69 'password': 'wrongpassword'
70 }
71
72 response = self.client.post(
73 self.login_url,
74 data=json.dumps(login_data),
75 content_type='application/json'
76 )
77
78 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)