84 lines
3.6 KiB
Python
84 lines
3.6 KiB
Python
import allure
|
|
import pytest
|
|
import requests_async
|
|
from httpx import HTTPStatusError
|
|
|
|
|
|
class TestPermissions:
|
|
|
|
async def test_get_access_token_positive(self, target_url:str)->None:
|
|
|
|
with allure.step("get_access_token"), pytest.raises(HTTPStatusError) as exc_info:
|
|
|
|
response = await requests_async.post(f"{target_url}/protected/token")
|
|
response.raise_for_status()
|
|
|
|
assert exc_info.value.response.status_code != 403
|
|
assert exc_info.value.response.status_code != 401
|
|
|
|
async def test_get_refresh_token_positive(self, target_url:str)->None:
|
|
|
|
with allure.step("get_refresh_token"), pytest.raises(HTTPStatusError) as exc_info:
|
|
|
|
response = await requests_async.post(f"{target_url}/protected/refresh")
|
|
response.raise_for_status()
|
|
assert exc_info.value.response.status_code != 403
|
|
assert exc_info.value.response.status_code != 401
|
|
|
|
async def test_get_root_unauthorized(self, target_url:str)->None:
|
|
|
|
with allure.step("get_root"), pytest.raises(HTTPStatusError) as exc_info:
|
|
|
|
response = await requests_async.get(f"{target_url}/protected")
|
|
response.raise_for_status()
|
|
assert exc_info.value.response.status_code != 403
|
|
assert exc_info.value.response.status_code == 401
|
|
|
|
@pytest.mark.parametrize("test_user_fixture", [([], [])], indirect=True)
|
|
async def test_get_logout_positive(self,test_user_fixture, target_url:str)->None:
|
|
|
|
session=test_user_fixture[0]
|
|
with allure.step("get_root"), pytest.raises(HTTPStatusError) as exc_info:
|
|
|
|
response = await session.get(f"{target_url}/protected/logout")
|
|
response.raise_for_status()
|
|
assert exc_info.value.response.status_code != 403
|
|
assert exc_info.value.response.status_code != 401
|
|
|
|
|
|
class TestRedis:
|
|
|
|
@pytest.mark.parametrize("wrong_user_data, expected_status",[
|
|
pytest.param({"username":"Wrong_user", "password":"Wrong_password"},429,id="Wrong_user_creds")
|
|
])
|
|
async def test_rate_limit_positive(self, wrong_user_data:dict, target_url:str, expected_status:int):
|
|
|
|
with allure.step("logging with invalid creds"):
|
|
|
|
for i in range(5):
|
|
with pytest.raises(HTTPStatusError) as exc_info:
|
|
response = await requests_async.post(target_url + "/protected/token", data=wrong_user_data)
|
|
response.raise_for_status()
|
|
assert exc_info.value.response.status_code == 401, f"Attempt {i+1} should be 401"
|
|
|
|
with allure.step("verify rate_limit works"),pytest.raises(HTTPStatusError) as exc_info:
|
|
response = await requests_async.post(target_url + "/protected/token", data=wrong_user_data)
|
|
response.raise_for_status()
|
|
assert exc_info.value.response.status_code == expected_status
|
|
|
|
|
|
@pytest.mark.parametrize("test_user_fixture", [([], [])], indirect=True)
|
|
async def test_get_logout_revoke_positive(self,test_user_fixture, target_url:str)->None:
|
|
|
|
session=test_user_fixture[0]
|
|
|
|
with allure.step("logout"):
|
|
|
|
response = await session.get(f"{target_url}/protected/logout")
|
|
response.raise_for_status()
|
|
|
|
with allure.step("verify token is revoked"), pytest.raises(HTTPStatusError) as exc_info:
|
|
response = await session.get(f"{target_url}/protected")
|
|
response.raise_for_status()
|
|
assert exc_info.value.response.status_code==429
|
|
|