Example #1
0
def test_notify_user_matches_for_new_item_in_history(client, app, mocker):
    company_ids = app.data.insert('companies', [{
        'name': 'Press co.',
        'is_enabled': True,
    }])

    user = {
        'email': '*****@*****.**',
        'first_name': 'Foo',
        'is_enabled': True,
        'is_approved': True,
        'receive_email': True,
        'company': company_ids[0],
    }

    user_ids = app.data.insert('users', [user])
    user['_id'] = user_ids[0]

    app.data.insert('history',
                    docs=[{
                        'version': '1',
                        '_id': 'bar',
                    }],
                    action='download',
                    user=user,
                    section='media_releases')

    with app.mail.record_messages() as outbox:
        key = b'something random'
        app.config['PUSH_KEY'] = key
        data = json.dumps({
            'guid': 'bar',
            'type': 'text',
            'headline': 'this is a test'
        })
        push_mock = mocker.patch('newsroom.push.push_notification')
        headers = get_signature_headers(data, key)
        resp = client.post('/push',
                           data=data,
                           content_type='application/json',
                           headers=headers)
        assert 200 == resp.status_code
        assert push_mock.call_args[1]['item']['_id'] == 'bar'
        assert len(push_mock.call_args[1]['users']) == 1

        notification = get_resource_service('notifications').find_one(
            req=None, user=user_ids[0])
        assert notification['item'] == 'bar'

    assert len(outbox) == 1
    assert 'http://localhost:5050/media_releases?item=bar' in outbox[0].body
Example #2
0
def test_notify_user_matches_for_new_item_in_bookmarks(client, app, mocker):
    app.data.insert('section_filters', [{
        '_id': 'f-1',
        'name': 'product test 1',
        'query': 'NOT service.code:a',
        'is_enabled': True,
        'filter_type': 'wire'
    }, {
        '_id': 'f-4',
        'name': 'product test 4',
        'query': 'NOT service.code:a',
        'is_enabled': True,
        'filter_type': 'am_news'
    }, {
        '_id': 'f-2',
        'name': 'product test 2',
        'query': 'service.code:a',
        'is_enabled': True,
        'filter_type': 'factcheck'
    }, {
        '_id': 'f-3',
        'name': 'product test 3',
        'query': 'NOT service.code:a',
        'is_enabled': True,
        'filter_type': 'aapX'
    }, {
        '_id': 'f-5',
        'name': 'product test 5',
        'query': 'NOT service.code:a',
        'is_enabled': True,
        'filter_type': 'media_releases'
    }])

    app.data.insert('companies', [{
        '_id': '2',
        'name': 'Press co.',
        'is_enabled': True,
        'sections': {
            'aapX': True,
            'agenda': True,
            'wire': True,
            'am_news': True,
            'media_releases': False,
            'factcheck': True,
        }
    }])

    user = {
        'email': '*****@*****.**',
        'first_name': 'Foo',
        'is_enabled': True,
        'is_approved': True,
        'receive_email': True,
        'company': '2',
    }

    user_ids = app.data.insert('users', [user])
    user['_id'] = user_ids[0]

    app.data.insert('products', [{
        "_id": 1,
        "name": "Service A",
        "query": "service.code: a",
        "is_enabled": True,
        "description": "Service A",
        "companies": ['2'],
        "sd_product_id": None,
        "product_type": "factcheck",
    }])

    app.data.insert('items', [{
        '_id': 'bar',
        'headline': 'testing',
        'service': [{'code': 'a', 'name': 'Service A'}],
        'products': [{'code': 1, 'name': 'product-1'}],
    }])

    with client.session_transaction() as session:
        session['user'] = user['_id']
        session['user_type'] = 'public'
        session['name'] = 'public'

    resp = client.post('/factcheck_bookmark', data=json.dumps({
        'items': ['bar'],
    }), content_type='application/json')
    assert resp.status_code == 200

    with app.mail.record_messages() as outbox:
        key = b'something random'
        app.config['PUSH_KEY'] = key
        data = json.dumps({'guid': 'bar', 'type': 'text', 'headline': 'this is a test'})
        push_mock = mocker.patch('newsroom.push.push_notification')
        headers = get_signature_headers(data, key)
        resp = client.post('/push', data=data, content_type='application/json', headers=headers)
        assert 200 == resp.status_code
        assert push_mock.call_args_list[0][0][0] == 'new_item'
        assert push_mock.call_args_list[1][0][0] == 'history_matches'
        assert push_mock.call_args[1]['item']['_id'] == 'bar'
        notification = get_resource_service('notifications').find_one(req=None, user=user_ids[0])
        assert notification['item'] == 'bar'

    assert len(outbox) == 1
    assert 'http://localhost:5050/factcheck?item=bar' in outbox[0].body