Skip to content

Commit 970a182

Browse files
Teomor Szczurektiangolo
andauthored
✨ Add email validation (#40)
* modify tests * ➕ Add email-validator to Dockerfiles * ♻️ Update random email generation * ♻️ Re-apply email validation after rebase Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent 1d86782 commit 970a182

File tree

8 files changed

+28
-25
lines changed

8 files changed

+28
-25
lines changed

{{cookiecutter.project_slug}}/backend/app/app/schemas/user.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from typing import Optional
22

3-
from pydantic import BaseModel
3+
from pydantic import BaseModel, EmailStr
44

55

66
# Shared properties
77
class UserBase(BaseModel):
8-
email: Optional[str] = None
8+
email: Optional[EmailStr] = None
99
is_active: Optional[bool] = True
1010
is_superuser: Optional[bool] = False
1111
full_name: Optional[str] = None
@@ -20,7 +20,7 @@ class Config:
2020

2121
# Properties to receive via API on creation
2222
class UserCreate(UserBaseInDB):
23-
email: str
23+
email: EmailStr
2424
password: str
2525

2626

{{cookiecutter.project_slug}}/backend/app/app/tests/api/api_v1/test_users.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from app.core import config
55
from app.db.session import db_session
66
from app.schemas.user import UserCreate
7-
from app.tests.utils.utils import get_server_api, random_lower_string
7+
from app.tests.utils.utils import get_server_api, random_lower_string, random_email
88

99

1010
def test_get_users_superuser_me(superuser_token_headers):
@@ -33,7 +33,7 @@ def test_get_users_normal_user_me(normal_user_token_headers):
3333

3434
def test_create_user_new_email(superuser_token_headers):
3535
server_api = get_server_api()
36-
username = random_lower_string()
36+
username = random_email()
3737
password = random_lower_string()
3838
data = {"email": username, "password": password}
3939
r = requests.post(
@@ -49,7 +49,7 @@ def test_create_user_new_email(superuser_token_headers):
4949

5050
def test_get_existing_user(superuser_token_headers):
5151
server_api = get_server_api()
52-
username = random_lower_string()
52+
username = random_email()
5353
password = random_lower_string()
5454
user_in = UserCreate(email=username, password=password)
5555
user = crud.user.create(db_session, obj_in=user_in)
@@ -66,7 +66,7 @@ def test_get_existing_user(superuser_token_headers):
6666

6767
def test_create_user_existing_username(superuser_token_headers):
6868
server_api = get_server_api()
69-
username = random_lower_string()
69+
username = random_email()
7070
# username = email
7171
password = random_lower_string()
7272
user_in = UserCreate(email=username, password=password)
@@ -84,7 +84,7 @@ def test_create_user_existing_username(superuser_token_headers):
8484

8585
def test_create_user_by_normal_user(normal_user_token_headers):
8686
server_api = get_server_api()
87-
username = random_lower_string()
87+
username = random_email()
8888
password = random_lower_string()
8989
data = {"email": username, "password": password}
9090
r = requests.post(
@@ -97,12 +97,12 @@ def test_create_user_by_normal_user(normal_user_token_headers):
9797

9898
def test_retrieve_users(superuser_token_headers):
9999
server_api = get_server_api()
100-
username = random_lower_string()
100+
username = random_email()
101101
password = random_lower_string()
102102
user_in = UserCreate(email=username, password=password)
103103
user = crud.user.create(db_session, obj_in=user_in)
104104

105-
username2 = random_lower_string()
105+
username2 = random_email()
106106
password2 = random_lower_string()
107107
user_in2 = UserCreate(email=username2, password=password2)
108108
crud.user.create(db_session, obj_in=user_in2)

{{cookiecutter.project_slug}}/backend/app/app/tests/crud/test_user.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from app import crud
44
from app.db.session import db_session
55
from app.schemas.user import UserCreate
6-
from app.tests.utils.utils import random_lower_string
6+
from app.tests.utils.utils import random_lower_string, random_email
77

88

99
def test_create_user():
10-
email = random_lower_string()
10+
email = random_email()
1111
password = random_lower_string()
1212
user_in = UserCreate(email=email, password=password)
1313
user = crud.user.create(db_session, obj_in=user_in)
@@ -16,7 +16,7 @@ def test_create_user():
1616

1717

1818
def test_authenticate_user():
19-
email = random_lower_string()
19+
email = random_email()
2020
password = random_lower_string()
2121
user_in = UserCreate(email=email, password=password)
2222
user = crud.user.create(db_session, obj_in=user_in)
@@ -28,14 +28,14 @@ def test_authenticate_user():
2828

2929

3030
def test_not_authenticate_user():
31-
email = random_lower_string()
31+
email = random_email()
3232
password = random_lower_string()
3333
user = crud.user.authenticate(db_session, email=email, password=password)
3434
assert user is None
3535

3636

3737
def test_check_if_user_is_active():
38-
email = random_lower_string()
38+
email = random_email()
3939
password = random_lower_string()
4040
user_in = UserCreate(email=email, password=password)
4141
user = crud.user.create(db_session, obj_in=user_in)
@@ -44,7 +44,7 @@ def test_check_if_user_is_active():
4444

4545

4646
def test_check_if_user_is_active_inactive():
47-
email = random_lower_string()
47+
email = random_email()
4848
password = random_lower_string()
4949
user_in = UserCreate(email=email, password=password, disabled=True)
5050
user = crud.user.create(db_session, obj_in=user_in)
@@ -53,7 +53,7 @@ def test_check_if_user_is_active_inactive():
5353

5454

5555
def test_check_if_user_is_superuser():
56-
email = random_lower_string()
56+
email = random_email()
5757
password = random_lower_string()
5858
user_in = UserCreate(email=email, password=password, is_superuser=True)
5959
user = crud.user.create(db_session, obj_in=user_in)
@@ -62,7 +62,7 @@ def test_check_if_user_is_superuser():
6262

6363

6464
def test_check_if_user_is_superuser_normal_user():
65-
username = random_lower_string()
65+
username = random_email()
6666
password = random_lower_string()
6767
user_in = UserCreate(email=username, password=password)
6868
user = crud.user.create(db_session, obj_in=user_in)
@@ -72,7 +72,7 @@ def test_check_if_user_is_superuser_normal_user():
7272

7373
def test_get_user():
7474
password = random_lower_string()
75-
username = random_lower_string()
75+
username = random_email()
7676
user_in = UserCreate(email=username, password=password, is_superuser=True)
7777
user = crud.user.create(db_session, obj_in=user_in)
7878
user_2 = crud.user.get(db_session, id=user.id)

{{cookiecutter.project_slug}}/backend/app/app/tests/utils/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from app.core import config
55
from app.db.session import db_session
66
from app.schemas.user import UserCreate, UserUpdate
7-
from app.tests.utils.utils import get_server_api, random_lower_string
7+
from app.tests.utils.utils import get_server_api, random_lower_string, random_email
88

99

1010
def user_authentication_headers(server_api, email, password):
@@ -18,7 +18,7 @@ def user_authentication_headers(server_api, email, password):
1818

1919

2020
def create_random_user():
21-
email = random_lower_string()
21+
email = random_email()
2222
password = random_lower_string()
2323
user_in = UserCreate(username=email, email=email, password=password)
2424
user = crud.user.create(db_session=db_session, obj_in=user_in)

{{cookiecutter.project_slug}}/backend/app/app/tests/utils/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import string
33

44
import requests
5-
65
from app.core import config
76

87

98
def random_lower_string():
109
return "".join(random.choices(string.ascii_lowercase, k=32))
1110

1211

12+
def random_email():
13+
return f"{random_lower_string()}@{random_lower_string()}.com"
14+
15+
1316
def get_server_api():
1417
server_name = f"http://{config.SERVER_NAME}"
1518
return server_name

{{cookiecutter.project_slug}}/backend/backend.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
22

3-
RUN pip install celery~=4.3 passlib[bcrypt] tenacity requests emails "fastapi>=0.47.0" "uvicorn>=0.11.1" gunicorn pyjwt python-multipart email_validator jinja2 psycopg2-binary alembic SQLAlchemy
3+
RUN pip install celery~=4.3 passlib[bcrypt] tenacity requests emails "fastapi>=0.47.0" "uvicorn>=0.11.1" gunicorn pyjwt python-multipart email_validator jinja2 psycopg2-binary alembic SQLAlchemy email_validator
44

55
# For development, Jupyter remote kernel, Hydrogen
66
# Using inside the container:

{{cookiecutter.project_slug}}/backend/celeryworker.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM python:3.7
22

3-
RUN pip install raven celery~=4.3 passlib[bcrypt] tenacity requests "fastapi>=0.16.0" emails pyjwt email_validator jinja2 psycopg2-binary alembic SQLAlchemy
3+
RUN pip install raven celery~=4.3 passlib[bcrypt] tenacity requests "fastapi>=0.16.0" emails pyjwt email_validator jinja2 psycopg2-binary alembic SQLAlchemy email_validator
44

55
# For development, Jupyter remote kernel, Hydrogen
66
# Using inside the container:

{{cookiecutter.project_slug}}/backend/tests.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM python:3.7
22

3-
RUN pip install requests pytest tenacity passlib[bcrypt] "fastapi>=0.16.0" psycopg2-binary SQLAlchemy
3+
RUN pip install requests pytest tenacity passlib[bcrypt] "fastapi>=0.16.0" psycopg2-binary SQLAlchemy email_validator
44

55
# For development, Jupyter remote kernel, Hydrogen
66
# Using inside the container:

0 commit comments

Comments
 (0)