"""Tests for ows_notifications interface.""" from unittest.mock import MagicMock import requests from flexmock import flexmock from oto import response from owsrequest import request from notifications_delivery import config from notifications_delivery.connectors import sentry from notifications_delivery.constants import error, ows_services from notifications_delivery.models import ows_notifications def test_unsubscribe_user(monkeypatch): """Test unsubscribe user when the request is successfull.""" mock_user_info = { 'email': 'email', 'user_id': 123, 'vendor_id': 123 } feed_group_mock = 'label_analytics_digest' expected_header = { 'Orchard-User-Id': 'alw:123', 'Grass-Account-Id': '123', 'Grass-Account-Type': 'vendor' } expected_body = { 'user_feed_name': 'user_email_notifications', 'feed_name': feed_group_mock, 'feed_id': 'vendor_123' } mock_ows_users_response = requests.models.Response() mock_ows_users_response.status_code = 200 monkeypatch.setattr( request, 'process', MagicMock(return_value=mock_ows_users_response)) ows_notifications.unsubscribe_user( mock_user_info, feed_group_mock) request.process.assert_called_once_with( application=ows_services.APPLICATION_NAME, environment=config.ENVIRONMENT, method=ows_services.METHOD_POST, service_name=ows_services.OWS_NOTIFICATIONS, path='/unsubscribe', headers=expected_header, json=expected_body) def test_unsubscribe_user_error(monkeypatch): """Test unsubscribe user when the request is not successfull.""" mock_user_info = { 'email': 'email', 'user_id': 123, 'vendor_id': 123 } feed_group_mock = 'label_analytics_digest' (flexmock(ows_notifications) .should_receive('unsubscribe_user') .and_return(response.create_fatal_response())) (flexmock(sentry) .should_receive('send_response_to_sentry')) ows_notifications.unsubscribe_user( mock_user_info, feed_group_mock) def test_get_vendor_subscriptions(monkeypatch): """Test get vendor subscription when the request is successfull.""" vendor_id = 123 feed_group = 'label_analytics_digest' subscriptions_mock = { 'items': [ 'label_analytics_digest:alw_123', 'label_analytics_digest:oa_123', ] } (flexmock(ows_notifications) .should_receive('get_vendor_subscriptions') .and_return(response.Response(subscriptions_mock))) result = ows_notifications.get_vendor_subscriptions(vendor_id, feed_group) assert result.status == 200 assert len(result.message) == 1 def test_get_vendor_subscriptions_error(monkeypatch): """Test get vendor subscriptions when the request is not successfull.""" vendor_id = 123 feed_group = 'label_analytics_digest' ows_notifications_error = 'error' ows_notifications_response = flexmock(requests.models.Response()) ows_notifications_response.status_code = 400 (flexmock(ows_notifications_response) .should_receive('text') .and_return(ows_notifications_error)) (flexmock(request) .should_receive('process') .and_return(ows_notifications_response)) result = ows_notifications.get_vendor_subscriptions(vendor_id, feed_group) assert result.status == 400 assert result.errors == { 'code': error.ERROR_CODE_OWS_NOTIFICATIONS_REQUEST, 'message': ows_notifications_error } def test_user_subscription_toggle_off(monkeypatch): """Test user_subscription_toggle_off when the request is successful.""" subscription_name = 'label_analytics_digest' identity_id = 'uuid-1' mock_response = requests.models.Response() mock_response.status_code = 200 monkeypatch.setattr(request, 'process', MagicMock(return_value=mock_response)) ows_notifications.user_subscription_toggle_off(identity_id, subscription_name) request.process.assert_called_once_with( application=ows_services.APPLICATION_NAME, environment=config.ENVIRONMENT, method=ows_services.METHOD_PUT, service_name=ows_services.OWS_NOTIFICATIONS, path=f'/identity/{identity_id}/subscriptions/{subscription_name}', headers={'Orchard-Identity-Id': identity_id}, json={'follow_all_resources': False} )