diff --git a/imagecreator/backend/app/__pycache__/__init__.cpython-310.pyc b/imagecreator/backend/app/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..02ae2e1 Binary files /dev/null and b/imagecreator/backend/app/__pycache__/__init__.cpython-310.pyc differ diff --git a/imagecreator/backend/app/__pycache__/config.cpython-310.pyc b/imagecreator/backend/app/__pycache__/config.cpython-310.pyc new file mode 100644 index 0000000..936bf64 Binary files /dev/null and b/imagecreator/backend/app/__pycache__/config.cpython-310.pyc differ diff --git a/imagecreator/backend/app/__pycache__/database.cpython-310.pyc b/imagecreator/backend/app/__pycache__/database.cpython-310.pyc new file mode 100644 index 0000000..53daa2c Binary files /dev/null and b/imagecreator/backend/app/__pycache__/database.cpython-310.pyc differ diff --git a/imagecreator/backend/app/__pycache__/main.cpython-310.pyc b/imagecreator/backend/app/__pycache__/main.cpython-310.pyc new file mode 100644 index 0000000..bfbfdd4 Binary files /dev/null and b/imagecreator/backend/app/__pycache__/main.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/__pycache__/__init__.cpython-310.pyc b/imagecreator/backend/app/api/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..f56788f Binary files /dev/null and b/imagecreator/backend/app/api/__pycache__/__init__.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/__pycache__/auth.cpython-310.pyc b/imagecreator/backend/app/api/__pycache__/auth.cpython-310.pyc new file mode 100644 index 0000000..14be8fa Binary files /dev/null and b/imagecreator/backend/app/api/__pycache__/auth.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/__pycache__/deps.cpython-310.pyc b/imagecreator/backend/app/api/__pycache__/deps.cpython-310.pyc new file mode 100644 index 0000000..d45e9da Binary files /dev/null and b/imagecreator/backend/app/api/__pycache__/deps.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/__pycache__/history.cpython-310.pyc b/imagecreator/backend/app/api/__pycache__/history.cpython-310.pyc new file mode 100644 index 0000000..2abe7f5 Binary files /dev/null and b/imagecreator/backend/app/api/__pycache__/history.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/__pycache__/images.cpython-310.pyc b/imagecreator/backend/app/api/__pycache__/images.cpython-310.pyc new file mode 100644 index 0000000..b262bfa Binary files /dev/null and b/imagecreator/backend/app/api/__pycache__/images.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/__pycache__/users.cpython-310.pyc b/imagecreator/backend/app/api/__pycache__/users.cpython-310.pyc new file mode 100644 index 0000000..9fc9cb2 Binary files /dev/null and b/imagecreator/backend/app/api/__pycache__/users.cpython-310.pyc differ diff --git a/imagecreator/backend/app/api/deps.py b/imagecreator/backend/app/api/deps.py index 3b2d212..bff2dc5 100644 --- a/imagecreator/backend/app/api/deps.py +++ b/imagecreator/backend/app/api/deps.py @@ -21,7 +21,7 @@ def get_current_user( detail="Invalid or expired token" ) - user_id_str = payload.get("sub") + user_id_str = payload.sub if not user_id_str: raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") diff --git a/imagecreator/backend/app/api/users.py b/imagecreator/backend/app/api/users.py index 4d5194a..6adecef 100644 --- a/imagecreator/backend/app/api/users.py +++ b/imagecreator/backend/app/api/users.py @@ -1,3 +1,32 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session -router = APIRouter() \ No newline at end of file +from app.database import get_db +from app.api.deps import get_current_user +from app.models.user import User +from app.schemas.user import UserResponse, UserUpdate +from app.services.auth import AuthService + +router = APIRouter() + +@router.get("/me", response_model=UserResponse) +def get_current_user_info(current_user: User = Depends(get_current_user)): + return current_user + +@router.put("/me", response_model=UserResponse) +def update_current_user( + data: UserUpdate, + current_user: User = Depends(get_current_user), + db: Session = Depends(get_db) +): + service = AuthService(db) + if data.email and data.email != current_user.email: + # Check if email is taken + existing = db.query(User).filter(User.email == data.email, User.id != current_user.id).first() + if existing: + raise HTTPException(status_code=400, detail="Email already registered") + current_user.email = data.email + + db.commit() + db.refresh(current_user) + return current_user \ No newline at end of file diff --git a/imagecreator/backend/app/core/__pycache__/__init__.cpython-310.pyc b/imagecreator/backend/app/core/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..e579766 Binary files /dev/null and b/imagecreator/backend/app/core/__pycache__/__init__.cpython-310.pyc differ diff --git a/imagecreator/backend/app/core/__pycache__/security.cpython-310.pyc b/imagecreator/backend/app/core/__pycache__/security.cpython-310.pyc new file mode 100644 index 0000000..0ea85e9 Binary files /dev/null and b/imagecreator/backend/app/core/__pycache__/security.cpython-310.pyc differ diff --git a/imagecreator/backend/app/models/__pycache__/__init__.cpython-310.pyc b/imagecreator/backend/app/models/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..de418c5 Binary files /dev/null and b/imagecreator/backend/app/models/__pycache__/__init__.cpython-310.pyc differ diff --git a/imagecreator/backend/app/models/__pycache__/history.cpython-310.pyc b/imagecreator/backend/app/models/__pycache__/history.cpython-310.pyc new file mode 100644 index 0000000..e26ed29 Binary files /dev/null and b/imagecreator/backend/app/models/__pycache__/history.cpython-310.pyc differ diff --git a/imagecreator/backend/app/models/__pycache__/user.cpython-310.pyc b/imagecreator/backend/app/models/__pycache__/user.cpython-310.pyc new file mode 100644 index 0000000..32a65db Binary files /dev/null and b/imagecreator/backend/app/models/__pycache__/user.cpython-310.pyc differ diff --git a/imagecreator/backend/app/schemas/__pycache__/__init__.cpython-310.pyc b/imagecreator/backend/app/schemas/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..e1b238a Binary files /dev/null and b/imagecreator/backend/app/schemas/__pycache__/__init__.cpython-310.pyc differ diff --git a/imagecreator/backend/app/schemas/__pycache__/auth.cpython-310.pyc b/imagecreator/backend/app/schemas/__pycache__/auth.cpython-310.pyc new file mode 100644 index 0000000..35b9172 Binary files /dev/null and b/imagecreator/backend/app/schemas/__pycache__/auth.cpython-310.pyc differ diff --git a/imagecreator/backend/app/schemas/__pycache__/user.cpython-310.pyc b/imagecreator/backend/app/schemas/__pycache__/user.cpython-310.pyc new file mode 100644 index 0000000..70a8ba8 Binary files /dev/null and b/imagecreator/backend/app/schemas/__pycache__/user.cpython-310.pyc differ diff --git a/imagecreator/backend/app/services/__pycache__/__init__.cpython-310.pyc b/imagecreator/backend/app/services/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..82a3168 Binary files /dev/null and b/imagecreator/backend/app/services/__pycache__/__init__.cpython-310.pyc differ diff --git a/imagecreator/backend/app/services/__pycache__/auth.cpython-310.pyc b/imagecreator/backend/app/services/__pycache__/auth.cpython-310.pyc new file mode 100644 index 0000000..1046d4f Binary files /dev/null and b/imagecreator/backend/app/services/__pycache__/auth.cpython-310.pyc differ diff --git a/imagecreator/backend/test.db b/imagecreator/backend/test.db new file mode 100644 index 0000000..c4f393e Binary files /dev/null and b/imagecreator/backend/test.db differ diff --git a/imagecreator/backend/tests/__pycache__/test_auth_api.cpython-310-pytest-9.0.2.pyc b/imagecreator/backend/tests/__pycache__/test_auth_api.cpython-310-pytest-9.0.2.pyc new file mode 100644 index 0000000..c7831f6 Binary files /dev/null and b/imagecreator/backend/tests/__pycache__/test_auth_api.cpython-310-pytest-9.0.2.pyc differ diff --git a/imagecreator/backend/tests/__pycache__/test_models.cpython-310-pytest-9.0.2.pyc b/imagecreator/backend/tests/__pycache__/test_models.cpython-310-pytest-9.0.2.pyc new file mode 100644 index 0000000..e65ee09 Binary files /dev/null and b/imagecreator/backend/tests/__pycache__/test_models.cpython-310-pytest-9.0.2.pyc differ diff --git a/imagecreator/backend/tests/__pycache__/test_security.cpython-310-pytest-9.0.2.pyc b/imagecreator/backend/tests/__pycache__/test_security.cpython-310-pytest-9.0.2.pyc new file mode 100644 index 0000000..2bc411f Binary files /dev/null and b/imagecreator/backend/tests/__pycache__/test_security.cpython-310-pytest-9.0.2.pyc differ diff --git a/imagecreator/backend/tests/__pycache__/test_users_api.cpython-310-pytest-9.0.2.pyc b/imagecreator/backend/tests/__pycache__/test_users_api.cpython-310-pytest-9.0.2.pyc new file mode 100644 index 0000000..0894105 Binary files /dev/null and b/imagecreator/backend/tests/__pycache__/test_users_api.cpython-310-pytest-9.0.2.pyc differ diff --git a/imagecreator/backend/tests/test_users_api.py b/imagecreator/backend/tests/test_users_api.py new file mode 100644 index 0000000..dd0faa2 --- /dev/null +++ b/imagecreator/backend/tests/test_users_api.py @@ -0,0 +1,49 @@ +import pytest +import os +from fastapi.testclient import TestClient +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from app.database import Base, get_db +from app.main import app +from app.models.user import User +from app.core.security import hash_password, create_access_token + +# Use file-based SQLite for testing to avoid in-memory connection issues +TEST_DB_PATH = "/tmp/test_users.db" +if os.path.exists(TEST_DB_PATH): + os.remove(TEST_DB_PATH) + +engine = create_engine(f"sqlite:///{TEST_DB_PATH}", connect_args={"check_same_thread": False}) +TestSessionLocal = sessionmaker(bind=engine) + +def override_get_db(): + db = TestSessionLocal() + try: + yield db + finally: + db.close() + +app.dependency_overrides[get_db] = override_get_db +client = TestClient(app) + +# Create tables once at module level +Base.metadata.create_all(bind=engine) + +def get_auth_header(user_id): + token = create_access_token({"sub": str(user_id)}) + return {"Authorization": f"Bearer {token}"} + +def test_get_current_user(): + db = TestSessionLocal() + user = User(username="testuser", email="test@example.com", hashed_password=hash_password("pass")) + db.add(user) + db.commit() + db.refresh(user) + db.close() + + response = client.get("/api/users/me", headers=get_auth_header(user.id)) + assert response.status_code == 200 + data = response.json() + assert data["username"] == "testuser" + assert data["email"] == "test@example.com" + assert "password" not in data \ No newline at end of file diff --git a/prompt-optimizer b/prompt-optimizer new file mode 160000 index 0000000..0a4785d --- /dev/null +++ b/prompt-optimizer @@ -0,0 +1 @@ +Subproject commit 0a4785d36533d90e4758e618d5bdc65436bf8990