Tesis 1.0.0
Loading...
Searching...
No Matches
test_payment_views.py
Go to the documentation of this file.
1from django.test import TestCase, RequestFactory
2from django.urls import reverse
3from unittest.mock import patch, MagicMock
4from payment_service.views import CreatePreferenceView
5from payment_service.models import PaymentRequest
6from rest_framework.test import APIClient
7from rest_framework import status
8import json
9
10class PaymentViewsTests(TestCase):
11 """
12 Pruebas para las vistas relacionadas con pagos
13 """
14
15 def setUp(self):
16 self.client = APIClient()
17 self.factory = RequestFactory()
18 self.create_preference_url = reverse('payment_service:create-preference')
19 self.cart_data = {
20 "productos": [
21 {
22 "id": 1,
23 "nombre": "Hamburguesa Clásica",
24 "descripcion": "Hamburguesa con queso y lechuga",
25 "precio": 500.0,
26 "cantidad": 2
27 }
28 ],
29 "total": 1000.0
30 }
32 "id": "test_preference_id",
33 "init_point": "https://www.mercadopago.com/init_point",
34 "sandbox_init_point": "https://sandbox.mercadopago.com/init_point",
35 "items": [
36 {
37 "id": "1",
38 "title": "Hamburguesa Clásica",
39 "description": "Hamburguesa con queso y lechuga",
40 "quantity": 2,
41 "unit_price": 500.0,
42 "currency_id": "ARS"
43 }
44 ]
45 }
46
47 @patch('payment_service.views.CartService')
48 @patch('payment_service.views.MercadoPagoService')
49 def test_create_preference_success(self, mock_mp_service, mock_cart_service):
50 """Prueba la creación exitosa de una preferencia de pago"""
51 # Configurar mocks
52 mock_cart_service.get_cart.return_value = self.cart_data
53
54 # Configurar el mock de MercadoPagoService
55 mock_mp_service_instance = MagicMock()
56 mock_mp_service.return_value = mock_mp_service_instance
57
58 # Configurar el proceso de items y la creación de preferencia
59 mock_mp_service_instance.process_cart_to_items.return_value = self.preference_data["items"]
60 mock_mp_service_instance.create_preference.return_value = self.preference_data
61
62 # Realizar la solicitud
63 token = "test_token_123"
64 response = self.client.post(
66 data=json.dumps({"user_token": token, "email": "cliente@example.com"}),
67 content_type="application/json"
68 )
69
70 # Verificar respuesta
71 self.assertEqual(response.status_code, status.HTTP_201_CREATED)
72 self.assertEqual(response.data["preference_id"], "test_preference_id")
73 self.assertEqual(response.data["init_point"], "https://www.mercadopago.com/init_point")
74
75 # Verificar que se llamó a los servicios correctamente
76 mock_cart_service.get_cart.assert_called_once_with(token)
77 mock_mp_service_instance.process_cart_to_items.assert_called_once_with(self.cart_data)
78
79 # Verificar que se guardó el PaymentRequest
80 self.assertTrue(PaymentRequest.objects.filter(preference_id="test_preference_id").exists())
81
82 @patch('payment_service.views.CartService')
83 def test_create_preference_empty_cart(self, mock_cart_service):
84 """Prueba que se maneja correctamente un carrito vacío"""
85 # Configurar el carrito vacío
86 mock_cart_service.get_cart.return_value = {"productos": [], "total": 0}
87
88 # Realizar la solicitud
89 token = "test_token_123"
90 response = self.client.post(
92 data=json.dumps({"user_token": token}),
93 content_type="application/json"
94 )
95
96 # Verificar respuesta de error
97 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
98 self.assertIn("error", response.data)
99 self.assertEqual(response.data["error"], "El carrito está vacío")
100
101 @patch('payment_service.views.CartService')
102 def test_create_preference_cart_service_error(self, mock_cart_service):
103 """Prueba que se maneja correctamente un error al obtener el carrito"""
104 # Configurar error en el servicio de carrito
105 mock_cart_service.get_cart.return_value = None
106
107 # Realizar la solicitud
108 token = "test_token_123"
109 response = self.client.post(
111 data=json.dumps({"user_token": token}),
112 content_type="application/json"
113 )
114
115 # Verificar respuesta de error
116 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
117 self.assertIn("error", response.data)
test_create_preference_success(self, mock_mp_service, mock_cart_service)
test_create_preference_empty_cart(self, mock_cart_service)
test_create_preference_cart_service_error(self, mock_cart_service)