Skip to content

Commit

Permalink
✅ [#3005] Update/Add/Delete tests after task orchestration refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Dec 11, 2023
1 parent 8f9e6db commit 10cf117
Show file tree
Hide file tree
Showing 21 changed files with 1,428 additions and 428 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from openforms.config.models import GlobalConfiguration
from openforms.forms.constants import SubmissionAllowedChoices
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.submissions.tests.mixins import SubmissionsMixin
from openforms.tests.search_strategies import json_values
Expand Down Expand Up @@ -114,8 +115,8 @@ def test_appointment_data_is_recorded(self):
self.submission.refresh_from_db()
self.assertTrue(self.submission.privacy_policy_accepted)

@patch("openforms.submissions.api.mixins.on_completion")
def test_submission_is_completed(self, mock_on_completion):
@patch("openforms.submissions.api.mixins.on_post_submission_event")
def test_submission_is_completed(self, mock_on_post_submission_event):
appointment_datetime = timezone.make_aware(
datetime.combine(TODAY, time(15, 15))
)
Expand Down Expand Up @@ -147,7 +148,9 @@ def test_submission_is_completed(self, mock_on_completion):
self.assertIn("statusUrl", response.json())

# assert that the async celery task execution is scheduled
mock_on_completion.assert_called_once_with(self.submission.id)
mock_on_post_submission_event.assert_called_once_with(
self.submission.id, PostSubmissionEvents.on_completion
)

def test_retry_does_not_cause_integrity_error(self):
# When there are on_completion processing errors, the client will re-post the
Expand Down
6 changes: 4 additions & 2 deletions src/openforms/appointments/tests/test_confirmation_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from unittest.mock import patch

from django.core import mail
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils import timezone
from django.utils.html import escape
from django.utils.translation import gettext as _

from openforms.config.models import GlobalConfiguration
from openforms.formio.typing import Component
from openforms.submissions.tasks import schedule_emails
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.submissions.utils import send_confirmation_email
from openforms.utils.tests.html_assert import HTMLAssertMixin
Expand Down Expand Up @@ -93,6 +94,7 @@ def get_required_customer_fields(self, *args, **kwargs) -> list[Component]:
return [LAST_NAME, EMAIL]


@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
class AppointmentCreationConfirmationMailTests(HTMLAssertMixin, TestCase):
@classmethod
def setUpTestData(cls):
Expand Down Expand Up @@ -127,7 +129,7 @@ def test_send_confirmation_mail_disabled(self):
contact_details={"lastName": "Powers", "email": "[email protected]"},
)

send_confirmation_email(appointment.submission)
schedule_emails(appointment.submission.id)

self.assertEqual(len(mail.outbox), 0)

Expand Down
24 changes: 17 additions & 7 deletions src/openforms/payments/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

from openforms.registrations.base import BasePlugin
from openforms.registrations.registry import Registry
from openforms.submissions.constants import RegistrationStatuses
from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses
from openforms.submissions.models import Submission
from openforms.submissions.tests.factories import SubmissionFactory

from ..constants import PaymentStatus
from ..services import update_submission_payment_registration
from ..tasks import update_submission_payment_status
from .factories import SubmissionPaymentFactory


Expand Down Expand Up @@ -65,7 +65,9 @@ def test_submission_default(self):
self.assertFalse(submission.payment_registered)

with patch.object(self.plugin, "update_payment_status") as update_mock:
update_submission_payment_registration(submission)
update_submission_payment_status(
submission.id, PostSubmissionEvents.on_completion
)
update_mock.assert_not_called()

def test_submission_complete(self):
Expand All @@ -79,7 +81,9 @@ def test_submission_complete(self):

# now check if we update
with patch.object(self.plugin, "update_payment_status") as update_mock:
update_submission_payment_registration(submission)
update_submission_payment_status(
submission.id, PostSubmissionEvents.on_completion
)
update_mock.assert_called_once_with(submission, dict())

payment.refresh_from_db()
Expand All @@ -91,7 +95,9 @@ def test_submission_complete(self):

# check we don't update again
with patch.object(self.plugin, "update_payment_status") as update_mock:
update_submission_payment_registration(submission)
update_submission_payment_status(
submission.id, PostSubmissionEvents.on_retry
)
update_mock.assert_not_called()

def test_submission_bad(self):
Expand All @@ -111,7 +117,9 @@ def test_submission_bad(self):

# now check we don't update on these
with patch.object(self.plugin, "update_payment_status") as update_mock:
update_submission_payment_registration(submission)
update_submission_payment_status(
submission.id, PostSubmissionEvents.on_completion
)
update_mock.assert_not_called()


Expand Down Expand Up @@ -143,7 +151,9 @@ def test_update_payment_registration(self):

def _thread(sid):
sub = Submission.objects.get(id=sid)
update_submission_payment_registration(sub)
update_submission_payment_status(
sub.id, PostSubmissionEvents.on_completion
)
close_old_connections()

threads = [Thread(target=_thread, args=(submission.id,)) for _ in range(5)]
Expand Down
43 changes: 25 additions & 18 deletions src/openforms/payments/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.test.client import RequestFactory

from openforms.config.models import GlobalConfiguration
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tests.factories import SubmissionFactory

from ..base import BasePlugin, PaymentInfo
Expand Down Expand Up @@ -33,8 +34,8 @@ class ViewsTests(TestCase):
@override_settings(
CORS_ALLOW_ALL_ORIGINS=False, CORS_ALLOWED_ORIGINS=["http://allowed.foo"]
)
@patch("openforms.payments.views.update_submission_payment_status.delay")
def test_views(self, update_payments_mock):
@patch("openforms.payments.views.on_post_submission_event")
def test_views(self, on_post_submission_event_mock):
register = Registry()
register("plugin1")(Plugin)
plugin = register["plugin1"]
Expand Down Expand Up @@ -84,28 +85,30 @@ def test_views(self, update_payments_mock):
self.assertRegex(url, r"^http://")

with self.subTest("return ok"):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()

with self.captureOnCommitCallbacks(execute=True):
response = self.client.get(url)

self.assertEqual(response.content, b"")
self.assertEqual(response.status_code, 302)

update_payments_mock.assert_called_once_with(submission.id)
on_post_submission_event_mock.assert_called_once_with(
submission.id, PostSubmissionEvents.on_payment_complete
)

with self.subTest("return bad method"):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()

with self.captureOnCommitCallbacks(execute=True):
response = self.client.post(url)

self.assertEqual(response.status_code, 405)
self.assertEqual(response["Allow"], "GET")
update_payments_mock.assert_not_called()
on_post_submission_event_mock.assert_not_called()

with self.subTest("return bad plugin"):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()
bad_payment = SubmissionPaymentFactory.for_backend("bad_plugin")
bad_url = bad_plugin.get_return_url(base_request, bad_payment)

Expand All @@ -114,10 +117,10 @@ def test_views(self, update_payments_mock):

self.assertEqual(response.data["detail"], "unknown plugin")
self.assertEqual(response.status_code, 404)
update_payments_mock.assert_not_called()
on_post_submission_event_mock.assert_not_called()

with self.subTest("return bad redirect"):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()
bad_payment = SubmissionPaymentFactory.for_backend(
"plugin1", submission__form_url="http://bad.com/form"
)
Expand All @@ -128,7 +131,7 @@ def test_views(self, update_payments_mock):

self.assertEqual(response.data["detail"], "redirect not allowed")
self.assertEqual(response.status_code, 400)
update_payments_mock.assert_not_called()
on_post_submission_event_mock.assert_not_called()

# check the webhook view
url = plugin.get_webhook_url(base_request)
Expand All @@ -137,43 +140,47 @@ def test_views(self, update_payments_mock):
with self.subTest("webhook ok"), patch.object(
plugin, "handle_webhook", return_value=payment
):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()

with self.captureOnCommitCallbacks(execute=True):
response = self.client.post(url)

self.assertEqual(response.content, b"")
self.assertEqual(response.status_code, 200)

update_payments_mock.assert_called_once_with(submission.id)
on_post_submission_event_mock.assert_called_once_with(
submission.id, PostSubmissionEvents.on_payment_complete
)

with self.subTest("webhook bad method"):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()

with self.captureOnCommitCallbacks(execute=True):
response = self.client.get(url)

self.assertEqual(response.status_code, 405)
self.assertEqual(response["Allow"], "POST")
update_payments_mock.assert_not_called()
on_post_submission_event_mock.assert_not_called()

with self.subTest("webhook bad plugin"):
update_payments_mock.reset_mock()
on_post_submission_event_mock.reset_mock()
bad_url = bad_plugin.get_webhook_url(base_request)

with self.captureOnCommitCallbacks(execute=True):
response = self.client.get(bad_url)

self.assertEqual(response.data["detail"], "unknown plugin")
self.assertEqual(response.status_code, 404)
update_payments_mock.assert_not_called()
on_post_submission_event_mock.assert_not_called()

@override_settings(
CORS_ALLOW_ALL_ORIGINS=False, CORS_ALLOWED_ORIGINS=["http://allowed.foo"]
)
@patch("openforms.plugins.registry.GlobalConfiguration.get_solo")
@patch("openforms.payments.views.update_submission_payment_status.delay")
def test_start_plugin_not_enabled(self, update_payments_mock, mock_get_solo):
@patch("openforms.payments.views.on_post_submission_event")
def test_start_plugin_not_enabled(
self, on_post_submission_event_mock, mock_get_solo
):
register = Registry()
register("plugin1")(Plugin)
plugin = register["plugin1"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from openforms.authentication.tests.factories import RegistratorInfoFactory
from openforms.forms.tests.factories import FormVariableFactory
from openforms.logging.models import TimelineLogProxy
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tasks import pre_registration
from openforms.submissions.tests.factories import (
SubmissionFactory,
Expand Down Expand Up @@ -2582,6 +2583,7 @@ def test_pre_registration_goes_wrong_sets_internal_reference(self, m):
"zds_documenttype_omschrijving_inzending": "aaabbc",
},
kvk="12345678",
completed_not_preregistered=True,
)

m.post(
Expand All @@ -2605,7 +2607,7 @@ def test_pre_registration_goes_wrong_sets_internal_reference(self, m):
"openforms.submissions.public_references.get_reference_for_submission",
return_value="OF-TEST!",
):
pre_registration(submission.id)
pre_registration(submission.id, PostSubmissionEvents.on_completion)

submission.refresh_from_db()

Expand Down Expand Up @@ -2681,7 +2683,7 @@ def test_backend_preregistration_sets_reference(self, m):

self.assertEqual(submission.public_registration_reference, "")

pre_registration(submission.id)
pre_registration(submission.id, PostSubmissionEvents.on_completion)
submission.refresh_from_db()

self.assertEqual(submission.public_registration_reference, "ZAAK-FOOO")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from openforms.config.models import GlobalConfiguration
from openforms.forms.tests.factories import FormRegistrationBackendFactory
from openforms.submissions.constants import RegistrationStatuses
from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses
from openforms.submissions.tasks import pre_registration
from openforms.submissions.tests.factories import SubmissionFactory
from stuf.stuf_zds.models import StufZDSConfig
Expand Down Expand Up @@ -114,8 +114,8 @@ def test_zaak_creation_fails_number_is_reserved(self):
additional_matcher=match_text("zakLk01"),
)

pre_registration(self.submission.id)
register_submission(self.submission.id)
pre_registration(self.submission.id, PostSubmissionEvents.on_completion)
register_submission(self.submission.id, PostSubmissionEvents.on_completion)
with self.subTest("Initial registration fails"):
self.submission.refresh_from_db()
self.assertEqual(
Expand All @@ -130,7 +130,7 @@ def test_zaak_creation_fails_number_is_reserved(self):

with self.subTest("Retry does not create new zaaknummer"):
with self.assertRaises(RegistrationFailed):
register_submission(self.submission.id)
register_submission(self.submission.id, PostSubmissionEvents.on_retry)

self.submission.refresh_from_db()
intermediate_results = self.submission.registration_result["intermediate"]
Expand Down Expand Up @@ -191,8 +191,8 @@ def test_doc_id_creation_fails_zaak_registration_succeeds(self):
additional_matcher=match_text("genereerDocumentIdentificatie_Di02"),
)

pre_registration(self.submission.id)
register_submission(self.submission.id)
pre_registration(self.submission.id, PostSubmissionEvents.on_completion)
register_submission(self.submission.id, PostSubmissionEvents.on_completion)
with self.subTest("Document id generation fails"):
self.submission.refresh_from_db()
self.assertEqual(
Expand All @@ -210,7 +210,7 @@ def test_doc_id_creation_fails_zaak_registration_succeeds(self):

with self.subTest("Retry does not create new zaak"):
with self.assertRaises(RegistrationFailed):
register_submission(self.submission.id)
register_submission(self.submission.id, PostSubmissionEvents.on_retry)

self.submission.refresh_from_db()
intermediate_results = self.submission.registration_result["intermediate"]
Expand Down Expand Up @@ -287,8 +287,8 @@ def test_doc_creation_failure_does_not_create_more_doc_ids(self):
additional_matcher=match_text("edcLk01"),
)

pre_registration(self.submission.id)
register_submission(self.submission.id)
pre_registration(self.submission.id, PostSubmissionEvents.on_completion)
register_submission(self.submission.id, PostSubmissionEvents.on_completion)
with self.subTest("Document id generation fails"):
self.submission.refresh_from_db()
self.assertEqual(
Expand All @@ -309,7 +309,7 @@ def test_doc_creation_failure_does_not_create_more_doc_ids(self):

with self.subTest("Retry does not create new zaak"):
with self.assertRaises(RegistrationFailed):
register_submission(self.submission.id)
register_submission(self.submission.id, PostSubmissionEvents.on_retry)

self.submission.refresh_from_db()
intermediate_results = self.submission.registration_result["intermediate"]
Expand Down
Loading

0 comments on commit 10cf117

Please sign in to comment.