def cleanup(): print("deleting test layers") filepath = "data/user/" + getUserId("testuser") + "/test_layer.json" if os.path.exists(filepath): os.remove(filepath) filepath = "data/user/" + getUserId("testuser") + "/test_layer2.json" if os.path.exists(filepath): os.remove(filepath) filepath = "data/user/" + getUserId("testuser") + "/hashes.json" if os.path.exists(filepath): os.remove(filepath) filepath = "data/user/" + getUserId("testuser") if os.path.exists(filepath): shutil.rmtree(filepath) print("deleting test user", "testuser") deleteUser(getUserId("testuser"))
def test_get_entire_layer(): data = { "userid": getUserId("testuser"), "layer": "test_layer" } response = requests.get(root_url + "getLayer", json=data) assert (response.status_code == 200)
def test_update_data_layer_all(): query = "test_layer/" data = { "userid": getUserId("testuser"), "data": {"data": [{"state": 0}, {"state": 2}, {"state": 4}]} } response = requests.post(root_url + "addLayerData/" + query, json=data) assert (response.status_code == 200)
def test_layerchange_update_geojson_feature_invalid(): query = "test_layer/features/0/" data = { "userid": getUserId("testuser"), "data": get_geojson_test_feature(to_have_id=1, to_be_valid=False) } response = requests.post(root_url + "addLayerData/" + query, json=data) assert (response.status_code == 400)
def test_layerchange_nonexistant(): query = "nonexistant_layer/features/0/id" data = { "userid": getUserId("testuser"), "data": -1 } response = requests.post(root_url + "addLayerData/" + query, json=data) assert (response.status_code == 400)
def test_update_data_layer_single_prop(): import random query = "test_layer2/data/1/state" data = { "userid": getUserId("testuser"), "data": {"somedata": random.randint(0, 1000)} } response = requests.post(root_url + "addLayerData/" + query, json=data) assert (response.status_code == 200)
def test_get_layerdata_single_prop(): query = "features/0/id" data = { "userid": getUserId("testuser"), "layer": "test_layer" } response = requests.post(root_url + "getLayer/" + query, json=data) assert (response.status_code == 200)
def test_layerchange_add_new_geojson_layer_invalid(): query = "test_layer_invalid" data = { "userid": getUserId("testuser"), "data": { "features": [ get_geojson_test_feature(to_have_id=0, to_be_valid=False) ] } } response = requests.post(root_url + "addLayerData/" + query, json=data) assert (response.status_code == 400)
def test_abm_request(): # make user not restricted user_db_file = "data/users.json" userid = getUserId("testuser") with open(user_db_file, 'r') as jsonread: jsondata = json.load(jsonread) jsondata[userid]["restricted"] = False with open(user_db_file, 'w') as jsonwrite: json.dump(jsondata, jsonwrite) add_abm_test_data(userid) query = "abmScenario" data_without_time_filter = { "userid": userid, "scenario_properties": { "bridge_1": True, "amenities_roof": "random", "blocks": "open", "bridge_2": False, "main_street_orientation": "vertical" }, "agent_filters": { "mode": "foot", "student_or_adult": "adult", "resident_or_visitor": "resident" } } data_with_time_filter = data_without_time_filter.copy() data_with_time_filter["time_filters"] = { "start_time": 10000.0, "end_time": 30000.0 } response_with_time_filter = requests.post( root_url + "getLayer/" + query, json=data_with_time_filter) response_without_time_filter = requests.post( root_url + "getLayer/" + query, json=data_without_time_filter) assert (response_with_time_filter.status_code == 200) assert (response_without_time_filter.status_code == 200) assert (len(response_without_time_filter.json()["data"]) > len( response_with_time_filter.json()["data"]))
def login(): params = parseReq(request) username = params.get('username') if username == "": abort(400) try: userid = str(getUserId(username)) except ValueError: userid = str(makeUser(username)) if len(params) > 1: # we received some new userdata, store it in DB setUserData(userid, params) try: userdata = getUserData(username) except: abort(401) response = {"id": userid, "userdata": userdata} return jsonify(response)
def login(): params = parseReq(request) if not params: abort(400) username = params.get('username') password = params.get('password') log_this_request = params.get('log_this_request') if log_this_request: log_login_request(username) if username == "": abort(400) try: userid = str(getUserId(username)) if not checkPass(userid, password): abort(401) restricted = isUserRestricted(userid) context = getUserContext(userid) except ValueError: abort(401) return {"user_id": userid, "restricted": restricted, "context": context}