Example #1
0
def test_share_items(client, app):
    user_ids = app.data.insert('users', [{
        'email': '*****@*****.**',
        'first_name': 'Foo',
        'last_name': 'Bar',
    }])

    with app.mail.record_messages() as outbox:
        resp = client.post('/wire_share?type=factcheck', data=json.dumps({
            'items': [item['_id'] for item in items],
            'users': [str(user_ids[0])],
            'message': 'Some info message',
        }), content_type='application/json')

        assert resp.status_code == 201, resp.get_data().decode('utf-8')
        assert len(outbox) == 1
        assert outbox[0].recipients == ['*****@*****.**']
        assert outbox[0].sender == 'newsroom@localhost'
        assert outbox[0].subject == 'From AAP Newsroom: %s' % items[0]['headline']
        assert 'Hi Foo Bar' in outbox[0].body
        assert 'admin admin ([email protected]) shared ' in outbox[0].body
        assert items[0]['headline'] in outbox[0].body
        assert items[1]['headline'] in outbox[0].body
        assert 'http://localhost:5050/factcheck?item=%s' % parse.quote(items[0]['_id']) in outbox[0].body
        assert 'http://localhost:5050/factcheck?item=%s' % parse.quote(items[1]['_id']) in outbox[0].body
        assert 'Some info message' in outbox[0].body

    resp = client.get('/factcheck/{}?format=json'.format(items[0]['_id']))
    data = json.loads(resp.get_data())
    assert 'shares' in data

    user_id = get_admin_user_id(app)
    assert str(user_id) in data['shares']
Example #2
0
def test_bookmarks(client, app):
    user_id = get_admin_user_id(app)
    assert user_id

    assert 0 == get_bookmarks_count(client, user_id)

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

    assert 1 == get_bookmarks_count(client, user_id)

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

    assert 0 == get_bookmarks_count(client, user_id)
Example #3
0
def test_bookmarks(client, app):
    user_id = app.data.find_all('users')[0]['_id']
    assert user_id

    assert 0 == get_bookmarks_count(client, user_id)

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

    assert 1 == get_bookmarks_count(client, user_id)

    client.delete('/am_news_bookmark', data=json.dumps({
        'items': [items[0]['_id']],
    }), content_type='application/json')
    assert resp.status_code == 200

    assert 0 == get_bookmarks_count(client, user_id)
Example #4
0
def test_bookmarks_by_section(client, app):
    products = [{
        "_id": 1,
        "name": "Service A",
        "query": "service.code: a",
        "is_enabled": True,
        "description": "Service A",
        "companies": ['1'],
        "sd_product_id": None,
        "product_type": "am_news"
    }]

    app.data.insert('products', products)
    product_id = app.data.find_all('products')[0]['_id']
    assert product_id == 1

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

    assert 0 == get_bookmarks_count(client, PUBLIC_USER_ID)

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

    assert 1 == get_bookmarks_count(client, PUBLIC_USER_ID)

    client.delete('/am_news_bookmark',
                  data=json.dumps({
                      'items': [items[0]['_id']],
                  }),
                  content_type='application/json')
    assert resp.status_code == 200

    assert 0 == get_bookmarks_count(client, PUBLIC_USER_ID)
Example #5
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 #6
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
Example #7
0
def test_filter_by_product_anonymous_user_gets_all(client):
    resp = client.get('/factcheck/search?products=%s' % json.dumps({'10': True}))
    data = json.loads(resp.get_data())
    assert 3 == len(data['_items'])
    assert '_aggregations' in data
def test_search_filter_by_category(client, app):
    resp = client.get('/aapX/search?filter=%s' %
                      json.dumps({'service': ['Service A']}))
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])