2020-04-20 20:31:29 +02:00
|
|
|
from fastapi.testclient import TestClient
|
2024-03-10 14:47:21 -05:00
|
|
|
from pytest_mock import MockerFixture
|
2024-02-29 15:42:55 -05:00
|
|
|
from sqlmodel import Session
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2019-03-11 13:36:42 +04:00
|
|
|
from app import crud
|
2020-04-16 23:56:10 -06:00
|
|
|
from app.core.config import settings
|
2024-02-29 15:42:55 -05:00
|
|
|
from app.models import UserCreate
|
2020-04-20 20:31:29 +02:00
|
|
|
from app.tests.utils.utils import random_email, random_lower_string
|
2019-02-09 19:42:36 +04:00
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_get_users_superuser_me(
|
2024-02-25 19:39:33 +01:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
2020-04-20 20:31:29 +02:00
|
|
|
) -> None:
|
|
|
|
r = client.get(f"{settings.API_V1_STR}/users/me", headers=superuser_token_headers)
|
2019-02-09 19:42:36 +04:00
|
|
|
current_user = r.json()
|
|
|
|
assert current_user
|
2019-02-23 18:44:29 +04:00
|
|
|
assert current_user["is_active"] is True
|
|
|
|
assert current_user["is_superuser"]
|
2020-04-16 23:56:10 -06:00
|
|
|
assert current_user["email"] == settings.FIRST_SUPERUSER
|
2019-02-09 19:42:36 +04:00
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_get_users_normal_user_me(
|
2024-02-25 19:39:33 +01:00
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str]
|
2020-04-20 20:31:29 +02:00
|
|
|
) -> None:
|
|
|
|
r = client.get(f"{settings.API_V1_STR}/users/me", headers=normal_user_token_headers)
|
2020-01-19 13:25:17 +01:00
|
|
|
current_user = r.json()
|
|
|
|
assert current_user
|
|
|
|
assert current_user["is_active"] is True
|
|
|
|
assert current_user["is_superuser"] is False
|
2020-04-16 23:56:10 -06:00
|
|
|
assert current_user["email"] == settings.EMAIL_TEST_USER
|
2020-01-19 13:25:17 +01:00
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_create_user_new_email(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient,
|
|
|
|
superuser_token_headers: dict[str, str],
|
|
|
|
db: Session,
|
|
|
|
mocker: MockerFixture,
|
2020-04-20 20:31:29 +02:00
|
|
|
) -> None:
|
2024-03-10 00:02:36 +01:00
|
|
|
mocker.patch("app.utils.send_email")
|
2024-03-11 16:34:18 +01:00
|
|
|
mocker.patch("app.core.config.settings.SMTP_HOST", "smtp.example.com")
|
|
|
|
mocker.patch("app.core.config.settings.SMTP_USER", "admin@example.com")
|
2020-04-06 11:36:29 +02:00
|
|
|
username = random_email()
|
2019-02-09 19:42:36 +04:00
|
|
|
password = random_lower_string()
|
2019-02-23 18:44:29 +04:00
|
|
|
data = {"email": username, "password": password}
|
2020-04-20 20:31:29 +02:00
|
|
|
r = client.post(
|
2024-02-25 19:39:33 +01:00
|
|
|
f"{settings.API_V1_STR}/users/",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
2019-02-09 19:42:36 +04:00
|
|
|
)
|
|
|
|
assert 200 <= r.status_code < 300
|
|
|
|
created_user = r.json()
|
2024-02-29 15:42:55 -05:00
|
|
|
user = crud.get_user_by_email(session=db, email=username)
|
: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
|
|
|
assert user
|
2019-02-23 18:44:29 +04:00
|
|
|
assert user.email == created_user["email"]
|
2019-02-09 19:42:36 +04:00
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_get_existing_user(
|
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:
|
2020-04-06 11:36:29 +02:00
|
|
|
username = random_email()
|
2019-02-09 19:42:36 +04:00
|
|
|
password = random_lower_string()
|
: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
|
|
|
user_in = UserCreate(email=username, password=password)
|
2024-02-29 15:42:55 -05:00
|
|
|
user = crud.create_user(session=db, user_create=user_in)
|
2019-02-23 18:44:29 +04:00
|
|
|
user_id = user.id
|
2020-04-20 20:31:29 +02:00
|
|
|
r = client.get(
|
2024-02-25 19:39:33 +01:00
|
|
|
f"{settings.API_V1_STR}/users/{user_id}",
|
|
|
|
headers=superuser_token_headers,
|
2019-02-09 19:42:36 +04:00
|
|
|
)
|
|
|
|
assert 200 <= r.status_code < 300
|
|
|
|
api_user = r.json()
|
2024-02-29 15:42:55 -05:00
|
|
|
existing_user = crud.get_user_by_email(session=db, email=username)
|
: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
|
|
|
assert existing_user
|
|
|
|
assert existing_user.email == api_user["email"]
|
2019-02-09 19:42:36 +04:00
|
|
|
|
|
|
|
|
2024-03-10 00:02:36 +01:00
|
|
|
def test_get_existing_user_current_user(client: TestClient, db: Session) -> None:
|
2024-03-07 18:21:46 -05:00
|
|
|
username = random_email()
|
|
|
|
password = random_lower_string()
|
|
|
|
user_in = UserCreate(email=username, password=password)
|
|
|
|
user = crud.create_user(session=db, user_create=user_in)
|
|
|
|
user_id = user.id
|
|
|
|
|
|
|
|
login_data = {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}
|
|
|
|
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
|
|
|
|
tokens = r.json()
|
|
|
|
a_token = tokens["access_token"]
|
|
|
|
headers = {"Authorization": f"Bearer {a_token}"}
|
|
|
|
|
|
|
|
r = client.get(
|
|
|
|
f"{settings.API_V1_STR}/users/{user_id}",
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
assert 200 <= r.status_code < 300
|
|
|
|
api_user = r.json()
|
|
|
|
existing_user = crud.get_user_by_email(session=db, email=username)
|
|
|
|
assert existing_user
|
|
|
|
assert existing_user.email == api_user["email"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_existing_user_permissions_error(
|
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
r = client.get(
|
|
|
|
f"{settings.API_V1_STR}/users/999999",
|
|
|
|
headers=normal_user_token_headers,
|
|
|
|
)
|
|
|
|
assert r.status_code == 403
|
|
|
|
assert r.json() == {"detail": "The user doesn't have enough privileges"}
|
|
|
|
|
|
|
|
|
: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
|
|
|
def test_create_user_existing_username(
|
2024-03-10 14:47:21 -05:00
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
: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
|
|
|
) -> None:
|
2020-04-06 11:36:29 +02:00
|
|
|
username = random_email()
|
2019-02-09 19:42:36 +04:00
|
|
|
# username = email
|
|
|
|
password = random_lower_string()
|
: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
|
|
|
user_in = UserCreate(email=username, password=password)
|
2024-02-29 15:42:55 -05:00
|
|
|
crud.create_user(session=db, user_create=user_in)
|
2019-02-23 18:44:29 +04:00
|
|
|
data = {"email": username, "password": password}
|
2020-04-20 20:31:29 +02:00
|
|
|
r = client.post(
|
2024-02-25 19:39:33 +01:00
|
|
|
f"{settings.API_V1_STR}/users/",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
2019-02-09 19:42:36 +04:00
|
|
|
)
|
|
|
|
created_user = r.json()
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert "_id" not in created_user
|
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_create_user_by_normal_user(
|
2024-02-25 19:39:33 +01:00
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str]
|
2020-04-20 20:31:29 +02:00
|
|
|
) -> None:
|
2020-04-06 11:36:29 +02:00
|
|
|
username = random_email()
|
2019-02-09 19:42:36 +04:00
|
|
|
password = random_lower_string()
|
2019-02-23 18:44:29 +04:00
|
|
|
data = {"email": username, "password": password}
|
2020-04-20 20:31:29 +02:00
|
|
|
r = client.post(
|
2024-02-25 19:39:33 +01:00
|
|
|
f"{settings.API_V1_STR}/users/",
|
|
|
|
headers=normal_user_token_headers,
|
|
|
|
json=data,
|
2019-02-09 19:42:36 +04:00
|
|
|
)
|
|
|
|
assert r.status_code == 400
|
|
|
|
|
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
def test_retrieve_users(
|
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:
|
2020-04-06 11:36:29 +02:00
|
|
|
username = random_email()
|
2019-02-09 19:42:36 +04:00
|
|
|
password = random_lower_string()
|
: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
|
|
|
user_in = UserCreate(email=username, password=password)
|
2024-02-29 15:42:55 -05:00
|
|
|
crud.create_user(session=db, user_create=user_in)
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2020-04-06 11:36:29 +02:00
|
|
|
username2 = random_email()
|
2019-02-09 19:42:36 +04:00
|
|
|
password2 = random_lower_string()
|
: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
|
|
|
user_in2 = UserCreate(email=username2, password=password2)
|
2024-02-29 15:42:55 -05:00
|
|
|
crud.create_user(session=db, user_create=user_in2)
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2020-04-20 20:31:29 +02:00
|
|
|
r = client.get(f"{settings.API_V1_STR}/users/", headers=superuser_token_headers)
|
2019-02-09 19:42:36 +04:00
|
|
|
all_users = r.json()
|
|
|
|
|
2024-02-25 10:04:47 -05:00
|
|
|
assert len(all_users["data"]) > 1
|
|
|
|
assert "count" in all_users
|
|
|
|
for item in all_users["data"]:
|
: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
|
|
|
assert "email" in item
|
2024-03-07 18:21:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_user_me(
|
|
|
|
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
full_name = "Updated Name"
|
|
|
|
email = "updated email"
|
|
|
|
data = {"full_name": full_name, "email": email}
|
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/me",
|
|
|
|
headers=normal_user_token_headers,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
updated_user = r.json()
|
|
|
|
assert updated_user["email"] == email
|
|
|
|
assert updated_user["full_name"] == full_name
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_password_me(
|
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:
|
|
|
|
new_password = random_lower_string()
|
2024-03-10 00:02:36 +01:00
|
|
|
data = {
|
|
|
|
"current_password": settings.FIRST_SUPERUSER_PASSWORD,
|
|
|
|
"new_password": new_password,
|
|
|
|
}
|
2024-03-07 18:21:46 -05:00
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/me/password",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
updated_user = r.json()
|
|
|
|
assert updated_user["message"] == "Password updated successfully"
|
|
|
|
|
|
|
|
# Revert to the old password to keep consistency in test
|
2024-03-10 00:02:36 +01:00
|
|
|
old_data = {
|
|
|
|
"current_password": new_password,
|
|
|
|
"new_password": settings.FIRST_SUPERUSER_PASSWORD,
|
|
|
|
}
|
2024-03-07 18:21:46 -05:00
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/me/password",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=old_data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_password_me_incorrect_password(
|
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:
|
|
|
|
new_password = random_lower_string()
|
|
|
|
data = {"current_password": new_password, "new_password": new_password}
|
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/me/password",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 400
|
|
|
|
updated_user = r.json()
|
|
|
|
assert updated_user["detail"] == "Incorrect password"
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_password_me_same_password_error(
|
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:
|
2024-03-10 00:02:36 +01:00
|
|
|
data = {
|
|
|
|
"current_password": settings.FIRST_SUPERUSER_PASSWORD,
|
|
|
|
"new_password": settings.FIRST_SUPERUSER_PASSWORD,
|
|
|
|
}
|
2024-03-07 18:21:46 -05:00
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/me/password",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 400
|
|
|
|
updated_user = r.json()
|
2024-03-10 00:02:36 +01:00
|
|
|
assert (
|
|
|
|
updated_user["detail"] == "New password cannot be the same as the current one"
|
|
|
|
)
|
2024-03-07 18:21:46 -05:00
|
|
|
|
|
|
|
|
2024-03-10 14:47:21 -05:00
|
|
|
def test_create_user_open(client: TestClient, mocker: MockerFixture) -> None:
|
2024-03-07 18:21:46 -05:00
|
|
|
mocker.patch("app.core.config.settings.USERS_OPEN_REGISTRATION", True)
|
|
|
|
username = random_email()
|
|
|
|
password = random_lower_string()
|
|
|
|
full_name = random_lower_string()
|
|
|
|
data = {"email": username, "password": password, "full_name": full_name}
|
|
|
|
r = client.post(
|
|
|
|
f"{settings.API_V1_STR}/users/open",
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
created_user = r.json()
|
|
|
|
assert created_user["email"] == username
|
|
|
|
assert created_user["full_name"] == full_name
|
|
|
|
|
|
|
|
|
2024-03-10 14:47:21 -05:00
|
|
|
def test_create_user_open_forbidden_error(
|
|
|
|
client: TestClient, mocker: MockerFixture
|
|
|
|
) -> None:
|
2024-03-07 18:21:46 -05:00
|
|
|
mocker.patch("app.core.config.settings.USERS_OPEN_REGISTRATION", False)
|
|
|
|
username = random_email()
|
|
|
|
password = random_lower_string()
|
|
|
|
full_name = random_lower_string()
|
|
|
|
data = {"email": username, "password": password, "full_name": full_name}
|
|
|
|
r = client.post(
|
|
|
|
f"{settings.API_V1_STR}/users/open",
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 403
|
|
|
|
assert r.json()["detail"] == "Open user registration is forbidden on this server"
|
|
|
|
|
|
|
|
|
2024-03-10 14:47:21 -05:00
|
|
|
def test_create_user_open_already_exists_error(
|
|
|
|
client: TestClient, mocker: MockerFixture
|
|
|
|
) -> None:
|
2024-03-07 18:21:46 -05:00
|
|
|
mocker.patch("app.core.config.settings.USERS_OPEN_REGISTRATION", True)
|
|
|
|
password = random_lower_string()
|
|
|
|
full_name = random_lower_string()
|
2024-03-10 00:02:36 +01:00
|
|
|
data = {
|
|
|
|
"email": settings.FIRST_SUPERUSER,
|
|
|
|
"password": password,
|
|
|
|
"full_name": full_name,
|
|
|
|
}
|
2024-03-07 18:21:46 -05:00
|
|
|
r = client.post(
|
|
|
|
f"{settings.API_V1_STR}/users/open",
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 400
|
2024-03-10 00:02:36 +01:00
|
|
|
assert (
|
|
|
|
r.json()["detail"] == "The user with this username already exists in the system"
|
|
|
|
)
|
2024-03-07 18:21:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_user(
|
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
username = random_email()
|
|
|
|
password = random_lower_string()
|
|
|
|
user_in = UserCreate(email=username, password=password)
|
|
|
|
user = crud.create_user(session=db, user_create=user_in)
|
|
|
|
|
|
|
|
data = {"full_name": "Updated_full_name"}
|
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/{user.id}",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
updated_user = r.json()
|
|
|
|
assert updated_user["full_name"] == "Updated_full_name"
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_user_not_exists(
|
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
data = {"full_name": "Updated_full_name"}
|
|
|
|
r = client.patch(
|
|
|
|
f"{settings.API_V1_STR}/users/99999999",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
2024-03-10 00:02:36 +01:00
|
|
|
assert (
|
|
|
|
r.json()["detail"] == "The user with this username does not exist in the system"
|
|
|
|
)
|
2024-03-07 18:21:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_user_super_user(
|
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
username = random_email()
|
|
|
|
password = random_lower_string()
|
|
|
|
user_in = UserCreate(email=username, password=password)
|
|
|
|
user = crud.create_user(session=db, user_create=user_in)
|
|
|
|
user_id = user.id
|
|
|
|
r = client.delete(
|
|
|
|
f"{settings.API_V1_STR}/users/{user_id}",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
deleted_user = r.json()
|
|
|
|
assert deleted_user["message"] == "User deleted successfully"
|
|
|
|
|
|
|
|
|
2024-03-10 00:02:36 +01:00
|
|
|
def test_delete_user_current_user(client: TestClient, db: Session) -> None:
|
2024-03-07 18:21:46 -05:00
|
|
|
username = random_email()
|
|
|
|
password = random_lower_string()
|
|
|
|
user_in = UserCreate(email=username, password=password)
|
|
|
|
user = crud.create_user(session=db, user_create=user_in)
|
|
|
|
user_id = user.id
|
|
|
|
|
|
|
|
login_data = {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}
|
|
|
|
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
|
|
|
|
tokens = r.json()
|
|
|
|
a_token = tokens["access_token"]
|
|
|
|
headers = {"Authorization": f"Bearer {a_token}"}
|
|
|
|
|
|
|
|
r = client.delete(
|
|
|
|
f"{settings.API_V1_STR}/users/{user_id}",
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
deleted_user = r.json()
|
|
|
|
assert deleted_user["message"] == "User deleted successfully"
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_user_not_found(
|
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
r = client.delete(
|
|
|
|
f"{settings.API_V1_STR}/users/99999999",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
|
|
|
assert r.json()["detail"] == "User not found"
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_user_current_super_user_error(
|
|
|
|
client: TestClient, superuser_token_headers: dict[str, str], db: Session
|
|
|
|
) -> None:
|
|
|
|
super_user = crud.get_user_by_email(session=db, email=settings.FIRST_SUPERUSER)
|
2024-03-10 14:47:21 -05:00
|
|
|
assert super_user
|
2024-03-07 18:21:46 -05:00
|
|
|
user_id = super_user.id
|
|
|
|
|
|
|
|
r = client.delete(
|
|
|
|
f"{settings.API_V1_STR}/users/{user_id}",
|
|
|
|
headers=superuser_token_headers,
|
|
|
|
)
|
|
|
|
assert r.status_code == 403
|
|
|
|
assert r.json()["detail"] == "Super users are not allowed to delete themselves"
|