Skip to content

Commit

Permalink
[#3477] Added missing test
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Oct 26, 2023
1 parent fc68e8f commit 96d7aed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/openforms/config/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from collections import defaultdict
from functools import partial
from typing import Tuple

from django.contrib.admin.options import get_content_type_for_model
from django.contrib.contenttypes.fields import GenericForeignKey
Expand Down Expand Up @@ -657,7 +656,7 @@ def as_dict(self):


class CSPSettingManager(models.Manager.from_queryset(CSPSettingQuerySet)):
def set_for(self, obj: models.Model, settings: list[Tuple[str, str]]) -> None:
def set_for(self, obj: models.Model, settings: list[tuple[str, str]]) -> None:
"""
Deletes all the connected csp settings and creates new ones based on the new provided data.
"""
Expand Down
34 changes: 34 additions & 0 deletions src/openforms/config/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.test import TestCase

from openforms.config.constants import CSPDirective
from openforms.config.models import CSPSetting
from openforms.payments.contrib.ogone.tests.factories import OgoneMerchantFactory


class CSPSettingManagerTests(TestCase):
def test_csp_setting_is_set(self):
merchant = OgoneMerchantFactory()

CSPSetting.objects.set_for(
merchant, [(CSPDirective.FORM_ACTION, "http://example.com")]
)
csp = CSPSetting.objects.get()

self.assertEqual(csp.content_object, merchant)
self.assertEqual(csp.directive, CSPDirective.FORM_ACTION)
self.assertEqual(csp.value, "http://example.com")

def test_csp_setting_is_not_set_with_wrong_directive(self):
merchant = OgoneMerchantFactory()

with self.assertLogs() as logs:
CSPSetting.objects.set_for(
merchant, [("wrong-directive", "http://example.com")]
)
message = logs.records[0].getMessage()

self.assertEqual(
message,
f"Could not create csp setting for model '{merchant.__str__()}'. 'wrong-directive' is not a valid directive.",
)
self.assertTrue(CSPSetting.objects.none)

0 comments on commit 96d7aed

Please sign in to comment.