Files
instagram-clone/tests/test_auth.py
OpenClaw Agent a3eca3b7da feat: implement Instagram clone SocialPhoto API
- FastAPI backend with SQLite database
- JWT authentication (register, login)
- User profiles with follow/unfollow
- Posts with image upload and likes/dislikes
- Comments with likes
- Global and personalized feed
- Comprehensive pytest test suite (37 tests)

TASK-ID: 758f4029-702
2026-04-16 03:20:48 +00:00

159 lines
4.1 KiB
Python

"""Tests for authentication routes."""
import pytest
from fastapi.testclient import TestClient
def test_register_success(client: TestClient):
"""Test successful user registration."""
response = client.post(
"/auth/register",
json={
"username": "newuser",
"email": "newuser@example.com",
"password": "password123",
},
)
assert response.status_code == 201
data = response.json()
assert "access_token" in data
assert data["token_type"] == "bearer"
def test_register_duplicate_username(client: TestClient):
"""Test registration with duplicate username."""
# First registration
client.post(
"/auth/register",
json={
"username": "duplicate",
"email": "first@example.com",
"password": "password123",
},
)
# Second registration with same username
response = client.post(
"/auth/register",
json={
"username": "duplicate",
"email": "second@example.com",
"password": "password123",
},
)
assert response.status_code == 400
assert "Username already registered" in response.json()["detail"]
def test_register_duplicate_email(client: TestClient):
"""Test registration with duplicate email."""
# First registration
client.post(
"/auth/register",
json={
"username": "firstuser",
"email": "same@example.com",
"password": "password123",
},
)
# Second registration with same email
response = client.post(
"/auth/register",
json={
"username": "seconduser",
"email": "same@example.com",
"password": "password123",
},
)
assert response.status_code == 400
assert "Email already registered" in response.json()["detail"]
def test_register_invalid_email(client: TestClient):
"""Test registration with invalid email format."""
response = client.post(
"/auth/register",
json={
"username": "validuser",
"email": "not-an-email",
"password": "password123",
},
)
assert response.status_code == 422 # Validation error
def test_register_short_password(client: TestClient):
"""Test registration with too short password."""
response = client.post(
"/auth/register",
json={
"username": "validuser",
"email": "valid@example.com",
"password": "12345",
},
)
assert response.status_code == 422 # Validation error
def test_login_success(client: TestClient):
"""Test successful login."""
# Register first
client.post(
"/auth/register",
json={
"username": "loginuser",
"email": "login@example.com",
"password": "password123",
},
)
# Login
response = client.post(
"/auth/login",
json={
"username": "loginuser",
"password": "password123",
},
)
assert response.status_code == 200
data = response.json()
assert "access_token" in data
assert data["token_type"] == "bearer"
def test_login_wrong_password(client: TestClient):
"""Test login with wrong password."""
# Register first
client.post(
"/auth/register",
json={
"username": "testuser",
"email": "test@example.com",
"password": "correctpassword",
},
)
# Login with wrong password
response = client.post(
"/auth/login",
json={
"username": "testuser",
"password": "wrongpassword",
},
)
assert response.status_code == 401
assert "Incorrect username or password" in response.json()["detail"]
def test_login_nonexistent_user(client: TestClient):
"""Test login with nonexistent username."""
response = client.post(
"/auth/login",
json={
"username": "nonexistent",
"password": "password123",
},
)
assert response.status_code == 401
assert "Incorrect username or password" in response.json()["detail"]