diff --git a/manager/api/tests/test_commander.py b/manager/api/tests/test_commander.py index a3cf7fce..a63ce437 100644 --- a/manager/api/tests/test_commander.py +++ b/manager/api/tests/test_commander.py @@ -19,12 +19,14 @@ import os +from unittest.mock import call, patch + +from api.models import Token +from django.contrib.auth.models import Permission, User from django.test import TestCase, override_settings from django.urls import reverse -from api.models import Token from rest_framework.test import APIClient -from django.contrib.auth.models import User, Permission -from unittest.mock import patch, call + from manager.utils import UserBasedPermission # python manage.py test api.tests.test_commander.CommanderTestCase @@ -73,11 +75,15 @@ def test_authorized_commander_data(self, mock_requests): "cmd": "cmd_setScalars", "params": {"a": 1, "b": 2}, } + data_with_identity = data.copy() + data_with_identity["identity"] = f"{self.user.username}@127.0.0.1" with self.assertRaises(ValueError): self.client.post(url, data, format="json") expected_url = "http://foo:bar/cmd" - self.assertEqual(mock_requests.call_args, call(expected_url, json=data)) + self.assertEqual( + mock_requests.call_args, call(expected_url, json=data_with_identity) + ) @patch("requests.post") def test_unauthorized_commander(self, mock_requests): diff --git a/manager/api/views.py b/manager/api/views.py index bc2a012d..e66373dd 100644 --- a/manager/api/views.py +++ b/manager/api/views.py @@ -437,7 +437,9 @@ def validate_config_schema(request): @permission_classes((IsAuthenticated, CommandPermission)) def commander(request): """Sends a command to the LOVE-commander - according to the received parameters + according to the received parameters. + + Command identity is arranged here. Params ------ @@ -449,8 +451,12 @@ def commander(request): Response The response and status code of the request to the LOVE-Commander """ + # Arrange command indentity + request_data = request.data.copy() + request_data["identity"] = f"{request.user.username}@{request.META['REMOTE_ADDR']}" + url = f"http://{os.environ.get('COMMANDER_HOSTNAME')}:{os.environ.get('COMMANDER_PORT')}/cmd" - response = requests.post(url, json=request.data) + response = requests.post(url, json=request_data) return Response(response.json(), status=response.status_code)