|
13 | 13 | from django.test import override_settings |
14 | 14 | from django.urls import reverse |
15 | 15 |
|
| 16 | +from dje.admin import PolicyRulesConfigurationForm |
16 | 17 | from dje.copier import copy_object |
17 | 18 | from dje.filters import DataspaceFilter |
18 | 19 | from dje.filters import MissingInFilter |
19 | 20 | from dje.models import Dataspace |
| 21 | +from dje.models import DataspaceConfiguration |
20 | 22 | from dje.models import History |
21 | 23 | from dje.search import advanced_search |
22 | 24 | from dje.tests import add_perm |
|
25 | 27 | from dje.tests import create_superuser |
26 | 28 | from dje.tests import create_user |
27 | 29 | from organization.models import Owner |
| 30 | +from policy.rules import RULE_REGISTRY |
28 | 31 |
|
29 | 32 |
|
30 | 33 | class DataspacedModelAdminTestCase(TestCase): |
@@ -613,3 +616,78 @@ def test_admin_group_permission_export_csv(self): |
613 | 616 | 'attachment; filename="dejacode_group_permission.csv"', response["Content-Disposition"] |
614 | 617 | ) |
615 | 618 | self.assertEqual(b",change_license\r\nchange license,X\r\n", response.content) |
| 619 | + |
| 620 | + |
| 621 | +class PolicyRulesConfigurationFormTestCase(TestCase): |
| 622 | + def setUp(self): |
| 623 | + self.dataspace = Dataspace.objects.create(name="nexB") |
| 624 | + self.config = DataspaceConfiguration.objects.create(dataspace=self.dataspace) |
| 625 | + self.Form = PolicyRulesConfigurationForm |
| 626 | + |
| 627 | + def _bound_form(self, extra_data=None): |
| 628 | + data = {} |
| 629 | + for rule_type in RULE_REGISTRY: |
| 630 | + data[f"rule_{rule_type}_enabled"] = False |
| 631 | + data[f"rule_{rule_type}_threshold"] = "" |
| 632 | + if extra_data: |
| 633 | + data.update(extra_data) |
| 634 | + return self.Form(data=data, instance=self.config) |
| 635 | + |
| 636 | + def test_enabled_rule_included_in_config(self): |
| 637 | + form = self._bound_form({"rule_usage_policy_error_enabled": True}) |
| 638 | + self.assertTrue(form.is_valid(), form.errors) |
| 639 | + result = form.build_policy_rules_config() |
| 640 | + self.assertIn("usage_policy_error", result) |
| 641 | + self.assertTrue(result["usage_policy_error"]["is_active"]) |
| 642 | + |
| 643 | + def test_disabled_rule_not_in_config(self): |
| 644 | + form = self._bound_form() |
| 645 | + self.assertTrue(form.is_valid(), form.errors) |
| 646 | + result = form.build_policy_rules_config() |
| 647 | + self.assertEqual({}, result) |
| 648 | + |
| 649 | + def test_threshold_saved_when_set(self): |
| 650 | + form = self._bound_form( |
| 651 | + { |
| 652 | + "rule_usage_policy_error_enabled": True, |
| 653 | + "rule_usage_policy_error_threshold": "3", |
| 654 | + } |
| 655 | + ) |
| 656 | + self.assertTrue(form.is_valid(), form.errors) |
| 657 | + result = form.build_policy_rules_config() |
| 658 | + self.assertEqual(3, result["usage_policy_error"]["threshold"]) |
| 659 | + |
| 660 | + def test_initial_values_loaded_from_existing_config(self): |
| 661 | + self.config.policy_rules_config = { |
| 662 | + "usage_policy_error": {"is_active": True, "threshold": 7} |
| 663 | + } |
| 664 | + self.config.save() |
| 665 | + form = self.Form(instance=self.config) |
| 666 | + self.assertTrue(form.fields["rule_usage_policy_error_enabled"].initial) |
| 667 | + self.assertEqual(7, form.fields["rule_usage_policy_error_threshold"].initial) |
| 668 | + self.assertFalse(form.fields["rule_license_coverage_gap_enabled"].initial) |
| 669 | + |
| 670 | + def test_save_persists_policy_rules_config_to_db(self): |
| 671 | + form = self._bound_form({"rule_usage_policy_error_enabled": True}) |
| 672 | + self.assertTrue(form.is_valid(), form.errors) |
| 673 | + form.save() |
| 674 | + self.config.refresh_from_db() |
| 675 | + self.assertTrue(self.config.policy_rules_config["usage_policy_error"]["is_active"]) |
| 676 | + |
| 677 | + def test_inline_shows_rule_fieldsets_on_existing_dataspace(self): |
| 678 | + self.super_user = create_superuser("super_user", self.dataspace) |
| 679 | + self.client.login(username="super_user", password="secret") |
| 680 | + url = reverse("admin:dje_dataspace_change", args=[self.dataspace.pk]) |
| 681 | + response = self.client.get(url) |
| 682 | + self.assertEqual(200, response.status_code) |
| 683 | + self.assertContains(response, "Policy Rules Configuration") |
| 684 | + for rule_type in RULE_REGISTRY: |
| 685 | + self.assertContains(response, f"rule_{rule_type}_enabled") |
| 686 | + |
| 687 | + def test_inline_not_shown_on_dataspace_add(self): |
| 688 | + self.super_user = create_superuser("super_user", self.dataspace) |
| 689 | + self.client.login(username="super_user", password="secret") |
| 690 | + url = reverse("admin:dje_dataspace_add") |
| 691 | + response = self.client.get(url) |
| 692 | + self.assertEqual(200, response.status_code) |
| 693 | + self.assertNotContains(response, "rule_usage_policy_error_enabled") |
0 commit comments