"""Functional tests for unsubscribing from a feed.""" import json from unittest.mock import patch from owsresponse import status from notifications.constants import header @patch('notifications.models.stream.stream_client') @patch('notifications.handlers.Neo4jSession.__enter__') @patch('notifications.handlers.Neo4jSession.__exit__') def test_unsubscribe(mock_exit, mock_enter, mock_stream_client, fixture_client): """Test unsubscribing from a feed.""" url = '/unsubscribe' headers = { header.ORCHARD_USER_ID: 'alw:123', header.GRASS_ACCOUNT_ID: 123, header.GRASS_ACCOUNT_TYPE: 'vendor', } data = { 'user_feed_name': 'user_workstation_notifications', 'feed_name': 'label_track_placement', } result = fixture_client.post(url, headers=headers, data=json.dumps(data)) assert result.status_code == status.OK assert json.loads(result.data.decode('utf-8')) == { 'feed_name': data['feed_name'], 'feed_id': 'vendor_123', } def test_unsubscribe_exception(fixture_client): """Test unsubscribing from a feed when an exception is raised.""" class StreamClient: def feed(self, feed_id, feed_name): raise Exception('error') stream_client = StreamClient() with patch('notifications.models.stream.stream_client', new=stream_client): url = '/unsubscribe' headers = { header.ORCHARD_USER_ID: 'alw:123', header.GRASS_ACCOUNT_ID: 123, header.GRASS_ACCOUNT_TYPE: 'vendor', } data = { 'user_feed_name': 'user_workstation_notifications', 'feed_name': 'label_track_placement', } result = fixture_client.post(url, headers=headers, data=json.dumps(data)) assert result.status_code == status.INTERNAL_ERROR