Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:16:07

0001 # test_open_edm4eic.py
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  # Import your Flask app
0009 from pyrobird.server import open_edm4eic_file  # Import the function to test
0010 from pyrobird.edm4eic import edm4eic_entry_to_dict  # Import the function used within the route
0011 
0012 
0013 # Path to the test ROOT file (adjust the path as needed)
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     # Configure the Flask app for testing
0021     flask_app.config['TESTING'] = True
0022     # Set the PYROBIRD_DOWNLOAD_PATH to the 'data' directory where test files are located
0023     flask_app.config['PYROBIRD_DOWNLOAD_PATH'] = os.path.abspath(TEST_ROOT_DATA_DIR)
0024     # Ensure downloads are allowed
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     # Test accessing a permitted local file
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     # Test accessing a local file outside of PYROBIRD_DOWNLOAD_PATH
0048     filename = '/etc/passwd'  # A file outside the allowed path
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     # Test accessing a local file outside of PYROBIRD_DOWNLOAD_PATH
0058     filename = '/etc/passwd'  # A file outside the allowed path
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  # OK
0064 
0065 
0066 def test_open_edm4eic_file_invalid_event_number(client):
0067     # Test accessing an invalid event number
0068     filename = 'reco_2024-09_craterlake_2evt.edm4eic.root'
0069     event_number = 100  # Assuming the file has less than 100 events
0070     response = client.get(f'/api/v1/convert/edm4eic/{event_number}?f={filename}')
0071 
0072     assert response.status_code == 400  # Bad Request
0073 
0074 
0075 def test_open_edm4eic_file_nonexistent_file(client):
0076     # Test accessing a file that does not exist
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  # Not Found
0082 
0083 
0084 def test_open_edm4eic_file_PYROBIRD_DOWNLOAD_IS_DISABLEDd(client):
0085     # Test accessing a file when downloads are disabled
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  # Forbidden
0093 
0094     # Re-enable downloads for other tests
0095     flask_app.config['PYROBIRD_DOWNLOAD_IS_DISABLED'] = False
0096 
0097 
0098 def test_open_edm4eic_file_invalid_file(client):
0099     # Test accessing a file that is not a valid ROOT file
0100     # Create an invalid file in the data directory
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  # Internal Server Error
0110 
0111     # Clean up the invalid file
0112     os.remove(invalid_file_path)