def test_get_by_service_object_id_without_type(testing_issues, mf_api_client): get_data_from_response(mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'citysdk', 'service_object_id': '10844', }), status_code=400)
def test_post_issue_multi_jurisdiction(mf_api_client, random_service): assert not Jurisdiction.objects.exists() # Precondition check Jurisdiction.objects.create(identifier="j1", name="j1") Jurisdiction.objects.create(identifier="j2", name="j2") # Can't post without a Jurisdiction when there are multiple 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(), }), 400 ) for j in Jurisdiction.objects.all(): # Can't post without a Jurisdiction when there are multiple issues = get_data_from_response( mf_api_client.post(ISSUE_LIST_ENDPOINT, { "jurisdiction_id": j.identifier, "service_code": random_service.service_code, "lat": 30, "long": 30, "description": get_random_string(), }), 201, schema=LIST_OF_ISSUES_SCHEMA ) assert Issue.objects.get(identifier=issues[0]["service_request_id"]).jurisdiction == j
def test_post_issue_multi_jurisdiction(mf_api_client, random_service): assert not Jurisdiction.objects.exists() # Precondition check Jurisdiction.objects.create(identifier="j1", name="j1") Jurisdiction.objects.create(identifier="j2", name="j2") # Can't post without a Jurisdiction when there are multiple 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), }), 400) for j in Jurisdiction.objects.all(): # Can't post without a Jurisdiction when there are multiple issues = get_data_from_response(mf_api_client.post( ISSUE_LIST_ENDPOINT, { "jurisdiction_id": j.identifier, "service_code": random_service.service_code, "lat": 30, "long": 30, "description": get_random_string(12), }), 201, schema=LIST_OF_ISSUES_SCHEMA) assert Issue.objects.get( identifier=issues[0]["service_request_id"]).jurisdiction == j
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']
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
def test_get_with_tasks(random_service, mf_api_client): if mf_api_client.format != "json": pytest.xfail("tasks are misrendered using the spark convention") # TODO: Fix that issue = Issue.objects.create( service=random_service, description=get_random_string(), address='Test Street 10', ) for x, time in enumerate(( now() - timedelta(days=5), now() - timedelta(days=2), ), 1): id = 'foo%04d' % x issue.tasks.create( task_state=id, task_type=id, owner_name=id, task_created=time, ) content = get_data_from_response( mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'hel', } ), schema=LIST_OF_ISSUES_SCHEMA ) tasks = content[0]['extended_attributes']['tasks'] assert tasks
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
def test_handler_query(random_service, mf_api_client): handlerless_issue = Issue.objects.create( service=random_service, description=get_random_string(12), address='Test Street 10', ) for x in range(3): handlerful_issue = Issue.objects.create( service=random_service, description=get_random_string(12), address='Test Street 10', ) handlerful_issue.log_entries.create( status='allocated', handler='*****@*****.**', ) content = get_data_from_response( mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'log', 'handler': '*****@*****.**', } ), schema=LIST_OF_ISSUES_SCHEMA ) assert len(content) == 3 assert all(i['extended_attributes']['handler'] == '*****@*****.**' for i in content)
def test_handler_query(random_service, mf_api_client): handlerless_issue = Issue.objects.create( service=random_service, description=get_random_string(), address='Test Street 10', ) for x in range(3): handlerful_issue = Issue.objects.create( service=random_service, description=get_random_string(), address='Test Street 10', ) handlerful_issue.log_entries.create( status='allocated', handler='*****@*****.**', ) content = get_data_from_response( mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'log', 'handler': '*****@*****.**', } ), schema=LIST_OF_ISSUES_SCHEMA ) assert len(content) == 3 assert all(i['extended_attributes']['handler'] == '*****@*****.**' for i in content)
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])
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']
def test_get_by_service_object(testing_issues, mf_api_client): issue = testing_issues.first() service_object_id = '10844' service_object_type = 'http://www.hel.fi/servicemap/v2' Issue_CitySDK.objects.create( issue=issue, service_object_id=service_object_id, service_object_type=service_object_type, ) content = get_data_from_response( mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'citysdk', 'service_object_id': service_object_id, 'service_object_type': service_object_type, } ), schema=LIST_OF_ISSUES_SCHEMA ) for issue in content: assert issue['extended_attributes']['service_object_id'] == service_object_id assert issue['extended_attributes']['service_object_type'] == service_object_type
def test_get_with_tasks(random_service, mf_api_client): if mf_api_client.format != "json": pytest.xfail("tasks are misrendered using the spark convention" ) # TODO: Fix that issue = Issue.objects.create( service=random_service, description=get_random_string(), address='Test Street 10', ) for x, time in enumerate(( now() - timedelta(days=5), now() - timedelta(days=2), ), 1): id = 'foo%04d' % x issue.tasks.create( task_state=id, task_type=id, owner_name=id, task_created=time, ) content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT, { 'extensions': 'hel', }), schema=LIST_OF_ISSUES_SCHEMA) tasks = content[0]['extended_attributes']['tasks'] assert tasks
def test_pagination(mf_api_client, random_service, param, count): for i in range(30): Issue.objects.create(service=random_service, description=i) resp = mf_api_client.get(ISSUE_LIST_ENDPOINT, {param: count}) assert resp['x-result-count'] == str(30) assert resp['x-page-count'] == str(int(ceil(30 / count))) content = get_data_from_response(resp, schema=LIST_OF_ISSUES_SCHEMA) assert len(content) == count
def test_pagination(mf_api_client, random_service, param, count): for i in range(30): Issue.objects.create(service=random_service, description=i) resp = mf_api_client.get(ISSUE_LIST_ENDPOINT, {param: count}) assert resp['x-result-count'] == str(30) assert resp['x-page-count'] == str(int(ceil(30 / count))) content = get_data_from_response(resp, schema=LIST_OF_ISSUES_SCHEMA) assert len(content) == count
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'])
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)
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
def test_by_description(testing_issues, mf_api_client): search = 'some' content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT, { 'extensions': 'citysdk', 'search': search }), schema=LIST_OF_ISSUES_SCHEMA) assert search.lower() in content[0]['description'].lower()
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
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'])
def test_service_list_i18n(mf_api_client, random_service): assert isinstance(random_service, Service) for lang in TEST_LOCALES: random_service.set_current_language(lang) random_service.service_name = f"Test-{lang}" random_service.save_translations() for lang in TEST_LOCALES: data = get_data_from_response( mf_api_client.get(SERVICE_LIST_ENDPOINT, {"locale": lang})) assert len(data) == 1 assert data[0]['service_name'].endswith(lang) # As we set above
def test_post_invalid_json(random_service, mf_api_client): response = get_data_from_response(mf_api_client.post( ISSUE_LIST_ENDPOINT, { 'extensions': 'geometry', 'service_code': random_service.service_code, 'description': 'Miten tätä ajetaan?', 'geometry': json.dumps(['oops']), }), status_code=400) assert 'JSON' in str( response) # Yeah, it complains about JSON, that's fine
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
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
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
def test_post_media(mf_api_client, random_service): if sys.version_info[0] == 2 and mf_api_client.format in ('xml', 'sjson'): pytest.xfail('this test is somehow broken on Py2') files = [ ContentFile(content=VERY_SMALL_JPEG, name="x%d.jpg" % x) for x in range(3) ] issues = get_data_from_response( mf_api_client.post( f'{ISSUE_LIST_ENDPOINT}?extensions=media', data={ 'service_code': random_service.service_code, 'lat': 30, 'long': 30, 'description': get_random_string(12), 'media': files, } ), status_code=201, schema=LIST_OF_ISSUES_SCHEMA, ) id = issues[0]['service_request_id'] assert Issue.objects.filter(identifier=id).exists() assert IssueMedia.objects.filter(issue__identifier=id).count() == 3 # Now see that we can actually get the media back issues = get_data_from_response( mf_api_client.get( reverse('georeport/v2:issue-detail', kwargs={"identifier": id}), { 'extensions': 'media', } ), schema=LIST_OF_ISSUES_SCHEMA ) issue = issues[0] media_list = issue['extended_attributes']['media_urls'] assert issue assert len(media_list) == 3
def test_post_media(mf_api_client, random_service): if sys.version_info[0] == 2 and mf_api_client.format in ('xml', 'sjson'): pytest.xfail('this test is somehow broken on Py2') files = [ ContentFile(content=VERY_SMALL_JPEG, name="x%d.jpg" % x) for x in range(3) ] issues = get_data_from_response( mf_api_client.post( '%s?extensions=media' % ISSUE_LIST_ENDPOINT, data={ 'service_code': random_service.service_code, 'lat': 30, 'long': 30, 'description': get_random_string(), 'media': files, } ), status_code=201, schema=LIST_OF_ISSUES_SCHEMA, ) id = issues[0]['service_request_id'] assert Issue.objects.filter(identifier=id).exists() assert IssueMedia.objects.filter(issue__identifier=id).count() == 3 # Now see that we can actually get the media back issues = get_data_from_response( mf_api_client.get( reverse('georeport/v2:issue-detail', kwargs={"identifier": id}), { 'extensions': 'media', } ), schema=LIST_OF_ISSUES_SCHEMA ) issue = issues[0] media_list = issue['extended_attributes']['media_urls'] assert issue assert len(media_list) == 3
def test_post_invalid_geojson(random_service, mf_api_client): response = get_data_from_response(mf_api_client.post( ISSUE_LIST_ENDPOINT, { 'extensions': 'geometry', 'service_code': random_service.service_code, 'description': 'Miten tätä ajetaan?', 'geometry': json.dumps({'tepe': 'palygon'}), }, ), status_code=400) assert 'Invalid GeoJSON' in str( response) # Complains about GeoJSON, that's nice
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)
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
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
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]
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
def test_post_invalid_geojson(random_service, mf_api_client): response = get_data_from_response( mf_api_client.post( ISSUE_LIST_ENDPOINT, { 'extensions': 'geometry', 'service_code': random_service.service_code, 'description': 'Miten tätä ajetaan?', 'geometry': json.dumps({'tepe': 'palygon'}), }, ), status_code=400 ) assert 'Invalid GeoJSON' in str(response) # Complains about GeoJSON, that's nice
def test_post_invalid_json(random_service, mf_api_client): response = get_data_from_response( mf_api_client.post( ISSUE_LIST_ENDPOINT, { 'extensions': 'geometry', 'service_code': random_service.service_code, 'description': 'Miten tätä ajetaan?', 'geometry': json.dumps(['oops']), } ), status_code=400 ) assert 'JSON' in str(response) # Yeah, it complains about JSON, that's fine
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')
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')
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])
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])
def test_get_with_log(random_service, mf_api_client): if mf_api_client.format != "json": pytest.xfail("logs are misrendered using the spark convention") # TODO: Fix that # Simulate an issue going through a loggy flow: creation = now() - timedelta(days=7) issue = Issue.objects.create( service=random_service, description=get_random_string(12), requested_datetime=creation, address='Test Street 10', ) issue.log_entries.create( time=creation + timedelta(days=2), status='allocated', handler='*****@*****.**', ) assert Issue_LogExtension.objects.get(issue=issue).last_handler == '*****@*****.**' issue.log_entries.create( time=creation + timedelta(days=5), status='handling', handler='*****@*****.**', note="i'll deal with this", ) assert Issue_LogExtension.objects.get(issue=issue).last_handler == '*****@*****.**' issue.log_entries.create( time=creation + timedelta(days=6), status='done', handler='', ) issue.updated_datetime = now() issue.status = 'closed' issue.save() content = get_data_from_response( mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'log', } ), schema=LIST_OF_ISSUES_SCHEMA ) assert len(content[0]['extended_attributes']['log']) == 3 assert content[0]['extended_attributes']['handler'] == '*****@*****.**' # last non-null handler
def test_service_list_jurisdiction_filter(mf_api_client): tku = Jurisdiction.objects.create(identifier='fi.turku') hel = Jurisdiction.objects.create(identifier='fi.hel') tku_service = Service.objects.create(service_code=get_random_string(), service_name='Aurajokipalvelu') hel_service = Service.objects.create(service_code=get_random_string(), service_name='Suomenlinnapalvelu') tku_service.jurisdictions.add(tku) hel_service.jurisdictions.add(hel) for query, expected in ( ({}, [tku_service, hel_service]), ({'jurisdiction_id': tku.identifier}, [tku_service]), ({'jurisdiction_id': hel.identifier}, [hel_service]), ): data = get_data_from_response( mf_api_client.get(SERVICE_LIST_ENDPOINT, query) ) assert len(data) == len(expected) assert set(s['service_name'] for s in data) == set(s.service_name for s in expected)
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]
def test_get_with_log(random_service, mf_api_client): if mf_api_client.format != "json": pytest.xfail("logs are misrendered using the spark convention") # TODO: Fix that # Simulate an issue going through a loggy flow: creation = now() - timedelta(days=7) issue = Issue.objects.create( service=random_service, description=get_random_string(), requested_datetime=creation, address='Test Street 10', ) issue.log_entries.create( time=creation + timedelta(days=2), status='allocated', handler='*****@*****.**', ) assert Issue_LogExtension.objects.get(issue=issue).last_handler == '*****@*****.**' issue.log_entries.create( time=creation + timedelta(days=5), status='handling', handler='*****@*****.**', note="i'll deal with this", ) assert Issue_LogExtension.objects.get(issue=issue).last_handler == '*****@*****.**' issue.log_entries.create( time=creation + timedelta(days=6), status='done', handler='', ) issue.updated_datetime = now() issue.status = 'closed' issue.save() content = get_data_from_response( mf_api_client.get( ISSUE_LIST_ENDPOINT, { 'extensions': 'log', } ), schema=LIST_OF_ISSUES_SCHEMA ) assert len(content[0]['extended_attributes']['log']) == 3 assert content[0]['extended_attributes']['handler'] == '*****@*****.**' # last non-null handler
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
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)
def test_get_issue_multi_jurisdiction_filters_correctly( mf_api_client, random_service): assert not Jurisdiction.objects.exists() # Precondition check jurisdictions = [ Jurisdiction.objects.create(identifier=f"j{x}", name=f"j{x}") for x in range(4) ] for j in jurisdictions: for x in range(5): Issue.objects.create( jurisdiction=j, service=random_service, description=get_random_string(12), address='Test Street 10', ) for j in jurisdictions: issues = get_data_from_response(mf_api_client.get( ISSUE_LIST_ENDPOINT, {'jurisdiction_id': j.identifier}), schema=LIST_OF_ISSUES_SCHEMA) # Only getting the Issues for the requested Jurisdiction: assert len(issues) == Issue.objects.filter(jurisdiction=j).count()
def test_get_issue_multi_jurisdiction_filters_correctly(mf_api_client, random_service): assert not Jurisdiction.objects.exists() # Precondition check jurisdictions = [ Jurisdiction.objects.create(identifier="j%s" % x, name="j%s" % x) for x in range(4) ] for j in jurisdictions: for x in range(5): Issue.objects.create( jurisdiction=j, service=random_service, description=get_random_string(), address='Test Street 10', ) for j in jurisdictions: issues = get_data_from_response( mf_api_client.get(ISSUE_LIST_ENDPOINT, {'jurisdiction_id': j.identifier}), schema=LIST_OF_ISSUES_SCHEMA ) # Only getting the Issues for the requested Jurisdiction: assert len(issues) == Issue.objects.filter(jurisdiction=j).count()
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)
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
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
def test_get_by_unexisting_request_id(testing_issues, mf_api_client): content = get_data_from_response( mf_api_client.get(ISSUE_LIST_ENDPOINT, {'service_request_id': 'unexisting_req_id'}), ) assert not content
def test_get_requests(testing_issues, mf_api_client): content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT), schema=LIST_OF_ISSUES_SCHEMA) assert len(content) == Issue.objects.count()
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])
def test_get_by_unexisting_request_id(testing_issues, mf_api_client): content = get_data_from_response( mf_api_client.get(ISSUE_LIST_ENDPOINT, {'service_request_id': 'unexisting_req_id'}), ) assert not content