Skip to content

Commit

Permalink
✅ Add tests for register_kanalen
Browse files Browse the repository at this point in the history
and clean up mock usage in other tests
  • Loading branch information
stevenbal committed Dec 5, 2024
1 parent db11c69 commit 741e8d9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
2 changes: 2 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

DEBUG = True

SITE_ID = 1

USE_TZ = False

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down
16 changes: 15 additions & 1 deletion testapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,19 @@
from django.urls import include, path

from .api import router
from .views import KanalenView

urlpatterns = [path("admin/", admin.site.urls), path("api/", include(router.urls))]
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(router.urls)),
path(
"notificaties/",
include(
(
[path("kanalen/", KanalenView.as_view(), name="kanalen")],
"notifications",
),
namespace="notifications",
),
),
]
5 changes: 5 additions & 0 deletions testapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.views import View


class KanalenView(View):
pass
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from notifications_api_common.models import NotificationsConfig
from testapp import urls # noqa

NOTIFICATIONS_API_ROOT = "http://some-api-root/api/v1/"


def dummy_get_response(request):
raise NotImplementedError()
Expand Down
28 changes: 11 additions & 17 deletions tests/test_register_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,30 @@
from django.utils.translation import gettext as _

import pytest
import requests_mock
from requests.exceptions import HTTPError, RequestException

from notifications_api_common.admin import register_webhook
from notifications_api_common.models import Subscription

from .conftest import NOTIFICATIONS_API_ROOT

class MockResponse:
def __init__(self, data):
self.data = data

def json(self):
return self.data


@patch(
"ape_pie.APIClient.post",
return_value=MockResponse({"url": "https://example.com/api/v1/abonnementen/1"}),
)
@pytest.mark.django_db
def test_register_webhook_success(
request_with_middleware, notifications_config, *mocks
):
def test_register_webhook_success(request_with_middleware, notifications_config):
subscription = Subscription.objects.create(
callback_url="https://example.com/callback",
client_id="client_id",
secret="secret",
channels=["zaken"],
)

register_webhook(object, request_with_middleware, Subscription.objects.all())
with requests_mock.Mocker() as m:
m.post(
f"{NOTIFICATIONS_API_ROOT}abonnement",
json={"url": "https://example.com/api/v1/abonnementen/1"},
)
register_webhook(object, request_with_middleware, Subscription.objects.all())

messages = list(get_messages(request_with_middleware))

Expand All @@ -54,7 +48,7 @@ def test_register_webhook_request_exception(
channels=["zaken"],
)

with patch("ape_pie.APIClient.post", side_effect=RequestException("exception")):
with patch("requests.Session.post", side_effect=RequestException("exception")):
register_webhook(object, request_with_middleware, Subscription.objects.all())

messages = list(get_messages(request_with_middleware))
Expand All @@ -74,7 +68,7 @@ def test_register_webhook_http_error(request_with_middleware, notifications_conf
channels=["zaken"],
)

with patch("ape_pie.APIClient.post", side_effect=HTTPError("400")):
with patch("requests.Session.post", side_effect=HTTPError("400")):
register_webhook(object, request_with_middleware, Subscription.objects.all())

messages = list(get_messages(request_with_middleware))
Expand Down
3 changes: 2 additions & 1 deletion tests/test_send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from notifications_api_common.tasks import NotificationException, send_notification
from testapp.models import Person

NOTIFICATIONS_API_ROOT = "http://some-api-root/api/v1/"
from .conftest import NOTIFICATIONS_API_ROOT

TESTS_DIR = Path(__file__).parent


Expand Down

0 comments on commit 741e8d9

Please sign in to comment.