From c4dc0c8acf617121769346f3e1d272a48db5c8f8 Mon Sep 17 00:00:00 2001 From: Heibert Date: Fri, 6 Dec 2024 16:42:36 -0500 Subject: [PATCH] Update LDAP server URI in settings and clean up cache-related code in SGCFileViewSet --- INSIGHTSAPI/INSIGHTSAPI/settings.py | 2 +- INSIGHTSAPI/sgc/tests.py | 199 ++++++++++++++-------------- INSIGHTSAPI/sgc/views.py | 59 +++++++-- INSIGHTSAPI/users/tests.py | 6 +- 4 files changed, 155 insertions(+), 111 deletions(-) diff --git a/INSIGHTSAPI/INSIGHTSAPI/settings.py b/INSIGHTSAPI/INSIGHTSAPI/settings.py index 8c22abe..b41cc63 100644 --- a/INSIGHTSAPI/INSIGHTSAPI/settings.py +++ b/INSIGHTSAPI/INSIGHTSAPI/settings.py @@ -380,7 +380,7 @@ def str_to_bool(value: str) -> bool: AUTH_USER_MODEL = "users.User" # LDAP configuration -AUTH_LDAP_SERVER_URI = "ldap://DOMINIO-CYC.CYC-SERVICES.COM.CO:389" +AUTH_LDAP_SERVER_URI = "ldap://DOMINIO-CYC.CYC-SERVICES.COM.CO" AUTH_LDAP_BIND_DN = "CN=StaffNet,OU=TECNOLOGÍA,OU=BOGOTA,DC=CYC-SERVICES,DC=COM,DC=CO" AUTH_LDAP_BIND_PASSWORD = os.environ["AdminLDAPPassword"] diff --git a/INSIGHTSAPI/sgc/tests.py b/INSIGHTSAPI/sgc/tests.py index fc203c8..228fce0 100644 --- a/INSIGHTSAPI/sgc/tests.py +++ b/INSIGHTSAPI/sgc/tests.py @@ -231,103 +231,106 @@ def test_delete_file_without_permission(self): # Assert that the response status code is HTTP 403 Forbidden self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - def test_cache_file(self): - """Test caching a file""" - url = reverse("SGCFile-list") - self.file_data["area"] = SGCArea.objects.first() - SGCFile.objects.create(**self.file_data) - response = self.client.get( - url, - cookies=str(self.client.cookies), - ) - # Assert that the response status code is HTTP 200 OK - self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) - # Check that have the correct data - self.assertEqual(response.data["objects"][0].get("name"), "Test Filé") - # Check that the cache is working - request = self.client.get(url).wsgi_request - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertEqual(cache.get(cache_key).data, response.data) - def test_cache_refresh(self): - """Test refresh the cache""" - url = reverse("SGCFile-list") - self.file_data["area"] = SGCArea.objects.first() - SGCFile.objects.create(**self.file_data) - response = self.client.get( - url, - cookies=str(self.client.cookies), - ) - # Assert that the response status code is HTTP 200 OK - self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) - # Check that have the correct data - self.assertEqual(response.data["objects"][0].get("name"), "Test Filé") - # Check that the cache is working - request = self.client.get(url).wsgi_request - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertEqual(cache.get(cache_key).data, response.data) - self.assertEqual(len(response.data["objects"]), 1) - # Refresh the file_data to avoid conflicts using a docx encode - with open("utils/excels/Lista_Robinsón.xlsx", "rb") as file: - file_content = file.read() - self.file_data["file"] = SimpleUploadedFile( - "Test_SGC_Robinsón.xlsx", file_content - ) - response = self.client.post( - reverse("SGCFile-list"), - self.file_data, - format="multipart", - cookies=str(self.client.cookies), - ) - # Assert that the response status code is HTTP 201 Created - self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) - # Check that the cache was deleted - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertIsNone(cache.get(cache_key)) - # Now check that the cache is working again - response = self.client.get( - url, - cookies=str(self.client.cookies), - ) - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertEqual(cache.get(cache_key).data, response.data) - self.assertEqual(len(response.data["objects"]), 2) + # ! Wait until resume the cache views - def test_cache_refresh_on_update(self): - """Test refresh the cache on update""" - url = reverse("SGCFile-list") - self.file_data["area"] = SGCArea.objects.first() - file = SGCFile.objects.create(**self.file_data) - response = self.client.get( - url, - cookies=str(self.client.cookies), - ) - # Assert that the response status code is HTTP 200 OK - self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) - # Check that have the correct data - self.assertEqual(response.data["objects"][0].get("name"), "Test Filé") - # Check that the cache is working - request = self.client.patch(url).wsgi_request - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertEqual(cache.get(cache_key).data, response.data) - # Refresh the file_data to avoid conflicts using a docx encode - self.file_data["name"] = "Test File Updated" - response = self.client.patch( - reverse("SGCFile-detail", kwargs={"pk": file.id}), - {"name": "Test File Updated"}, - format="json", - cookies=str(self.client.cookies), - ) - # Assert that the response status code is HTTP 200 OK - self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) - # Check that the cache was deleted - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertIsNone(cache.get(cache_key)) - # Now check that the cache is working again - response = self.client.get( - url, - cookies=str(self.client.cookies), - ) - cache_key = get_cache_key(request, key_prefix="sgc") - self.assertEqual(cache.get(cache_key).data, response.data) - self.assertEqual(response.data["objects"][0].get("name"), "Test File Updated") + # def test_cache_file(self): + # """Test caching a file""" + # url = reverse("SGCFile-list") + # self.file_data["area"] = SGCArea.objects.first() + # SGCFile.objects.create(**self.file_data) + # response = self.client.get( + # url, + # cookies=str(self.client.cookies), + # ) + # # Assert that the response status code is HTTP 200 OK + # self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + # # Check that have the correct data + # self.assertEqual(response.data["objects"][0].get("name"), "Test Filé") + # # Check that the cache is working + # request = self.client.get(url).wsgi_request + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertEqual(cache.get(cache_key).data, response.data) + + # def test_cache_refresh(self): + # """Test refresh the cache""" + # url = reverse("SGCFile-list") + # self.file_data["area"] = SGCArea.objects.first() + # SGCFile.objects.create(**self.file_data) + # response = self.client.get( + # url, + # cookies=str(self.client.cookies), + # ) + # # Assert that the response status code is HTTP 200 OK + # self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + # # Check that have the correct data + # self.assertEqual(response.data["objects"][0].get("name"), "Test Filé") + # # Check that the cache is working + # request = self.client.get(url).wsgi_request + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertEqual(cache.get(cache_key).data, response.data) + # self.assertEqual(len(response.data["objects"]), 1) + # # Refresh the file_data to avoid conflicts using a docx encode + # with open("utils/excels/Lista_Robinsón.xlsx", "rb") as file: + # file_content = file.read() + # self.file_data["file"] = SimpleUploadedFile( + # "Test_SGC_Robinsón.xlsx", file_content + # ) + # response = self.client.post( + # reverse("SGCFile-list"), + # self.file_data, + # format="multipart", + # cookies=str(self.client.cookies), + # ) + # # Assert that the response status code is HTTP 201 Created + # self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) + # # Check that the cache was deleted + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertIsNone(cache.get(cache_key)) + # # Now check that the cache is working again + # response = self.client.get( + # url, + # cookies=str(self.client.cookies), + # ) + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertEqual(cache.get(cache_key).data, response.data) + # self.assertEqual(len(response.data["objects"]), 2) + + # def test_cache_refresh_on_update(self): + # """Test refresh the cache on update""" + # url = reverse("SGCFile-list") + # self.file_data["area"] = SGCArea.objects.first() + # file = SGCFile.objects.create(**self.file_data) + # response = self.client.get( + # url, + # cookies=str(self.client.cookies), + # ) + # # Assert that the response status code is HTTP 200 OK + # self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + # # Check that have the correct data + # self.assertEqual(response.data["objects"][0].get("name"), "Test Filé") + # # Check that the cache is working + # request = self.client.patch(url).wsgi_request + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertEqual(cache.get(cache_key).data, response.data) + # # Refresh the file_data to avoid conflicts using a docx encode + # self.file_data["name"] = "Test File Updated" + # response = self.client.patch( + # reverse("SGCFile-detail", kwargs={"pk": file.id}), + # {"name": "Test File Updated"}, + # format="json", + # cookies=str(self.client.cookies), + # ) + # # Assert that the response status code is HTTP 200 OK + # self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + # # Check that the cache was deleted + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertIsNone(cache.get(cache_key)) + # # Now check that the cache is working again + # response = self.client.get( + # url, + # cookies=str(self.client.cookies), + # ) + # cache_key = get_cache_key(request, key_prefix="sgc") + # self.assertEqual(cache.get(cache_key).data, response.data) + # self.assertEqual(response.data["objects"][0].get("name"), "Test File Updated") diff --git a/INSIGHTSAPI/sgc/views.py b/INSIGHTSAPI/sgc/views.py index 037115b..4c46557 100644 --- a/INSIGHTSAPI/sgc/views.py +++ b/INSIGHTSAPI/sgc/views.py @@ -1,19 +1,60 @@ """Views for the SGC app""" import logging -from django.core.cache import cache -from django.utils.decorators import method_decorator -from django.views.decorators.cache import cache_page -from rest_framework.permissions import IsAuthenticated, DjangoModelPermissions + from rest_framework import viewsets +from rest_framework.permissions import DjangoModelPermissions, IsAuthenticated + from services.views import FileDownloadMixin -from .models import SGCFile, SGCArea -from .serializers import SGCFileSerializer, SGCAreaSerializer +from .models import SGCArea, SGCFile +from .serializers import SGCAreaSerializer, SGCFileSerializer logger = logging.getLogger("requests") -CACHE_DURATION = 60 * 15 # 15 minutes + + +# ! This viewset is disabled until fix the cache issue + +# CACHE_DURATION = 60 * 15 # 15 minutes + +# class SGCFileViewSet(viewsets.ModelViewSet): +# """ViewSet for the SGC class""" + +# queryset = SGCFile.objects.all().select_related("area") +# serializer_class = SGCFileSerializer +# permission_classes = [IsAuthenticated, DjangoModelPermissions] + +# @method_decorator(cache_page(CACHE_DURATION, key_prefix="sgc")) +# def list(self, request, *args, **kwargs): +# """List the objects""" +# response = super().list(request, *args, **kwargs) +# data_list = list(response.data) +# permissions = { +# "add": request.user.has_perm("sgc.add_sgcfile"), +# "change": request.user.has_perm("sgc.change_sgcfile"), +# "delete": request.user.has_perm("sgc.delete_sgcfile"), +# } +# response.data = {"objects": data_list, "permissions": permissions} +# return response + +# def create(self, request, *args, **kwargs): +# """Create a new object""" +# response = super().create(request, *args, **kwargs) +# cache.delete_pattern("*sgc*") # Delete all cache keys with "sgc" +# return response + +# def update(self, request, *args, **kwargs): +# """Update an object""" +# response = super().update(request, *args, **kwargs) +# cache.delete_pattern("*sgc*") # Delete all cache keys with "sgc" +# return response + +# def destroy(self, request, *args, **kwargs): +# """Destroy an object""" +# response = super().destroy(request, *args, **kwargs) +# cache.delete_pattern("*sgc*") # Delete all cache keys with "sgc" +# return response class SGCFileViewSet(viewsets.ModelViewSet): @@ -23,7 +64,6 @@ class SGCFileViewSet(viewsets.ModelViewSet): serializer_class = SGCFileSerializer permission_classes = [IsAuthenticated, DjangoModelPermissions] - @method_decorator(cache_page(CACHE_DURATION, key_prefix="sgc")) def list(self, request, *args, **kwargs): """List the objects""" response = super().list(request, *args, **kwargs) @@ -39,19 +79,16 @@ def list(self, request, *args, **kwargs): def create(self, request, *args, **kwargs): """Create a new object""" response = super().create(request, *args, **kwargs) - cache.delete_pattern("*sgc*") # Delete all cache keys with "sgc" return response def update(self, request, *args, **kwargs): """Update an object""" response = super().update(request, *args, **kwargs) - cache.delete_pattern("*sgc*") # Delete all cache keys with "sgc" return response def destroy(self, request, *args, **kwargs): """Destroy an object""" response = super().destroy(request, *args, **kwargs) - cache.delete_pattern("*sgc*") # Delete all cache keys with "sgc" return response diff --git a/INSIGHTSAPI/users/tests.py b/INSIGHTSAPI/users/tests.py index cbc85bb..db08694 100644 --- a/INSIGHTSAPI/users/tests.py +++ b/INSIGHTSAPI/users/tests.py @@ -10,6 +10,7 @@ from django.test import TestCase from django.test.client import Client from django.urls import reverse + from hierarchy.models import Area from notifications.models import Notification from services.tests import BaseTestCase @@ -24,6 +25,7 @@ class LDAPAuthenticationTest(TestCase): def setUp(self): """Sets up the test client.""" self.client = Client() + print("wtf1") def test_ldap_connection(self): """Tests that the connection to the LDAP server is successful.""" @@ -32,6 +34,7 @@ def test_ldap_connection(self): ldap_bind_password = settings.AUTH_LDAP_BIND_PASSWORD conn = None try: + print("2") conn = ldap.initialize(ldap_server_uri) conn.simple_bind_s(ldap_bind_dn, ldap_bind_password) except ldap.LDAPError as err: @@ -39,6 +42,7 @@ def test_ldap_connection(self): finally: if conn: conn.unbind_s() + print("3") def test_login(self): """Tests that the user can login using LDAP.""" @@ -147,6 +151,7 @@ class UserTestCase(BaseTestCase): def setUp(self): """Sets up the test client.""" super().setUp() + print("wtf") self.user.user_permissions.add(Permission.objects.get(codename="upload_points")) def test_get_full_name(self): @@ -351,7 +356,6 @@ def test_upload_points_user_not_found(self): response.data["error"], "Actualización exitosa, pero algunos usuarios no fueron encontrados: 1001185391", ) - self.assertEqual(response.data["errors"], ["1001185391"]) self.assertEqual(Notification.objects.count(), 1) def test_upload_points_not_perm(self):