Example #1
0
def test_time_limited_access(client, app):
    app.data.insert('products', [{
        '_id': 10,
        'name': 'product test',
        'query': 'versioncreated:<=now-2d',
        'companies': ['1'],
        'is_enabled': True,
        'product_type': 'factcheck'
    }])

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

    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])

    g.settings['wire_time_limit_days']['value'] = 1
    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 0 == len(data['_items'])

    g.settings['wire_time_limit_days']['value'] = 100
    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])

    g.settings['wire_time_limit_days']['value'] = 1
    company = app.data.find_one('companies', req=None, _id=1)
    app.data.update('companies', 1, {'archive_access': True}, company)
    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])
Example #2
0
def test_search_filter_by_individual_navigation(client, app):
    app.data.insert('navigations', [{
        '_id': 51,
        'name': 'navigation-1',
        'is_enabled': True,
        'product_type': 'aapX'
    }, {
        '_id': 52,
        'name': 'navigation-2',
        'is_enabled': True,
        'product_type': 'aapX'
    }])

    app.data.insert('products', [{
        '_id': 10,
        'name': 'product test',
        'sd_product_id': 1,
        'companies': ['1'],
        'navigations': ['51'],
        'product_type': 'aapX',
        'is_enabled': True
    }, {
        '_id': 11,
        'name': 'product test 2',
        'sd_product_id': 2,
        'companies': ['1'],
        'navigations': ['52'],
        'product_type': 'aapX',
        'is_enabled': True
    }])
    with client.session_transaction() as session:
        session['user'] = str(PUBLIC_USER_ID)
        session['user_type'] = 'public'

    resp = client.get('/aapX/search')
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])
    assert '_aggregations' in data
    resp = client.get('/aapX/search?navigation=51')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
    assert '_aggregations' in data

    # test admin user filtering
    with client.session_transaction() as session:
        session['user'] = ADMIN_USER_ID
        session['user_type'] = 'administrator'

    resp = client.get('/aapX/search')
    data = json.loads(resp.get_data())
    assert 3 == len(data['_items'])  # gets all by default

    resp = client.get('/aapX/search?navigation=51')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
Example #3
0
def test_search_created_to(client):
    resp = client.get('/factcheck/search?created_to=%s' % datetime.now().strftime('%Y-%m-%d'))
    data = json.loads(resp.get_data())
    assert 3 == len(data['_items'])

    resp = client.get('/factcheck/search?created_to=%s&timezone_offset=%s' % (
        (datetime.now() - timedelta(days=5)).strftime('%Y-%m-%d'),
        -120
    ))
    data = json.loads(resp.get_data())
    assert 0 == len(data['_items'])
Example #4
0
def test_versions(client):
    resp = client.get('/factcheck/%s/versions' % items[0]['_id'])
    assert 200 == resp.status_code
    data = json.loads(resp.get_data())
    assert len(data.get('_items')) == 0

    resp = client.get('/factcheck/%s/versions' % items[1]['_id'])
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])
    assert 'tag:weather' == data['_items'][0]['_id']
    assert 'AAP' == data['_items'][0]['source']
    assert 'c' == data['_items'][1]['service'][0]['code']
Example #5
0
def test_search_created_from(client):
    resp = client.get('/factcheck/search?created_from=now/d')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])

    resp = client.get('/factcheck/search?created_from=now/w')
    data = json.loads(resp.get_data())
    assert 1 <= len(data['_items'])

    resp = client.get('/factcheck/search?created_from=now/M')
    data = json.loads(resp.get_data())

    assert 1 <= len(data['_items'])
Example #6
0
def test_item_json(client):
    resp = client.get('/factcheck/tag:foo?format=json')
    assert resp.status_code == 200

    data = json.loads(resp.get_data())
    assert 'headline' in data
    assert data['headline'] == 'Amazon Is Opening More Bookstores'
Example #7
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 #8
0
def test_search_by_products_id(client, app):
    app.data.insert('items', [
        {'_id': 'foo', 'headline': 'product test', 'products': [{'code': '12345'}]}
    ])
    resp = client.get('/factcheck/search?q=products.code:12345')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
Example #9
0
def test_administrator_gets_all_results(client):
    with client.session_transaction() as session:
        session['user'] = ADMIN_USER_ID
        session['user_type'] = 'administrator'

    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 3 == len(data['_items'])
Example #10
0
def test_administrator_gets_all_results(client):
    with client.session_transaction() as session:
        session['user'] = str(ObjectId())
        session['user_type'] = 'administrator'

    resp = client.get('/am_news/search')
    data = json.loads(resp.get_data())
    assert 3 == len(data['_items'])
def test_search_includes_killed_items(client, app):
    app.data.insert('items', [{
        '_id': 'foo',
        'pubstatus': 'canceled',
        'headline': 'killed'
    }])
    resp = client.get('/aapX/search?q=headline:killed')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
Example #12
0
def test_search_pagination(client):
    resp = client.get('/factcheck/search?from=25')
    assert 200 == resp.status_code
    data = json.loads(resp.get_data())
    assert 0 == len(data['_items'])
    assert '_aggregations' not in data

    resp = client.get('/factcheck/search?from=2000')
    assert 400 == resp.status_code
Example #13
0
def test_item_copy(client, app):
    resp = client.post('/aapX/{}/copy'.format(items[0]['_id']), content_type='application/json')
    assert resp.status_code == 200

    resp = client.get('/aapX/tag:foo?format=json')
    data = json.loads(resp.get_data())
    assert 'copies' in data

    user_id = app.data.find_all('users')[0]['_id']
    assert str(user_id) in data['copies']
def test_search_filtered_by_query_product(client, app):
    app.data.insert('navigations', [{
        '_id': 51,
        'name': 'navigation-1',
        'is_enabled': True,
        'product_type': 'aapX',
    }, {
        '_id': 52,
        'name': 'navigation-2',
        'is_enabled': True,
        'product_type': 'aapX'
    }])

    app.data.insert('products', [{
        '_id': 12,
        'name': 'product test',
        'query': 'headline:more',
        'companies': ['1'],
        'navigations': ['51'],
        'product_type': 'aapX',
        'is_enabled': True
    }, {
        '_id': 13,
        'name': 'product test 2',
        'query': 'headline:Weather',
        'companies': ['1'],
        'navigations': ['52'],
        'product_type': 'aapX',
        'is_enabled': True
    }])

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

    resp = client.get('/aapX/search')
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])
    assert '_aggregations' in data
    resp = client.get('/aapX/search?navigation=52')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
    assert '_aggregations' in data
Example #15
0
def test_company_type_filter(client, app):
    app.data.insert('products', [{
        '_id': 10,
        'name': 'product test',
        'query': 'versioncreated:<=now-2d',
        'companies': ['1'],
        'is_enabled': True,
        'product_type': 'am_news'
    }])

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

    resp = client.get('/am_news/search')
    data = json.loads(resp.get_data())
    assert 2 == len(data['_items'])

    app.config['COMPANY_TYPES'] = [
        dict(id='test', wire_must={'term': {
            'service.code': 'b'
        }}),
    ]

    company = app.data.find_one('companies', req=None, _id=1)
    app.data.update('companies', 1, {'company_type': 'test'}, company)

    resp = client.get('/am_news/search')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
    assert 'WEATHER' == data['_items'][0]['slugline']

    app.config['COMPANY_TYPES'] = [
        dict(id='test', wire_must_not={'term': {
            'service.code': 'b'
        }}),
    ]

    resp = client.get('/am_news/search')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
    assert 'WEATHER' != data['_items'][0]['slugline']
Example #16
0
def test_item_copy(client, app):
    resp = client.post('/media_releases/{}/copy'.format(items[0]['_id']),
                       content_type='application/json')
    assert resp.status_code == 200

    resp = client.get('/media_releases/tag:foo?format=json')
    data = json.loads(resp.get_data())
    assert 'copies' in data

    user_id = get_admin_user_id(app)
    assert str(user_id) in data['copies']
Example #17
0
def test_administrator_gets_results_based_on_section_filter(client, app):
    with client.session_transaction() as session:
        session['user'] = ADMIN_USER_ID
        session['user_type'] = 'administrator'

    app.data.insert('section_filters', [{
        '_id': 'f-1',
        'name': 'product test 2',
        'query': 'headline:Weather',
        'is_enabled': True,
        'filter_type': 'factcheck'
    }])

    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
Example #18
0
def test_search_filtered_by_users_products(client, app):
    app.data.insert('products', [{
        '_id': 10,
        'name': 'product test',
        'sd_product_id': 1,
        'companies': ['1'],
        'is_enabled': True,
        'product_type': 'factcheck'
    }])

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

    resp = client.get('/factcheck/search')
    data = json.loads(resp.get_data())
    assert 1 == len(data['_items'])
    assert '_aggregations' in data
Example #19
0
def get_bookmarks_count(client, user):
    resp = client.get('/api/factcheck_search?bookmarks=%s' % str(user))
    assert resp.status_code == 200
    data = json.loads(resp.get_data())
    return data['_meta']['total']
def test_search_filters_items_with_updates(client, app):
    resp = client.get('/aapX/search')
    data = json.loads(resp.get_data())
    assert 3 == len(data['_items'])
    assert 'tag:weather' not in [item['_id'] for item in data['_items']]
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'])
def test_item_json(client):
    resp = client.get('/aapX/tag:foo?format=json')
    data = json.loads(resp.get_data())
    assert 'headline' in data
Example #23
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