Skip to content

Commit

Permalink
✅ Add normal-user fixture for testing (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebreton authored and tiangolo committed Jan 19, 2020
1 parent 44d8a43 commit 248ea56
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 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 @@ -51,3 +51,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["email"] == 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["is_active"] is True
assert current_user["is_superuser"] is False
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 @@ -71,16 +83,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(email=username, password=password)
user = crud.user.create(db_session, user_in=user_in)
user_token_headers = user_authentication_headers(server_api, username, password)
data = {"email": 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)
22 changes: 20 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.session import db_session
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 get_server_api, random_lower_string


def user_authentication_headers(server_api, email, password):
Expand All @@ -23,3 +23,21 @@ def create_random_user():
user_in = UserCreate(username=email, email=email, password=password)
user = crud.user.create(db_session=db_session, user_in=user_in)
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()
user = crud.user.get_by_email(db_session, email=email)
if not user:
user_in = UserCreate(username=email, email=email, password=password)
user = crud.user.create(db_session=db_session, user_in=user_in)
else:
user_in = UserUpdate(password=password)
user = crud.user.update(db_session, user=user, user_in=user_in)

return user_authentication_headers(get_server_api(), email, password)

0 comments on commit 248ea56

Please sign in to comment.