def test_post_service_object(random_service, mf_api_client):
    service_object_id = get_random_string(12)
    service_object_type = 'http://www.hel.fi/servicemap/v2'
    title = get_random_string(12)

    issues = get_data_from_response(
        mf_api_client.post(
            ISSUE_LIST_ENDPOINT + '?extensions=citysdk',
            {
                'service_code': random_service.service_code,
                'lat': 42,
                'long': 42,
                'description': 'hellote',
                'service_object_id': service_object_id,
                'service_object_type': service_object_type,
                'title': title,
            }
        ),
        schema=LIST_OF_ISSUES_SCHEMA,
        status_code=201
    )
    issue = issues[0]
    verify_issue(issue)
    iex = Issue_CitySDK.objects.get(issue__identifier=issue['service_request_id'])

    assert iex.service_object_id == issue['extended_attributes']['service_object_id'] == service_object_id
    assert iex.service_object_type == issue['extended_attributes']['service_object_type'] == service_object_type
    assert iex.title == issue['extended_attributes']['title'] == title
Ejemplo n.º 2
0
def test_post_issue_roundtrip(mf_api_client, random_service, input_data):
    sig_issue = [None]

    def signal_handler(sender, issue, request, **kwargs):
        sig_issue[0] = issue

    issue_posted.connect(signal_handler)

    input_data = dict(input_data, service_code=random_service.service_code)
    issues = get_data_from_response(
        mf_api_client.post(ISSUE_LIST_ENDPOINT, input_data),
        201,
        schema=LIST_OF_ISSUES_SCHEMA,
    )
    issue = issues[0]
    verify_issue(issue)

    # Test that the issue matches what we posted
    for key, input_value in input_data.items():
        output_value = issue[key]
        if isinstance(input_value, float):
            assert close_enough(input_value, float(output_value))
        else:
            assert input_value == output_value

    # Test that the issue_posted signal was fired and the last issue posted
    # is what we expect
    assert sig_issue[0].identifier == issue['service_request_id']
Ejemplo n.º 3
0
def test_get_by_service_request_id(testing_issues, mf_api_client):
    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'service_request_id': '1982hglaqe8pdnpophff'}),
        schema=LIST_OF_ISSUES_SCHEMA
    )
    assert len(content) == 1
    verify_issue(content[0])
Ejemplo n.º 4
0
def test_post_issue_roundtrip(mf_api_client, random_service, input_data):
    sig_issue = [None]

    def signal_handler(sender, issue, request, **kwargs):
        sig_issue[0] = issue

    issue_posted.connect(signal_handler)

    input_data = dict(input_data, service_code=random_service.service_code)
    issues = get_data_from_response(
        mf_api_client.post(ISSUE_LIST_ENDPOINT, input_data),
        201,
        schema=LIST_OF_ISSUES_SCHEMA,
    )
    issue = issues[0]
    verify_issue(issue)

    # Test that the issue matches what we posted
    for key, input_value in input_data.items():
        output_value = issue[key]
        if isinstance(input_value, float):
            assert close_enough(input_value, float(output_value))
        else:
            assert input_value == output_value

    # Test that the issue_posted signal was fired and the last issue posted
    # is what we expect
    assert sig_issue[0].identifier == issue['service_request_id']
Ejemplo n.º 5
0
def test_post_issue_api_key(mf_api_client, random_service, api_key_mode,
                            pass_api_key):
    expected_app = Application.autodetermine()
    expected_status = 201
    if api_key_mode == 'actual-apps':
        for x in range(5):
            expected_app = Application.objects.create(identifier='app%d' %
                                                      (x + 1))
        if not pass_api_key:
            expected_status = 400

    input_data = dict(
        description=get_random_string(),
        service_code=random_service.service_code,
        address='hello',
        api_key=(expected_app.key if pass_api_key else ''),
    )
    issues = get_data_from_response(
        mf_api_client.post(ISSUE_LIST_ENDPOINT, input_data),
        status_code=expected_status,
        schema=LIST_OF_ISSUES_SCHEMA,
    )
    if expected_status >= 400:
        return  # Nothing more to do here
    issue = verify_issue(issues[0])
    assert issue.application == expected_app
Ejemplo n.º 6
0
def test_post_geometry(random_service, mf_api_client, geometry_data):
    if sys.version_info[0] == 2 and mf_api_client.format != 'json':
        pytest.xfail('unsupported')
    from issues_geometry.models import IssueGeometry
    post_data = {
        'extensions': 'geometry',
        'service_code': random_service.service_code,
        'description': 'Olut on loppu koko jokirannasta',
        'geometry': (
            json.dumps(geometry_data)
            if geometry_data
            else ''
        ),
    }
    if not post_data.get('geometry'):
        post_data['address'] = 'foo street'
    response = mf_api_client.post(ISSUE_LIST_ENDPOINT, data=post_data)

    content = get_data_from_response(
        response,
        status_code=201,
        schema=LIST_OF_ISSUES_SCHEMA,
    )
    issue_data = content[0]
    issue = verify_issue(issue_data)
    if not geometry_data:
        # This exercises the code path where one requests the geometry extension
        # but doesn't actually post geometry after all.
        return
    # No matter the format, we should always have a GeoJSON fragment, whether encoded or indented, in there:
    assert re.search(r'\\*"type\\*":\s*\\*"Polygon\\*"', response.content.decode('utf8'))
    assert IssueGeometry.objects.filter(issue=issue).exists()
    retrieved_issue_data = get_data_from_response(
        mf_api_client.get(
            reverse('georeport/v2:issue-detail', kwargs={'identifier': issue.identifier}),
            {'extensions': 'geometry'},
        )
    )[0]

    for data in (issue_data, retrieved_issue_data):
        verify_issue(data)
        if mf_api_client.format == 'json':
            # We can't access the extended attribute correctly when it has been mangled by the
            # test harness, so only test it when doing native JSON.
            GeoJSONValidator.validate(data['extended_attributes']['geometry'])
Ejemplo n.º 7
0
def test_get_by_service_request_ids(testing_issues, mf_api_client):
    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT,
        {'service_request_id': '1982hglaqe8pdnpophff,2981hglaqe8pdnpoiuyt'}),
                                     schema=LIST_OF_ISSUES_SCHEMA)
    assert set(c['service_request_id'] for c in content) == {
        '1982hglaqe8pdnpophff', '2981hglaqe8pdnpoiuyt'
    }
    assert all(verify_issue(c) for c in content)
Ejemplo n.º 8
0
def test_get_by_service_code(testing_issues, mf_api_client):
    service_code = '171'
    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT, {'service_code': service_code}),
                                     schema=LIST_OF_ISSUES_SCHEMA)

    for issue in content:
        assert verify_issue(issue)
        assert issue['service_code'] == service_code
Ejemplo n.º 9
0
def test_get_by_service_code(testing_issues, mf_api_client):
    service_code = '171'
    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'service_code': service_code}),
        schema=LIST_OF_ISSUES_SCHEMA
    )

    for issue in content:
        assert verify_issue(issue)
        assert issue['service_code'] == service_code
Ejemplo n.º 10
0
def test_post_geometry(random_service, mf_api_client, geometry_data):
    if sys.version_info[0] == 2 and mf_api_client.format != 'json':
        pytest.xfail('unsupported')
    from issues_geometry.models import IssueGeometry
    post_data = {
        'extensions': 'geometry',
        'service_code': random_service.service_code,
        'description': 'Olut on loppu koko jokirannasta',
        'geometry': (json.dumps(geometry_data) if geometry_data else ''),
    }
    if not post_data.get('geometry'):
        post_data['address'] = 'foo street'
    response = mf_api_client.post(ISSUE_LIST_ENDPOINT, data=post_data)

    content = get_data_from_response(
        response,
        status_code=201,
        schema=LIST_OF_ISSUES_SCHEMA,
    )
    issue_data = content[0]
    issue = verify_issue(issue_data)
    if not geometry_data:
        # This exercises the code path where one requests the geometry extension
        # but doesn't actually post geometry after all.
        return
    # No matter the format, we should always have a GeoJSON fragment, whether encoded or indented, in there:
    assert re.search(r'\\*"type\\*":\s*\\*"Polygon\\*"',
                     response.content.decode('utf8'))
    assert IssueGeometry.objects.filter(issue=issue).exists()
    retrieved_issue_data = get_data_from_response(
        mf_api_client.get(
            reverse('georeport/v2:issue-detail',
                    kwargs={'identifier': issue.identifier}),
            {'extensions': 'geometry'},
        ))[0]

    for data in (issue_data, retrieved_issue_data):
        verify_issue(data)
        if mf_api_client.format == 'json':
            # We can't access the extended attribute correctly when it has been mangled by the
            # test harness, so only test it when doing native JSON.
            GeoJSONValidator.validate(data['extended_attributes']['geometry'])
Ejemplo n.º 11
0
def test_get_by_status(testing_issues, mf_api_client):
    issue_status = 'open'
    expected_number_of_requests = 2

    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT, {'status': issue_status}),
                                     schema=LIST_OF_ISSUES_SCHEMA)

    assert len(content) == expected_number_of_requests
    for issue in content:
        assert verify_issue(issue)
        assert issue['status'] == issue_status
Ejemplo n.º 12
0
def test_get_by_end_data(testing_issues, mf_api_client):
    end_date = '2015-06-23T15:51:11Z'
    expected_number_of_requests = 1

    content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT,
                                                       {'end_date': end_date}),
                                     schema=LIST_OF_ISSUES_SCHEMA)

    assert len(content) == expected_number_of_requests
    for issue in content:
        assert verify_issue(issue)
        assert issue['requested_datetime'] < end_date
Ejemplo n.º 13
0
def test_get_by_updated_before(testing_issues, mf_api_client):
    updated_before = '2015-07-24T12:01:44Z'
    expected_number_of_requests = 1

    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT, {'updated_before': updated_before}),
                                     schema=LIST_OF_ISSUES_SCHEMA)

    assert len(content) == expected_number_of_requests
    for issue in content:
        assert verify_issue(issue)
        assert issue['updated_datetime'] < updated_before
Ejemplo n.º 14
0
def test_get_by_service_request_ids(testing_issues, mf_api_client):
    content = get_data_from_response(
        mf_api_client.get(
            ISSUE_LIST_ENDPOINT,
            {'service_request_id': '1982hglaqe8pdnpophff,2981hglaqe8pdnpoiuyt'}
        ),
        schema=LIST_OF_ISSUES_SCHEMA
    )
    assert set(c['service_request_id'] for c in content) == {
        '1982hglaqe8pdnpophff',
        '2981hglaqe8pdnpoiuyt'
    }
    assert all(verify_issue(c) for c in content)
Ejemplo n.º 15
0
def test_get_by_end_data(testing_issues, mf_api_client):
    end_date = '2015-06-23T15:51:11Z'
    expected_number_of_requests = 1

    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'end_date': end_date}),
        schema=LIST_OF_ISSUES_SCHEMA
    )

    assert len(content) == expected_number_of_requests
    for issue in content:
        assert verify_issue(issue)
        assert issue['requested_datetime'] < end_date
Ejemplo n.º 16
0
def test_get_by_status(testing_issues, mf_api_client):
    issue_status = 'open'
    expected_number_of_requests = 2

    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'status': issue_status}),
        schema=LIST_OF_ISSUES_SCHEMA
    )

    assert len(content) == expected_number_of_requests
    for issue in content:
        assert verify_issue(issue)
        assert issue['status'] == issue_status
Ejemplo n.º 17
0
def test_get(testing_issues, mf_api_client, extensions):
    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT, {
            'format': format,
            'service_request_id': '1982hglaqe8pdnpophff',
            'extensions': ('true' if extensions else 'false'),
        }),
                                     schema=LIST_OF_ISSUES_SCHEMA)
    assert verify_issue(content[0])
    if extensions:
        assert 'extended_attributes' in content[0]
    else:
        assert 'extended_attributes' not in content[0]
Ejemplo n.º 18
0
def test_get_by_updated_before(testing_issues, mf_api_client):
    updated_before = '2015-07-24T12:01:44Z'
    expected_number_of_requests = 1

    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'updated_before': updated_before}),
        schema=LIST_OF_ISSUES_SCHEMA
    )

    assert len(content) == expected_number_of_requests
    for issue in content:
        assert verify_issue(issue)
        assert issue['updated_datetime'] < updated_before
Ejemplo n.º 19
0
def test_default_moderation_status(mf_api_client, random_service, settings, status):
    """
    Test that when the default mod status is not 'public',
    freshly created issues are not visible via the list endpoint
    """
    settings.ISSUES_DEFAULT_MODERATION_STATUS = status
    posted_issues = get_data_from_response(
        mf_api_client.post(ISSUE_LIST_ENDPOINT, {
            "lat": 15,
            "long": 15,
            "description": get_random_string(),
            "service_code": random_service.service_code,
        }),
        201,
        schema=LIST_OF_ISSUES_SCHEMA
    )
    verify_issue(posted_issues[0])

    listed_issues = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT),
        200,
    )
    assert bool(listed_issues) == (status == 'public')
Ejemplo n.º 20
0
def test_default_moderation_status(mf_api_client, random_service, settings,
                                   status):
    """
    Test that when the default mod status is not 'public',
    freshly created issues are not visible via the list endpoint
    """
    settings.ISSUES_DEFAULT_MODERATION_STATUS = status
    posted_issues = get_data_from_response(mf_api_client.post(
        ISSUE_LIST_ENDPOINT, {
            "lat": 15,
            "long": 15,
            "description": get_random_string(),
            "service_code": random_service.service_code,
        }),
                                           201,
                                           schema=LIST_OF_ISSUES_SCHEMA)
    verify_issue(posted_issues[0])

    listed_issues = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT),
        200,
    )
    assert bool(listed_issues) == (status == 'public')
Ejemplo n.º 21
0
def test_post_issue_no_jurisdiction(mf_api_client, random_service):
    assert not Jurisdiction.objects.exists()
    for attempt in [1, 2]:
        issues = get_data_from_response(
            mf_api_client.post(ISSUE_LIST_ENDPOINT, {
                "service_code": random_service.service_code,
                "lat": 30,
                "long": 30,
                "description": get_random_string(),
            }),
            201,
            schema=LIST_OF_ISSUES_SCHEMA
        )
        issue = issues[0]
        assert Issue.objects.filter(identifier=issue['service_request_id']).exists()
        assert Jurisdiction.objects.filter(identifier="default").exists()  # default Jurisdiction was created
        assert Jurisdiction.objects.count() == 1

        issues = get_data_from_response(
            mf_api_client.get(
                reverse('georeport/v2:issue-detail', kwargs={'identifier': issue['service_request_id']}),
            ), schema=LIST_OF_ISSUES_SCHEMA
        )
        verify_issue(issues[0])
Ejemplo n.º 22
0
def test_post_issue_no_jurisdiction(mf_api_client, random_service):
    assert not Jurisdiction.objects.exists()
    for attempt in [1, 2]:
        issues = get_data_from_response(mf_api_client.post(
            ISSUE_LIST_ENDPOINT, {
                "service_code": random_service.service_code,
                "lat": 30,
                "long": 30,
                "description": get_random_string(12),
            }),
                                        201,
                                        schema=LIST_OF_ISSUES_SCHEMA)
        issue = issues[0]
        assert Issue.objects.filter(
            identifier=issue['service_request_id']).exists()
        assert Jurisdiction.objects.filter(
            identifier="default").exists()  # default Jurisdiction was created
        assert Jurisdiction.objects.count() == 1

        issues = get_data_from_response(mf_api_client.get(
            reverse('georeport/v2:issue-detail',
                    kwargs={'identifier': issue['service_request_id']}), ),
                                        schema=LIST_OF_ISSUES_SCHEMA)
        verify_issue(issues[0])
Ejemplo n.º 23
0
def test_get(testing_issues, mf_api_client, extensions):
    content = get_data_from_response(
        mf_api_client.get(
            ISSUE_LIST_ENDPOINT,
            {
                'format': format,
                'service_request_id': '1982hglaqe8pdnpophff',
                'extensions': ('true' if extensions else 'false'),
            }
        ),
        schema=LIST_OF_ISSUES_SCHEMA
    )
    assert verify_issue(content[0])
    if extensions:
        assert 'extended_attributes' in content[0]
    else:
        assert 'extended_attributes' not in content[0]
Ejemplo n.º 24
0
def test_get_with_bbox(testing_issues, mf_api_client, flip_lat, flip_long, sep):
    longs = (24.768, 24.77)
    lats = (60.191, 60.194)
    if flip_lat:
        lats = lats[::-1]
    if flip_long:
        longs = longs[::-1]

    bbox_string = sep.join(str(c) for c in (longs[0], lats[0], longs[1], lats[1]))

    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'bbox': bbox_string}),
        schema=LIST_OF_ISSUES_SCHEMA
    )

    assert len(content) == 1
    assert content[0]['service_request_id'] == '9374kdfksdfhsdfasdf'

    for issue in content:
        assert verify_issue(issue)
Ejemplo n.º 25
0
def test_get_within_radius(testing_issues, mf_api_client):
    lat = 60.187394
    long = 24.940773
    radius = 1000
    expected_number_of_requests = 3

    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'lat': lat, 'long': long, 'radius': radius}),
        schema=(LIST_OF_ISSUES_SCHEMA if GISSY else None),
        status_code=(500 if not GISSY else 200)
    )

    if not GISSY:
        return

    assert len(content) == expected_number_of_requests

    for issue in content:
        assert verify_issue(issue)
        assert float(issue['distance']) < 1000
Ejemplo n.º 26
0
def test_get_with_bbox(testing_issues, mf_api_client, flip_lat, flip_long,
                       sep):
    longs = (24.768, 24.77)
    lats = (60.191, 60.194)
    if flip_lat:
        lats = lats[::-1]
    if flip_long:
        longs = longs[::-1]

    bbox_string = sep.join(
        str(c) for c in (longs[0], lats[0], longs[1], lats[1]))

    content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT,
                                                       {'bbox': bbox_string}),
                                     schema=LIST_OF_ISSUES_SCHEMA)

    assert len(content) == 1
    assert content[0]['service_request_id'] == '9374kdfksdfhsdfasdf'

    for issue in content:
        assert verify_issue(issue)
Ejemplo n.º 27
0
def test_get_within_radius(testing_issues, mf_api_client):
    lat = 60.187394
    long = 24.940773
    radius = 1000
    expected_number_of_requests = 3

    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {
            'lat': lat,
            'long': long,
            'radius': radius
        }),
        schema=(LIST_OF_ISSUES_SCHEMA if GISSY else None),
        status_code=(500 if not GISSY else 200))

    if not GISSY:
        return

    assert len(content) == expected_number_of_requests

    for issue in content:
        assert verify_issue(issue)
        assert float(issue['distance']) < 1000
Ejemplo n.º 28
0
def test_post_issue_api_key(mf_api_client, random_service, api_key_mode, pass_api_key):
    expected_app = Application.autodetermine()
    expected_status = 201
    if api_key_mode == 'actual-apps':
        for x in range(5):
            expected_app = Application.objects.create(identifier='app%d' % (x + 1))
        if not pass_api_key:
            expected_status = 400

    input_data = dict(
        description=get_random_string(),
        service_code=random_service.service_code,
        address='hello',
        api_key=(expected_app.key if pass_api_key else ''),
    )
    issues = get_data_from_response(
        mf_api_client.post(ISSUE_LIST_ENDPOINT, input_data),
        status_code=expected_status,
        schema=LIST_OF_ISSUES_SCHEMA,
    )
    if expected_status >= 400:
        return  # Nothing more to do here
    issue = verify_issue(issues[0])
    assert issue.application == expected_app
Ejemplo n.º 29
0
def test_get_by_service_request_id(testing_issues, mf_api_client):
    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT, {'service_request_id': '1982hglaqe8pdnpophff'}),
                                     schema=LIST_OF_ISSUES_SCHEMA)
    assert len(content) == 1
    verify_issue(content[0])