Esempio n. 1
0
def test_item_detail_access(client, app):
    item_url = '/factcheck/%s' % items[0]['_id']
    data = get_json(client, item_url)
    assert data['_access']
    assert data['body_html']

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

    # no access by default
    data = get_json(client, item_url)
    assert not data['_access']
    assert not data.get('body_html')

    # add product
    app.data.insert('products', [{
        '_id': 10,
        'name': 'matching product',
        'companies': ['1'],
        'is_enabled': True,
        'product_type': 'factcheck',
        'query': 'slugline:%s' % items[0]['slugline']
    }])

    # normal access
    data = get_json(client, item_url)
    assert data['_access']
    assert data['body_html']
Esempio n. 2
0
def test_stats(client_with_entries_scope_session):
    time.sleep(5)
    entries = get_json(client_with_entries_scope_session, "/query/places")
    assert len(entries) == 9

    entries = get_json(client_with_entries_scope_session, "/stats/places/area")
    assert len(entries) == 4
Esempio n. 3
0
def test_update_via_get_doesnt_mess_up(es, client_with_data_f):
    client = init(
        client_with_data_f,
        es,
        [
            {
                "code": 1,
                "name": "test1",
                "population": 10,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            },
            {
                "code": 2,
                "name": "test2",
                "population": 5,
                "larger_place": 1,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            },
        ],
    )

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 2
    entry_id = entries["hits"][0]["id"]
    assert entry_id == "1"

    response = client.get(
        "places/%s/update" % entry_id,
        data=json.dumps({
            "entry": {
                "code": 4,
                "name": "test3",
                "population": 5,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            },
            "message": "changes",
            "version": 1,
        }),
        content_type="application/json",
    )
    assert response.status_code == 400
    # response_data = json.loads(response.data.decode())
    # assert "newID" in response_data
    # assert "4" == response_data["newID"]

    # check that the old entry with old id has been removed
    entries = get_json(client, "/query/places")
    assert 2 == len(entries["hits"])
Esempio n. 4
0
def test_update_refs2(es, client_with_data_f):
    client = init(client_with_data_f, es, [{
        "code": 3,
        "name": "test3",
        "municipality": [2, 3]
    }])

    client.post(
        "places/3/update",
        data=json.dumps({
            "entry": {
                "code": 3,
                "name": "test3",
                "municipality": [2]
            },
            "message": "changes",
            "version": 1,
        }),
        content_type="application/json",
    )

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 1
    assert entries["hits"][0]["id"] == "3"
    assert entries["hits"][0]["entry"]["municipality"] == [2]
    assert ("v_municipality" not in entries["hits"][0]
            or len(entries["hits"][0]["municipality"]) == 0)
    with client.application.app_context():
        db_entry = entryread.get_entry("places", "3")
        assert len(db_entry.municipality) == 1
        assert db_entry.municipality[0].municipality == 2
Esempio n. 5
0
def test_query_no_q(client_with_entries_scope_session):
    entries = get_json(client_with_entries_scope_session, "/query/places")

    names = extract_names(entries)

    assert entries["total"] == len(ENTRIES)
    assert len(names) == len(ENTRIES)
    print("names = {}".format(names))

    for entry in ENTRIES:
        assert entry["name"] in names

    for i, name in enumerate(names):
        print("name = {}".format(name))
        print("  entry = {}".format(entries["hits"][i]["entry"]))
        if name in ["Grund test", "Hambo", "Alhamn"]:
            print("Testing 'larger_place' for '{}' ...".format(name))
            print("  entry = {}".format(entries["hits"][i]["entry"]))
            assert "larger_place" in entries["hits"][i]["entry"]
            assert "v_larger_place" in entries["hits"][i]["entry"]
        if name == "Alvik":
            print("Testing 'smaller_places' for '{}' ...".format(name))
            print("  entry = {}".format(entries["hits"][i]["entry"]))
            assert "v_smaller_places" in entries["hits"][i]["entry"]
            assert (
                entries["hits"][i]["entry"]["v_smaller_places"][0]["name"] == "Bjurvik"
            )
        elif name in ["Botten test", "Alhamn"]:
            print("Testing 'smaller_places' for '{}' ...".format(name))
            print("  entry = {}".format(entries["hits"][i]["entry"]))
            assert "v_smaller_places" in entries["hits"][i]["entry"]
Esempio n. 6
0
    def render(self, template, json_mocks):
        if type(json_mocks) is not list:
            json_mocks = [json_mocks]
        responses = [HTTPretty.Response(get_json(_)) for _ in json_mocks]

        HTTPretty.register_uri(HTTPretty.GET, self.api_url, responses=responses, content_type='application/json')
        return render_template(template=template)
Esempio n. 7
0
    def test_get_from_cache_when_twitter_api_fails(self, logging_mock):
        exception_message = 'Too many requests'
        HTTPretty.register_uri(
            HTTPretty.GET,
            self.api_url,
            responses=[
                HTTPretty.Response(body=get_json('jeresig.json'),
                                   status=200,
                                   content_encoding='identity'),
                HTTPretty.Response(body=exception_message,
                                   status=429,
                                   content_encoding='identity'),
            ])

        # it should be ok by now
        output, context = render_template(
            """{% get_tweets for "jresig" as tweets %}""")
        cache_key = get_user_cache_key(asvar='tweets', username='******')
        expect(cache.get(cache_key)).should.have.length_of(1)
        expect(context['tweets'][0]['text']).to.equal(
            "This is not John Resig - you should be following @jeresig instead!!!"
        )

        # when twitter api fails, should use cache
        output2, context2 = render_template(
            """{% get_tweets for "jresig" as tweets %}""")
        expect(cache.get(cache_key)).should.have.length_of(1)
        expect(context2['tweets'][0]['text']).to.equal(
            "This is not John Resig - you should be following @jeresig instead!!!"
        )
        logging_mock.assert_called_with(self.logger_name)
        expect(logging_mock.return_value.error.call_args[0][0]).should.contain(
            exception_message)
def test_distribution_in_result(client_with_entries_scope_session, query, endpoint):
    path = "/{endpoint}/places?{query}lexicon_stats=true".format(
        endpoint=endpoint, query="q={}&".format(query) if query else ""
    )
    result = get_json(client_with_entries_scope_session, path)

    assert "distribution" in result
Esempio n. 9
0
    def check_render(self, template, json_mock, expected_kwargs, length=None, asvar='tweets'):
        output, context = self.render(template, json_mock)

        expect(output).should.be.empty
        expect(clear_query_dict(HTTPretty.last_request.querystring)).should.equal(expected_kwargs)
        if length is None:
            length = len(json.loads(get_json(json_mock).decode('utf8')))
        expect(context[asvar]).should.have.length_of(length)
        return context
Esempio n. 10
0
    def render(self, template, json_mocks):
        if type(json_mocks) is not list:
            json_mocks = [json_mocks]
        responses = [HTTPretty.Response(get_json(_)) for _ in json_mocks]

        HTTPretty.register_uri(HTTPretty.GET,
                               self.api_url,
                               responses=responses,
                               content_type='application/json')
        return render_template(template=template)
Esempio n. 11
0
def test_last_modified(es, client_with_data_f):
    before_add = datetime.now(timezone.utc).timestamp()

    time.sleep(1)
    client = init(client_with_data_f, es, [{
        "code": 1,
        "name": "test1",
        "municipality": [1]
    }])
    time.sleep(1)

    after_add = datetime.now(timezone.utc).timestamp()

    entries = get_json(client, "/query/places")
    hit = entries["hits"][0]
    assert "dummy" == hit["last_modified_by"]
    assert before_add < hit["last_modified"]
    assert after_add > hit["last_modified"]

    time.sleep(1)
    client.post(
        "places/1/update",
        data=json.dumps({
            "entry": {
                "code": 1,
                "name": "test2",
                "municipality": [1]
            },
            "message": "changes",
            "version": 1,
        }),
        content_type="application/json",
    )
    time.sleep(1)

    after_update = datetime.now(timezone.utc).timestamp()

    entries = get_json(client, "/query/places")
    hit = entries["hits"][0]
    assert "dummy" == hit["last_modified_by"]
    assert after_add < hit["last_modified"]
    assert after_update > hit["last_modified"]
Esempio n. 12
0
def test_update_refs(es, client_with_data_f):
    client = init(
        client_with_data_f,
        es,
        [
            {
                "code": 5,
                "name": "test1",
                "population": 10,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            },
            {
                "code": 6,
                "name": "test2",
                "population": 5,
                "larger_place": 5,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            },
        ],
    )

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 2
    for val in entries["hits"]:
        assert "entry" in val
        entry = val["entry"]
        print("entry = {}".format(entry))
        if entry["code"] == 5:
            assert "v_smaller_places" in entry
            assert entry["v_smaller_places"][0]["code"] == 6

    client.delete("/places/6/delete")

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 1
    entry = entries["hits"][0]
    assert "v_smaller_places" not in entry
Esempio n. 13
0
def test_update(es, client_with_data_f):
    client = init(
        client_with_data_f,
        es,
        [{
            "code": 3,
            "name": "test3",
            "population": 4,
            "area": 50000,
            "density": 5,
            "municipality": [2, 3],
        }],
    )

    entries = get_json(client, "/query/places")
    entry_id = entries["hits"][0]["id"]

    response = client.post(
        "places/%s/update" % entry_id,
        data=json.dumps({
            "entry": {
                "code": 3,
                "name": "test3",
                "population": 5,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            },
            "message": "changes",
            "version": 1,
        }),
        content_type="application/json",
    )
    response_data = json.loads(response.data.decode())
    assert "newID" in response_data
    assert "3" == response_data["newID"]

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 1
    assert entries["hits"][0]["id"] == entry_id
    assert entries["hits"][0]["entry"]["population"] == 5
Esempio n. 14
0
def test_delete(es, client_with_data_f):
    client = init(
        client_with_data_f,
        es,
        [{
            "code": 3,
            "name": "test3",
            "population": 4,
            "area": 50000,
            "density": 5,
            "municipality": [2, 3],
        }],
    )

    entries = get_json(client, "/query/places")
    entry_id = entries["hits"][0]["id"]

    client.delete("places/%s/delete" % entry_id)

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 0
Esempio n. 15
0
def _test_path(client, path: str, expected_result: List[str]):
    entries = get_json(client, path)

    names = extract_names(entries)
    print("names = {names}".format(names=names))

    assert len(names) == len(expected_result)

    for name in names:
        assert name in expected_result

    for expected in expected_result:
        assert expected in names
Esempio n. 16
0
    def check_render(self,
                     template,
                     json_mock,
                     expected_kwargs,
                     length=None,
                     asvar='tweets'):
        output, context = self.render(template, json_mock)

        expect(output).should.be.empty
        expect(clear_query_dict(
            HTTPretty.last_request.querystring)).should.equal(expected_kwargs)
        if length is None:
            length = len(json.loads(get_json(json_mock).decode('utf8')))
        expect(context[asvar]).should.have.length_of(length)
        return context
Esempio n. 17
0
    def test_get_from_cache_when_twitter_api_fails(self, logging_mock):
        exception_message = 'Too many requests'
        HTTPretty.register_uri(HTTPretty.GET, self.api_url,
                               responses=[
                                   HTTPretty.Response(body=get_json('jeresig.json'), status=200, content_encoding='identity'),
                                   HTTPretty.Response(body=exception_message, status=429, content_encoding='identity'),
                               ])

        # it should be ok by now
        output, context = render_template("""{% get_tweets for "jresig" as tweets %}""")
        cache_key = get_user_cache_key(asvar='tweets', username='******')
        expect(cache.get(cache_key)).should.have.length_of(1)
        expect(context['tweets'][0]['text']).to.equal("This is not John Resig - you should be following @jeresig instead!!!")

        # when twitter api fails, should use cache
        output2, context2 = render_template("""{% get_tweets for "jresig" as tweets %}""")
        expect(cache.get(cache_key)).should.have.length_of(1)
        expect(context2['tweets'][0]['text']).to.equal("This is not John Resig - you should be following @jeresig instead!!!")
        logging_mock.assert_called_with(self.logger_name)
        expect(logging_mock.return_value.error.call_args[0][0]).should.contain(exception_message)
Esempio n. 18
0
def _test_against_entries(
    client, path: str, field: str, predicate: Callable, expected_n_hits: int = None
):
    entries = get_json(client, path)
    names = extract_names(entries)

    if field.endswith(".raw"):
        field = field[:-4]

    assert len(names) == sum(
        1 for entry in ENTRIES if field in entry and predicate(entry[field])
    )

    num_names_to_match = len(names)
    for entry in ENTRIES:
        if field in entry and predicate(entry[field]):
            assert entry["name"] in names
            num_names_to_match -= 1
    assert num_names_to_match == 0

    if expected_n_hits:
        assert len(entries["hits"]) == expected_n_hits
Esempio n. 19
0
def test_contains_and_separate_calls(
    client_with_entries_scope_session,
    fields: Tuple,
    values: Tuple,
    expected_result: List[str],
):
    names = set()
    for field, value in zip(fields, values):
        query = "/query/places?q=contains|{field}|{value}".format(
            field=field, value=value
        )
        entries = get_json(client_with_entries_scope_session, query)
        if not names:
            print("names is empty")
            names = extract_names_set(entries)
        else:
            names = names & extract_names_set(entries)

    assert len(names) == len(expected_result)
    for name in names:
        assert name in expected_result
    for expected in expected_result:
        assert expected in names
Esempio n. 20
0
def test_add(es, client_with_data_f):
    client = init(client_with_data_f, es, [])

    response = client.post(
        "places/add",
        data=json.dumps({
            "entry": {
                "code": 3,
                "name": "test3",
                "population": 4,
                "area": 50000,
                "density": 5,
                "municipality": [2, 3],
            }
        }),
        content_type="application/json",
    )
    response_data = json.loads(response.data.decode())
    assert "newID" in response_data
    assert "3" == response_data["newID"]

    entries = get_json(client, "/query/places")
    assert len(entries["hits"]) == 1
    assert entries["hits"][0]["entry"]["name"] == "test3"
Esempio n. 21
0
def _test_against_entries_general(
    client,
    path: str,
    fields: Tuple[str],
    predicate: Callable,
    expected_n_hits: int = None,
):
    entries = get_json(client, path)
    names = extract_names(entries)

    print("names = {names}".format(names=names))
    for i, field in enumerate(fields):
        if field.endswith(".raw"):
            fields[i] = field[:-4]

    for field in fields:
        print("field = {field}".format(field=field))

    # assert len(names) == sum(1 for entry in ENTRIES
    #                          if all(field in entry for field in fields) and predicate([entry[field]
    #                          for field in fields]))

    num_names_to_match = len(names)
    for entry in ENTRIES:
        print("entry = {entry}".format(entry=entry))
        # if all(field in entry for field in fields) and predicate(
        #     [entry[field] for field in fields]
        # ):
        if predicate(entry, fields):
            print("entry '{entry}' satisfied predicate".format(entry=entry))
            assert entry["name"] in names
            num_names_to_match -= 1
    assert num_names_to_match == 0

    if expected_n_hits:
        assert len(entries["hits"]) == expected_n_hits
Esempio n. 22
0
def test_external_refs(es, client_with_data_f):
    client = init(
        client_with_data_f,
        es,
        [
            {
                "code": 1,
                "name": "test1",
                "population": 10,
                "area": 50000,
                "density": 5,
                "municipality": [1],
            },
            {
                "code": 2,
                "name": "test2",
                "population": 5,
                "larger_place": 1,
                "area": 50000,
                "density": 5,
                "municipality": [1, 2],
            },
            {
                "code": 3,
                "name": "test2",
                "population": 5,
                "larger_place": 1,
                "area": 50000,
                "density": 5,
                "municipality": [2],
            },
        ],
    )

    client.post(
        "municipalities/add",
        data=json.dumps({
            "entry": {
                "code": 1,
                "name": "municipality1",
                "state": "state1",
                "region": "region1",
            }
        }),
        content_type="application/json",
    )

    client.post(
        "municipalities/add",
        data=json.dumps({
            "entry": {
                "code": 2,
                "name": "municipality2",
                "state": "state2",
                "region": "region2",
            }
        }),
        content_type="application/json",
    )

    entries = get_json(client, "/query/municipalities")
    for val in entries["hits"]:
        assert "entry" in val
        entry = val["entry"]

        assert "v_places" in entry
        place_codes = [place["code"] for place in entry["v_places"]]
        assert len(place_codes) == 2
        if entry["code"] == 1:
            assert 1 in place_codes
            assert 2 in place_codes
        else:
            assert 2 in place_codes
            assert 3 in place_codes

    places_entries = get_json(client, "/query/places")
    for val in places_entries["hits"]:
        assert "entry" in val
        entry = val["entry"]
        assert "municipality" in entry
        assert isinstance(entry["v_municipality"], list)
        if entry["code"] == 2:
            assert {
                "code": 1,
                "name": "municipality1",
                "state": "state1"
            } in entry["v_municipality"]
            assert {
                "code": 2,
                "name": "municipality2",
                "state": "state2"
            } in entry["v_municipality"]