Skip to content

Feature/auth 002#4

Open
AlexandrSmolyachkovGH wants to merge 11 commits intodevfrom
feature/AUTH-002
Open

Feature/auth 002#4
AlexandrSmolyachkovGH wants to merge 11 commits intodevfrom
feature/AUTH-002

Conversation

@AlexandrSmolyachkovGH
Copy link
Copy Markdown
Owner

No description provided.

Copy link
Copy Markdown
Collaborator

@mitrandir-pl mitrandir-pl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Всё надо повыносить в фикстуры. К тестам надо добавить коменты, неясно что вообще тестируется в некоторых.

Comment thread tests/services/test_ses.py Outdated
Comment on lines +16 to +23
def test_generate_email_payload() -> None:
email_payload = ses_handler.generate_email_payload(
message="Test message",
subject="Test subject"
)
assert isinstance(email_payload, EmailPayloadScheme)
assert isinstance(email_payload.message, str)
assert isinstance(email_payload.subject, str)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

что проверяет этот тест? ты по сути проверил что "Test message" и "Test subject" это строки. Чего ради?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def generate_email_payload(
self,
message: str,
subject: str,
source: str | None = None,
) -> EmailPayloadScheme:
payload = {
'message': message,
'subject': subject,
}
if source:
payload['source'] = source
return EmailPayloadScheme(**payload) функция просто генерирует пейлоад для сообщения, она отдает EmailPayloadScheme где есть поля релевантные тем, что проверялись в тесте, т.е. мне казалось, что нужно проверить: а) создание ответа, б) поля в ответе, но так как EmailPayloadScheme - пайдантик модель - пункт б оверхед. Получается, достаточно оставить что на выходе у меня генерится результат инстанс от модели.

Comment thread tests/services/test_ses.py Outdated
Comment on lines +34 to +43
with patch.object(ses_handler, "generate_email_payload") as mock_gen_payload, \
patch.object(ses_handler, "send_email", new_callable=AsyncMock) as mock_send_email, \
patch.object(ses_handler, "generate_otp") as mock_generate_otp:
mock_send_email.return_value = None
mock_generate_otp.return_value = "TestOTP123"
mock_gen_payload.return_value = EmailPayloadScheme(
message="Test message",
subject="Test subject",
source="sender@example.com",
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ты замокал кучу объектов. Если будут добавляться новые тесты, придётся в них повторять этот код, и так множество раз. Такое надо выносить в фикстуры

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

понял, поправил

Comment thread tests/services/test_ses.py Outdated
Comment on lines +54 to +63
with patch.object(ses_handler, "generate_otp") as mock_generate_otp, \
patch.object(ses_handler, "send_email", new_callable=AsyncMock) as mock_send_email, \
patch.object(msg_creator, "get_ses_confirmation_message") as mock_get_ses_message, \
patch.object(redis_client, "set", new_callable=AsyncMock) as mock_set_redis:
mock_generate_otp.return_value = "TestOTP123"
mock_get_ses_message.return_value = {
"message": "test message",
"subject": "test subject",
"response_message": "test response",
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

аналогично, такое надо в фикстуры выносить

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил

Comment thread tests/services/test_tokens.py Outdated
Comment on lines +22 to +27
auth_data = AuthUserScheme(
**{
"email": "example@email.com",
"password_hash": "examplePassword123!",
}
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут вообще не понимаю в чём прикол? Зачем так объект класса создавать ??

Suggested change
auth_data = AuthUserScheme(
**{
"email": "example@email.com",
"password_hash": "examplePassword123!",
}
)
auth_data = AuthUserScheme(
email="example@email.com",
password_hash="examplePassword123!",
)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

согласен, не вспомню уже причину такого оформления)

Comment thread tests/services/test_tokens.py Outdated
}
)
refresh_orm = RefreshTokenORM(
id=UUID("cfe82c6f-7d8c-4172-bc33-04a24a065c20"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
id=UUID("cfe82c6f-7d8c-4172-bc33-04a24a065c20"),
id=uuid.uuid4(),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

окэй

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему ты в этом файле делаешь какие-то объекты для тестов. Это же всё надо было делать в фикстурах

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В тестах нет аннотаций типов, надо добавить

Comment thread tests/services/test_tokens.py Outdated
Comment on lines +117 to +121
return_value=token_data,
)
mock_dependencies["token_repo"].update_refresh.return_value = refresh_orm
token_service = TokenService(**mock_dependencies)
res = await token_service.exchange_refresh_token(token_data=token_data)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты получились крайне непонятными, что за токен дата, откуда она взялась. Аннотаций типов нет, коментов нет.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/services/test_users.py Outdated
"auth_app.services.users.msg_creator.get_code_message",
return_value="Code sent",
)
user_service = UserService(**mock_dependencies)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

опять-таки, ты в каждом тесте этот сервис создаёшь, почему тогда не вынести это в фикстуру?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +19 to +21
def test_hash_password() -> None:
hashed_password = hash_password(pwd)
assert isinstance(hashed_password, str)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нет никакого смысла проверять тип, проверять надо логику

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут сложно что-то глубоко проверить тк на базе либы хеширую, т.е. я базово чекнул что результат хеширования нужный мне тип и добавил верификацию в данный тест

Comment on lines +14 to +16
email = "email@example.com"
pwd = "12345Password!"
hashed_pwd = hash_password(pwd)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему это не фикстуры

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread auth_app/services/tokens.py Outdated
Comment on lines +35 to +36
self.__redis = redis # pylint: disable=W0238
self.__ses = ses # pylint: disable=W0238
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем тебе поля которые ты не используешь?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему-то думал, что буду их тоже юзать, поэтому делал заглушки. сейчас убрал.

Comment thread tests/conftest.py Outdated
Comment on lines +74 to +92
@pytest.fixture
def mock_dependencies() -> dict:
return {
"user_repo": Mock(
create_user=AsyncMock(),
get_user=AsyncMock(),
update_user=AsyncMock(),
get_users=AsyncMock(),
),
"token_repo": Mock(
create_refresh=AsyncMock(),
get_refresh=AsyncMock(),
update_refresh=AsyncMock(),
),
"redis": Mock(),
"ses": Mock(

),
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

видимо забыл удалить, но старайся перепроверять такие штуки

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

точно, сори, поправил

Comment thread tests/conftest.py
Comment on lines +176 to +178
@pytest.fixture(scope="session")
def user_id() -> UUID:
return uuid4()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я не вижу ни одного теста для которого это было бы необходимо. Можно просто обойтись uuid4()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

идея была в том, чтобы просто 1 раз создать uuid и переиспользовать его, как это делаю с имейлом, паролем и другими данными для юзера. пусть будет так в этом сервисе, если ты не против, в других учту момент

Comment thread tests/services/test_ses.py Outdated
Comment on lines +44 to +45
assert email_payload.message == "Test message"
assert email_payload.subject == "Test subject"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

непонятно откуда данные вылазят
правильнее было бы так

Suggested change
assert email_payload.message == "Test message"
assert email_payload.subject == "Test subject"
assert email_payload.message == generate_email_payload_data["message"]
assert email_payload.subject == generate_email_payload_data["subject"]

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

полностью поддерживаю, проглядел

Comment thread tests/services/test_ses.py Outdated


def test_generate_email_payload(
generate_email_payload_data,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему нет аннотаций типов

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

все поправил

Comment thread tests/services/test_ses.py Outdated
Comment on lines +60 to +61
send_email_patch,
get_user_data,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аннотации типов, и так везде

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

все поправил

Comment on lines +121 to +123
role_auth_data = RoleDataScheme(
**auth_user_data_mock.model_dump(),
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здесь точно надо так создавать объект? а то опять через словарь и нифига неясно какие там поля

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в этом, мне кажется, есть логика, поясню:
RoleDataScheme - расширенная схема AuthUserScheme (где кроме имейла и пароля есть ролевые данные),
но они не являются обязательными и имеют дефолты, поэтому я от базового класса (он уже мокнут) делаю дамп для дикта и просто прокидываю данные необходимые, а остальные - дефолт тянут

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я ничего не понял. Переделай под нормальное создание. Явность всегда важнее чем то что ты описываешь. А здесь нифига не явно. Плюс, меняется схема -> ломаются такие создания объектов. А что там сломалось - непонятно, так как создание неявное

Comment on lines +7 to +37
env:
#POSTGRES
POSTGRES_DB: default_db
POSTGRES_USER: default_user
POSTGRES_PASSWORD: default_password
POSTGRES_HOST: localhost
POSTGRES_PORT: '5438'
# REDIS
REDIS_PASSWORD: default_password
REDIS_PORT: 6380
REDIS_HOST: localhost
# PASSWORD HASHING
HASHING_ALGORITHM: bcrypt
HASHING_DEPRECATED: auto
# JWT SECRET KEY
KEY: secret_key
ALGORITHM: HS256
REFRESH_LASTING: 3000
ACCESS_LASTING: 300
ADMIN_SECRET: default_secret
# LOCALSTACK
SERVICES: ses,s3
AWS_DEFAULT_REGION: us-east-1
LOCALSTACK_HOST: localstack
DEBUG: 1
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test
# LOCAL AWS
AWS_ENDPOINT: http://localhost:4566
RESET_PWD_LENGTH: 10
VERIFICATION_CODE_LENGTH: 5
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему переменные энва прямо в файле хранятся

Comment on lines +64 to +69
- name: Install requirements
run: |
python -m pip install --upgrade pip
pip install poetry
poetry config virtualenvs.create false
poetry install
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

надо ещё покрытие проверять, если < 80, то пайплайн должен падать

Comment on lines +121 to +123
role_auth_data = RoleDataScheme(
**auth_user_data_mock.model_dump(),
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я ничего не понял. Переделай под нормальное создание. Явность всегда важнее чем то что ты описываешь. А здесь нифига не явно. Плюс, меняется схема -> ломаются такие создания объектов. А что там сломалось - непонятно, так как создание неявное

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants