"""Functional tests for adding an activity.""" import json from unittest.mock import patch from owsresponse import response, status from notifications import handlers # noqa @patch( 'notifications.logic.stream.add_label_activities_to_user', return_value=response.Response(status=201, message={'message': 'sent'}), ) @patch('notifications.handlers.Neo4jSession.__enter__') @patch('notifications.handlers.Neo4jSession.__exit__') def test_add_activity(neo4j_exit, neo4j_enter, mock_stream_client, fixture_client): """Test adding an activity.""" url = '/activity' data = { 'feed_name': 'label_track_placement', 'feed_id': 'vendor_123', 'payload': { 'actor': 'The Orchard Activity Detector', 'verb': 'Placed', 'object': 'Track', 'target': 'Playlist', 'track_id': 123, }, } result = fixture_client.post(url, data=json.dumps(data)) assert result.status_code == status.CREATED assert json.loads(result.data.decode('utf-8')) == {'message': 'sent'} @patch('notifications.handlers.Neo4jSession.__enter__') @patch('notifications.handlers.Neo4jSession.__exit__') def test_add_activity_exception(neo4j_exit, neo4j_enter, fixture_client): """Test adding an activity when an exception is raised.""" class StreamClient: def feed(self, feed_id, feed_name): raise Exception('error') stream_client = StreamClient() with patch('notifications.logic.stream.add_label_activities_to_user', new=stream_client): url = '/activity' data = { 'feed_name': 'label_track_placement', 'feed_id': 'vendor_123', 'payload': { 'actor': 'The Orchard Activity Detector', 'verb': 'Placed', 'object': 'Track', 'target': 'Playlist', 'track_id': 123, }, } result = fixture_client.post(url, data=json.dumps(data)) assert result.status_code == status.INTERNAL_ERROR