Skip to content

Commit e25d44e

Browse files
committed
add toke, authorized_clients fixtures and test_get_all_posts
1 parent 5379fd8 commit e25d44e

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
new_user,
77
user_create,
88
user_create_json,
9-
user_login_json
9+
user_login_json,
10+
token,
11+
authorized_client
1012
)

tests/fixtures/user.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from typing import Generator
2+
from fastapi.testclient import TestClient
13
import pytest
24
import schemas, schemas.user
5+
from app import oauth2
6+
from .database import client
37

48
@pytest.fixture
59
def user_create() -> schemas.user.UserCreate:
@@ -35,3 +39,15 @@ def new_user(client, user_create, user_create_json) -> schemas.user.UserDb:
3539
password=user_create.password
3640
)
3741
return userdb
42+
43+
@pytest.fixture
44+
def token(new_user) -> str:
45+
return oauth2.create_access_token({'user_id': new_user.id})
46+
47+
@pytest.fixture
48+
def authorized_client(client, token) -> Generator[TestClient, None, None]:
49+
client.headers = {
50+
**client.headers,
51+
'Authorization': f'Bearer {token}'
52+
}
53+
yield client

tests/test_posts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def test_get_all_posts(authorized_client):
2+
response = authorized_client.get('/posts')
3+
assert response.status_code == 200
4+
print(response.json())

0 commit comments

Comments
 (0)