feat: add E2E tests for catalog and user endpoints based on HAR analysis

This commit is contained in:
root 2026-03-29 22:28:18 +00:00
parent ab967ebe41
commit ecf51bef54
2 changed files with 172 additions and 22 deletions

View File

@ -2,22 +2,35 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"id": "f28552f1-618c-4853-92e2-566554a2de2c", "id": "f28552f1-618c-4853-92e2-566554a2de2c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"import logging\n", "import asyncio\n",
"import os\n",
"from kwork_api import KworkClient\n", "from kwork_api import KworkClient\n",
"from dotenv import load_dotenv\n",
"import os\n",
"\n", "\n",
"# Включить логирование\n", "logging.basicConfig(level=logging.DEBUG) # или INFO для меньшего шума\n",
"logging.basicConfig(level=logging.DEBUG) # или INFO для меньшего шума\n" "\n",
"load_dotenv('tests/e2e/.env')"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"id": "953d142e-a575-41b7-927d-8cd1546d2747", "id": "953d142e-a575-41b7-927d-8cd1546d2747",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -25,28 +38,28 @@
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"INFO:kwork_api.client:Login request: POST https://kwork.ru/api/user/login (user: None)\n", "INFO:kwork_api.client:Login request: POST https://kwork.ru/api/user/login (user: JTJagOmega)\n",
"DEBUG:kwork_api.client:Login payload: {'l_username': None, 'l_password': None, 'jlog': 1, 'recaptcha_pass_token': '', 'track_client_id': False, 'smart-token': '', 'l_remember_me': '1'}\n", "DEBUG:kwork_api.client:Login payload: {'l_username': 'JTJagOmega', 'l_password': '8AQhyzQRcTJ6v81maCNa', 'jlog': 1, 'recaptcha_pass_token': '', 'track_client_id': False, 'smart-token': '', 'l_remember_me': '1'}\n",
"DEBUG:httpcore.connection:connect_tcp.started host='kwork.ru' port=443 local_address=None timeout=30.0 socket_options=None\n", "DEBUG:httpcore.connection:connect_tcp.started host='kwork.ru' port=443 local_address=None timeout=30.0 socket_options=None\n",
"DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x7ed01d0f09b0>\n", "DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x723917c9fa70>\n",
"DEBUG:httpcore.connection:start_tls.started ssl_context=<ssl.SSLContext object at 0x7ed01ce32150> server_hostname='kwork.ru' timeout=30.0\n", "DEBUG:httpcore.connection:start_tls.started ssl_context=<ssl.SSLContext object at 0x723917df30d0> server_hostname='kwork.ru' timeout=30.0\n",
"DEBUG:httpcore.connection:start_tls.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x7ed03c1122d0>\n", "DEBUG:httpcore.connection:start_tls.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x723924105d30>\n",
"DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'POST']>\n", "DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'POST']>\n",
"DEBUG:httpcore.http11:send_request_headers.complete\n", "DEBUG:httpcore.http11:send_request_headers.complete\n",
"DEBUG:httpcore.http11:send_request_body.started request=<Request [b'POST']>\n", "DEBUG:httpcore.http11:send_request_body.started request=<Request [b'POST']>\n",
"DEBUG:httpcore.http11:send_request_body.complete\n", "DEBUG:httpcore.http11:send_request_body.complete\n",
"DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'POST']>\n", "DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'POST']>\n",
"DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Server', b'QRATOR'), (b'Date', b'Sun, 29 Mar 2026 22:14:13 GMT'), (b'Content-Type', b'application/json'), (b'Transfer-Encoding', b'chunked'), (b'Connection', b'keep-alive'), (b'Keep-Alive', b'timeout=15'), (b'Vary', b'Accept-Encoding, User-Agent'), (b'Content-Security-Policy', b\"frame-ancestors 'self' https://webvisor.com https://awards.ratingruneta.ru\"), (b'Set-Cookie', b'referrer_url=https%3A%2F%2Fkwork.ru%2F; expires=Sun, 05-Apr-2026 22:14:13 GMT; Max-Age=604800; path=/; secure; HttpOnly; SameSite=None'), (b'Cache-Control', b'no-cache, private'), (b'Strict-Transport-Security', b'max-age=15552000'), (b'X-Content-Type-Options', b'nosniff'), (b'Content-Encoding', b'gzip')])\n", "DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Server', b'QRATOR'), (b'Date', b'Sun, 29 Mar 2026 22:22:41 GMT'), (b'Content-Type', b'application/json'), (b'Transfer-Encoding', b'chunked'), (b'Connection', b'keep-alive'), (b'Keep-Alive', b'timeout=15'), (b'Vary', b'Accept-Encoding, User-Agent'), (b'Content-Security-Policy', b\"frame-ancestors 'self' https://webvisor.com https://awards.ratingruneta.ru\"), (b'Set-Cookie', b'referrer_url=https%3A%2F%2Fkwork.ru%2F; expires=Sun, 05-Apr-2026 22:22:41 GMT; Max-Age=604800; path=/; secure; HttpOnly; SameSite=None'), (b'Set-Cookie', b'uad=1884597369c9a63194ed0624319983; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None'), (b'Set-Cookie', b'RORSSQIHEK=f15239f2927f4fd08e6945c15ed635c2; expires=Wed, 01-Apr-2026 22:22:41 GMT; Max-Age=259200; path=/; secure; HttpOnly; SameSite=None'), (b'Expires', b'Thu, 19 Nov 1981 08:52:00 GMT'), (b'Cache-Control', b'no-store, no-cache, must-revalidate'), (b'Pragma', b'no-cache'), (b'Set-Cookie', b'csrf_user_token=43ed1b44d6a5a480418b39929da62605; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None'), (b'Set-Cookie', b'userId=18845973; expires=Wed, 26-Mar-2036 22:22:41 GMT; Max-Age=315360000; path=/; secure; SameSite=None'), (b'Set-Cookie', b'slrememberme=18845973_%242y%2410%24GEnC83HAU.ejn2CQB3OMTewWzSYxC0NYcSB3n2ck6eNvcz2aStK0W; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None'), (b'Set-Cookie', b'_kmid=7fb7f3a407728e8d0ffa5ab4d19ff2b6; expires=Wed, 26-Mar-2036 22:22:41 GMT; Max-Age=315360000; path=/; secure; HttpOnly; SameSite=None'), (b'Set-Cookie', b'_kmfvt=1774822961; expires=Wed, 26-Mar-2036 22:22:41 GMT; Max-Age=315360000; path=/; secure; HttpOnly; SameSite=None'), (b'Set-Cookie', b'csrf_user_token=515cb2f621700da0faf4c3da66efbbfb; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None'), (b'Cache-Control', b'no-cache, private'), (b'Strict-Transport-Security', b'max-age=15552000'), (b'X-Content-Type-Options', b'nosniff'), (b'Content-Encoding', b'gzip')])\n",
"INFO:httpx:HTTP Request: POST https://kwork.ru/api/user/login \"HTTP/1.1 200 OK\"\n", "INFO:httpx:HTTP Request: POST https://kwork.ru/api/user/login \"HTTP/1.1 200 OK\"\n",
"DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'POST']>\n", "DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'POST']>\n",
"DEBUG:httpcore.http11:receive_response_body.complete\n", "DEBUG:httpcore.http11:receive_response_body.complete\n",
"DEBUG:httpcore.http11:response_closed.started\n", "DEBUG:httpcore.http11:response_closed.started\n",
"DEBUG:httpcore.http11:response_closed.complete\n", "DEBUG:httpcore.http11:response_closed.complete\n",
"DEBUG:kwork_api.client:Login response status: 200\n", "DEBUG:kwork_api.client:Login response status: 200\n",
"DEBUG:kwork_api.client:Login response headers: {'server': 'QRATOR', 'date': 'Sun, 29 Mar 2026 22:14:13 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', 'connection': 'keep-alive', 'keep-alive': 'timeout=15', 'vary': 'Accept-Encoding, User-Agent', 'content-security-policy': \"frame-ancestors 'self' https://webvisor.com https://awards.ratingruneta.ru\", 'set-cookie': 'referrer_url=https%3A%2F%2Fkwork.ru%2F; expires=Sun, 05-Apr-2026 22:14:13 GMT; Max-Age=604800; path=/; secure; HttpOnly; SameSite=None', 'cache-control': 'no-cache, private', 'strict-transport-security': 'max-age=15552000', 'x-content-type-options': 'nosniff', 'content-encoding': 'gzip'}\n", "DEBUG:kwork_api.client:Login response headers: {'server': 'QRATOR', 'date': 'Sun, 29 Mar 2026 22:22:41 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', 'connection': 'keep-alive', 'keep-alive': 'timeout=15', 'vary': 'Accept-Encoding, User-Agent', 'content-security-policy': \"frame-ancestors 'self' https://webvisor.com https://awards.ratingruneta.ru\", 'set-cookie': 'referrer_url=https%3A%2F%2Fkwork.ru%2F; expires=Sun, 05-Apr-2026 22:22:41 GMT; Max-Age=604800; path=/; secure; HttpOnly; SameSite=None, uad=1884597369c9a63194ed0624319983; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None, RORSSQIHEK=f15239f2927f4fd08e6945c15ed635c2; expires=Wed, 01-Apr-2026 22:22:41 GMT; Max-Age=259200; path=/; secure; HttpOnly; SameSite=None, csrf_user_token=43ed1b44d6a5a480418b39929da62605; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None, userId=18845973; expires=Wed, 26-Mar-2036 22:22:41 GMT; Max-Age=315360000; path=/; secure; SameSite=None, slrememberme=18845973_%242y%2410%24GEnC83HAU.ejn2CQB3OMTewWzSYxC0NYcSB3n2ck6eNvcz2aStK0W; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None, _kmid=7fb7f3a407728e8d0ffa5ab4d19ff2b6; expires=Wed, 26-Mar-2036 22:22:41 GMT; Max-Age=315360000; path=/; secure; HttpOnly; SameSite=None, _kmfvt=1774822961; expires=Wed, 26-Mar-2036 22:22:41 GMT; Max-Age=315360000; path=/; secure; HttpOnly; SameSite=None, csrf_user_token=515cb2f621700da0faf4c3da66efbbfb; expires=Mon, 29-Mar-2027 22:22:41 GMT; Max-Age=31536000; path=/; secure; HttpOnly; SameSite=None', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'cache-control': 'no-store, no-cache, must-revalidate, no-cache, private', 'pragma': 'no-cache', 'strict-transport-security': 'max-age=15552000', 'x-content-type-options': 'nosniff', 'content-encoding': 'gzip'}\n",
"INFO:kwork_api.client:Login successful: user_id=None, csrf_token=N/A\n", "INFO:kwork_api.client:Login successful: user_id=18845973, csrf_token=515cb2f621700da0faf4\n",
"DEBUG:kwork_api.client:Login response data: {'success': False, 'error': 'Нужно ввести логин', 'redirect': '', 'recaptcha_show': False, 'error_code': 109}\n", "DEBUG:kwork_api.client:Login response data: {'success': True, 'error': '', 'redirect': '', 'action_after': '', 'isUserVerified': True, 'need_2fa': False, 'csrftoken': '515cb2f621700da0faf4c3da66efbbfb'}\n",
"DEBUG:kwork_api.client:Login cookies: ['referrer_url']\n", "DEBUG:kwork_api.client:Login cookies: ['referrer_url', 'uad', 'RORSSQIHEK', 'csrf_user_token', 'userId', 'slrememberme', '_kmid', '_kmfvt']\n",
"DEBUG:httpcore.connection:close.started\n", "DEBUG:httpcore.connection:close.started\n",
"DEBUG:httpcore.connection:close.complete\n" "DEBUG:httpcore.connection:close.complete\n"
] ]
@ -55,7 +68,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"KworkAuthError: Login failed: no userId in cookies. Response: {'success': False, 'error': 'Нужно ввести логин', 'redirect': '', 'recaptcha_show': False, 'error_code': 109}\n" "✅ Logged in as: 18845973_%242y%2410%...\n"
] ]
} }
], ],
@ -72,24 +85,32 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 7,
"id": "655aa71e-5645-4c7a-aadd-5b044a0713c9", "id": "655aa71e-5645-4c7a-aadd-5b044a0713c9",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'8AQhyzQRcTJ6v81maCNa'" "'18845973_%242y%2410%24GEnC83HAU.ejn2CQB3OMTewWzSYxC0NYcSB3n2ck6eNvcz2aStK0W'"
] ]
}, },
"execution_count": 4, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"os.getenv('KWORK_PASSWORD')" "client.token"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9a5161a-4051-4321-849b-c3b416a939a0",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {

129
tests/e2e/test_catalog.py Normal file
View File

@ -0,0 +1,129 @@
"""
E2E тесты для каталога и проектов.
Все тесты read-only - ничего не изменяют на сервере.
Endpoints основаны на анализе HAR файла.
"""
import pytest
from kwork_api import KworkClient
@pytest.mark.e2e
async def test_get_projects_list(require_credentials):
"""E2E: Получить список проектов с биржи.
Endpoint: GET https://kwork.ru/projects
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
try:
# Note: Это может возвращать HTML страницу, не JSON API
# Пока просто проверяем что запрос работает
# В будущем нужно реализовать парсинг HTML или найти JSON API endpoint
assert client is not None
finally:
await client.close()
@pytest.mark.e2e
async def test_get_categories(require_credentials):
"""E2E: Получить категорию.
Endpoint: GET https://kwork.ru/categories/{slug}
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
try:
# Note: Это возвращает HTML страницу категории
# Пока просто проверяем что запрос работает
assert client is not None
finally:
await client.close()
@pytest.mark.e2e
async def test_get_user_profile(require_credentials):
"""E2E: Получить профиль пользователя.
Endpoint: GET https://kwork.ru/user/{username}
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
try:
# Note: Это возвращает HTML страницу профиля
# Пока просто проверяем что запрос работает
assert client is not None
finally:
await client.close()
@pytest.mark.e2e
async def test_api_checknotify(require_credentials):
"""E2E: Проверить уведомления.
Endpoint: POST https://kwork.ru/api/user/checknotify
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
try:
# Note: Нужно реализовать endpoint в client.py
# Пока просто проверяем что логин работает
assert client.token is not None
finally:
await client.close()
@pytest.mark.e2e
async def test_api_addview(require_credentials):
"""E2E: Добавить просмотр (read-only операция).
Endpoint: POST https://kwork.ru/api/offer/addview
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
try:
# Note: Нужно реализовать endpoint в client.py
# Пока просто проверяем что логин работает
assert client.token is not None
finally:
await client.close()
@pytest.mark.e2e
async def test_get_reviews(require_credentials):
"""E2E: Получить отзывы пользователя.
Endpoint: POST https://kwork.ru/user/get_reviews
HAR shows:
POST https://kwork.ru/user/get_reviews
{"userId":126921,"type":"positive"}
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
try:
# Note: Нужно реализовать endpoint в client.py с правильным путём
# Пока просто проверяем что логин работает
assert client.token is not None
finally:
await client.close()