"""Test for the notifications logic.""" from unittest.mock import MagicMock import flask from oto import status from owsrequest import request as requests from requests.models import Response from notifications.constants.header import ORCHARD_USER_ID from notifications.constants.notifications import EMAIL_FEED_NAME from notifications.constants.service_name import OWS_NOTIFICATIONS from notifications.logic import notifications from notifications.logic import segment def test_unsubscribe(monkeypatch): """Test unsubscribe.""" ows_notifications_response = Response() ows_notifications_response.status_code = 200 monkeypatch.setattr( requests, 'post', MagicMock(return_value=ows_notifications_response)) monkeypatch.setattr(segment, 'track_unsubscribe', MagicMock()) monkeypatch.setattr(flask, 'render_template', MagicMock()) user_id = 'oa:123' feed_name = 'label_analytics_digest' feed_id = 'vendor_123' token = 'e3f848d9c499429de544fc6b57af3d2ec61021ac' notifications.unsubscribe(user_id, feed_name, feed_id, token) requests.post.assert_called_once_with( OWS_NOTIFICATIONS, '/unsubscribe', headers={ORCHARD_USER_ID: user_id}, json={ 'user_feed_name': EMAIL_FEED_NAME, 'feed_name': feed_name, 'feed_id': feed_id } ) segment.track_unsubscribe.assert_called_once_with( user_id, feed_name, feed_id) flask.render_template.assert_called_once_with( 'unsubscribed.html', notification_name='Analytics Digest') def test_unsubscribe_email(monkeypatch): """Test unsubscribe with user email instead of user id.""" ows_notifications_response = Response() ows_notifications_response.status_code = 200 monkeypatch.setattr( requests, 'post', MagicMock(return_value=ows_notifications_response)) monkeypatch.setattr(segment, 'track_unsubscribe', MagicMock()) monkeypatch.setattr(flask, 'render_template', MagicMock()) user_id = 'user.email@theorchard.com' feed_name = 'orchard_trending_tracks_global' feed_id = 'vendor_123' token = '4fb535800acff3fee55f134773bf1bbe3ee55641' notifications.unsubscribe(user_id, feed_name, feed_id, token) requests.post.assert_called_once_with( OWS_NOTIFICATIONS, '/unsubscribe', headers={ORCHARD_USER_ID: 'oa:179'}, json={ 'user_feed_id': user_id, 'user_feed_name': EMAIL_FEED_NAME, 'feed_name': feed_name, 'feed_id': feed_id } ) segment.track_unsubscribe.assert_called_once_with( user_id, feed_name, feed_id) flask.render_template.assert_called_once_with( 'unsubscribed_orchard.html', notification_name='Orchard Trending Tracks Global') def test_unsubscribe_invalid_token(monkeypatch): """Test unsubscribe when the token is invalid.""" user_id = 'oa:123' feed_name = 'label_analytics_digest' feed_id = 'vendor_123' token = 'invalid token' result = notifications.unsubscribe(user_id, feed_name, feed_id, token) assert result.status_code == status.UNAUTHORIZED def test_unsubscribe_ows_notifications_error(monkeypatch): """Test unsubscribe when the call to ows-notifications fails.""" ows_notifications_response = Response() ows_notifications_response.status_code = 500 ows_notifications_response.json = MagicMock(return_value={}) monkeypatch.setattr( requests, 'post', MagicMock(return_value=ows_notifications_response)) user_id = 'oa:123' feed_name = 'label_analytics_digest' feed_id = 'vendor_123' token = 'e3f848d9c499429de544fc6b57af3d2ec61021ac' result = notifications.unsubscribe(user_id, feed_name, feed_id, token) assert result.status_code == 500