|
| 1 | +def test_root(client): |
| 2 | + """Test the health check endpoint""" |
| 3 | + response = client.get("/") |
| 4 | + assert response.status_code == 200 |
| 5 | + assert response.json() == {"message": "FastAPI Image Upload Server"} |
| 6 | + |
| 7 | +def test_register_user(client): |
| 8 | + """Test successful user registration""" |
| 9 | + response = client.post( |
| 10 | + "/register", |
| 11 | + json={"username": "testuser", "password": "securepassword"} |
| 12 | + ) |
| 13 | + assert response.status_code == 201 |
| 14 | + assert response.json() == {"message": "User created successfully"} |
| 15 | + |
| 16 | +def test_register_duplicate_user(client): |
| 17 | + """Test that duplicate usernames are rejected""" |
| 18 | + client.post("/register", json={"username": "testuser", "password": "securepassword"}) |
| 19 | + |
| 20 | + response = client.post("/register", json={"username": "testuser", "password": "newpassword"}) |
| 21 | + assert response.status_code == 400 |
| 22 | + assert response.json() == {"detail": "Username already registered"} |
| 23 | + |
| 24 | +def test_login_success(client): |
| 25 | + """Test login and JWT token generation""" |
| 26 | + client.post("/register", json={"username": "testuser", "password": "securepassword"}) |
| 27 | + |
| 28 | + # Note: OAuth2PasswordRequestForm expects form data (data=...), not JSON |
| 29 | + response = client.post( |
| 30 | + "/token", |
| 31 | + data={"username": "testuser", "password": "securepassword"} |
| 32 | + ) |
| 33 | + assert response.status_code == 200 |
| 34 | + data = response.json() |
| 35 | + assert "access_token" in data |
| 36 | + assert data["token_type"] == "bearer" |
0 commit comments