|
| 1 | +# Copyright 2025 Marcus Furlong <furlongm@gmail.com> |
| 2 | +# |
| 3 | +# This file is part of Patchman. |
| 4 | +# |
| 5 | +# Patchman is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, version 3 only. |
| 8 | +# |
| 9 | +# Patchman is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with Patchman. If not, see <http://www.gnu.org/licenses/> |
| 16 | + |
| 17 | +from django.test import TestCase, override_settings |
| 18 | +from django.utils import timezone |
| 19 | + |
| 20 | +from errata.models import Erratum |
| 21 | +from operatingsystems.models import OSRelease |
| 22 | +from security.models import CVE |
| 23 | + |
| 24 | + |
| 25 | +@override_settings( |
| 26 | + CELERY_TASK_ALWAYS_EAGER=True, |
| 27 | + CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}} |
| 28 | +) |
| 29 | +class ErratumMethodTests(TestCase): |
| 30 | + """Tests for Erratum model methods.""" |
| 31 | + |
| 32 | + def test_erratum_creation(self): |
| 33 | + """Test creating an Erratum.""" |
| 34 | + erratum = Erratum.objects.create( |
| 35 | + name='USN-1234-1', |
| 36 | + e_type='security', |
| 37 | + synopsis='Security update', |
| 38 | + issue_date=timezone.now(), |
| 39 | + ) |
| 40 | + self.assertEqual(erratum.name, 'USN-1234-1') |
| 41 | + |
| 42 | + def test_erratum_str(self): |
| 43 | + """Test Erratum __str__ method.""" |
| 44 | + erratum = Erratum.objects.create( |
| 45 | + name='USN-1234-1', |
| 46 | + e_type='security', |
| 47 | + synopsis='Security update', |
| 48 | + issue_date=timezone.now(), |
| 49 | + ) |
| 50 | + self.assertIn('USN-1234-1', str(erratum)) |
| 51 | + |
| 52 | + def test_erratum_get_absolute_url(self): |
| 53 | + """Test Erratum.get_absolute_url().""" |
| 54 | + erratum = Erratum.objects.create( |
| 55 | + name='USN-1234-1', |
| 56 | + e_type='security', |
| 57 | + synopsis='Security update', |
| 58 | + issue_date=timezone.now(), |
| 59 | + ) |
| 60 | + url = erratum.get_absolute_url() |
| 61 | + self.assertIn(erratum.name, url) |
| 62 | + |
| 63 | + def test_erratum_unique_name(self): |
| 64 | + """Test Erratum name is unique.""" |
| 65 | + Erratum.objects.create( |
| 66 | + name='USN-1234-1', |
| 67 | + e_type='security', |
| 68 | + synopsis='Security update', |
| 69 | + issue_date=timezone.now(), |
| 70 | + ) |
| 71 | + from django.db import IntegrityError |
| 72 | + with self.assertRaises(IntegrityError): |
| 73 | + Erratum.objects.create( |
| 74 | + name='USN-1234-1', |
| 75 | + e_type='bugfix', |
| 76 | + synopsis='Bugfix update', |
| 77 | + issue_date=timezone.now(), |
| 78 | + ) |
| 79 | + |
| 80 | + def test_erratum_with_cves(self): |
| 81 | + """Test Erratum with associated CVEs.""" |
| 82 | + erratum = Erratum.objects.create( |
| 83 | + name='USN-1234-1', |
| 84 | + e_type='security', |
| 85 | + synopsis='Security update', |
| 86 | + issue_date=timezone.now(), |
| 87 | + ) |
| 88 | + cve = CVE.objects.create(cve_id='CVE-2024-12345') |
| 89 | + erratum.cves.add(cve) |
| 90 | + self.assertIn(cve, erratum.cves.all()) |
| 91 | + |
| 92 | + def test_erratum_with_osreleases(self): |
| 93 | + """Test Erratum with associated OS releases.""" |
| 94 | + erratum = Erratum.objects.create( |
| 95 | + name='USN-1234-1', |
| 96 | + e_type='security', |
| 97 | + synopsis='Security update', |
| 98 | + issue_date=timezone.now(), |
| 99 | + ) |
| 100 | + release = OSRelease.objects.create(name='Ubuntu 22.04') |
| 101 | + erratum.osreleases.add(release) |
| 102 | + self.assertIn(release, erratum.osreleases.all()) |
| 103 | + |
| 104 | + def test_security_erratum(self): |
| 105 | + """Test creating a security erratum.""" |
| 106 | + erratum = Erratum.objects.create( |
| 107 | + name='RHSA-2024:1234', |
| 108 | + e_type='security', |
| 109 | + synopsis='Important security update', |
| 110 | + issue_date=timezone.now(), |
| 111 | + ) |
| 112 | + self.assertEqual(erratum.e_type, 'security') |
| 113 | + |
| 114 | + def test_bugfix_erratum(self): |
| 115 | + """Test creating a bugfix erratum.""" |
| 116 | + erratum = Erratum.objects.create( |
| 117 | + name='RHBA-2024:1234', |
| 118 | + e_type='bugfix', |
| 119 | + synopsis='Bug fix update', |
| 120 | + issue_date=timezone.now(), |
| 121 | + ) |
| 122 | + self.assertEqual(erratum.e_type, 'bugfix') |
0 commit comments