"""Functional tests for getting notifications.""" import datetime import json from unittest.mock import patch from owsresponse import status from notifications.constants import header def test_get_user_notifications(fixture_client): """Test getting user notifications.""" time = datetime.datetime(2000, 1, 1) date = datetime.date(1999, 12, 31) stream_response = { 'results': [ { 'actor': 'Tester', 'verb': 'Testing', 'object': 'Test', 'target': '1', 'time': time, 'date': date, }, { 'actor': 'Tester', 'verb': 'Testing', 'object': 'Test', 'target': '2', 'time': time, 'date': date, }, ] } class StreamFeed: def get(self, limit=10): return stream_response class StreamClient: def feed(self, feed_id, feed_name): return StreamFeed() stream_client = StreamClient() with patch('notifications.models.stream.stream_client', new=stream_client): url = '/user/notifications' headers = {header.ORCHARD_USER_ID: 'alw:123'} result = fixture_client.get(url, headers=headers) assert result.status_code == status.OK assert json.loads(result.data.decode('utf-8')) == { 'items': [ { 'actor': 'Tester', 'verb': 'Testing', 'object': 'Test', 'target': '1', 'time': time.timestamp(), 'date': date.isoformat(), }, { 'actor': 'Tester', 'verb': 'Testing', 'object': 'Test', 'target': '2', 'time': time.timestamp(), 'date': date.isoformat(), }, ] } def test_get_user_notifications_exception(fixture_client): """Test getting user notifications 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 = '/user/notifications' headers = {header.ORCHARD_USER_ID: 'alw:123'} result = fixture_client.get(url, headers=headers) assert result.status_code == status.INTERNAL_ERROR