Example #1
0
 def test_update_session_no_file(self, tmp_path):
     with app.test_request_context():
         sessions.config.UPLOAD_FOLDER = tmp_path
         sessions.config.TMP_FOLDER = tmp_path
         sessions.new_session()
         print(os.listdir(tmp_path))
         assert len(os.listdir(tmp_path)) == 0
Example #2
0
 def test_update_expired_session(self, tmp_path):
     with app.test_request_context():
         sessions.config.UPLOAD_FOLDER = tmp_path
         sessions.config.TMP_FOLDER = tmp_path
         sessions.new_session()
         session['timeout'] = time.time() - 1
         print(os.listdir(tmp_path))
         assert len(os.listdir(tmp_path)) == 0
Example #3
0
 def test_update_session(self, tmp_path):
     from pathlib import Path
     tmp = ['output', 'graph', 'model', 'counter_graph']
     with app.test_request_context():
         sessions.config.UPLOAD_FOLDER = tmp_path
         sessions.config.TMP_FOLDER = tmp_path
         sessions.new_session()
         for target in tmp:
             Path(session[target]).touch()
         print(os.listdir(tmp_path))
         assert len(os.listdir(tmp_path)) == 4
Example #4
0
 def test_close_session(self, tmp_path):
     from pathlib import Path
     tmp = ['output', 'graph', 'model', 'counter_graph']
     with app.test_request_context():
         sessions.config.UPLOAD_FOLDER = tmp_path
         sessions.config.TMP_FOLDER = tmp_path
         sessions.new_session()
         session['model'] = os.path.join(tmp_path, 'test.dot')
         session['graph'] = os.path.join(tmp_path, 'test.svg')
         session['counter_graph'] = os.path.join(tmp_path,
                                                 'counter_test.svg')
         for target in tmp:
             Path(session[target]).touch()
         sessions.close_session()
         print(session)
         print(os.listdir(tmp_path))
         assert len(os.listdir(tmp_path)) == 0 and len(session) == 0
Example #5
0
def upload_file():
    """This function allows file uploading while checking if the provided
    file is valid and creating necessary folders if not present"""
    payload = {}
    dot = ""
    sessions.close_session()
    sessions.new_session()
    if not os.path.exists(config.UPLOAD_FOLDER):
        os.makedirs(config.UPLOAD_FOLDER)
    if not os.path.exists(os.path.dirname(session['output'])):
        os.makedirs(os.path.dirname(session['output']))
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            return {"text": 'No file part'}, 400
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            return {"text": 'No selected file'}, 400
        if file:
            file_path = session['model']
            file.save(file_path)
            if not puremagic.from_file(file_path) in ALLOWED_EXTENSIONS:
                os.remove(file_path)
                return {"text": "Incompatible file format"}, 400
            if not is_fts(file_path):
                os.remove(file_path)
                return {
                    "text": "The given file is not a FTS or contains errors"
                }, 400
            with open(file_path, 'r') as source:
                dot = source.read()
            graph = graphviz.Graph(dot)
            payload['mts'] = graph.get_mts()
            graph.draw_graph(session['graph'])
            payload['graph'] = dot
            payload['edges'], payload['nodes'] = graph.get_graph_number()
            payload['text'] = "Model loaded"
            return payload, 200
        else:
            return {"text": "Incompatible file format"}, 400
    return {"text": "Invalid request"}, 400