|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from django.urls import reverse |
| 4 | +from rest_framework import status |
| 5 | +from rest_framework.test import APITestCase |
| 6 | + |
| 7 | +from core.models import Automation |
| 8 | +from core.models import ControllerLabel |
| 9 | +from core.models import Pattern |
| 10 | +from core.models import PatternInstance |
| 11 | +from core.models import Task |
| 12 | + |
| 13 | + |
| 14 | +class SharedDataMixin: |
| 15 | + @classmethod |
| 16 | + def setUpTestData(cls): |
| 17 | + cls.pattern = Pattern.objects.create( |
| 18 | + collection_name="mynamespace.mycollection", |
| 19 | + collection_version="1.0.0", |
| 20 | + collection_version_uri="https://example.com/mynamespace/mycollection/", |
| 21 | + pattern_name="example_pattern", |
| 22 | + pattern_definition={"key": "value"}, |
| 23 | + ) |
| 24 | + |
| 25 | + cls.pattern_instance = PatternInstance.objects.create( |
| 26 | + organization_id=1, |
| 27 | + controller_project_id=123, |
| 28 | + controller_ee_id=456, |
| 29 | + credentials={"user": "admin"}, |
| 30 | + executors=[{"executor_type": "container"}], |
| 31 | + pattern=cls.pattern, |
| 32 | + ) |
| 33 | + |
| 34 | + cls.label = ControllerLabel.objects.create(label_id=5) |
| 35 | + cls.pattern_instance.controller_labels.add(cls.label) |
| 36 | + |
| 37 | + cls.automation = Automation.objects.create( |
| 38 | + automation_type="job_template", |
| 39 | + automation_id=789, |
| 40 | + primary=True, |
| 41 | + pattern_instance=cls.pattern_instance, |
| 42 | + ) |
| 43 | + |
| 44 | + cls.task = Task.objects.create(status="Running", details={"progress": "50%"}) |
| 45 | + |
| 46 | + |
| 47 | +class PatternInstanceViewSetTest(SharedDataMixin, APITestCase): |
| 48 | + def test_pattern_instance_list_view(self): |
| 49 | + url = reverse("patterninstance-list") |
| 50 | + response = self.client.get(url) |
| 51 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 52 | + self.assertEqual(len(response.data), 1) |
| 53 | + |
| 54 | + def test_pattern_instance_detail_view(self): |
| 55 | + url = reverse("patterninstance-detail", args=[self.pattern_instance.pk]) |
| 56 | + response = self.client.get(url) |
| 57 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 58 | + self.assertEqual(response.data["organization_id"], 1) |
| 59 | + |
| 60 | + @patch("core.views.async_to_sync") |
| 61 | + def test_pattern_instance_create_view(self, mock_async_to_sync): |
| 62 | + url = reverse("patterninstance-list") |
| 63 | + data = { |
| 64 | + "organization_id": 2, |
| 65 | + "controller_project_id": 0, |
| 66 | + "controller_ee_id": 0, |
| 67 | + "credentials": {"user": "tester"}, |
| 68 | + "executors": [], |
| 69 | + "pattern": self.pattern.id, |
| 70 | + } |
| 71 | + |
| 72 | + response = self.client.post(url, data, format="json") |
| 73 | + self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) |
| 74 | + |
| 75 | + instance = PatternInstance.objects.get(organization_id=2) |
| 76 | + self.assertIsNotNone(instance) |
| 77 | + |
| 78 | + task_id = response.data["task_id"] |
| 79 | + task = Task.objects.get(id=task_id) |
| 80 | + self.assertEqual(task.status, "Initiated") |
| 81 | + |
| 82 | + mock_async_to_sync.assert_called_once() |
| 83 | + self.assertIn("task_id", response.data) |
| 84 | + self.assertIn("message", response.data) |
| 85 | + |
| 86 | + |
| 87 | +class AutomationViewSetTest(SharedDataMixin, APITestCase): |
| 88 | + def test_automation_list_view(self): |
| 89 | + url = reverse("automation-list") |
| 90 | + response = self.client.get(url) |
| 91 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 92 | + self.assertEqual(len(response.data), 1) |
| 93 | + |
| 94 | + def test_automation_detail_view(self): |
| 95 | + url = reverse("automation-detail", args=[self.automation.pk]) |
| 96 | + response = self.client.get(url) |
| 97 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 98 | + self.assertEqual(response.data["automation_type"], "job_template") |
| 99 | + |
| 100 | + |
| 101 | +class ControllerLabelViewSetTest(SharedDataMixin, APITestCase): |
| 102 | + def test_label_list_view(self): |
| 103 | + url = reverse("controllerlabel-list") |
| 104 | + response = self.client.get(url) |
| 105 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 106 | + self.assertEqual(len(response.data), 1) |
| 107 | + |
| 108 | + def test_label_detail_view(self): |
| 109 | + url = reverse("controllerlabel-detail", args=[self.label.id]) |
| 110 | + response = self.client.get(url) |
| 111 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 112 | + self.assertIn('id', response.data) |
| 113 | + self.assertIn('label_id', response.data) |
| 114 | + self.assertEqual(response.data['label_id'], 5) |
0 commit comments