Skip to content

Commit

Permalink
✅ [#4398] Fix failing tests due to new ownership check
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Oct 29, 2024
1 parent ca09bd7 commit 6e9b708
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/openforms/authentication/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from unittest.mock import patch

from django.contrib.sessions.middleware import SessionMiddleware
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from django.test import TestCase, override_settings, tag
from django.test.client import RequestFactory
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -32,6 +35,11 @@
from .mocks import FailingPlugin, Plugin, RequiresAdminPlugin, mock_register


def add_session_to_request(request: WSGIRequest) -> None:
middleware = SessionMiddleware(lambda x: HttpResponse())
middleware.process_request(request)


class AuthenticationFlowTests(APITestCase):
@override_settings(
CORS_ALLOW_ALL_ORIGINS=False, CORS_ALLOWED_ORIGINS=["http://foo.bar"]
Expand All @@ -54,6 +62,7 @@ def test_standard_authentication_flow(self):
# we need an arbitrary request
factory = RequestFactory()
init_request = factory.get("/foo")
add_session_to_request(init_request)

next_url = "http://foo.bar"
bad_url = "http://buzz.bazz"
Expand All @@ -65,24 +74,28 @@ def test_standard_authentication_flow(self):

with self.subTest("start ok"):
request = factory.get(url, {"next": next_url})
add_session_to_request(request)
response = start_view(request, slug=form.slug, plugin_id=plugin.identifier)
self.assertEqual(response.content, b"start")
self.assertEqual(response.status_code, 200)

with self.subTest("start missing next"):
request = factory.get(url)
add_session_to_request(request)
response = start_view(request, slug=form.slug, plugin_id=plugin.identifier)
self.assertEqual(response.content, b"missing 'next' parameter")
self.assertEqual(response.status_code, 400)

with self.subTest("start bad plugin"):
request = factory.get(url, {"next": next_url})
add_session_to_request(request)
response = start_view(request, slug=form.slug, plugin_id="bad_plugin")
self.assertEqual(response.content, b"unknown plugin")
self.assertEqual(response.status_code, 400)

with self.subTest("start bad redirect"):
request = factory.get(url, {"next": bad_url})
add_session_to_request(request)
response = start_view(request, slug=form.slug, plugin_id=plugin.identifier)
self.assertEqual(response.content, b"redirect not allowed")
self.assertEqual(response.status_code, 400)
Expand All @@ -94,25 +107,29 @@ def test_standard_authentication_flow(self):

with self.subTest("return ok"):
request = factory.get(url, {"next": next_url})
add_session_to_request(request)
response = return_view(request, slug=form.slug, plugin_id=plugin.identifier)
self.assertEqual(response.content, b"")
self.assertEqual(response.status_code, 302)

with self.subTest("return bad method"):
request = factory.post(f"{url}?next={next_url}")
add_session_to_request(request)
response = return_view(request, slug=form.slug, plugin_id=plugin.identifier)
self.assertEqual(response.content, b"")
self.assertEqual(response.status_code, 405)
self.assertEqual(response["Allow"], "GET")

with self.subTest("return bad plugin"):
request = factory.get(url, {"next": next_url})
add_session_to_request(request)
response = return_view(request, slug=form.slug, plugin_id="bad_plugin")
self.assertEqual(response.content, b"unknown plugin")
self.assertEqual(response.status_code, 400)

with self.subTest("return bad redirect"):
request = factory.get(url, {"next": bad_url})
add_session_to_request(request)
response = return_view(request, slug=form.slug, plugin_id=plugin.identifier)
self.assertEqual(response.content, b"redirect not allowed")
self.assertEqual(response.status_code, 400)
Expand All @@ -133,14 +150,17 @@ def test_plugin_start_failure_redirects(self):
# we need an arbitrary request
factory = RequestFactory()
init_request = factory.get("/foo")
add_session_to_request(init_request)

# actual test starts here
next_url = furl("http://foo.bar?bazz=buzz")
url = plugin.get_start_url(init_request, form)
start_view = AuthenticationStartView.as_view(register=register)

request = factory.get(url, {"next": next_url})
add_session_to_request(request)
response = start_view(
factory.get(url, {"next": next_url}),
request,
slug=form.slug,
plugin_id="plugin1",
)
Expand Down Expand Up @@ -169,13 +189,16 @@ def test_plugin_start_failure_not_enabled(self, mock_get_solo):
# we need an arbitrary request
factory = RequestFactory()
init_request = factory.get("/foo")
add_session_to_request(init_request)

# actual test starts here
next_url = furl("http://foo.bar?bazz=buzz")
url = plugin.get_start_url(init_request, form)
start_view = AuthenticationStartView.as_view(register=register)
request = factory.get(url, {"next": next_url})
add_session_to_request(request)
response = start_view(
factory.get(url, {"next": next_url}),
request,
slug=form.slug,
plugin_id="plugin1",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from rest_framework.test import APITestCase

from openforms.authentication.service import AuthAttribute
from openforms.contrib.objects_api.clients import get_objects_client
from openforms.contrib.objects_api.helpers import prepare_data_for_registration
from openforms.contrib.objects_api.tests.factories import ObjectsAPIGroupConfigFactory
Expand Down Expand Up @@ -47,13 +48,16 @@ def test_prefill_values_happy_flow(self):
data={
"name": {"last.name": "My last name"},
"age": 45,
"bsn": "111222333",
},
objecttype_version=3,
),
objecttype_url="http://objecttypes-web:8000/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48",
)

submission = SubmissionFactory.from_components(
auth_info__value="111222333",
auth_info__attribute=AuthAttribute.bsn,
initial_data_reference=created_obj["uuid"],
components_list=[
{
Expand Down Expand Up @@ -146,6 +150,8 @@ def test_prefill_values_when_reference_returns_empty_values(self):
)

submission = SubmissionFactory.from_components(
auth_info__value="111222333",
auth_info__attribute=AuthAttribute.bsn,
initial_data_reference=created_obj["uuid"],
components_list=[
{
Expand Down

0 comments on commit 6e9b708

Please sign in to comment.