<s> from __future__ import unicode_literals <EOL> import mock <EOL> import pytest <EOL> from pyramid import testing <EOL> from h import subscribers <EOL> from h . api import models <EOL> from h . api . events import AnnotationEvent <EOL> class FakeMailer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . calls = [ ] <EOL> def __call__ ( self , recipients , subject , body , html ) : <EOL> self . calls . append ( ( recipients , subject , body , html ) ) <EOL> @ pytest . mark . usefixtures ( '<STR_LIT>' ) <EOL> class TestPublishAnnotationEvent : <EOL> def test_it_presents_the_annotation ( self , event , presenters ) : <EOL> subscribers . publish_annotation_event ( event ) <EOL> presenters . AnnotationJSONPresenter . assert_called_once_with ( <EOL> event . request , <EOL> event . annotation , <EOL> ) <EOL> presenters . AnnotationJSONPresenter . return_value . asdict . assert_called_once_with ( ) <EOL> def test_it_publishes_the_serialized_data ( self , event , presenters ) : <EOL> event . request . headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> subscribers . publish_annotation_event ( event ) <EOL> event . request . realtime . publish_annotation . assert_called_once_with ( { <EOL> '<STR_LIT:action>' : event . action , <EOL> '<STR_LIT>' : presenters . AnnotationJSONPresenter . return_value . asdict . return_value , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> @ pytest . fixture <EOL> def event ( self ) : <EOL> return mock . Mock ( <EOL> spec = AnnotationEvent ( testing . DummyRequest ( ) , <EOL> mock . Mock ( spec = models . Annotation ( ) ) , <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> @ pytest . fixture <EOL> def presenters ( self , patch ) : <EOL> return patch ( '<STR_LIT>' ) <EOL> class TestSendReplyNotifications ( object ) : <EOL> def test_calls_generate_notifications_with_request_annotation_and_action ( self ) : <EOL> send = FakeMailer ( ) <EOL> generate_notifications = mock . Mock ( spec_set = [ ] , return_value = [ ] ) <EOL> generate_mail = mock . Mock ( spec_set = [ ] , return_value = [ ] ) <EOL> event = AnnotationEvent ( mock . sentinel . request , <EOL> mock . sentinel . annotation , <EOL> mock . sentinel . action ) <EOL> subscribers . send_reply_notifications ( event , <EOL> generate_notifications = generate_notifications , <EOL> generate_mail = generate_mail , <EOL> send = send ) <EOL> generate_notifications . assert_called_once_with ( mock . sentinel . request , <EOL> mock . sentinel . annotation , <EOL> mock . sentinel . action ) <EOL> def test_generates_and_sends_mail_for_each_notification ( self ) : <EOL> s = mock . sentinel <EOL> send = FakeMailer ( ) <EOL> generate_notifications = mock . MagicMock ( spec_set = [ ] ) <EOL> generate_notifications . return_value . __iter__ . return_value = iter ( [ <EOL> s . notification1 , <EOL> s . notification2 , <EOL> ] ) <EOL> generate_mail = mock . Mock ( spec_set = [ ] ) <EOL> generate_mail . side_effect = [ <EOL> ( [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> event = AnnotationEvent ( s . request , None , None ) <EOL> subscribers . send_reply_notifications ( event , <EOL> generate_notifications = generate_notifications , <EOL> generate_mail = generate_mail , <EOL> send = send ) <EOL> assert generate_mail . call_args_list == [ <EOL> mock . call ( s . request , s . notification1 ) , <EOL> mock . call ( s . request , s . notification2 ) , <EOL> ] <EOL> assert send . calls == [ <EOL> ( [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> def test_catches_exceptions_and_reports_to_sentry ( self ) : <EOL> send = FakeMailer ( ) <EOL> generate_notifications = mock . Mock ( spec_set = [ ] , side_effect = RuntimeError ( '<STR_LIT>' ) ) <EOL> generate_mail = mock . Mock ( spec_set = [ ] , return_value = [ ] ) <EOL> request = testing . DummyRequest ( sentry = mock . Mock ( ) , debug = False ) <EOL> event = AnnotationEvent ( request , None , None ) <EOL> subscribers . send_reply_notifications ( event , <EOL> generate_notifications = generate_notifications , <EOL> generate_mail = generate_mail , <EOL> send = send ) <EOL> event . request . sentry . captureException . assert_called_once_with ( ) <EOL> def test_reraises_exceptions_in_debug_mode ( self ) : <EOL> send = FakeMailer ( ) <EOL> generate_notifications = mock . Mock ( spec_set = [ ] , side_effect = RuntimeError ( '<STR_LIT>' ) ) <EOL> generate_mail = mock . Mock ( spec_set = [ ] , return_value = [ ] ) <EOL> request = testing . DummyRequest ( sentry = mock . Mock ( ) , debug = True ) <EOL> event = AnnotationEvent ( request , None , None ) <EOL> with pytest . raises ( RuntimeError ) : <EOL> subscribers . send_reply_notifications ( event , <EOL> generate_notifications = generate_notifications , <EOL> generate_mail = generate_mail , <EOL> send = send ) </s>
