diff --git a/{{cookiecutter.project_slug}}/backend/app/app/core/config.py b/{{cookiecutter.project_slug}}/backend/app/app/core/config.py index 0334ba2..d01e45e 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/core/config.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/core/config.py @@ -76,3 +76,5 @@ def getenv_boolean(var_name, default_value=False): FIRST_SUPERUSER_PASSWORD = os.getenv("FIRST_SUPERUSER_PASSWORD") USERS_OPEN_REGISTRATION = getenv_boolean("USERS_OPEN_REGISTRATION") + +EMAIL_TEST_USER = "test@example.com" diff --git a/{{cookiecutter.project_slug}}/backend/app/app/tests/api/api_v1/test_users.py b/{{cookiecutter.project_slug}}/backend/app/app/tests/api/api_v1/test_users.py index 01e4ebc..c56784f 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/tests/api/api_v1/test_users.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/tests/api/api_v1/test_users.py @@ -20,6 +20,18 @@ def test_get_users_superuser_me(superuser_token_headers): assert current_user["username"] == config.FIRST_SUPERUSER +def test_get_users_normal_user_me(normal_user_token_headers): + server_api = get_server_api() + r = requests.get( + f"{server_api}{config.API_V1_STR}/users/me", headers=normal_user_token_headers + ) + current_user = r.json() + assert current_user + assert current_user["disabled"] is False + assert "superuser" not in current_user["admin_roles"] + assert current_user["email"] == config.EMAIL_TEST_USER + + def test_create_user_new_email(superuser_token_headers): server_api = get_server_api() username = random_lower_string() @@ -73,17 +85,13 @@ def test_create_user_existing_username(superuser_token_headers): assert "_id" not in created_user -def test_create_user_by_normal_user(): +def test_create_user_by_normal_user(normal_user_token_headers): server_api = get_server_api() username = random_lower_string() password = random_lower_string() - user_in = UserCreate(username=username, email=username, password=password) - bucket = get_default_bucket() - user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) - user_token_headers = user_authentication_headers(server_api, username, password) data = {"username": username, "password": password} r = requests.post( - f"{server_api}{config.API_V1_STR}/users/", headers=user_token_headers, json=data + f"{server_api}{config.API_V1_STR}/users/", headers=normal_user_token_headers, json=data ) assert r.status_code == 400 diff --git a/{{cookiecutter.project_slug}}/backend/app/app/tests/conftest.py b/{{cookiecutter.project_slug}}/backend/app/app/tests/conftest.py index 0e3c044..e383ed1 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/tests/conftest.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/tests/conftest.py @@ -1,6 +1,8 @@ import pytest +from app.core import config from app.tests.utils.utils import get_server_api, get_superuser_token_headers +from app.tests.utils.user import authentication_token_from_email @pytest.fixture(scope="module") @@ -11,3 +13,8 @@ def server_api(): @pytest.fixture(scope="module") def superuser_token_headers(): return get_superuser_token_headers() + + +@pytest.fixture(scope="module") +def normal_user_token_headers(): + return authentication_token_from_email(config.EMAIL_TEST_USER) diff --git a/{{cookiecutter.project_slug}}/backend/app/app/tests/utils/user.py b/{{cookiecutter.project_slug}}/backend/app/app/tests/utils/user.py index 1e9f696..e20a33c 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/tests/utils/user.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/tests/utils/user.py @@ -3,8 +3,8 @@ from app import crud from app.core import config from app.db.database import get_default_bucket -from app.models.user import UserCreate -from app.tests.utils.utils import random_lower_string +from app.models.user import UserCreate, UserUpdate +from app.tests.utils.utils import random_lower_string, get_server_api def user_authentication_headers(server_api, email, password): @@ -24,3 +24,23 @@ def create_random_user(): bucket = get_default_bucket() user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) return user + + +def authentication_token_from_email(email): + """ + Return a valid token for the user with given email. + + If the user doesn't exist it is created first. + """ + password = random_lower_string() + bucket = get_default_bucket() + + user = crud.user.get_by_email(bucket, email=email) + if not user: + user_in = UserCreate(username=email, email=email, password=password) + user = crud.user.upsert(bucket, user_in=user_in, persist_to=1) + else: + user_in = UserUpdate(password=password) + user = crud.user.update(bucket, user=user, user_in=user_in) + + return user_authentication_headers(get_server_api(), email, password)