nthinspectiontool/tests/test_photo_upload.py

114 lines
No EOL
3.8 KiB
Python

"""
Unit tests for photo upload functionality.
"""
import pytest
import io
from app import db
from app.models import Photo, Inspection, User
def test_save_photo_function(app):
"""Test the save_photo helper function."""
from app.routes.inspections import save_photo
# Create a test file - need to add filename attribute to BytesIO
test_file = io.BytesIO(b"fake image content")
test_file.name = "test.jpg" # BytesIO uses 'name' not 'filename'
# For compatibility with the save_photo function, we'll set filename attribute
test_file.filename = "test.jpg"
# Test saving the photo
with app.app_context():
filename = save_photo(test_file)
assert filename is not None
assert filename.endswith(".jpg")
assert len(filename) > 10 # UUID prefix + original filename
# Verify file was saved
import os
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
assert os.path.exists(filepath)
# Clean up
os.remove(filepath)
def test_save_photo_invalid_extension(app):
"""Test that invalid file extensions are rejected."""
from app.routes.inspections import save_photo
# Test with invalid extension
test_file = io.BytesIO(b"fake content")
test_file.name = "test.exe"
test_file.filename = "test.exe"
with app.app_context():
filename = save_photo(test_file)
assert filename is None # Should return None for invalid extension
# Test with no extension
test_file = io.BytesIO(b"fake content")
test_file.name = "test"
test_file.filename = "test"
with app.app_context():
filename = save_photo(test_file)
assert filename is None # Should return None for no extension
def test_photo_upload_in_inspection_creation(auth_client, test_user, app):
"""Test uploading photos when creating an inspection."""
# Create a test image file - need to add filename attribute to BytesIO
test_image = io.BytesIO(b"fake image content for testing")
test_image.name = "test_photo.jpg"
test_image.filename = "test_photo.jpg"
# We need to simulate the multipart form data that would be sent
# This is a bit tricky with the test client, so we'll test the save_photo function directly
# and test the route integration in the end-to-end tests
from app.routes.inspections import save_photo
with app.app_context():
filename = save_photo(test_image)
assert filename is not None
assert filename.endswith(".jpg")
# Clean up
import os
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename))
def test_photo_model_creation(app, test_user):
"""Test creating a photo record in the database."""
with app.app_context():
# Get the user object from the ID
user = User.query.get(test_user)
# Create inspection first
inspection = Inspection(
installation_name='Test Installation',
location='Test Location',
inspection_date='2026-01-01',
reference_number='77777',
created_by=user.id
)
db.session.add(inspection)
db.session.commit()
# Create photo
photo = Photo(
inspection_id=inspection.id,
filename='test_upload.jpg',
caption='Test photo caption',
action_required='urgent'
)
db.session.add(photo)
db.session.commit()
assert photo.id is not None
assert photo.inspection_id == inspection.id
assert photo.filename == 'test_upload.jpg'
assert photo.caption == 'Test photo caption'
assert photo.action_required == 'urgent'
# Test relationship
assert inspection.photos.first() == photo