File indexing completed on 2025-01-18 09:16:07
0001
0002
0003 import pytest
0004 import os
0005 import json
0006 from flask import Flask
0007 from flask.testing import FlaskClient
0008 from pyrobird.server import flask_app
0009 from pyrobird.server import open_edm4eic_file
0010 from pyrobird.edm4eic import edm4eic_entry_to_dict
0011
0012
0013
0014 TEST_ROOT_FILE = os.path.join(os.path.dirname(__file__), 'data', 'reco_2024-09_craterlake_2evt.edm4eic.root')
0015 TEST_ROOT_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
0016
0017
0018 @pytest.fixture
0019 def client():
0020
0021 flask_app.config['TESTING'] = True
0022
0023 flask_app.config['PYROBIRD_DOWNLOAD_PATH'] = os.path.abspath(TEST_ROOT_DATA_DIR)
0024
0025 flask_app.config['PYROBIRD_DOWNLOAD_IS_DISABLED'] = False
0026 flask_app.config['PYROBIRD_DOWNLOAD_IS_UNRESTRICTED'] = False
0027
0028 return flask_app.test_client()
0029
0030
0031 def test_open_edm4eic_file_local_allowed(client):
0032
0033 filename = TEST_ROOT_FILE
0034 event_number = 0
0035 response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={filename}')
0036
0037 assert response.status_code == 200
0038 data = response.get_json()
0039 assert data is not None
0040 assert 'entries' in data
0041 assert 'components' in data['entries'][0]
0042 assert data['entries'][0]["id"] == event_number
0043
0044
0045 def test_open_edm4eic_file_local_not_allowed(client):
0046 from urllib.parse import quote
0047
0048 filename = '/etc/passwd'
0049 event_number = 0
0050 encoded_filename = quote(filename, safe='')
0051 response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={encoded_filename}')
0052
0053 assert response.status_code == 403
0054
0055
0056 def test_open_dangerous(client):
0057
0058 filename = '/etc/passwd'
0059 event_number = 0
0060 flask_app.config['PYROBIRD_DOWNLOAD_IS_UNRESTRICTED'] = True
0061 flask_app.config['PYROBIRD_DOWNLOAD_IS_DISABLED'] = False
0062 response = client.get(f'/api/v1/download?filename={filename}')
0063 assert response.status_code == 200
0064
0065
0066 def test_open_edm4eic_file_invalid_event_number(client):
0067
0068 filename = 'reco_2024-09_craterlake_2evt.edm4eic.root'
0069 event_number = 100
0070 response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={filename}')
0071
0072 assert response.status_code == 400
0073
0074
0075 def test_open_edm4eic_file_nonexistent_file(client):
0076
0077 filename = 'nonexistent_file.edm4eic.root'
0078 event_number = 0
0079 response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={filename}')
0080
0081 assert response.status_code == 404
0082
0083
0084 def test_open_edm4eic_file_PYROBIRD_DOWNLOAD_IS_DISABLEDd(client):
0085
0086 flask_app.config['PYROBIRD_DOWNLOAD_IS_DISABLED'] = True
0087
0088 filename = 'reco_2024-09_craterlake_2evt.edm4eic.root'
0089 event_number = 0
0090 response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={filename}')
0091
0092 assert response.status_code == 403
0093
0094
0095 flask_app.config['PYROBIRD_DOWNLOAD_IS_DISABLED'] = False
0096
0097
0098 def test_open_edm4eic_file_invalid_file(client):
0099
0100
0101 invalid_filename = 'invalid_file.root'
0102 invalid_file_path = os.path.join(flask_app.config['PYROBIRD_DOWNLOAD_PATH'], invalid_filename)
0103 with open(invalid_file_path, 'w') as f:
0104 f.write('This is not a valid ROOT file.')
0105
0106 event_number = 0
0107 response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={invalid_filename}')
0108
0109 assert response.status_code == 500
0110
0111
0112 os.remove(invalid_file_path)