Skip to content

Commit

Permalink
Adjust tests to comply with new permissions feature:
Browse files Browse the repository at this point in the history
* Change HTTP.status_codes to 403

* Change denied permission request payload

* Refactor patching of os.environ to avoid strange behavior
  • Loading branch information
sebastian-aranda committed Jun 19, 2023
1 parent 708c5a1 commit 9e20e2a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 151 deletions.
201 changes: 70 additions & 131 deletions manager/api/tests/test_commander.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
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
# python manage.py test api.tests.test_commander.SalinfoTestCase
Expand Down Expand Up @@ -34,15 +36,11 @@ def setUp(self):
Permission.objects.get(codename="delete_view"),
Permission.objects.get(codename="change_view"),
)
os.environ["COMMANDER_HOSTNAME"] = "foo"
os.environ["COMMANDER_PORT"] = "bar"

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.post")
def test_authorized_commander_data(self, mock_requests, mock_environ):
def test_authorized_commander_data(self, mock_requests):
"""Test authorized user commander data is sent to love-commander"""
# Arrange:
self.user.user_permissions.add(Permission.objects.get(name="Execute Commands"))
Expand All @@ -58,17 +56,11 @@ def test_authorized_commander_data(self, mock_requests, mock_environ):

with self.assertRaises(ValueError):
self.client.post(url, data, format="json")
expected_url = f"http://fakehost:fakeport/cmd"
expected_url = "http://foo:bar/cmd"
self.assertEqual(mock_requests.call_args, call(expected_url, json=data))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.post")
def test_unauthorized_commander(self, mock_requests, mock_environ):
def test_unauthorized_commander(self, mock_requests):
"""Test an unauthorized user can't send commands"""
# Act:
url = reverse("commander")
Expand All @@ -82,10 +74,8 @@ def test_unauthorized_commander(self, mock_requests, mock_environ):
response = self.client.post(url, data, format="json")
result = response.json()

self.assertEqual(response.status_code, 401)
self.assertEqual(
result, {"ack": "User does not have permissions to execute commands."}
)
self.assertEqual(response.status_code, 403)
self.assertEqual(result, UserBasedPermission.message)


@override_settings(DEBUG=True)
Expand All @@ -112,93 +102,62 @@ def setUp(self):
Permission.objects.get(codename="change_view"),
)

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
os.environ["COMMANDER_HOSTNAME"] = "foo"
os.environ["COMMANDER_PORT"] = "bar"

@patch("requests.get")
def test_salinfo_metadata(self, mock_requests, mock_environ):
def test_salinfo_metadata(self, mock_requests):
"""Test authorized user can get salinfo metadata"""
# Act:
url = reverse("salinfo-metadata")

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = f"http://fakehost:fakeport/salinfo/metadata"
expected_url = "http://foo:bar/salinfo/metadata"
self.assertEqual(mock_requests.call_args, call(expected_url))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.get")
def test_salinfo_topic_names(self, mock_requests, mock_environ):
def test_salinfo_topic_names(self, mock_requests):
"""Test authorized user can get salinfo topic_names"""
# Act:
url = reverse("salinfo-topic-names")

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = f"http://fakehost:fakeport/salinfo/topic-names"
expected_url = "http://foo:bar/salinfo/topic-names"
self.assertEqual(mock_requests.call_args, call(expected_url))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.get")
def test_salinfo_topic_names_with_param(self, mock_requests, mock_environ):
def test_salinfo_topic_names_with_param(self, mock_requests):
"""Test authorized user can get salinfo topic_names with query param"""
# Act:
url = reverse("salinfo-topic-names") + "?categories=telemetry"

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = (
f"http://fakehost:fakeport/salinfo/topic-names?categories=telemetry"
)
expected_url = "http://foo:bar/salinfo/topic-names?categories=telemetry"
self.assertEqual(mock_requests.call_args, call(expected_url))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.get")
def test_salinfo_topic_data(self, mock_requests, mock_environ):
def test_salinfo_topic_data(self, mock_requests):
"""Test authorized user can get salinfo topic_data"""
# Act:
url = reverse("salinfo-topic-data")

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = f"http://fakehost:fakeport/salinfo/topic-data"
expected_url = "http://foo:bar/salinfo/topic-data"
self.assertEqual(mock_requests.call_args, call(expected_url))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.get")
def test_salinfo_topic_data_with_param(self, mock_requests, mock_environ):
def test_salinfo_topic_data_with_param(self, mock_requests):
"""Test authorized user can get salinfo topic_data with query param"""
# Act:
url = reverse("salinfo-topic-data") + "?categories=telemetry"

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = (
f"http://fakehost:fakeport/salinfo/topic-data?categories=telemetry"
)
expected_url = "http://foo:bar/salinfo/topic-data?categories=telemetry"
self.assertEqual(mock_requests.call_args, call(expected_url))


Expand Down Expand Up @@ -226,19 +185,20 @@ def setUp(self):
Permission.objects.get(codename="change_view"),
)

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
os.environ["COMMANDER_HOSTNAME"] = "foo"
os.environ["COMMANDER_PORT"] = "bar"

@patch("requests.post")
def test_timeseries_query(self, mock_requests, mock_environ):
def test_timeseries_query(self, mock_requests):
"""Test authorized user can query and get a timeseries"""
# Act:
cscs = {
"ATDome": {"0": {"topic1": ["field1"]},},
"ATMCS": {"1": {"topic2": ["field2", "field3"]},},
"ATDome": {
"0": {"topic1": ["field1"]},
},
"ATMCS": {
"1": {"topic2": ["field2", "field3"]},
},
}
data = {
"start_date": "2020-03-16T12:00:00",
Expand All @@ -250,7 +210,7 @@ def test_timeseries_query(self, mock_requests, mock_environ):

with self.assertRaises(ValueError):
self.client.post(url, data, format="json")
expected_url = f"http://fakehost:fakeport/efd/timeseries"
expected_url = "http://foo:bar/efd/timeseries"
self.assertEqual(mock_requests.call_args, call(expected_url, json=data))


Expand Down Expand Up @@ -278,130 +238,109 @@ def setUp(self):
Permission.objects.get(codename="change_view"),
)

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
os.environ["COMMANDER_HOSTNAME"] = "foo"
os.environ["COMMANDER_PORT"] = "bar"

@patch("requests.post")
def test_command_query_atcs(self, mock_requests, mock_environ):
def test_command_query_atcs(self, mock_requests):
"""Test authorized user can send a ATCS command"""
self.user.user_permissions.add(Permission.objects.get(name="Execute Commands"))
# Act:
data = {
"command_name": "atcs_command",
"params": {"param1": "value1", "param2": 2, "param3": True,}, # noqa: E231
"params": {
"param1": "value1",
"param2": 2,
"param3": True,
}, # noqa: E231
}
url = reverse("TCS-aux")

with self.assertRaises(ValueError):
self.client.post(url, data, format="json")
expected_url = f"http://fakehost:fakeport/tcs/aux"
expected_url = "http://foo:bar/tcs/aux"
self.assertEqual(mock_requests.call_args, call(expected_url, json=data))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.post")
def test_command_query_atcs_unauthorized(self, mock_requests, mock_environ):
def test_command_query_atcs_unauthorized(self, mock_requests):
"""Test unauthorized user cannot send a ATCS command"""
self.user.user_permissions.remove(
Permission.objects.get(name="Execute Commands")
)
# Act:
data = {
"command_name": "atcs_command",
"params": {"param1": "value1", "param2": 2, "param3": True,}, # noqa: E231
"params": {
"param1": "value1",
"param2": 2,
"param3": True,
}, # noqa: E231
}
url = reverse("TCS-aux")
response = self.client.post(url, data, format="json")
result = response.json()
self.assertEqual(response.status_code, 401)
self.assertEqual(
result, {"ack": "User does not have permissions to execute commands."}
)
self.assertEqual(response.status_code, 403)
self.assertEqual(result, UserBasedPermission.message)

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.get")
def test_docstrings_query_atcs(self, mock_requests, mock_environ):
def test_docstrings_query_atcs(self, mock_requests):
"""Test authorized user can send a ATCS command"""
# Act:
url = reverse("TCS-aux-docstrings")

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = f"http://fakehost:fakeport/tcs/aux/docstrings"
expected_url = "http://foo:bar/tcs/aux/docstrings"
self.assertEqual(mock_requests.call_args, call(expected_url))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.post")
def test_command_query_mtcs(self, mock_requests, mock_environ):
def test_command_query_mtcs(self, mock_requests):
"""Test authorized user can send a MTCS command"""
self.user.user_permissions.add(Permission.objects.get(name="Execute Commands"))
# Act:
data = {
"command_name": "mtcs_command",
"params": {"param1": "value1", "param2": 2, "param3": True,}, # noqa: E231
"params": {
"param1": "value1",
"param2": 2,
"param3": True,
}, # noqa: E231
}
url = reverse("TCS-main")

with self.assertRaises(ValueError):
self.client.post(url, data, format="json")
expected_url = f"http://fakehost:fakeport/tcs/main"
expected_url = "http://foo:bar/tcs/main"
self.assertEqual(mock_requests.call_args, call(expected_url, json=data))

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.post")
def test_command_query_mtcs_unauthorized(self, mock_requests, mock_environ):
def test_command_query_mtcs_unauthorized(self, mock_requests):
"""Test unauthorized user cannot send a MTCS command"""
self.user.user_permissions.remove(
Permission.objects.get(name="Execute Commands")
)
# Act:
data = {
"command_name": "mtcs_command",
"params": {"param1": "value1", "param2": 2, "param3": True,}, # noqa: E231
"params": {
"param1": "value1",
"param2": 2,
"param3": True,
}, # noqa: E231
}
url = reverse("TCS-main")
response = self.client.post(url, data, format="json")
result = response.json()
self.assertEqual(response.status_code, 401)
self.assertEqual(
result, {"ack": "User does not have permissions to execute commands."}
)
self.assertEqual(response.status_code, 403)
self.assertEqual(result, UserBasedPermission.message)

@patch(
"os.environ.get",
side_effect=lambda arg: "fakehost"
if arg == "COMMANDER_HOSTNAME"
else "fakeport",
)
@patch("requests.get")
def test_docstrings_query_mtcs(self, mock_requests, mock_environ):
def test_docstrings_query_mtcs(self, mock_requests):
"""Test authorized user can send a MTCS command"""
# Act:
url = reverse("TCS-main-docstrings")

with self.assertRaises(ValueError):
self.client.get(url)
expected_url = f"http://fakehost:fakeport/tcs/main/docstrings"
expected_url = "http://foo:bar/tcs/main/docstrings"
self.assertEqual(mock_requests.call_args, call(expected_url))
Loading

0 comments on commit 9e20e2a

Please sign in to comment.