Skip to content

Commit

Permalink
✅ [#4908] Update config check tests for json registration plugin
Browse files Browse the repository at this point in the history
* Use VCR instead of mock now
* Add test for invalid response from API endpoint
  • Loading branch information
viktorvanwijk committed Jan 2, 2025
1 parent 61b638d commit 707ef37
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzU4MTE1NTAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.9NfTz6nrkM3ysWrsMYIV1kxDGvtXXF3uugyVpuUFqD0
Connection:
- keep-alive
User-Agent:
- python-requests/2.32.2
method: GET
uri: http://localhost/test_connection
response:
body:
string: "{\n \"message\": \"OK\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '22'
Content-Type:
- application/json
Date:
- Thu, 02 Jan 2025 09:52:30 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzU4MTE1NTAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.9NfTz6nrkM3ysWrsMYIV1kxDGvtXXF3uugyVpuUFqD0
Connection:
- keep-alive
User-Agent:
- python-requests/2.32.2
method: GET
uri: http://localhost/fake_endpoint
response:
body:
string: '<!doctype html>
<html lang=en>
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually
please check your spelling and try again.</p>
'
headers:
Connection:
- close
Content-Length:
- '207'
Content-Type:
- text/html; charset=utf-8
Date:
- Thu, 02 Jan 2025 09:52:30 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
code: 404
message: NOT FOUND
version: 1
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
from pathlib import Path
from unittest.mock import patch

from django.test import TestCase

from zgw_consumers.test.factories import ServiceFactory

from openforms.plugins.exceptions import InvalidPluginConfiguration
from openforms.utils.tests.vcr import OFVCRMixin

from ..models import JSONConfig
from ..plugin import JSONRegistration


class ConfigCheckTests(TestCase):
VCR_TEST_FILES = Path(__file__).parent / "files"

@patch("zgw_consumers.nlx.NLXClient.get")
def test_config_check(self, mock_post):

class ConfigCheckTests(OFVCRMixin, TestCase):

VCR_TEST_FILES = VCR_TEST_FILES

def test_config_check_happy_flow(self):
json_plugin = JSONRegistration("json_registration_plugin")

config = JSONConfig(
service=ServiceFactory(api_root="https://example.com/", api_connection_check_path="test")
service=ServiceFactory(
api_root="http://localhost:80/",
api_connection_check_path="test_connection",
)
)

with patch(
"openforms.registrations.contrib.json.plugin.JSONConfig.get_solo",
return_value=config
):
json_plugin.check_config()
mock_post.assert_called_once_with("test")

def test_no_service_configured(self):
config = JSONConfig(service=None)
Expand All @@ -36,3 +44,19 @@ def test_no_service_configured(self):
return_value=config
):
self.assertRaises(InvalidPluginConfiguration, json_plugin.check_config)

def test_invalid_response_from_api_test_connection_endpoint(self):
json_plugin = JSONRegistration("json_registration_plugin")

config = JSONConfig(
service=ServiceFactory(
api_root="http://localhost:80/",
api_connection_check_path="fake_endpoint",
)
)

with patch(
"openforms.registrations.contrib.json.plugin.JSONConfig.get_solo",
return_value=config
):
self.assertRaises(InvalidPluginConfiguration, json_plugin.check_config)

0 comments on commit 707ef37

Please sign in to comment.