Skip to content

Commit

Permalink
change names to uppercase
Browse files Browse the repository at this point in the history
  • Loading branch information
FireFading committed Apr 14, 2023
1 parent 45922fe commit 4fa8d95
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 100 deletions.
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from tests.settings import SQLALCHEMY_DATABASE_URL, test_product, test_user, urls
from tests.settings import SQLALCHEMY_DATABASE_URL, Urls, User, test_product

engine = create_async_engine(
SQLALCHEMY_DATABASE_URL,
Expand Down Expand Up @@ -53,13 +53,13 @@ async def _get_test_db():
@pytest_asyncio.fixture
async def register_user(client: AsyncGenerator | TestClient, mocker: MockerFixture) -> AsyncGenerator:
mocker.patch("app.routers.users.send_mail", return_value=True)
response = client.post(urls.register, json={"email": test_user.email, "password": test_user.password})
response = client.post(Urls.REGISTER, json={"email": User.EMAIL, "password": User.PASSWORD})
assert response.status_code == status.HTTP_201_CREATED


@pytest_asyncio.fixture
async def auth_client(register_user, client: AsyncGenerator | TestClient) -> AsyncGenerator | TestClient:
response = client.post(urls.login, json={"email": test_user.email, "password": test_user.password})
response = client.post(Urls.LOGIN, json={"email": User.EMAIL, "password": User.PASSWORD})
assert response.status_code == status.HTTP_200_OK
access_token = response.json().get("access_token")
client.headers.update({"Authorization": f"Bearer {access_token}"})
Expand All @@ -70,5 +70,5 @@ async def auth_client(register_user, client: AsyncGenerator | TestClient) -> Asy
async def create_product(
auth_client: AsyncGenerator | TestClient,
) -> AsyncGenerator | TestClient:
response = auth_client.post(urls.create_product, json=test_product)
response = auth_client.post(Urls.CREATE_PRODUCT, json=test_product)
assert response.status_code == status.HTTP_201_CREATED
69 changes: 35 additions & 34 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from datetime import datetime

import jwt
Expand All @@ -10,52 +11,57 @@
SQLALCHEMY_DATABASE_URL = "sqlite+aiosqlite://"


@dataclass
class Urls:
login = "/accounts/login/"
register = "/accounts/register/"
logout = "/accounts/logout/"
user_info = "/accounts/profile/"
LOGIN = "/accounts/login/"
REGISTER = "/accounts/register/"
LOGOUT = "/accounts/logout/"
USER_INFO = "/accounts/profile/"

update_email = "/accounts/profile/update/email/"
update_phone = "/accounts/profile/update/phone/"
update_name = "/accounts/profile/update/name/"
UPDATE_EMAIL = "/accounts/profile/update/email/"
UPDATE_PHONE = "/accounts/profile/update/phone/"
UPDATE_NAME = "/accounts/profile/update/name/"

forgot_password = "/accounts/forgot-password/"
reset_password = "/accounts/reset-password/"
change_password = "/accounts/change-password/"
FORGOT_PASSWORD = "/accounts/forgot-password/"
RESET_PASSWORD = "/accounts/reset-password/"
CHANGE_PASSWORD = "/accounts/change-password/"

delete_profile = "/accounts/profile/delete/"
DELETE_PROFILE = "/accounts/profile/delete/"

create_product = "/products/new/"
get_products = "/products/get/"
delete_product = "/products/delete/"
CREATE_PRODUCT = "/products/new/"
GET_PRODUCTS = "/products/get/"
DELETE_PRODUCT = "/products/delete/"

create_rating = "/products/ratings/new/"
get_ratings = "/products/ratings/get/"
CREATE_RATING = "/products/ratings/new/"
GET_RATINGS = "/products/ratings/get/"


class TestUser:
email = "test@mail.ru"
new_email = "new_test@mail.ru"
wrong_email = "wrong_test@mail.ru"
@dataclass
class User:
EMAIL = "test@mail.ru"
NEW_EMAIL = "new_test@mail.ru"
WRONG_EMAIL = "wrong_test@mail.ru"

phone = None
new_phone = "89101111111"
PHONE = None
NEW_PHONE = "89101111111"

name = None
new_name = "UserName"
NAME = None
NEW_NAME = "UserName"

password = "Abc123!@#def456$%^"
new_password = "NewAbc123!@#def456$%^"
wrong_password = "WrongAbc123!@#def456$%^"
PASSWORD = "Abc123!@#def456$%^"
NEW_PASSWORD = "NewAbc123!@#def456$%^"
WRONG_PASSWORD = "WrongAbc123!@#def456$%^"


class BaseTestSettings(Settings):
database_url: str = Field(env="TEST_DATABASE_URL")


urls = Urls()
test_user = TestUser()
def create_fake_token(expires_in: datetime = datetime(1999, 1, 1), email: str = User.EMAIL) -> str:
to_encode = {"exp": expires_in, "email": email, "is_active": True}
return jwt.encode(to_encode, settings.secret_key, settings.algorithm)


settings = BaseTestSettings(_env_file=".env.example")


Expand All @@ -67,8 +73,3 @@ class BaseTestSettings(Settings):
}

rating = {"stars": 2, "product_id": "00000000-0000-0000-0000-000000000000"}


def create_fake_token(expires_in: datetime = datetime(1999, 1, 1), email: str = test_user.email) -> str:
to_encode = {"exp": expires_in, "email": email, "is_active": True}
return jwt.encode(to_encode, settings.secret_key, settings.algorithm)
74 changes: 37 additions & 37 deletions tests/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,79 @@
from app.utils.tokens import create_token
from fastapi import status
from pytest_mock import MockerFixture
from tests.settings import create_fake_token, test_user, urls
from tests.settings import Urls, User, create_fake_token


class TestRegister:
async def test_register_user(self, client, mocker: MockerFixture):
mocker.patch("app.routers.users.send_mail", return_value=True)
response = client.post(
urls.register,
json={"email": test_user.email, "password": test_user.password},
Urls.REGISTER,
json={"email": User.EMAIL, "password": User.PASSWORD},
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json().get("email") == test_user.email
assert response.json().get("email") == User.EMAIL
assert response.json().get("detail") == messages.CONFIRM_REGISTRATION_MAIL_SENT

async def test_failed_repeat_register_user(self, register_user, client):
response = client.post(
urls.register,
json={"email": test_user.email, "password": test_user.password},
Urls.REGISTER,
json={"email": User.EMAIL, "password": User.PASSWORD},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json().get("detail") == messages.USER_ALREADY_EXISTS

async def test_login_unregistered_user(self, client):
response = client.post(urls.login, json={"email": test_user.email, "password": test_user.password})
response = client.post(Urls.LOGIN, json={"email": User.EMAIL, "password": User.PASSWORD})
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json().get("detail") == messages.USER_NOT_FOUND


class TestLogin:
async def test_login_user(self, register_user, client):
response = client.post(urls.login, json={"email": test_user.email, "password": test_user.password})
response = client.post(Urls.LOGIN, json={"email": User.EMAIL, "password": User.PASSWORD})
assert response.status_code == status.HTTP_200_OK
assert "access_token" in response.json()
assert "refresh_token" in response.json()

async def test_wrong_password_login(self, register_user, client):
response = client.post(
urls.login,
json={"email": test_user.email, "password": test_user.wrong_password},
Urls.LOGIN,
json={"email": User.EMAIL, "password": User.WRONG_PASSWORD},
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
assert response.json().get("detail") == messages.WRONG_PASSWORD


class TestLogout:
async def test_logout_user(self, auth_client):
response = auth_client.delete(urls.logout)
response = auth_client.delete(Urls.LOGOUT)
assert response.status_code == status.HTTP_200_OK
assert response.json().get("detail") == messages.USER_LOGOUT


class TestForgotPassword:
async def test_user_forgot_password(self, register_user, client, mocker: MockerFixture):
mocker.patch("app.routers.users.send_mail", return_value=True)
response = client.post(urls.forgot_password, json={"email": test_user.email})
response = client.post(Urls.FORGOT_PASSWORD, json={"email": User.EMAIL})
assert response.status_code == status.HTTP_202_ACCEPTED
assert response.json().get("detail") == messages.RESET_PASSWORD_MAIL_SENT

async def test_unregistered_user_forgot_password(self, client, mocker: MockerFixture):
mocker.patch("app.routers.users.send_mail", return_value=True)
response = client.post(urls.forgot_password, json={"email": test_user.email})
response = client.post(Urls.FORGOT_PASSWORD, json={"email": User.EMAIL})
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json().get("detail") == messages.USER_NOT_FOUND


class TestResetPassword:
async def test_user_reset_password(self, register_user, client):
reset_password_token = create_token(email=test_user.email)
reset_password_token = create_token(email=User.EMAIL)
response = client.post(
f"{urls.reset_password}{reset_password_token}",
f"{Urls.RESET_PASSWORD}{reset_password_token}",
json={
"password": test_user.new_password,
"confirm_password": test_user.new_password,
"password": User.NEW_PASSWORD,
"confirm_password": User.NEW_PASSWORD,
},
)
assert response.status_code == status.HTTP_202_ACCEPTED
Expand All @@ -83,34 +83,34 @@ async def test_user_reset_password(self, register_user, client):
async def test_user_reset_password_with_invalid_token(self, register_user, client):
reset_password_token = create_fake_token()
response = client.post(
f"{urls.reset_password}{reset_password_token}",
f"{Urls.RESET_PASSWORD}{reset_password_token}",
json={
"password": test_user.new_password,
"confirm_password": test_user.new_password,
"password": User.NEW_PASSWORD,
"confirm_password": User.NEW_PASSWORD,
},
)
assert response.status_code == status.HTTP_203_NON_AUTHORITATIVE_INFORMATION
assert response.json().get("detail") == messages.INVALID_TOKEN

async def test_unregistered_user_reset_password(self, client):
reset_password_token = create_token(email=test_user.email)
reset_password_token = create_token(email=User.EMAIL)
response = client.post(
f"{urls.reset_password}{reset_password_token}",
f"{Urls.RESET_PASSWORD}{reset_password_token}",
json={
"password": test_user.new_password,
"confirm_password": test_user.new_password,
"password": User.NEW_PASSWORD,
"confirm_password": User.NEW_PASSWORD,
},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json().get("detail") == messages.USER_NOT_FOUND

async def test_user_reset_password_not_match_password(self, register_user, client):
reset_password_token = create_token(email=test_user.email)
reset_password_token = create_token(email=User.EMAIL)
response = client.post(
f"{urls.reset_password}{reset_password_token}",
f"{Urls.RESET_PASSWORD}{reset_password_token}",
json={
"password": test_user.new_password,
"confirm_password": test_user.wrong_password,
"password": User.NEW_PASSWORD,
"confirm_password": User.WRONG_PASSWORD,
},
)
assert response.status_code == status.HTTP_203_NON_AUTHORITATIVE_INFORMATION
Expand All @@ -120,32 +120,32 @@ async def test_user_reset_password_not_match_password(self, register_user, clien
class TestChangePassword:
async def test_user_change_password(self, auth_client):
response = auth_client.post(
urls.change_password,
Urls.CHANGE_PASSWORD,
json={
"password": test_user.new_password,
"confirm_password": test_user.new_password,
"password": User.NEW_PASSWORD,
"confirm_password": User.NEW_PASSWORD,
},
)
assert response.status_code == status.HTTP_202_ACCEPTED
assert response.json().get("detail") == messages.PASSWORD_UPDATED

async def test_user_change_password_not_match(self, auth_client):
response = auth_client.post(
urls.change_password,
Urls.CHANGE_PASSWORD,
json={
"password": test_user.new_password,
"confirm_password": test_user.wrong_password,
"password": User.NEW_PASSWORD,
"confirm_password": User.WRONG_PASSWORD,
},
)
assert response.status_code == status.HTTP_203_NON_AUTHORITATIVE_INFORMATION
assert response.json().get("detail") == messages.PASSWORDS_NOT_MATCH

async def test_user_change_password_to_old(self, auth_client):
response = auth_client.post(
urls.change_password,
Urls.CHANGE_PASSWORD,
json={
"password": test_user.password,
"confirm_password": test_user.password,
"password": User.PASSWORD,
"confirm_password": User.PASSWORD,
},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
Expand Down
12 changes: 6 additions & 6 deletions tests/test_products.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from app.utils.messages import messages
from fastapi import status
from tests.settings import test_product, urls
from tests.settings import Urls, test_product


class TestProducts:
async def test_not_available_without_auth(self, client):
response = client.post(urls.create_product, json=test_product)
response = client.post(Urls.CREATE_PRODUCT, json=test_product)
assert response.status_code == status.HTTP_403_FORBIDDEN

async def test_create_product(self, auth_client):
response = auth_client.post(urls.create_product, json=test_product)
response = auth_client.post(Urls.CREATE_PRODUCT, json=test_product)
assert response.status_code == status.HTTP_201_CREATED
assert response.json().get("detail") == messages.PRODUCT_CREATED

response = auth_client.get(urls.get_products)
response = auth_client.get(Urls.GET_PRODUCTS)
assert response.status_code == status.HTTP_200_OK
result = response.json()[0]
assert result.get("name") == test_product.get("name")
Expand All @@ -22,9 +22,9 @@ async def test_create_product(self, auth_client):
assert result.get("price") == test_product.get("price")

async def test_delete_product(self, create_product, auth_client):
response = auth_client.get(urls.get_products)
response = auth_client.get(Urls.GET_PRODUCTS)
assert response.status_code == status.HTTP_200_OK
product_id = (response.json()[0]).get("guid")
response = auth_client.delete(f"{urls.delete_product}{product_id}")
response = auth_client.delete(f"{Urls.DELETE_PRODUCT}{product_id}")
assert response.status_code == status.HTTP_200_OK
assert response.json().get("detail") == messages.PRODUCT_DELETED
Loading

0 comments on commit 4fa8d95

Please sign in to comment.