2024-07-22 17:49:02 -05:00
|
|
|
import uuid
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
from fastapi.testclient import TestClient
|
2024-02-29 15:42:55 -05:00
|
|
|
from sqlmodel import Session
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
2020-04-16 23:56:10 -06:00
|
|
|
from app.core.config import settings
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
from app.tests.utils.item import create_random_item
|
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_create_item(
|
2024-04-05 22:05:28 +02:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
2020-04-20 20:31:29 +02:00
|
|
|
) -> None:
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
data = {"title": "Foo", "description": "Fighters"}
|
2020-04-20 20:31:29 +02:00
|
|
|
response = client.post(
|
2024-02-25 19:39:33 +01:00
|
|
|
f"{settings.API_V1_STR}/items/",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
)
|
2020-01-19 22:40:50 +01:00
|
|
|
assert response.status_code == 200
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
content = response.json()
|
|
|
|
assert content["title"] == data["title"]
|
|
|
|
assert content["description"] == data["description"]
|
|
|
|
assert "id" in content
|
|
|
|
assert "owner_id" in content
|
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_read_item(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
2020-04-20 20:31:29 +02:00
|
|
|
) -> None:
|
:recycle: Refactor backend, settings, DB sessions, types, configs, plugins (#158)
* :recycle: Refactor backend, update DB session handling
* :sparkles: Add mypy config and plugins
* :heavy_plus_sign: Use Python-jose instead of PyJWT
as it has some extra functionalities and features
* :sparkles: Add/update scripts for test, lint, format
* :wrench: Update lint and format configs
* :art: Update import format, comments, and types
* :art: Add types to config
* :sparkles: Add types for all the code, and small fixes
* :art: Use global imports to simplify exploring with Jupyter
* :recycle: Import schemas and models, instead of each class
* :truck: Rename db_session to db for simplicity
* :pushpin: Update dependencies installation for testing
2020-04-20 19:03:13 +02:00
|
|
|
item = create_random_item(db)
|
2020-04-20 20:31:29 +02:00
|
|
|
response = client.get(
|
2024-02-25 19:39:33 +01:00
|
|
|
f"{settings.API_V1_STR}/items/{item.id}",
|
|
|
|
headers=superuser_token_headers,
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
)
|
2020-01-19 22:40:50 +01:00
|
|
|
assert response.status_code == 200
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
content = response.json()
|
|
|
|
assert content["title"] == item.title
|
|
|
|
assert content["description"] == item.description
|
2024-07-22 17:49:02 -05:00
|
|
|
assert content["id"] == str(item.id)
|
|
|
|
assert content["owner_id"] == str(item.owner_id)
|
2024-03-07 18:21:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_item_not_found(
|
2024-04-05 22:05:28 +02:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> None:
|
|
|
|
response = client.get(
|
2024-07-22 17:49:02 -05:00
|
|
|
f"{settings.API_V1_STR}/items/{uuid.uuid4()}",
|
2024-03-07 18:21:46 -05:00
|
|
|
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(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> 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(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> 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(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> 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"]
|
2024-07-22 17:49:02 -05:00
|
|
|
assert content["id"] == str(item.id)
|
|
|
|
assert content["owner_id"] == str(item.owner_id)
|
2024-03-07 18:21:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_item_not_found(
|
2024-04-05 22:05:28 +02:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> None:
|
|
|
|
data = {"title": "Updated title", "description": "Updated description"}
|
|
|
|
response = client.put(
|
2024-07-22 17:49:02 -05:00
|
|
|
f"{settings.API_V1_STR}/items/{uuid.uuid4()}",
|
2024-03-07 18:21:46 -05:00
|
|
|
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(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> 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(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> 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(
|
2024-04-05 22:05:28 +02:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> None:
|
|
|
|
response = client.delete(
|
2024-07-22 17:49:02 -05:00
|
|
|
f"{settings.API_V1_STR}/items/{uuid.uuid4()}",
|
2024-03-07 18:21:46 -05:00
|
|
|
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(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
2024-03-07 18:21:46 -05:00
|
|
|
) -> 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"
|