From 220d718d043d8378f2cd641043f61227164d2d01 Mon Sep 17 00:00:00 2001 From: sergio_gimenez Date: Tue, 29 Oct 2024 08:30:33 +0100 Subject: [PATCH] Add unit tests --- device/tests/__init__.py | 0 device/tests/test_mock_device.py | 76 ++++++++++++++++++++++++++ device/tests/test_public_device_web.py | 67 +++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 device/tests/__init__.py create mode 100644 device/tests/test_mock_device.py create mode 100644 device/tests/test_public_device_web.py diff --git a/device/tests/__init__.py b/device/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/device/tests/test_mock_device.py b/device/tests/test_mock_device.py new file mode 100644 index 0000000..a3fe019 --- /dev/null +++ b/device/tests/test_mock_device.py @@ -0,0 +1,76 @@ +from device.models import Device +from unittest.mock import MagicMock + + +class TestDevice(Device): + """A test subclass of Device that overrides the database-dependent methods""" + # TODO Leaving commented bc not used, but might be useful at some point + # def get_annotations(self): + # """Return empty list instead of querying database""" + # return [] + + # def get_uuids(self): + # """Set uuids directly instead of querying""" + # self.uuids = ['uuid1', 'uuid2'] + + # def get_hids(self): + # """Set hids directly instead of querying""" + # self.hids = ['hid1', 'hid2'] + + # def get_evidences(self): + # """Set evidences directly instead of querying""" + # self.evidences = [] + + # def get_lots(self): + # """Set lots directly instead of querying""" + # self.lots = [] + + def get_last_evidence(self): + if not hasattr(self, '_evidence'): + self._evidence = MagicMock() + self._evidence.doc = { + 'type': 'Computer', + 'manufacturer': 'Test Manufacturer', + 'model': 'Test Model', + 'device': { + 'serialNumber': 'SN123456', + 'type': 'Computer' + } + } + self._evidence.get_manufacturer = lambda: 'Test Manufacturer' + self._evidence.get_model = lambda: 'Test Model' + self._evidence.get_chassis = lambda: 'Computer' + self._evidence.get_components = lambda: [ + { + 'type': 'CPU', + 'model': 'Intel i7', + 'manufacturer': 'Intel' + }, + { + 'type': 'RAM', + 'size': '8GB', + 'manufacturer': 'Kingston' + } + ] + self.last_evidence = self._evidence + + +class TestWebSnapshotDevice(TestDevice): + """A test subclass of Device that simulates a WebSnapshot device""" + + def get_last_evidence(self): + if not hasattr(self, '_evidence'): + self._evidence = MagicMock() + self._evidence.doc = { + 'type': 'WebSnapshot', + 'kv': { + 'URL': 'http://example.com', + 'Title': 'Test Page', + 'Timestamp': '2024-01-01' + }, + 'device': { + 'type': 'Laptop' + } + } + self.last_evidence = self._evidence + return self._evidence diff --git a/device/tests/test_public_device_web.py b/device/tests/test_public_device_web.py new file mode 100644 index 0000000..33efe05 --- /dev/null +++ b/device/tests/test_public_device_web.py @@ -0,0 +1,67 @@ +from django.test import TestCase, Client +from django.urls import reverse +from unittest.mock import patch +from device.views import PublicDeviceWebView +from device.tests.test_mock_device import TestDevice, TestWebSnapshotDevice + + +class PublicDeviceWebViewTests(TestCase): + def setUp(self): + self.client = Client() + self.test_id = "test123" + self.test_url = reverse('device:device_web', + kwargs={'pk': self.test_id}) + + def test_url_resolves_correctly(self): + """Test that the URL is constructed correctly""" + url = reverse('device:device_web', kwargs={'pk': self.test_id}) + self.assertEqual(url, f'/device/{self.test_id}/public/') + + @patch('device.views.Device') + def test_html_response(self, MockDevice): + test_device = TestDevice(id=self.test_id) + MockDevice.return_value = test_device + response = self.client.get(self.test_url) + + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'device_web.html') + + self.assertContains(response, 'Test Manufacturer') + self.assertContains(response, 'Test Model') + self.assertContains(response, 'Computer') + self.assertContains(response, self.test_id) + + self.assertContains(response, 'CPU') + self.assertContains(response, 'Intel') + self.assertContains(response, 'RAM') + self.assertContains(response, 'Kingston') + + @patch('device.views.Device') + def test_json_response(self, MockDevice): + test_device = TestDevice(id=self.test_id) + MockDevice.return_value = test_device + + response = self.client.get( + self.test_url, + HTTP_ACCEPT='application/json' + ) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'application/json') + + json_data = response.json() + self.assertEqual(json_data['id'], self.test_id) + self.assertEqual(json_data['shortid'], self.test_id[:6].upper()) + self.assertEqual(json_data['components'], test_device.components) + + @patch('device.views.Device') + def test_websnapshot_device(self, MockDevice): + test_device = TestWebSnapshotDevice(id=self.test_id) + MockDevice.return_value = test_device + response = self.client.get(self.test_url) + + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'device_web.html') + + self.assertContains(response, 'http://example.com') + self.assertContains(response, 'Test Page')