Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
✨ Add normal-user testing fixture (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored Jan 19, 2020
1 parent 8f77b57 commit de80678
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
2 changes: 2 additions & 0 deletions {{cookiecutter.project_slug}}/backend/app/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
24 changes: 22 additions & 2 deletions {{cookiecutter.project_slug}}/backend/app/app/tests/utils/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit de80678

Please sign in to comment.