<s> """<STR_LIT>""" <EOL> from mock import patch , Mock <EOL> import pytest <EOL> from pyramid . testing import DummyRequest <EOL> from h . api import storage <EOL> from h . notification import reply_template as rt <EOL> store_fake_data = [ <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:5>' , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : [ <NUM_LIT:5> ] , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:title>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : [ <NUM_LIT:5> ] , <EOL> '<STR_LIT:user>' : '<STR_LIT>' <EOL> } , <EOL> ] <EOL> def _fake_request ( ) : <EOL> mock_dumps = Mock ( return_value = '<STR_LIT>' ) <EOL> request = DummyRequest ( ) <EOL> request . domain = '<STR_LIT>' <EOL> request . registry . notification_serializer = Mock ( dumps = mock_dumps ) <EOL> request . route_url = Mock ( ) <EOL> request . route_url . return_value = '<STR_LIT>' <EOL> return request <EOL> def _fake_anno ( id ) : <EOL> try : <EOL> offset = int ( id ) <EOL> except TypeError : <EOL> return None <EOL> return storage . annotation_from_dict ( store_fake_data [ offset ] ) <EOL> class MockSubscription ( Mock ) : <EOL> def __json__ ( self , request ) : <EOL> return { <EOL> '<STR_LIT:id>' : self . id or '<STR_LIT>' , <EOL> '<STR_LIT>' : self . uri or '<STR_LIT>' <EOL> } <EOL> def test_dont_send_to_the_same_user ( ) : <EOL> """<STR_LIT>""" <EOL> annotation = _fake_anno ( <NUM_LIT:3> ) <EOL> data = { <EOL> '<STR_LIT>' : _fake_anno ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : <NUM_LIT:1> } <EOL> } <EOL> send = rt . check_conditions ( annotation , data ) <EOL> assert send is False <EOL> def test_different_subscription ( ) : <EOL> """<STR_LIT>""" <EOL> annotation = _fake_anno ( <NUM_LIT:1> ) <EOL> data = { <EOL> '<STR_LIT>' : _fake_anno ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> send = rt . check_conditions ( annotation , data ) <EOL> assert send is False <EOL> def test_good_conditions ( ) : <EOL> """<STR_LIT>""" <EOL> annotation = _fake_anno ( <NUM_LIT:1> ) <EOL> data = { <EOL> '<STR_LIT>' : _fake_anno ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> send = rt . check_conditions ( annotation , data ) <EOL> assert send is True <EOL> generate_notifications_fixtures = pytest . mark . usefixtures ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ generate_notifications_fixtures <EOL> def test_generate_notifications_empty_if_action_not_create ( ) : <EOL> """<STR_LIT>""" <EOL> annotation = storage . annotation_from_dict ( { } ) <EOL> request = DummyRequest ( ) <EOL> notifications = rt . generate_notifications ( request , annotation , '<STR_LIT>' ) <EOL> assert list ( notifications ) == [ ] <EOL> @ generate_notifications_fixtures <EOL> def test_generate_notifications_empty_if_annotation_has_no_parent ( ) : <EOL> """<STR_LIT>""" <EOL> annotation = _fake_anno ( <NUM_LIT:0> ) <EOL> request = DummyRequest ( ) <EOL> notifications = rt . generate_notifications ( request , annotation , '<STR_LIT>' ) <EOL> assert list ( notifications ) == [ ] <EOL> @ generate_notifications_fixtures <EOL> def test_generate_notifications_does_not_fetch_if_annotation_has_no_parent ( fetch ) : <EOL> """<STR_LIT>""" <EOL> annotation = _fake_anno ( <NUM_LIT:0> ) <EOL> request = DummyRequest ( ) <EOL> notifications = rt . generate_notifications ( request , annotation , '<STR_LIT>' ) <EOL> list ( notifications ) <EOL> fetch . assert_not_called ( ) <EOL> @ generate_notifications_fixtures <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_generate_notifications_only_if_author_can_read_reply ( <EOL> Subscriptions , <EOL> auth ) : <EOL> """<STR_LIT>""" <EOL> Subscriptions . get_active_subscriptions_for_a_type . return_value = [ <EOL> MockSubscription ( id = <NUM_LIT:1> , uri = '<STR_LIT>' ) <EOL> ] <EOL> auth . has_permission . return_value = False <EOL> notifications = rt . generate_notifications ( _fake_request ( ) , <EOL> _fake_anno ( <NUM_LIT:6> ) , <EOL> '<STR_LIT>' ) <EOL> assert list ( notifications ) == [ ] <EOL> auth . has_permission . return_value = True <EOL> notifications = rt . generate_notifications ( _fake_request ( ) , <EOL> _fake_anno ( <NUM_LIT:7> ) , <EOL> '<STR_LIT>' ) <EOL> assert list ( notifications ) != [ ] <EOL> @ generate_notifications_fixtures <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_generate_notifications_checks_subscriptions ( Subscriptions ) : <EOL> """<STR_LIT>""" <EOL> request = _fake_request ( ) <EOL> annotation = _fake_anno ( <NUM_LIT:1> ) <EOL> Subscriptions . get_active_subscriptions_for_a_type . return_value = [ ] <EOL> notifications = rt . generate_notifications ( request , annotation , '<STR_LIT>' ) <EOL> list ( notifications ) <EOL> Subscriptions . get_active_subscriptions_for_a_type . assert_called_with ( '<STR_LIT>' ) <EOL> @ generate_notifications_fixtures <EOL> def test_check_conditions_false_stops_sending ( ) : <EOL> """<STR_LIT>""" <EOL> request = _fake_request ( ) <EOL> annotation = _fake_anno ( <NUM_LIT:1> ) <EOL> with patch ( '<STR_LIT>' ) as mock_subs : <EOL> mock_subs . get_active_subscriptions_for_a_type . return_value = [ <EOL> MockSubscription ( id = <NUM_LIT:1> , uri = '<STR_LIT>' ) <EOL> ] <EOL> with patch ( '<STR_LIT>' ) as mock_conditions : <EOL> mock_conditions . return_value = False <EOL> with pytest . raises ( StopIteration ) : <EOL> msgs = rt . generate_notifications ( request , annotation , '<STR_LIT>' ) <EOL> msgs . next ( ) <EOL> @ generate_notifications_fixtures <EOL> def test_send_if_everything_is_okay ( ) : <EOL> """<STR_LIT>""" <EOL> request = _fake_request ( ) <EOL> annotation = _fake_anno ( <NUM_LIT:1> ) <EOL> with patch ( '<STR_LIT>' ) as mock_subs : <EOL> mock_subs . get_active_subscriptions_for_a_type . return_value = [ <EOL> MockSubscription ( id = <NUM_LIT:1> , uri = '<STR_LIT>' ) <EOL> ] <EOL> with patch ( '<STR_LIT>' ) as mock_conditions : <EOL> mock_conditions . return_value = True <EOL> msgs = rt . generate_notifications ( request , annotation , '<STR_LIT>' ) <EOL> msgs . next ( ) <EOL> @ pytest . fixture <EOL> def auth ( patch ) : <EOL> return patch ( '<STR_LIT>' ) <EOL> @ pytest . fixture <EOL> def get_user ( patch ) : <EOL> return patch ( '<STR_LIT>' ) <EOL> @ pytest . fixture ( autouse = True ) <EOL> def fetch ( request ) : <EOL> patcher = patch . object ( storage , '<STR_LIT>' ) <EOL> fetch = patcher . start ( ) <EOL> fetch . side_effect = lambda _ , id : _fake_anno ( id ) <EOL> request . addfinalizer ( patcher . stop ) <EOL> return fetch </s>
