-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
56 lines (45 loc) · 2.28 KB
/
test_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pytest
from main import app
from httpx import AsyncClient
@pytest.mark.asyncio
async def test_register_user():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.post("/register", data={"username": "testuser", "password": "password123"})
assert response.status_code == 303
@pytest.mark.asyncio
async def test_login_user():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.post("/login", data={"username": "testuser", "password": "password123"})
assert response.status_code == 303
@pytest.mark.asyncio
async def test_login_invalid_credentials():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.post("/login", data={"username": "wronguser", "password": "wrongpassword"})
assert response.status_code == 401
@pytest.mark.asyncio
async def test_access_admin_tools_without_permission():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/admin/tools")
assert response.status_code == 403
@pytest.mark.asyncio
async def test_access_manager_tools_without_permission():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/manager/tools")
assert response.status_code == 403
@pytest.mark.asyncio
async def test_create_post_without_login():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.post("/add_post", data={"name": "Test Post", "post": "This is a test post"})
assert response.status_code == 403
@pytest.mark.asyncio
async def test_delete_post_as_user():
async with AsyncClient(app=app, base_url="http://test") as client:
login_response = await client.post("/login", data={"username": "testuser", "password": "password123"})
cookies = login_response.cookies
response = await client.post("/user/delete_post/1", cookies=cookies)
assert response.status_code in [200, 303]
@pytest.mark.asyncio
async def test_access_denied_for_user_to_admin_tools():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/admin/tools")
assert response.status_code == 403