Exemplo n.º 1
0
def test_send_alerts_respects_last_run_time(client, app):
    test_login_succeeds_for_admin(client)
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'),
                    {'schedule': {'interval': 'two_hour'}}, w)
    app.data.insert('items', [{
        '_id': 'foo_yesterday',
        'headline': 'product yesterday',
        'products': [{'code': '12345'}],
        "versioncreated": even_now - timedelta(days=1)
    }])
    app.data.insert('items', [{
        '_id': 'foo_last_hour',
        'headline': 'product last hour',
        'products': [{'code': '12345'}],
        "versioncreated": even_now - timedelta(minutes=90)
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().scheduled_worker(even_now)
        assert len(outbox) == 1
        assert outbox[0].recipients == ['*****@*****.**', '*****@*****.**']
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Monitoring Subject'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
        assert 'monitoring-export.pdf' in outbox[0].attachments[0]

    with app.mail.record_messages() as newoutbox:
        w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
        assert w is not None
        assert w.get('last_run_time') is not None
        last_run_time = local_to_utc(app.config['DEFAULT_TIMEZONE'], even_now)
        assert w['last_run_time'] > (last_run_time - timedelta(minutes=5))
        MonitoringEmailAlerts().scheduled_worker(last_run_time)
        assert len(newoutbox) == 0
Exemplo n.º 2
0
def test_last_run_time_always_updated_with_matching_content_immediate(
        client, app):
    test_login_succeeds_for_admin(client)
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": utcnow(),
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 1
        assert outbox[0].recipients == [
            '*****@*****.**', '*****@*****.**'
        ]
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Monitoring Subject'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
        assert 'monitoring-export.pdf' in outbox[0].attachments[0]
        w = app.data.find_one('monitoring',
                              None,
                              _id='5db11ec55f627d8aa0b545fb')
        assert w is not None
        assert w.get('last_run_time') is not None
        assert w['last_run_time'] > (mock_utcnow() - timedelta(minutes=5))
Exemplo n.º 3
0
def test_last_run_time_always_updated_with_no_users_scheduled(client, app):
    test_login_succeeds_for_admin(client)
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'), {
        'schedule': {
            'interval': 'two_hour'
        },
        'users': []
    }, w)
    app.data.insert('items', [{
        '_id': 'foo_yesterday',
        'headline': 'product yesterday',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": even_now,
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().scheduled_worker(even_now)
        assert len(outbox) == 0
        w = app.data.find_one('monitoring',
                              None,
                              _id='5db11ec55f627d8aa0b545fb')
        assert w is not None
        assert w.get('last_run_time') is not None
        last_run_time = local_to_utc(app.config['DEFAULT_TIMEZONE'], even_now)
        assert w['last_run_time'] > (last_run_time - timedelta(minutes=5))
Exemplo n.º 4
0
def test_send_immediate_email_alerts(client, app):
    test_login_succeeds_for_admin(client)
    post_json(client, '/settings/general_settings',
              {"monitoring_report_logo_path": get_fixture_path('thumbnail.jpg')})
    app.data.insert('items', [{
        '_id': 'foo',
        'version': '1',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
        'byline': 'Testy McTestface',
        'body_html': '<p>line 1 of the article text\nline 2 of the story\nand a bit more.</p>',
        'source': 'AAAA'
    }])
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'),
                    {"format_type": "monitoring_email", "alert_type": "linked_text",
                     'keywords': ['text']}, w)
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 1
        assert outbox[0].recipients == ['*****@*****.**', '*****@*****.**']
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Monitoring Subject'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
Exemplo n.º 5
0
def test_send_four_hour_alerts(client, app):
    test_login_succeeds_for_admin(client)
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'),
                    {'schedule': {
                        'interval': 'four_hour'
                    }}, w)
    app.data.insert('items', [{
        '_id': 'foo_yesterday',
        'headline': 'product yesterday',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": even_now - timedelta(days=1)
    }])
    app.data.insert('items', [{
        '_id': 'foo_last_hour',
        'headline': 'product three hours',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": even_now - timedelta(hours=3)
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().scheduled_worker(even_now)
        assert len(outbox) == 1
        assert outbox[0].recipients == [
            '*****@*****.**', '*****@*****.**'
        ]
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Monitoring Subject'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
        assert 'monitoring-export.pdf' in outbox[0].attachments[0]
Exemplo n.º 6
0
def test_wont_send_four_hour_alerts_on_odd_hours(client, app):
    now = even_now.replace(hour=3, minute=0)
    test_login_succeeds_for_admin(client)
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'),
                    {'schedule': {
                        'interval': 'four_hour'
                    }}, w)
    app.data.insert('items', [{
        '_id': 'foo_yesterday',
        'headline': 'product yesterday',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": even_now - timedelta(days=1)
    }])
    app.data.insert('items',
                    [{
                        '_id': 'foo_last_hour',
                        'headline': 'product last hour',
                        'products': [{
                            'code': '12345'
                        }],
                        "versioncreated": even_now - timedelta(minutes=90)
                    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().scheduled_worker(now)
        assert len(outbox) == 0
Exemplo n.º 7
0
def test_always_send_immediate_alerts(client, app):
    test_login_succeeds_for_admin(client)
    get_resource_service('monitoring').patch(ObjectId("5db11ec55f627d8aa0b545fb"), {'always_send': False})
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": even_now - timedelta(days=31),
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 0
Exemplo n.º 8
0
def test_disable_always_send_schedule_alerts(client, app):
    test_login_succeeds_for_admin(client)
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'),
                    {'schedule': {'interval': 'two_hour'}, 'always_send': False}, w)
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": even_now - timedelta(days=31),
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().scheduled_worker(even_now)
        assert len(outbox) == 0
Exemplo n.º 9
0
def test_last_run_time_always_updated_with_no_matching_content_immediate(client, app):
    test_login_succeeds_for_admin(client)
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": even_now - timedelta(days=31),
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 0
        w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
        assert w is not None
        assert w.get('last_run_time') is not None
        assert w['last_run_time'] > (mock_utcnow() - timedelta(minutes=5))
Exemplo n.º 10
0
def test_send_weekly_alerts(client, app):
    now = utcnow()
    now = utc_to_local(app.config['DEFAULT_TIMEZONE'], now)
    test_login_succeeds_for_admin(client)
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update(
        'monitoring', ObjectId('5db11ec55f627d8aa0b545fb'), {
            'schedule': {
                'interval': 'weekly',
                'time': (now - timedelta(minutes=1)).strftime('%H:%M'),
                'day': now.strftime('%a').lower(),
            }
        }, w)
    app.data.insert('items', [{
        '_id': 'foo_yesterday',
        'headline': 'product yesterday',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": now - timedelta(hours=22)
    }])
    app.data.insert('items', [{
        '_id': 'foo_last_hour',
        'headline': 'product three hours',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": now - timedelta(hours=3)
    }])
    app.data.insert('items', [{
        '_id': 'foo_four_days',
        'headline': 'product four days',
        'products': [{
            'code': '12345'
        }],
        "versioncreated": now - timedelta(days=4)
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run()
        assert len(outbox) == 1
        assert outbox[0].recipients == [
            '*****@*****.**', '*****@*****.**'
        ]
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Monitoring Subject'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
        assert 'monitoring-export.pdf' in outbox[0].attachments[0]
Exemplo n.º 11
0
def test_send_immediate_alerts(client, app):
    test_login_succeeds_for_admin(client)
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 1
        assert outbox[0].recipients == ['*****@*****.**', '*****@*****.**']
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Monitoring Subject'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
        assert 'monitoring-export.pdf' in outbox[0].attachments[0]
Exemplo n.º 12
0
def test_dont_send_email_to_disabled_companies(client, app):
    test_login_succeeds_for_admin(client)
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
        'byline': 'Testy McTestface',
        'body_html': '<p>line 1 of the article text\nline 2 of the story\nand a bit more.</p>',
        'source': 'AAAA'
    }])
    c = app.data.find_one('companies', None, _id=company_id)
    assert c is not None
    app.data.update('companies', ObjectId(company_id), {'is_enabled': False}, c)
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 0
Exemplo n.º 13
0
def test_send_immediate_headline_subject_alerts(client, app):
    test_login_succeeds_for_admin(client)
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'Article headline about product',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
    }])
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'),
                    {"headline_subject": True}, w)
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 1
        assert outbox[0].recipients == ['*****@*****.**', '*****@*****.**']
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'Article headline about product'
        assert 'Newsroom Monitoring: W1' in outbox[0].body
Exemplo n.º 14
0
def test_dont_send_immediate_email_alerts_twice(client, app):
    test_login_succeeds_for_admin(client)
    post_json(client, '/settings/general_settings',
              {"monitoring_report_logo_path": get_fixture_path('thumbnail.jpg')})
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
        'byline': 'Testy McTestface',
        'body_html': '<p>line 1 of the article text\nline 2 of the story\nand a bit more.</p>',
        'source': 'AAAA'
    }])
    app.data.insert('history', docs=[
        {
            "_id": "foo",
        }
    ], action='email', user={'_id': None, 'company': ObjectId("5c3eb6975f627db90c84093c")}, section='monitoring',
                    monitoring=ObjectId('5db11ec55f627d8aa0b545fb'))
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 0
Exemplo n.º 15
0
def test_dont_send_email_to_disabled_users(client, app):
    test_login_succeeds_for_admin(client)
    app.data.insert('users', [{
        '_id': ObjectId("5d4ccb7265af3eaa4a8395bc"),
        'email': '*****@*****.**',
        'first_name': 'Boo_First_name',
        'is_enabled': False,
        'receive_email': True,
        'company': ObjectId(company_id),
        }, {
        '_id': ObjectId("617f257c04bfdad4366b6997"),
        'email': '*****@*****.**',
        'first_name': 'Ring_In_First_name',
        'is_enabled': True,
        'receive_email': True,
        'company': 'ring in',
    }
    ])
    w = app.data.find_one('monitoring', None, _id='5db11ec55f627d8aa0b545fb')
    assert w is not None
    users = [ObjectId("5c53afa45f627d8333220f15"), ObjectId("5c4684645f627debec1dc3db"),
             ObjectId("5d4ccb7265af3eaa4a8395bc"), ObjectId("617f257c04bfdad4366b6997")]
    app.data.update('monitoring', ObjectId('5db11ec55f627d8aa0b545fb'), {'users': users}, w)

    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
        'byline': 'Testy McTestface',
        'body_html': '<p>line 1 of the article text\nline 2 of the story\nand a bit more.</p>',
        'source': 'AAAA'
    }])
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 1
        assert len(outbox[0].recipients) == 2
        assert outbox[0].recipients == ['*****@*****.**', '*****@*****.**']
Exemplo n.º 16
0
def test_send_profile_email(client, app):
    test_login_succeeds_for_admin(client)
    post_json(client, '/settings/general_settings',
              {"monitoring_report_logo_path": get_fixture_path('thumbnail.jpg')})
    app.data.insert('items', [{
        '_id': 'foo',
        'headline': 'product immediate',
        'products': [{'code': '12345'}],
        "versioncreated": utcnow(),
        'byline': 'Testy McTestface',
        'body_html': '<p>line 1 of the article text\nline 2 of the story\nand a bit more.</p>',
        'source': 'AAAA'
    }])
    m = app.data.find_one('monitoring', None, _id="5db11ec55f627d8aa0b545fb")
    assert m is not None
    app.data.update('monitoring', ObjectId("5db11ec55f627d8aa0b545fb"),
                    {'email': '[email protected],[email protected],[email protected]',
                     'format_type': 'monitoring_email'}, m)
    with app.mail.record_messages() as outbox:
        MonitoringEmailAlerts().run(immediate=True)
        assert len(outbox) == 1
        assert len(outbox[0].recipients) == 4
        assert '*****@*****.**' in outbox[0].recipients
        assert '*****@*****.**' in outbox[0].recipients
Exemplo n.º 17
0
def send_monitoring_immediate_alerts():
    MonitoringEmailAlerts().run(True)
Exemplo n.º 18
0
def send_monitoring_schedule_alerts():
    MonitoringEmailAlerts().run()