def test_ire_delayed_post_twice(mock_rabbitmq): """ double delayed stops post """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' res = api_post('/ire', data=ire_96231) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_ire_two_trip_removal_post_twice(mock_rabbitmq): """ post twice ire trip removal on two trips """ ire_JOHN_trip_removal = get_ire_data('train_JOHN_trip_removal.xml') res = api_post('/ire', data=ire_JOHN_trip_removal) assert res == 'OK' res = api_post('/ire', data=ire_JOHN_trip_removal) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_JOHN_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_wrong_ire_post(): """ simple xml post on the api """ res, status = api_post('/ire', check=False, data='<bob></bob>') assert status == 400 print res.get('error') == 'invalid'
def test_ire_trip_delayed_then_removal(mock_rabbitmq): """ post delayed stops then trip removal on the same trip """ ire_96231_delayed = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231_delayed) assert res == 'OK' ire_96231_trip_removal = get_ire_data('train_96231_trip_removal.xml') res = api_post('/ire', data=ire_96231_trip_removal) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_96231_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_add_user_invalid_type(): """ creation of a user with an invalid endpoint """ user = {'login': '******', 'email': '*****@*****.**', 'type': 'foo'} resp, status = api_post('/v0/users/', check=False, data=json.dumps(user), content_type='application/json') assert status == 400
def test_post_pbf_autocomplete_without_files(create_instance_fr): with app.app_context(): filename = 'empty_pbf.osm.pbf' path = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tests/fixtures/', filename) with open(path, 'rb'): response, status = api_post( '/v0/autocomplete_parameters/fr/update_data', check=False) assert status == 400
def test_create_traveler_profile(create_instance, traveler_profile_params): resp = api_post( '/v0/instances/fr/traveler_profiles/standard', data=json.dumps(traveler_profile_params), content_type='application/json', ) check_traveler_profile(resp, traveler_profile_params) resp = api_get('/v0/instances/fr/traveler_profiles/standard') check_traveler_profile(resp[0], traveler_profile_params)
def test_cots_trip_delayed_then_removal(mock_rabbitmq): """ post delayed stops then trip removal on the same trip """ cots_96231_delayed = get_fixture_data('cots_train_96231_delayed.json') res = api_post('/cots', data=cots_96231_delayed) assert res == 'OK' cots_96231_trip_removal = get_fixture_data( 'cots_train_96231_trip_removal.json') res = api_post('/cots', data=cots_96231_trip_removal) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_96231_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_post_and_get_poi_type_json(create_instance, poi_types_json): resp = api_post('/v0/instances/fr/poi_types', data=json.dumps(poi_types_json), content_type='application/json') assert resp == poi_types_json wrong_poi_types_json = {"poi_types": []} resp, status_code = api_post( '/v0/instances/fr/poi_types', data=json.dumps(wrong_poi_types_json), content_type='application/json', check=False, ) assert status_code == 400 # check special characters correctly handled with app.app_context(): ptp = models.PoiTypeJson.query.all() assert u'Hôtel de ville' in ptp[0].poi_types_json
def test_post_zip_file_on_job_with_wrong_extension(create_instance_fr, enable_mimir2): with app.app_context(): filename = 'empty_file_without_extension' path = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tests/fixtures/', filename ) with open(path, 'rb') as f: files = {'file': (f, filename)} resp, status = api_post('/v0/jobs/fr', data=files, check=False) assert status == 400
def test_post_zip_file_on_job_should_succeed(create_instance_fr, enable_mimir2): with app.app_context(): filename = 'fusio.zip' path = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tests/fixtures/', filename ) with open(path, 'rb') as f: files = {'file': (f, filename)} resp, status = api_post('/v0/jobs/fr', data=files, check=False) assert status == 200
def post_and_check(url, expected_status, expected_message, expected_error): resp, status = api_post(url, check=False) assert status == expected_status assert expected_message in resp.get("message") if expected_error: assert expected_error == resp.get("error") with app.app_context(): assert len(RealTimeUpdate.query.all()) == 0 assert len(TripUpdate.query.all()) == 0 assert len(StopTimeUpdate.query.all()) == 0
def test_ire_delayed_and_trip_removal_post(mock_rabbitmq): """ post delayed stops on one trip than trip removal on another """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' ire_6113 = get_ire_data('train_6113_trip_removal.xml') res = api_post('/ire', data=ire_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() check_db_ire_6113_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_cots_delayed_and_trip_removal_post(mock_rabbitmq): """ post delayed stops on one trip then trip removal on another """ cots_96231 = get_fixture_data('cots_train_96231_delayed.json') res = api_post('/cots', data=cots_96231) assert res == 'OK' cots_6113 = get_fixture_data('cots_train_6113_trip_removal.json') res = api_post('/cots', data=cots_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 6 check_db_96231_delayed(contributor='realtime.cots') check_db_6113_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_add_user_invalid_email(mock_rabbit): """ creation of a user with an invalid email """ user = {'login': '******', 'email': 'user1'} resp, status = api_post('/v0/users/', check=False, data=json.dumps(user), content_type='application/json') assert status == 400 assert mock_rabbit.call_count == 0
def test_post_autocomplete(autocomplete_parameter_json): resp = api_post('/v0/autocomplete_parameters', data=json.dumps(autocomplete_parameter_json), content_type='application/json') assert resp['name'] == 'peru' assert resp['street'] == 'OSM' assert resp['address'] == 'BANO' assert resp['poi'] == 'FUSIO' assert resp['admin'] == 'OSM' assert resp['admin_level'] == [8]
def test_add_user_with_plus_in_query(mock_rabbit): """ creation of a user with a "+" in the email """ user = { 'email': '*****@*****.**', 'login': '******' } _, status = api_post( '/v0/users/?login={email}&email={email}'.format(email=user['email']), check=False) assert status == 400 resp = api_post('/v0/users/?login={email}&email={email}'.format( email=quote(user['email']))) _check_user_resp(user, resp) resp = api_get('/v0/users/') assert len(resp) == 1 _check_user_resp(user, resp[0]) assert mock_rabbit.called
def test_piv_purge(mock_rabbitmq): """ Simple PIV post, then test the purge """ piv_feed = "{}" # TODO: use a valid PIV feed res = api_post("/piv/{}".format(PIV_CONTRIBUTOR_ID), data=piv_feed) assert "PIV feed processed" in res.get("message") with app.app_context(): # Check there's really something before purge assert len(RealTimeUpdate.query.all()) == 1 # Put an old (realistic) date to RealTimeUpdate object so that RTU purge affects it rtu = RealTimeUpdate.query.all()[0] rtu.created_at = datetime.datetime(2012, 6, 15, 15, 33) assert len(TripUpdate.query.all() ) == 0 # TODO xfail: =1 with a valid feed and processing assert len(VehicleJourney.query.all()) == 0 # TODO xfail: =1 assert len(StopTimeUpdate.query.all()) == 0 # TODO xfail: =some assert (db.session.execute( "select * from associate_realtimeupdate_tripupdate").rowcount == 0 ) # TODO xfail: =1 # TODO VehicleJourney affected is old, so it's affected by TripUpdate purge (based on base-VJ's date) config = { "contributor": PIV_CONTRIBUTOR_ID, "nb_days_to_keep": int(app.config.get("NB_DAYS_TO_KEEP_TRIP_UPDATE")), } purge_trip_update(config) assert len(TripUpdate.query.all()) == 0 assert len(VehicleJourney.query.all()) == 0 assert len(StopTimeUpdate.query.all()) == 0 assert db.session.execute( "select * from associate_realtimeupdate_tripupdate").rowcount == 0 assert len(RealTimeUpdate.query.all() ) == 1 # keeping RTU longer for potential debug need config = { "nb_days_to_keep": app.config.get(str("NB_DAYS_TO_KEEP_RT_UPDATE")), "connector": ConnectorType.piv.value, } purge_rt_update(config) assert len(TripUpdate.query.all()) == 0 assert len(VehicleJourney.query.all()) == 0 assert len(StopTimeUpdate.query.all()) == 0 assert db.session.execute( "select * from associate_realtimeupdate_tripupdate").rowcount == 0 assert len(RealTimeUpdate.query.all()) == 0
def test_post_autocomplete_cosmo(): resp = api_post( '/v0/autocomplete_parameters', data=json.dumps({"name": "bobette", "admin": "COSMOGONY"}), content_type='application/json', ) assert resp['name'] == 'bobette' assert resp['street'] == 'OSM' assert resp['address'] == 'BANO' assert resp['poi'] == 'OSM' assert resp['admin'] == 'COSMOGONY' assert resp['admin_level'] == []
def test_ire_delayed_simple_post(mock_rabbitmq): """ simple delayed stops post """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() assert mock_rabbitmq.call_count == 1
def test_add_user_with_plus_in_query(mock_rabbit): """ creation of a user with a "+" in the email """ user = {'email': '*****@*****.**', 'login': '******'} _, status = api_post('/v0/users/?login={email}&email={email}'.format(email=user['email']), check=False) assert status == 400 resp = api_post('/v0/users/?login={email}&email={email}'.format(email=urllib.quote(user['email']))) def check(u): for k in user.iterkeys(): assert u[k] == user[k] assert u['end_point']['name'] == 'navitia.io' assert u['type'] == 'with_free_instances' assert u['block_until'] is None check(resp) resp = api_get('/v0/users/') assert len(resp) == 1 check(resp[0]) assert mock_rabbit.called
def test_save_bad_raw_ire(): """ send a bad formatted ire, the bad raw ire should be saved in db """ bad_ire = get_ire_data('bad_ire.xml') res = api_post('/ire', data=bad_ire, check=False) assert res[1] == 400 assert res[0]['message'] == 'Invalid arguments' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert RealTimeUpdate.query.first().status == 'KO' assert RealTimeUpdate.query.first().error == \ 'invalid xml, impossible to find "Train" in xml elt InfoRetard' assert RealTimeUpdate.query.first().raw_data == bad_ire
def test_cots_trip_removal_simple_post(mock_rabbitmq): """ simple trip removal post """ cots_6113 = get_fixture_data('cots_train_6113_trip_removal.json') res = api_post('/cots', data=cots_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_save_bad_raw_cots(): """ send a bad formatted COTS, the bad raw COTS should be saved in db """ bad_cots = get_fixture_data('bad_cots.json') res = api_post('/cots', data=bad_cots, check=False) assert res[1] == 400 assert res[0]['message'] == 'Invalid arguments' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert RealTimeUpdate.query.first().status == 'KO' assert RealTimeUpdate.query.first().error == \ 'invalid json, impossible to find "numeroCourse" in json dict {"bad":"one","cots":"toto"}' assert RealTimeUpdate.query.first().raw_data == bad_cots
def test_ire_trip_removal_simple_post(mock_rabbitmq): """ simple trip removal post """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') res = api_post('/ire', data=ire_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_full_registration_then_deletion(create_instance, mock_rabbit): """ we create a user, then a token for him, and finaly we give to him some authorization after that we delete him """ user = {'login': '******', 'email': '*****@*****.**'} resp_user = api_post('/v0/users/', data=json.dumps(user), content_type='application/json') api_post( '/v0/users/{}/keys'.format(resp_user['id']), data=json.dumps({'app_name': 'myApp'}), content_type='application/json', ) auth = {'instance_id': create_instance, 'api_id': 1} api_post( '/v0/users/{}/authorizations'.format(resp_user['id']), data=json.dumps(auth), content_type='application/json', ) resp = api_get('/v0/users/{}'.format(resp_user['id'])) assert len(resp['keys']) == 1 assert resp['keys'][0]['app_name'] == 'myApp' assert len(resp['authorizations']) == 1 assert resp['authorizations'][0]['instance']['name'] == 'instance' _, status = api_delete('/v0/users/{}'.format(resp_user['id']), check=False, no_json=True) assert status == 204 assert mock_rabbit.called _, status = api_get('/v0/users/{}'.format(resp_user['id']), check=False) assert status == 404
def test_deletion_keys_and_auth(create_instance, mock_rabbit): """ We start by creating the user, it's easier than using a fixture, then we delete the auth and the key """ # first, test that with an unknown user, we get a 404 _, status = api_delete('/v0/users/75/keys/1', check=False, no_json=True) assert status == 404 user = {'login': '******', 'email': '*****@*****.**'} resp_user = api_post('/v0/users/', data=json.dumps(user), content_type='application/json') api_post( '/v0/users/{}/keys'.format(resp_user['id']), data=json.dumps({'app_name': 'myApp'}), content_type='application/json', ) auth = {'instance_id': create_instance, 'api_id': 1} api_post( '/v0/users/{}/authorizations'.format(resp_user['id']), data=json.dumps(auth), content_type='application/json', ) resp = api_get('/v0/users/{}'.format(resp_user['id'])) assert len(resp['keys']) == 1 # key created but not modified yet assert resp['keys'][0]['created_at'] is not None assert resp['keys'][0]['updated_at'] is None resp_key = api_delete('/v0/users/{user_id}/keys/{key_id}'.format( user_id=resp['id'], key_id=resp['keys'][0]['id'])) assert len(resp_key['keys']) == 0 resp_auth = api_delete('/v0/users/{}/authorizations/'.format(resp['id']), data=json.dumps(auth), content_type='application/json') assert len(resp_auth['authorizations']) == 0 assert mock_rabbit.called
def test_add_user_with_plus_no_json(mock_rabbit): """ creation of a user with a "+" in the email """ user = { 'login': '******', 'email': '*****@*****.**' } resp = api_post('/v0/users/', data=user) _check_user_resp(user, resp) resp = api_get('/v0/users/') assert len(resp) == 1 _check_user_resp(user, resp[0]) assert mock_rabbit.called
def test_cots_added_stop_time_earlier_than_previous(): """ A new stop time is added in the VJ 96231 whose arrival/departure is earlier than the previous one. This cots should be rejected """ cots_add_file = get_fixture_data( 'cots_train_96231_add_stop_time_earlier_than_previous.json') res, status = api_post('/cots', data=cots_add_file, check=False) assert status == 400 assert res.get('message') == 'Invalid arguments' with app.app_context(): assert RealTimeUpdate.query.first().error == \ 'invalid cots: stop_point\'s(0087-713065-BV) time is not consistent'
def test_deletion_keys_and_auth(create_instance): """ We start by creating the user, it's easier than using a fixture, then we delete the auth and the key """ user = {'login': '******', 'email': '*****@*****.**'} resp_user = api_post('/v0/users/', data=json.dumps(user), content_type='application/json') api_post('/v0/users/{}/keys'.format(resp_user['id']), data=json.dumps({'app_name': 'myApp'}), content_type='application/json') auth = {'instance_id': create_instance, 'api_id': 1} api_post('/v0/users/{}/authorizations'.format(resp_user['id']), data=json.dumps(auth), content_type='application/json') resp = api_get('/v0/users/{}'.format(resp_user['id'])) resp_key = api_delete('/v0/users/{user_id}/keys/{key_id}'.format( user_id=resp['id'], key_id=resp['keys'][0]['id'])) assert len(resp_key['keys']) == 0 resp_auth = api_delete('/v0/users/{}/authorizations/'.format(resp['id']), data=json.dumps(auth), content_type='application/json') assert len(resp_auth['authorizations']) == 0
def test_add_user_invalid_billingplan(mock_rabbit): """ creation of a user with an invalid endpoint """ user = { 'login': '******', 'email': '*****@*****.**', 'billing_plan_id': 100 } resp, status = api_post('/v0/users/', check=False, data=json.dumps(user), content_type='application/json') assert status == 400 assert mock_rabbit.call_count == 0
def test_add_user_with_plus_in_query(mock_rabbit): """ creation of a user with a "+" in the email """ user = { 'email': '*****@*****.**', 'login': '******' } resp, status = api_post( '/v0/users/?login={email}&email={email}'.format(email=user['email']), check=False) assert status == 400 assert resp["error"] == 'email invalid, you give "user1 test******"' resp = api_post('/v0/users/?login={email}&email={email}'.format( email=quote(user['email']))) _check_user_resp(user, resp) assert_default_scop_shape(resp) resp = api_get('/v0/users/') assert len(resp) == 1 _check_user_resp(user, resp[0]) assert mock_rabbit.called assert_default_scop_shape(resp[0])
def test_ire_trip_removal_parity(mock_rabbitmq): """ simple parity trip removal post """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') ire_6113_14 = ire_6113.replace('<NumeroTrain>006113</NumeroTrain>', '<NumeroTrain>006113/14</NumeroTrain>') res = api_post('/ire', data=ire_6113_14) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_ire_two_trip_removal_one_post(mock_rabbitmq): """ post one ire trip removal on two trips (navitia mock returns 2 vj for 'JOHN' headsign) """ ire_JOHN_trip_removal = get_ire_data('train_JOHN_trip_removal.xml') res = api_post('/ire', data=ire_JOHN_trip_removal) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_JOHN_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 1
def test_ire_trip_with_parity_one_unknown_vj(mock_rabbitmq): """ a trip with a parity has been impacted, but the train 6112 is not known by navitia there should be only the train 6113 impacted """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') ire_6112_13 = ire_6113.replace('<NumeroTrain>006113</NumeroTrain>', '<NumeroTrain>006112/3</NumeroTrain>') res = api_post('/ire', data=ire_6112_13) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_ire_trip_with_parity(mock_rabbitmq): """ a trip with a parity has been impacted, there should be 2 VJ impacted """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') ire_6113_14 = ire_6113.replace('<NumeroTrain>006113</NumeroTrain>', '<NumeroTrain>006113/4</NumeroTrain>') res = api_post('/ire', data=ire_6113_14) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 # there should be 2 trip updated, # - trip:OCETGV-87686006-87751008-2:25768-2 for the headsign 6114 # - trip:OCETGV-87686006-87751008-2:25768 for the headsign 6113 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() check_db_ire_6114_trip_removal() assert mock_rabbitmq.call_count == 1