-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix registering kanalen #17
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
985bec3
Fix notification kanalen not being created
SonnyBA 5ea25c8
Add `register_kanalen` tests
SonnyBA f6e69a7
Apply formatting
SonnyBA ea8b34d
Add changelog entry
SonnyBA a8e6f22
Remove reference to commit in CHANGELOG
SonnyBA 1395c27
Remove mutable default value
SonnyBA a349dde
Remove duplicate assignment
SonnyBA 7945a43
Show exception string representation in stderr
SonnyBA 8e096db
Update error messages in tests
SonnyBA 253f750
Use service in exceptions & inherit from same base exception
SonnyBA 9be893c
Use `KanaalException` base exception to catch exceptions
SonnyBA e9ccf75
Apply formatting
SonnyBA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
from io import StringIO | ||
from unittest.mock import Mock, patch | ||
from urllib.parse import urlencode | ||
|
||
from django.test.testcases import call_command | ||
|
||
import pytest | ||
|
||
from notifications_api_common.kanalen import KANAAL_REGISTRY, Kanaal | ||
|
||
kanalen = set( | ||
( | ||
Kanaal(label="foobar", main_resource=Mock()), | ||
Kanaal(label="boofar", main_resource=Mock()), | ||
) | ||
) | ||
|
||
KANAAL_REGISTRY.clear() | ||
KANAAL_REGISTRY.update(kanalen) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_register_kanalen_success(notifications_config, requests_mock): | ||
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal" | ||
params = urlencode(dict(naam="foobar")) | ||
|
||
requests_mock.get(f"{kanaal_url}?{params}", json=[]) | ||
|
||
requests_mock.post( | ||
kanaal_url, | ||
json={ | ||
"url": "http://example.com", | ||
"naam": "string", | ||
"documentatieLink": "http://example.com", | ||
"filters": ["string"], | ||
}, | ||
status_code=201, | ||
) | ||
|
||
reverse_patch = ( | ||
"notifications_api_common.management.commands.register_kanalen.reverse" | ||
) | ||
|
||
with patch(reverse_patch) as mocked_reverse: | ||
mocked_reverse.return_value = "/notifications/kanalen" | ||
|
||
call_command("register_kanalen", kanalen=["foobar"]) | ||
|
||
assert len(requests_mock.request_history) == 2 | ||
|
||
get_request = requests_mock.request_history[0] | ||
|
||
assert get_request._request.url == f"{kanaal_url}?{params}" | ||
|
||
post_request = requests_mock.request_history[1] | ||
|
||
assert post_request._request.url == kanaal_url | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_register_kanalen_from_registry_success(notifications_config, requests_mock): | ||
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal" | ||
|
||
url_mapping = { | ||
kanaal.label: f"{kanaal_url}?{urlencode(dict(naam=kanaal.label))}" | ||
for kanaal in kanalen | ||
} | ||
|
||
for kanaal in kanalen: | ||
requests_mock.get(url_mapping[kanaal.label], json=[]) | ||
|
||
requests_mock.post( | ||
kanaal_url, | ||
json={ | ||
"url": "http://example.com", | ||
"naam": kanaal.label, | ||
"documentatieLink": "http://example.com", | ||
"filters": ["string"], | ||
}, | ||
status_code=201, | ||
) | ||
|
||
reverse_patch = ( | ||
"notifications_api_common.management.commands.register_kanalen.reverse" | ||
) | ||
|
||
with patch(reverse_patch) as mocked_reverse: | ||
mocked_reverse.return_value = "/notifications/kanalen" | ||
|
||
call_command("register_kanalen") | ||
|
||
assert len(requests_mock.request_history) == 4 | ||
|
||
# kanalen are sorted by label | ||
first_get_request = requests_mock.request_history[0] | ||
assert first_get_request._request.url == url_mapping["boofar"] | ||
|
||
first_post_request = requests_mock.request_history[1] | ||
assert first_post_request._request.url == kanaal_url | ||
|
||
second_get_request = requests_mock.request_history[2] | ||
assert second_get_request._request.url == url_mapping["foobar"] | ||
|
||
second_post_request = requests_mock.request_history[3] | ||
assert second_post_request._request.url == kanaal_url | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_register_kanalen_existing_kanalen(notifications_config, requests_mock): | ||
""" | ||
Test that already registered kanalen does not cause issues | ||
""" | ||
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal" | ||
params = urlencode(dict(naam="foobar")) | ||
|
||
requests_mock.get( | ||
f"{kanaal_url}?{params}", | ||
json=[ | ||
{ | ||
"url": "http://example.com", | ||
"naam": "foobar", | ||
"documentatieLink": "http://example.com", | ||
"filters": ["string"], | ||
} | ||
], | ||
) | ||
|
||
call_command("register_kanalen", kanalen=["foobar"]) | ||
|
||
assert len(requests_mock.request_history) == 1 | ||
|
||
request = requests_mock.request_history[0] | ||
|
||
assert request._request.url == f"{kanaal_url}?{params}" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_register_kanalen_unknown_url(notifications_config, requests_mock): | ||
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal" | ||
params = urlencode(dict(naam="foobar")) | ||
|
||
requests_mock.get(f"{kanaal_url}?{params}", status_code=404) | ||
|
||
stderr = StringIO() | ||
|
||
call_command("register_kanalen", kanalen=["foobar"], stderr=stderr) | ||
|
||
failure_message = ( | ||
"Request to retrieve existing kanalen for foobar failed. Skipping.." | ||
) | ||
|
||
assert failure_message in stderr.getvalue() | ||
|
||
assert len(requests_mock.request_history) == 1 | ||
|
||
request = requests_mock.request_history[0] | ||
|
||
assert request._request.url == f"{kanaal_url}?{params}" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_register_kanalen_incorrect_post(notifications_config, requests_mock): | ||
kanaal_url = f"{notifications_config.notifications_api_service.api_root}kanaal" | ||
params = urlencode(dict(naam="foobar")) | ||
|
||
requests_mock.get(f"{kanaal_url}?{params}", json=[]) | ||
|
||
requests_mock.post(kanaal_url, json={"error": "foo"}, status_code=400) | ||
|
||
stderr = StringIO() | ||
|
||
reverse_patch = ( | ||
"notifications_api_common.management.commands.register_kanalen.reverse" | ||
) | ||
|
||
with patch(reverse_patch) as mocked_reverse: | ||
mocked_reverse.return_value = "/notifications/kanalen" | ||
|
||
call_command("register_kanalen", kanalen=["foobar"], stderr=stderr) | ||
|
||
failure_message = "Request to create kanaal for foobar failed. Skipping.." | ||
|
||
assert failure_message in stderr.getvalue() | ||
|
||
assert len(requests_mock.request_history) == 2 | ||
|
||
get_request = requests_mock.request_history[0] | ||
|
||
assert get_request._request.url == f"{kanaal_url}?{params}" | ||
|
||
post_request = requests_mock.request_history[1] | ||
|
||
assert post_request._request.url == kanaal_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the commit hash is a little extensive, the issue number should be enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was no issue created for this but I removed the commit hash