"""Functional tests for subscribing to a feed.""" import json from unittest.mock import patch from owsresponse import response, status from notifications.constants import header @patch('notifications.models.ows_users.get_user', return_value=response.Response()) @patch('notifications.models.stream.stream_client') @patch('notifications.handlers.Neo4jSession.__enter__') @patch('notifications.handlers.Neo4jSession.__exit__') def test_subscribe(mock_exit, mock_enter, mock_stream_client, mock_get_user, fixture_client): """Test subscribing to a feed.""" url = '/subscribe' 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', } @patch('notifications.models.ows_users.get_user', return_value=response.Response()) def test_subscribe_exception(mock_get_user, fixture_client): """Test subscribing to 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 = '/subscribe' 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 @patch('notifications.models.ows_users.get_user', return_value=response.create_fatal_response()) def test_subscribe_invalid_user(mock_get_user, fixture_client): """Test subscribing to a feed when the user is invalid.""" url = '/subscribe' 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