Skip to content

Commit

Permalink
Merge pull request #278 from lsst-ts/tickets/DM-46045
Browse files Browse the repository at this point in the history
Add identity to issued commands through the commander view
  • Loading branch information
sebastian-aranda authored Sep 10, 2024
2 parents 0f1313c + 129e045 commit 84eb6c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Version History
===============

v7.0.4
v7.1.0
------

* Add identity to issued commands through the commander view `<https://github.com/lsst-ts/LOVE-manager/pull/278>`_
* Remove cmd user creation for production deployments `<https://github.com/lsst-ts/LOVE-manager/pull/280>`_

v7.0.3
Expand Down
15 changes: 11 additions & 4 deletions manager/api/tests/test_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,6 +62,7 @@ def setUp(self):
os.environ["COMMANDER_PORT"] = "bar"

@patch("requests.post")
@patch.dict(os.environ, {"SERVER_URL": "localhost"})
def test_authorized_commander_data(self, mock_requests):
"""Test authorized user commander data is sent to love-commander"""
# Arrange:
Expand All @@ -73,11 +76,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}@localhost"

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):
Expand Down
11 changes: 9 additions & 2 deletions manager/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand All @@ -449,8 +451,13 @@ def commander(request):
Response
The response and status code of the request to the LOVE-Commander
"""
# Arrange command indentity
request_data = request.data.copy()
host_fqdn = os.environ.get("SERVER_URL", None)
request_data["identity"] = f"{request.user.username}@{host_fqdn}"

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)

Expand Down

0 comments on commit 84eb6c6

Please sign in to comment.