123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- from app.core.config import settings
- from apps.tests.utils.item import create_random_item
- from sqlmodel import Session
- from fastapi.testclient import TestClient
- def test_create_item(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- data = {"title": "Foo", "description": "Fighters"}
- response = client.post(
- f"{settings.API_V1_STR}/items/",
- headers=superuser_token_headers,
- json=data,
- )
- assert response.status_code == 200
- content = response.json()
- assert content["title"] == data["title"]
- assert content["description"] == data["description"]
- assert "id" in content
- assert "owner_id" in content
- def test_read_item(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- item = create_random_item(db)
- response = client.get(
- f"{settings.API_V1_STR}/items/{item.id}",
- headers=superuser_token_headers,
- )
- assert response.status_code == 200
- content = response.json()
- assert content["title"] == item.title
- assert content["description"] == item.description
- assert content["id"] == item.id
- assert content["owner_id"] == item.owner_id
- def test_read_item_not_found(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- response = client.get(
- f"{settings.API_V1_STR}/items/999",
- headers=superuser_token_headers,
- )
- assert response.status_code == 404
- content = response.json()
- assert content["detail"] == "Item not found"
- def test_read_item_not_enough_permissions(
- client: TestClient, normal_user_token_headers: dict[str, str], db: Session
- ) -> None:
- item = create_random_item(db)
- response = client.get(
- f"{settings.API_V1_STR}/items/{item.id}",
- headers=normal_user_token_headers,
- )
- assert response.status_code == 400
- content = response.json()
- assert content["detail"] == "Not enough permissions"
- def test_read_items(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- create_random_item(db)
- create_random_item(db)
- response = client.get(
- f"{settings.API_V1_STR}/items/",
- headers=superuser_token_headers,
- )
- assert response.status_code == 200
- content = response.json()
- assert len(content["data"]) >= 2
- def test_update_item(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- item = create_random_item(db)
- data = {"title": "Updated title", "description": "Updated description"}
- response = client.put(
- f"{settings.API_V1_STR}/items/{item.id}",
- headers=superuser_token_headers,
- json=data,
- )
- assert response.status_code == 200
- content = response.json()
- assert content["title"] == data["title"]
- assert content["description"] == data["description"]
- assert content["id"] == item.id
- assert content["owner_id"] == item.owner_id
- def test_update_item_not_found(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- data = {"title": "Updated title", "description": "Updated description"}
- response = client.put(
- f"{settings.API_V1_STR}/items/999",
- headers=superuser_token_headers,
- json=data,
- )
- assert response.status_code == 404
- content = response.json()
- assert content["detail"] == "Item not found"
- def test_update_item_not_enough_permissions(
- client: TestClient, normal_user_token_headers: dict[str, str], db: Session
- ) -> None:
- item = create_random_item(db)
- data = {"title": "Updated title", "description": "Updated description"}
- response = client.put(
- f"{settings.API_V1_STR}/items/{item.id}",
- headers=normal_user_token_headers,
- json=data,
- )
- assert response.status_code == 400
- content = response.json()
- assert content["detail"] == "Not enough permissions"
- def test_delete_item(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- item = create_random_item(db)
- response = client.delete(
- f"{settings.API_V1_STR}/items/{item.id}",
- headers=superuser_token_headers,
- )
- assert response.status_code == 200
- content = response.json()
- assert content["message"] == "Item deleted successfully"
- def test_delete_item_not_found(
- client: TestClient, superuser_token_headers: dict[str, str], db: Session
- ) -> None:
- response = client.delete(
- f"{settings.API_V1_STR}/items/999",
- headers=superuser_token_headers,
- )
- assert response.status_code == 404
- content = response.json()
- assert content["detail"] == "Item not found"
- def test_delete_item_not_enough_permissions(
- client: TestClient, normal_user_token_headers: dict[str, str], db: Session
- ) -> None:
- item = create_random_item(db)
- response = client.delete(
- f"{settings.API_V1_STR}/items/{item.id}",
- headers=normal_user_token_headers,
- )
- assert response.status_code == 400
- content = response.json()
- assert content["detail"] == "Not enough permissions"
|