Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/old_apps
Browse files Browse the repository at this point in the history
  • Loading branch information
ggsdc authored Dec 18, 2024
2 parents 967ca36 + 161e0d4 commit 656b116
Show file tree
Hide file tree
Showing 9 changed files with 1,709 additions and 31 deletions.
342 changes: 342 additions & 0 deletions cornflow-server/cornflow/tests/custom_test_case.py

Large diffs are not rendered by default.

47 changes: 46 additions & 1 deletion cornflow-server/cornflow/tests/unit/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
"""
Unit test for the actions endpoint
Unit tests for the actions endpoint.
This module contains tests for the actions API endpoint functionality, including:
- Authorization checks for different user roles
- Validation of action listings
- Access control verification
Classes
-------
TestActionsListEndpoint
Tests for the actions list endpoint functionality
"""

# Import from libraries
Expand All @@ -10,7 +20,24 @@


class TestActionsListEndpoint(CustomTestCase):
"""
Test cases for the actions list endpoint.
This class tests the functionality of listing available actions, including:
- Authorization checks for different user roles
- Validation of returned action data
- Access control for authorized and unauthorized roles
"""

def setUp(self):
"""
Set up test environment before each test.
Initializes test data including:
- Base test case setup
- Roles with access permissions
- Test payload with action data
"""
super().setUp()
self.roles_with_access = ActionListEndpoint.ROLES_WITH_ACCESS
self.payload = [
Expand All @@ -19,9 +46,20 @@ def setUp(self):
]

def tearDown(self):
"""
Clean up test environment after each test.
"""
super().tearDown()

def test_get_actions_authorized(self):
"""
Test that authorized roles can access the actions list.
Verifies that users with proper roles can:
- Successfully access the actions endpoint
- Receive the correct list of actions
- Get properly formatted action data
"""
for role in self.roles_with_access:
self.token = self.create_user_with_role(role)
response = self.client.get(
Expand All @@ -36,6 +74,13 @@ def test_get_actions_authorized(self):
self.assertCountEqual(self.payload, response.json)

def test_get_actions_not_authorized(self):
"""
Test that unauthorized roles cannot access the actions list.
Verifies that users without proper roles:
- Are denied access to the actions endpoint
- Receive appropriate error responses
"""
for role in ROLES_MAP:
if role not in self.roles_with_access:
self.token = self.create_user_with_role(role)
Expand Down
66 changes: 57 additions & 9 deletions cornflow-server/cornflow/tests/unit/test_alarms.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,87 @@
"""
Unit tests for the alarms endpoint.
This module contains tests for the alarms API endpoint functionality, including:
- Creating new alarms
- Retrieving alarm listings
- Validating alarm data and properties
Classes
-------
TestAlarmsEndpoint
Tests for the alarms endpoint functionality
"""

# Imports from internal modules
from cornflow.models import AlarmsModel
from cornflow.tests.const import ALARMS_URL
from cornflow.tests.custom_test_case import CustomTestCase


class TestAlarmsEndpoint(CustomTestCase):
"""
Test cases for the alarms endpoint.
This class tests the functionality of managing alarms, including:
- Creating new alarms with various properties
- Retrieving and validating alarm data
- Checking alarm schema and criticality levels
"""

def setUp(self):
"""
Set up test environment before each test.
Initializes test data including:
- Base test case setup
- URL endpoint configuration
- Model and response field definitions
- Test items to check
"""
super().setUp()
self.url = ALARMS_URL
self.model = AlarmsModel
self.response_items = {"id", "name", "description", "criticality", "schema"}
self.items_to_check = ["name", "description", "schema", "criticality"]

def test_post_alarm(self):
payload = {"name": "Alarm 1", "description": "Description Alarm 1", "criticality": 1}
"""
Test creating a new alarm.
Verifies that an alarm can be created with:
- A name
- A description
- A criticality level
"""
payload = {
"name": "Alarm 1",
"description": "Description Alarm 1",
"criticality": 1,
}
self.create_new_row(self.url, self.model, payload)

def test_get_alarms(self):
"""
Test retrieving multiple alarms.
Verifies:
- Retrieval of multiple alarms with different properties
- Proper handling of alarms with and without schema
- Correct validation of alarm data fields
"""
data = [
{"name": "Alarm 1", "description": "Description Alarm 1", "criticality": 1},
{"name": "Alarm 2", "description": "Description Alarm 2", "criticality": 2, "schema": "solve_model_dag"},
{
"name": "Alarm 2",
"description": "Description Alarm 2",
"criticality": 2,
"schema": "solve_model_dag",
},
]
rows = self.get_rows(
self.url,
data,
check_data=False
)
rows = self.get_rows(self.url, data, check_data=False)
rows_data = list(rows.json)
for i in range(len(data)):
for key in self.get_keys_to_check(data[i]):
self.assertIn(key, rows_data[i])
if key in data[i]:
self.assertEqual(rows_data[i][key], data[i][key])


46 changes: 45 additions & 1 deletion cornflow-server/cornflow/tests/unit/test_apiview.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
"""
Unit test for the api views endpoint
Unit tests for the API views endpoint.
This module contains tests for the API views endpoint functionality, including:
- Authorization checks for different user roles
- Validation of API view listings
- Access control verification for endpoints
Classes
-------
TestApiViewListEndpoint
Tests for the API views list endpoint functionality
"""

# Import from internal modules
Expand All @@ -10,7 +20,25 @@


class TestApiViewListEndpoint(CustomTestCase):
"""
Test cases for the API views list endpoint.
This class tests the functionality of listing available API views, including:
- Authorization checks for different user roles
- Validation of returned API view data
- Access control for authorized and unauthorized roles
"""

def setUp(self):
"""
Set up test environment before each test.
Initializes test data including:
- Base test case setup
- Roles with access permissions
- Test payload with API view data
- Items to check in responses
"""
super().setUp()
self.roles_with_access = ApiViewListEndpoint.ROLES_WITH_ACCESS
self.payload = [
Expand All @@ -24,9 +52,18 @@ def setUp(self):
self.items_to_check = ["name", "description", "url_rule"]

def tearDown(self):
"""Clean up test environment after each test."""
super().tearDown()

def test_get_api_view_authorized(self):
"""
Test that authorized roles can access the API views list.
Verifies that users with proper roles can:
- Successfully access the API views endpoint
- Receive the correct list of views
- Get properly formatted view data with all required fields
"""
for role in self.roles_with_access:
self.token = self.create_user_with_role(role)
response = self.client.get(
Expand All @@ -45,6 +82,13 @@ def test_get_api_view_authorized(self):
)

def test_get_api_view_not_authorized(self):
"""
Test that unauthorized roles cannot access the API views list.
Verifies that users without proper roles:
- Are denied access to the API views endpoint
- Receive appropriate error responses with 403 status code
"""
for role in ROLES_MAP:
if role not in self.roles_with_access:
self.token = self.create_user_with_role(role)
Expand Down
Loading

0 comments on commit 656b116

Please sign in to comment.