"""Test for ows_notifications interface.""" from unittest.mock import call from owsrequest import request as requests from owsrequest.test_utils import MockOwsResponse import pytest from users.models import ows_notifications @pytest.mark.parametrize( ('ows_response_status', 'expected_status'), [ (200, 200), (201, 200), (404, 200), (409, 200), (500, 400), (400, 400), ], ) def test_create_subscription(mocker, ows_response_status, expected_status): """Test owsrequest to notifications format and response behavior.""" post_patch = mocker.patch.object( requests, 'post', return_value=MockOwsResponse(ows_response_status, {}) ) result = ows_notifications.create_notification_subscription( 'xyx', 'InsightsProfile', 'push_notifications', 'social_spike', False, 'abc' ) assert post_patch.call_args_list == [ call( 'ows-notifications', '/notifications/subscribe', correlation_id='abc', headers={ 'Orchard-Profile-Id': 'xyx', 'Orchard-Profile-Type': 'InsightsProfile', 'Content-Type': 'application/json', }, json={'feed_type': 'social_spike', 'notification_type': 'push_notifications'}, params={'undelete': 'false'}, ) ] assert result.status == expected_status @pytest.mark.parametrize( ('ows_response_status', 'expected_status'), [ (200, 200), (201, 200), (404, 200), (409, 200), (500, 400), (400, 400), ], ) def test_subscribe_label(mocker, ows_response_status, expected_status): """Test subscribe_label.""" post_patch = mocker.patch.object( requests, 'post', return_value=MockOwsResponse(ows_response_status, {}) ) result = ows_notifications.subscribe_label(1234, 'LabelProfile', 8869, None) assert post_patch.call_count == 1 assert result.status == expected_status assert post_patch.call_args_list[0][1] == { 'headers': { 'Content-Type': 'application/json', 'Orchard-Profile-Id': '1234', 'Orchard-Profile-Type': 'LabelProfile', }, 'json': {'automatic': False, 'id': 8869, 'relationship': 'HAS_FOLLOWED'}, } @pytest.mark.parametrize( ('ows_response_status', 'expected_status'), [ (200, 200), (201, 200), (404, 200), (409, 200), (500, 400), (400, 400), ], ) def test_unsubscribe_label(mocker, ows_response_status, expected_status): """Test unsubscribe_label.""" post_patch = mocker.patch.object( requests, 'delete', return_value=MockOwsResponse(ows_response_status, {}) ) result = ows_notifications.unsubscribe_label(1234, 'LabelProfile', 8869, None) assert post_patch.call_count == 1 assert result.status == expected_status assert post_patch.call_args_list[0][1] == { 'headers': { 'Content-Type': 'application/json', 'Orchard-Profile-Id': '1234', 'Orchard-Profile-Type': 'LabelProfile', } }