Test helpers

HTML assertions

class openforms.utils.tests.html_assert.HTMLAssertMixin

Mixin HTML-related assertions.

assertHTMLValid(html_text)

check basic HTML syntax validity

assertNotTagWithTextIn(tag, text, document_str)

check for html tags and their content while ignoring tag attributes

assertTagWithTextIn(tag, text, document_str)

check for html tags and their content while ignoring tag attributes

openforms.utils.tests.html_assert.strip_all_attributes(document: str) str

Reduce an HTML document to just the tags, stripping any attributes.

Useful for testing with self.assertInHTML without having to worry about class names, style tags etc.

Taken and adapted from https://stackoverflow.com/a/7472003

class openforms.utils.tests.webtest_base.WebTestPyQueryMixin

Mixin PyQuery assertions for WebTest tests.

Frontend redirects

class openforms.frontend.tests.FrontendRedirectMixin

A mixin providing a helper method checking frontend redirects.

assertRedirectsToFrontend(response: django.http.response.HttpResponse, frontend_base_url: str, action: Literal['resume', 'afspraak-annuleren', 'cosign', 'payment'], action_params: Optional[dict[str, str]] = None, **kwargs: Any) None

Assert that a response redirected to a specific frontend URL.

Parameters
  • response – The response to test the redirection on.

  • frontend_base_url – The base URL of the frontend.

  • action – The SDK action performed.

  • action_params – Optional parameters for the action.

  • **kwargs – Additional kwargs to be passed to django.test.SimpleTestCase.assertRedirects().

Migrations

class openforms.utils.tests.test_migrations.TestMigrations(methodName='runTest')

Test the effect of applying a migration

Adapted from https://www.caktusgroup.com/blog/2016/02/02/writing-unit-tests-django-migrations/

setUp()

Hook method for setting up the test fixture before exercising it.

classmethod tearDownClass()

Hook method for deconstructing the class fixture after running all tests in the class.

Formio assertions

class openforms.formio.tests.assertions.FormioMixin
assertFormioComponent(configuration: dict[str, 'JSONPrimitive | JSONObject | list[JSONValue]'], key: str, properties_map: dict[str, 'JSONPrimitive | JSONObject | list[JSONValue]']) None

Assert that the formio component with specified key has the expected properties.

Parameters
  • configuration – Formio form configuration

  • key – the unique key of the component to check

  • properties_map – a mapping of formio property name to expected property value. Note that the dict keys can be dotted paths for nested properties.

Recording HTTP traffic

Open Forms configuration and wrapper around vcr.py.

There are essentially two approaches for testing the interaction with third party services:

  • Mocking the requests using requests_mock

  • Recording the real interactions and replaying those during tests

The tooling here assists with the second approach.

Advantages of this approach

  • no bugs/mistakes in mock data/setup, as you are talking to a real service

  • no need to manually update mocks or create them, you can automatically (re-)record the interactions

  • if/when the remote service changes, you just run the same tests again while re-recording to capture the new behaviour

Disadvantages

  • you need access to a real implementation and sharing those credentials in an open source project is a challenge

  • there is a risk of exposing data you didn’t mean to expose

Strategies to avoid leaking information

The host/domain of a particular service provided by a client to test with should not be exposed, as these are internal domains. You can use mitmproxy to hide this, e.g.:

mitmdump --mode reverse:https://example.com --ssl-insecure

If it is a SOAP service that requires client certificates for in-band messages, the --set client_certs=DIRECTORY|FILE flag of mitmproxy to setup mTLS can’t help. E.g. the Suwinet client (and prefill plugin) tests rewrite urls in requests and responses with a url from an environment variable (see dotenv-example).

Specifying the record mode

You can set the environment variable VCR_RECORD_MODE to any of the supported record modes.

Note

When you use VCR for tests with obfuscated URLs or credentials (or any sensitive data in general), you must document this information (in Taiga) so that other people have all the necessary information/steps at hand to re-record cassettes.

Re-recording is done as part of the release process.

class openforms.utils.tests.vcr.OFVCRMixin

Mixin to use VCR in your unit tests.

Using this mixin will result in HTTP requests/responses being recorded.

VCR_TEST_FILES: pathlib.Path

A pathlib.Path instance where the cassettes should be stored.