Skip to content

Commit

Permalink
👌 [maykinmedia/open-api-framework#66] change mocks for 'register_kana…
Browse files Browse the repository at this point in the history
…len' tests
  • Loading branch information
annashamray committed Nov 29, 2024
1 parent 14137d1 commit e0ff502
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions src/objects/tests/v2/test_notifications_kanaal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from io import StringIO
from unittest.mock import call, patch

from django.contrib.sites.models import Site
from django.core.management import call_command
from django.test import override_settings

import requests_mock
from notifications_api_common.kanalen import KANAAL_REGISTRY
from notifications_api_common.models import NotificationsConfig
from rest_framework.test import APITestCase
Expand All @@ -14,6 +14,8 @@
from objects.api.kanalen import ObjectKanaal
from objects.core.models import Object

NOTIFICATIONS_API_ROOT = "https://notificaties-api.vng.cloud/api/v1/"


@override_settings(IS_HTTPS=True)
class CreateNotifKanaalTestCase(APITestCase):
Expand All @@ -28,7 +30,7 @@ def setUpTestData(cls):
cls.addClassCleanup(lambda: KANAAL_REGISTRY.remove(kanaal))

service, _ = Service.objects.update_or_create(
api_root="https://notificaties-api.vng.cloud/api/v1/",
api_root=NOTIFICATIONS_API_ROOT,
defaults=dict(
api_type=APITypes.nrc,
client_id="test",
Expand All @@ -41,13 +43,13 @@ def setUpTestData(cls):
config.notifications_api_service = service
config.save()

@patch("notifications_api_common.models.NotificationsConfig.get_client")
def test_kanaal_create_with_name(self, mock_get_client):
@requests_mock.Mocker()
def test_kanaal_create_with_name(self, m):
"""
Test is request to create kanaal is send with specified kanaal name
"""
client = mock_get_client.return_value
client.get.return_value = []
m.get(f"{NOTIFICATIONS_API_ROOT}kanaal?naam=kanaal_test", json=[])
m.post(f"{NOTIFICATIONS_API_ROOT}kanaal")

stdout = StringIO()
call_command(
Expand All @@ -56,47 +58,50 @@ def test_kanaal_create_with_name(self, mock_get_client):
stdout=stdout,
)

client.post.assert_called_once_with(
"kanaal",
json={
self.assertEqual(m.last_request.url, f"{NOTIFICATIONS_API_ROOT}kanaal")
self.assertEqual(m.last_request.method, "POST")
self.assertEqual(
m.last_request.json(),
{
"naam": "kanaal_test",
"documentatieLink": "https://example.com/ref/kanalen/#kanaal_test",
"filters": [],
},
)

@patch("notifications_api_common.models.NotificationsConfig.get_client")
@override_settings(NOTIFICATIONS_KANAAL="dummy-kanaal")
def test_kanaal_create_without_name(self, mock_get_client):
@requests_mock.Mocker()
def test_kanaal_create_without_name(self, m):
"""
Test is request to create kanaal is send with default kanaal name
"""
client = mock_get_client.return_value
client.get.return_value = []
m.get(f"{NOTIFICATIONS_API_ROOT}kanaal", json=[])
m.post(f"{NOTIFICATIONS_API_ROOT}kanaal")

stdout = StringIO()
call_command(
"register_kanalen",
stdout=stdout,
)

client.post.assert_has_calls(
[
call(
"kanaal",
json={
"naam": "kanaal_test",
"documentatieLink": "https://example.com/ref/kanalen/#kanaal_test",
"filters": [],
},
),
call(
"kanaal",
json={
"naam": "objecten",
"documentatieLink": "https://example.com/ref/kanalen/#objecten",
"filters": ["object_type"],
},
),
]
post_req1, post_req2 = [
req
for req in m.request_history
if req.method == "POST" and req.url == f"{NOTIFICATIONS_API_ROOT}kanaal"
]
self.assertEqual(
post_req1.json(),
{
"naam": "kanaal_test",
"documentatieLink": "https://example.com/ref/kanalen/#kanaal_test",
"filters": [],
},
)
self.assertEqual(
post_req2.json(),
{
"naam": "objecten",
"documentatieLink": "https://example.com/ref/kanalen/#objecten",
"filters": ["object_type"],
},
)

0 comments on commit e0ff502

Please sign in to comment.