diff --git a/manager/api/tests/test_authlist.py b/manager/api/tests/test_authlist.py index 77b7455d..9b9c8176 100644 --- a/manager/api/tests/test_authlist.py +++ b/manager/api/tests/test_authlist.py @@ -1,3 +1,5 @@ +import requests +from unittest.mock import patch from django.test import TestCase, override_settings from django.urls import reverse from api.models import Token, CSCAuthorizationRequest @@ -127,8 +129,17 @@ def test_csc_authorization_create_request_authlist_user(self): "message": "This will last for 30 minutes.", "duration": 30, } + mock_patcher = patch("requests.post") + mock_client = mock_patcher.start() + mock_response = requests.Response() + mock_response.status_code = 200 + mock_response.json = lambda: {"ack": "Command sent."} + mock_client.return_value = mock_response + response = self.client.post(url, payload, format="json") + mock_client.stop() + # Assert self.assertEqual(response.status_code, 201) self.assertEqual(response.data[0]["user"], self.user_authlist.username) diff --git a/manager/api/views.py b/manager/api/views.py index a14499a2..42f24a0a 100644 --- a/manager/api/views.py +++ b/manager/api/views.py @@ -792,11 +792,6 @@ def get_queryset(self): | Q(authorized_users__icontains=self.request.user.username) ) - def query_authorize_csc(self, request_data): - url = f"http://{os.environ.get('AUTHORIZE_HOSTNAME')}/manager-connection/" - response = requests.post(url, json=json.dumps(request_data)) - return Response(response.json(), status=response.status_code) - @swagger_auto_schema(responses={201: CSCAuthorizationRequestSerializer(many=True)}) def create(self, request, *args, **kwargs): created_authorizations = [] @@ -839,7 +834,9 @@ def create(self, request, *args, **kwargs): authorization_self_remove_obj.resolved_by = request.user authorization_self_remove_obj.resolved_at = timezone.now() authorization_self_remove_obj.save() - # authorize_csc_response = self.query_authorize_csc() + query_authorize_csc( + CSCAuthorizationRequestSerializer(authorization_self_remove_obj).data + ) created_authorizations.append(authorization_self_remove_obj) new_authorized_users = request.data.get("authorized_users").split(",") @@ -851,16 +848,19 @@ def create(self, request, *args, **kwargs): or authorization_obj.unauthorized_cscs != "" ): authorization_obj.save() - # if authorization_obj.status == "Authorized": - # authorize_csc_response = self.query_authorize_csc() created_authorizations.append(authorization_obj) - if authorization_obj.duration and int(authorization_obj.duration) > 0: - authlist_revert_authorization_task( - CSCAuthorizationRequestSerializer(authorization_obj).data, - schedule=int(authorization_obj.duration) * 60, + if authorization_obj.status == "Authorized": + query_authorize_csc( + CSCAuthorizationRequestSerializer(authorization_obj).data ) + if authorization_obj.duration and int(authorization_obj.duration) > 0: + authlist_revert_authorization_task( + CSCAuthorizationRequestSerializer(authorization_obj).data, + schedule=(int(authorization_obj.duration) * 60) - 5, + ) + if len(created_authorizations) > 0: return Response( CSCAuthorizationRequestSerializer( @@ -886,12 +886,14 @@ def update(self, request, *args, **kwargs): updated_instance.resolved_by = request.user updated_instance.resolved_at = timezone.now() updated_instance.save() - # authorize_csc_response = self.query_authorize_csc() + query_authorize_csc( + CSCAuthorizationRequestSerializer(updated_instance).data + ) if updated_instance.duration and int(updated_instance.duration) > 0: authlist_revert_authorization_task( CSCAuthorizationRequestSerializer(updated_instance).data, - schedule=int(updated_instance.duration) * 60, + schedule=(int(updated_instance.duration) * 60) - 5, ) return Response( @@ -900,22 +902,39 @@ def update(self, request, *args, **kwargs): return Response({"error": "Bad request"}, status=status.HTTP_400_BAD_REQUEST) +def query_authorize_csc(authorization_dict): + cmd_payload = { + "csc": "Authorize", + "salindex": 0, + "cmd": "cmd_requestAuthorization", + "params": { + "cscsToChange": authorization_dict["cscs_to_change"], + "authorizedUsers": authorization_dict["authorized_users"], + "nonAuthorizedCSCs": authorization_dict["unauthorized_cscs"], + }, + } + + url = f"http://{os.environ.get('COMMANDER_HOSTNAME')}:{os.environ.get('COMMANDER_PORT')}/cmd" + response = requests.post(url, json=cmd_payload) + return Response(response.json(), status=response.status_code) + + @background(schedule=60) -def authlist_revert_authorization_task(request): +def authlist_revert_authorization_task(authorization_dict): new_authorized_users = ( - request["authorized_users"] + authorization_dict["authorized_users"] .replace("+", "[plus]") .replace("-", "[minus]") .replace("[plus]", "-") .replace("[minus]", "+") ) new_unauthorized_cscs = ( - request["unauthorized_cscs"] + authorization_dict["unauthorized_cscs"] .replace("+", "[plus]") .replace("-", "[minus]") .replace("[plus]", "-") .replace("[minus]", "+") ) - request["authorized_users"] = new_authorized_users - request["unauthorized_cscs"] = new_unauthorized_cscs - # self.query_authorize_csc(request) + authorization_dict["authorized_users"] = new_authorized_users + authorization_dict["unauthorized_cscs"] = new_unauthorized_cscs + query_authorize_csc(authorization_dict)