Skip to content

Commit

Permalink
Make sure a Package dependency cannot resolve to itself #138
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Aug 2, 2024
1 parent 4256988 commit 6c1cd66
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
9 changes: 9 additions & 0 deletions product_portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,3 +1417,12 @@ class Meta:

def __str__(self):
return self.dependency_uid

def save(self, *args, **kwargs):
"""Make sure a Package dependency cannot resolve to itself."""
if self.for_package and self.resolved_to_package:
if self.for_package == self.resolved_to_package:
raise ValidationError(
"The 'for_package' cannot be the same as 'resolved_to_package'."
)
super().save(*args, **kwargs)
42 changes: 12 additions & 30 deletions product_portfolio/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,34 +904,16 @@ def test_product_portfolio_scancode_project_model_can_start_import(self):
scancode_project.status = ScanCodeProject.Status.FAILURE
self.assertFalse(scancode_project.can_start_import)

def test_package_model_queryset_declared_dependencies_count(self):
product2 = Product.objects.create(name="Product2", dataspace=self.dataspace)
product3 = Product.objects.create(name="Product3", dataspace=self.dataspace)
def test_product_dependency_model_save_validation(self):
package1 = Package.objects.create(filename="package1", dataspace=self.dataspace)
package2 = Package.objects.create(filename="package2", dataspace=self.dataspace)

product1_qs = Package.objects.declared_dependencies_count(self.product1)
self.assertEqual(0, product1_qs.get(pk=package1.pk).declared_dependencies_count)
self.assertEqual(0, product1_qs.get(pk=package2.pk).declared_dependencies_count)
product2_qs = Package.objects.declared_dependencies_count(product2)
self.assertEqual(0, product2_qs.get(pk=package1.pk).declared_dependencies_count)
self.assertEqual(0, product2_qs.get(pk=package2.pk).declared_dependencies_count)
product3_qs = Package.objects.declared_dependencies_count(product3)
self.assertEqual(0, product3_qs.get(pk=package1.pk).declared_dependencies_count)
self.assertEqual(0, product3_qs.get(pk=package2.pk).declared_dependencies_count)

ProductDependency.objects.create(
product=self.product1, for_package=package1, dataspace=self.dataspace
)
ProductDependency.objects.create(
product=product2, for_package=package1, dataspace=self.dataspace
)
product1_qs = Package.objects.declared_dependencies_count(self.product1)
self.assertEqual(1, product1_qs.get(pk=package1.pk).declared_dependencies_count)
self.assertEqual(0, product1_qs.get(pk=package2.pk).declared_dependencies_count)
product2_qs = Package.objects.declared_dependencies_count(product2)
self.assertEqual(1, product2_qs.get(pk=package1.pk).declared_dependencies_count)
self.assertEqual(0, product2_qs.get(pk=package2.pk).declared_dependencies_count)
product3_qs = Package.objects.declared_dependencies_count(product3)
self.assertEqual(0, product3_qs.get(pk=package1.pk).declared_dependencies_count)
self.assertEqual(0, product3_qs.get(pk=package2.pk).declared_dependencies_count)
with self.assertRaises(ValidationError) as cm:
ProductDependency.objects.create(
product=self.product1,
for_package=package1,
resolved_to_package=package1,
dataspace=self.dataspace,
)
self.assertEqual(
["The 'for_package' cannot be the same as 'resolved_to_package'."],
cm.exception.messages,
)

0 comments on commit 6c1cd66

Please sign in to comment.