def test_generate_notifications_only_if_author_can_read_reply(Subscriptions, render_reply_notification, effective_principals): """ If the annotation is not readable by the parent author, no notifications should be generated. """ private_annotation = Annotation.fetch(6) shared_annotation = Annotation.fetch(7) request = _create_request() effective_principals.return_value = [ security.Everyone, security.Authenticated, 'acct:[email protected]', 'group:wibble', ] Subscriptions.get_active_subscriptions_for_a_type.return_value = [ MockSubscription(id=1, uri='acct:[email protected]') ] render_reply_notification.return_value = ( 'Dummy subject', 'Dummy text', 'Dummy HTML', ['*****@*****.**'] ) notifications = rt.generate_notifications(request, private_annotation, 'create') assert list(notifications) == [] notifications = rt.generate_notifications(request, shared_annotation, 'create') assert list(notifications) != []
def test_send_if_everything_is_okay(): """Test whether we generate notifications if every condition is okay""" with patch('h.notification.reply_template.Annotation') as mock_annotation: mock_annotation.fetch = MagicMock(side_effect=fake_fetch) request = _create_request() annotation = store_fake_data[1] with patch('h.notification.reply_template.Subscriptions') as mock_subs: mock_subs.get_active_subscriptions_for_a_type.return_value = [ MockSubscription(id=1, uri='acct:[email protected]') ] with patch('h.notification.reply_template.check_conditions' ) as mock_conditions: mock_conditions.return_value = True with patch( 'h.notification.reply_template.render') as mock_render: mock_render.return_value = '' with patch('h.notification.reply_template.get_user_by_name' ) as mock_user_db: user = Mock() user.email = '*****@*****.**' mock_user_db.return_value = user msgs = rt.generate_notifications( request, annotation, 'create') msgs.next()
def test_generate_notifications_empty_if_action_not_create(): """If the action is not 'create', no notifications should be generated.""" annotation = Annotation() request = DummyRequest() notifications = rt.generate_notifications(request, annotation, 'update') assert list(notifications) == []
def test_generate_notifications_empty_if_annotation_has_no_parent(): """If the annotation has no parent no notifications should be generated.""" annotation = Annotation.fetch(0) request = DummyRequest() notifications = rt.generate_notifications(request, annotation, 'create') assert list(notifications) == []
def test_action_update(): """It action is not create, it should immediately return""" annotation = {} request = DummyRequest() with patch('h.notification.reply_template.parent_values') as mock_parent: msgs = rt.generate_notifications(request, annotation, 'update') with raises(StopIteration): msgs.next() assert mock_parent.call_count == 0
def send_notifications(message): data = json.loads(message.body) action = data['action'] annotation = storage.annotation_from_dict(data['annotation']) mailer = get_mailer(request) notifications = generate_notifications(request, annotation, action) for (subject, body, html, recipients) in notifications: m = Message(subject=subject, recipients=recipients, body=body, html=html) mailer.send_immediately(m)
def test_generate_notifications_does_not_fetch_if_annotation_has_no_parent(fetch): """Don't try and fetch None if the annotation has no parent""" annotation = _fake_anno(0) request = DummyRequest() notifications = rt.generate_notifications(request, annotation, 'create') # Read the generator list(notifications) fetch.assert_not_called()
def test_generate_notifications_checks_subscriptions(Subscriptions): """If the annotation has a parent, then proceed to check subscriptions.""" request = _create_request() annotation = Annotation.fetch(1) Subscriptions.get_active_subscriptions_for_a_type.return_value = [] notifications = rt.generate_notifications(request, annotation, "create") # Read the generator list(notifications) Subscriptions.get_active_subscriptions_for_a_type.assert_called_with(REPLY_TYPE)
def test_action_create(): """If the action is create, it'll try to get the subscriptions""" with patch('h.notification.reply_template.Annotation') as mock_annotation: mock_annotation.fetch = MagicMock(side_effect=fake_fetch) request = _create_request() annotation = store_fake_data[1] with patch('h.notification.reply_template.Subscriptions') as mock_subs: mock_subs.get_active_subscriptions_for_a_type.return_value = [] msgs = rt.generate_notifications(request, annotation, 'create') with raises(StopIteration): msgs.next() assert mock_subs.get_active_subscriptions_for_a_type.called
def test_generate_notifications_checks_subscriptions(Subscriptions): """If the annotation has a parent, then proceed to check subscriptions.""" request = _create_request() annotation = Annotation.fetch(1) Subscriptions.get_active_subscriptions_for_a_type.return_value = [] notifications = rt.generate_notifications(request, annotation, 'create') # Read the generator list(notifications) Subscriptions.get_active_subscriptions_for_a_type.assert_called_with( REPLY_TYPE)
def test_check_conditions_false_stops_sending(): """If the check conditions() returns False, no notifications are generated""" request = _create_request() annotation = Annotation.fetch(1) with patch('h.notification.reply_template.Subscriptions') as mock_subs: mock_subs.get_active_subscriptions_for_a_type.return_value = [ MockSubscription(id=1, uri='acct:[email protected]') ] with patch('h.notification.reply_template.check_conditions') as mock_conditions: mock_conditions.return_value = False with pytest.raises(StopIteration): msgs = rt.generate_notifications(request, annotation, 'create') msgs.next()
def test_generate_notifications_only_if_author_can_read_reply( Subscriptions, render_reply_notification, auth, effective_principals): """ If the annotation is not readable by the parent author, no notifications should be generated. """ effective_principals.return_value = [ security.Everyone, security.Authenticated, 'acct:[email protected]', 'group:wibble', ] Subscriptions.get_active_subscriptions_for_a_type.return_value = [ MockSubscription(id=1, uri='acct:[email protected]') ] render_reply_notification.return_value = ( 'Dummy subject', 'Dummy text', 'Dummy HTML', ['*****@*****.**'] ) auth.has_permission.return_value = False notifications = rt.generate_notifications(_fake_request(), _fake_anno(6), 'create') assert list(notifications) == [] auth.has_permission.return_value = True notifications = rt.generate_notifications(_fake_request(), _fake_anno(7), 'create') assert list(notifications) != []
def test_send_if_everything_is_okay(): """Test whether we generate notifications if every condition is okay""" request = _create_request() annotation = Annotation.fetch(1) with patch('h.notification.reply_template.Subscriptions') as mock_subs: mock_subs.get_active_subscriptions_for_a_type.return_value = [ MockSubscription(id=1, uri='acct:[email protected]') ] with patch('h.notification.reply_template.check_conditions') as mock_conditions: mock_conditions.return_value = True with patch('h.notification.reply_template.render') as mock_render: mock_render.return_value = '' with patch('h.notification.reply_template.get_user_by_name') as mock_user_db: user = Mock() user.email = '*****@*****.**' mock_user_db.return_value = user msgs = rt.generate_notifications(request, annotation, 'create') msgs.next()