Files
TimeTracker/tests/test_profile_avatar.py
Dries Peeters 90dde470da style: standardize code formatting and normalize line endings
- Normalize line endings from CRLF to LF across all files to match .editorconfig
- Standardize quote style from single quotes to double quotes
- Normalize whitespace and formatting throughout codebase
- Apply consistent code style across 372 files including:
  * Application code (models, routes, services, utils)
  * Test files
  * Configuration files
  * CI/CD workflows

This ensures consistency with the project's .editorconfig settings and
improves code maintainability.
2025-11-28 20:05:37 +01:00

93 lines
2.7 KiB
Python

import io
import os
import pytest
from PIL import Image
def _make_test_image_bytes(fmt="PNG", size=(10, 10), color=(255, 0, 0, 255)):
img = Image.new("RGBA", size, color)
buf = io.BytesIO()
img.save(buf, format=fmt)
buf.seek(0)
return buf
@pytest.fixture
def avatar_test_app(app, temp_dir):
"""Configure app with temporary upload folder for avatar tests"""
app.config["UPLOAD_FOLDER"] = temp_dir
# Ensure the avatars directory exists
avatars_dir = os.path.join(temp_dir, "avatars")
os.makedirs(avatars_dir, exist_ok=True)
return app
@pytest.mark.routes
def test_upload_avatar(app, temp_dir, user):
"""Test uploading an avatar"""
from app import db
# Configure temp upload folder
app.config["UPLOAD_FOLDER"] = temp_dir
avatars_dir = os.path.join(temp_dir, "avatars")
os.makedirs(avatars_dir, exist_ok=True)
# Create authenticated client
with app.test_client() as client:
with client.session_transaction() as sess:
sess["_user_id"] = str(user.id)
sess["_fresh"] = True
assert user.avatar_filename is None
data = {
"full_name": "Test User",
"preferred_language": "en",
"avatar": (_make_test_image_bytes("PNG"), "avatar.png"),
}
resp = client.post("/profile/edit", data=data, content_type="multipart/form-data", follow_redirects=True)
assert resp.status_code == 200
from app.models import User
u = User.query.get(user.id)
assert u.avatar_filename is not None
assert u.get_avatar_url() is not None
@pytest.mark.routes
def test_remove_avatar(app, temp_dir, user):
"""Test removing an avatar"""
from app import db
# Configure temp upload folder
app.config["UPLOAD_FOLDER"] = temp_dir
avatars_dir = os.path.join(temp_dir, "avatars")
os.makedirs(avatars_dir, exist_ok=True)
# Create authenticated client
with app.test_client() as client:
with client.session_transaction() as sess:
sess["_user_id"] = str(user.id)
sess["_fresh"] = True
# First upload an avatar
data = {
"full_name": "Test User",
"preferred_language": "en",
"avatar": (_make_test_image_bytes("PNG"), "avatar.png"),
}
client.post("/profile/edit", data=data, content_type="multipart/form-data")
from app.models import User
u = User.query.get(user.id)
assert u.avatar_filename
# Remove it
resp = client.post("/profile/avatar/remove", data={"csrf_token": "disabled-in-tests"}, follow_redirects=True)
assert resp.status_code == 200
u = User.query.get(user.id)
assert u.avatar_filename is None