|
| 1 | +from django.test import TestCase |
| 2 | +from unittest.mock import patch |
| 3 | +from rest_framework.test import APIClient |
| 4 | +from rest_framework import status |
| 5 | +from authentication.services.cognito_service import CognitoService |
| 6 | +from rest_framework.response import Response |
| 7 | +from .views import ExpenseViewSet, ExpenseGroupedByTypeAndCategoryViewSet |
| 8 | + |
| 9 | + |
| 10 | +class ExpenseViewSetTestCase(TestCase): |
| 11 | + def setUp(self): |
| 12 | + self.client = APIClient() |
| 13 | + self.view = ExpenseViewSet() |
| 14 | + self.expense = { |
| 15 | + "user_expense_type": 1, |
| 16 | + "category": 1, |
| 17 | + "bankcard_id": 1, |
| 18 | + "amount": 100, |
| 19 | + } |
| 20 | + |
| 21 | + @patch.object(ExpenseViewSet, "create") |
| 22 | + @patch.object(CognitoService, "login_user") |
| 23 | + def test_create(self, mock_login_user, mock_create): |
| 24 | + mock_login_user.return_value = { |
| 25 | + "AuthenticationResult": {"AccessToken": "mock_access_token", "IdToken": "mock_id_token"} |
| 26 | + } |
| 27 | + mock_create.return_value = Response(status=status.HTTP_201_CREATED, data=self.expense) |
| 28 | + |
| 29 | + request = self.client.post("/expenses/", self.expense) |
| 30 | + request.headers["Authorization"] = "Bearer mock_access_token" |
| 31 | + |
| 32 | + response = self.client.post("/expenses/", self.expense) |
| 33 | + |
| 34 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 35 | + self.assertEqual(response.data["amount"], 100) |
| 36 | + |
| 37 | + @patch.object(ExpenseViewSet, "list") |
| 38 | + @patch.object(CognitoService, "login_user") |
| 39 | + def test_list(self, mock_login_user, mock_list): |
| 40 | + mock_login_user.return_value = { |
| 41 | + "AuthenticationResult": {"AccessToken": "mock_access_token", "IdToken": "mock_id_token"} |
| 42 | + } |
| 43 | + mock_list.return_value = Response(status=status.HTTP_200_OK, data=self.expense) |
| 44 | + |
| 45 | + request = self.client.get("/expenses/") |
| 46 | + request.headers["Authorization"] = "Bearer mock_access_token" |
| 47 | + |
| 48 | + response = self.client.get("/expenses/") |
| 49 | + |
| 50 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 51 | + self.assertEqual(response.data["amount"], 100) |
| 52 | + |
| 53 | + @patch.object(ExpenseViewSet, "retrieve") |
| 54 | + @patch.object(CognitoService, "login_user") |
| 55 | + def test_retrieve(self, mock_login_user, mock_retrieve): |
| 56 | + mock_login_user.return_value = { |
| 57 | + "AuthenticationResult": {"AccessToken": "mock_access_token", "IdToken": "mock_id_token"} |
| 58 | + } |
| 59 | + mock_retrieve.return_value = Response(status=status.HTTP_200_OK, data=self.expense) |
| 60 | + |
| 61 | + request = self.client.get("/expenses/1/") |
| 62 | + request.headers["Authorization"] = "Bearer mock_access_token" |
| 63 | + |
| 64 | + response = self.client.get("/expenses/1/") |
| 65 | + |
| 66 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 67 | + self.assertEqual(response.data["amount"], 100) |
| 68 | + |
| 69 | + @patch.object(ExpenseViewSet, "destroy") |
| 70 | + @patch.object(CognitoService, "login_user") |
| 71 | + def test_destroy(self, mock_login_user, mock_destroy): |
| 72 | + mock_login_user.return_value = { |
| 73 | + "AuthenticationResult": {"AccessToken": "mock_access_token", "IdToken": "mock_id_token"} |
| 74 | + } |
| 75 | + mock_destroy.return_value = Response(status=status.HTTP_204_NO_CONTENT) |
| 76 | + |
| 77 | + request = self.client.delete("/expenses/1/") |
| 78 | + request.headers["Authorization"] = "Bearer mock_access_token" |
| 79 | + |
| 80 | + response = self.client.delete("/expenses/1/") |
| 81 | + |
| 82 | + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) |
| 83 | + |
| 84 | + @patch.object(ExpenseViewSet, "partial_update") |
| 85 | + @patch.object(CognitoService, "login_user") |
| 86 | + def test_partial_update(self, mock_login_user, mock_partial_update): |
| 87 | + mock_login_user.return_value = { |
| 88 | + "AuthenticationResult": {"AccessToken": "mock_access_token", "IdToken": "mock_id_token"} |
| 89 | + } |
| 90 | + new_expense = { |
| 91 | + "expense_type": 1, |
| 92 | + "category": 1, |
| 93 | + "bankcard_id": 1, |
| 94 | + "amount": 200, |
| 95 | + } |
| 96 | + mock_partial_update.return_value = Response(status=status.HTTP_200_OK, data=new_expense) |
| 97 | + |
| 98 | + request = self.client.put("/expenses/1/", {"amount": 200}) |
| 99 | + request.headers["Authorization"] = "Bearer mock_access_token" |
| 100 | + |
| 101 | + response = self.client.put("/expenses/1/", {"amount": 200}) |
| 102 | + |
| 103 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 104 | + self.assertEqual(response.data["amount"], 200) |
| 105 | + |
| 106 | + |
| 107 | +class ExpenseGroupedByTypeAndCategoryViewSetTestCase(TestCase): |
| 108 | + def setUp(self): |
| 109 | + self.client = APIClient() |
| 110 | + self.view = ExpenseGroupedByTypeAndCategoryViewSet() |
| 111 | + self.expense = { |
| 112 | + "expense_type": 1, |
| 113 | + "category": 1, |
| 114 | + "bankcard_id": 1, |
| 115 | + "amount": 100, |
| 116 | + } |
| 117 | + self.expenses_grouped = {"Personal": {"Comida": 100}} |
| 118 | + |
| 119 | + @patch.object(ExpenseGroupedByTypeAndCategoryViewSet, "list") |
| 120 | + @patch.object(CognitoService, "login_user") |
| 121 | + def test_list(self, mock_login_user, mock_list): |
| 122 | + mock_login_user.return_value = { |
| 123 | + "AuthenticationResult": {"AccessToken": "mock_access_token", "IdToken": "mock_id_token"} |
| 124 | + } |
| 125 | + mock_list.return_value = Response(status=status.HTTP_200_OK, data=self.expenses_grouped) |
| 126 | + |
| 127 | + request = self.client.get("/expenses/grouped/") |
| 128 | + request.headers["Authorization"] = "Bearer mock_access_token" |
| 129 | + |
| 130 | + response = self.client.get("/expenses/grouped/") |
| 131 | + |
| 132 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 133 | + self.assertIn("Personal", response.data) |
| 134 | + self.assertIn("Comida", response.data["Personal"]) |
| 135 | + self.assertEqual(response.data["Personal"]["Comida"], 100) |
0 commit comments