Skip to content

Commit d63b392

Browse files
committed
tested getting all post by an authorised user
1 parent d9dc557 commit d63b392

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

app/oauth2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
def create_access_token(data: dict):
10+
# data is an id key value pair values
1011
to_encode = data.copy()
1112

1213
expire = datetime.utcnow() + timedelta(minutes=int(settings.jwt_access_token_expire_minutes))

app/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
44

5-
5+
# for user registration
66
def hash(password: str):
77
hashed_password = pwd_context.hash(password)
88
return hashed_password

test/conftest.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,53 @@ def test_user_exist(session):
6464

6565

6666
@pytest.fixture
67-
def test_user_exist_not(client):
68-
user_data = {"email": "ola@gmail.com",
69-
"password": "123"}
70-
res = client.post("/users/", json=user_data)
67+
def test_user_exist_not(session):
68+
user = models.Users(
69+
email="ola@gmail.com",
70+
password=utils.hash("123")
71+
)
72+
session.add(user)
73+
session.commit()
74+
session.refresh(user)
75+
76+
return {"id": user.id, "email": user.email, "password": "password123"}
77+
78+
79+
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<< AUTHENTICATE USERS FOR POSTS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
80+
@pytest.fixture
81+
def token(test_user_exist):
82+
return create_access_token({"user_id": test_user_exist['id']})
83+
7184

72-
assert res.status_code == 201
85+
@pytest.fixture
86+
def authorized_client(client, token):
87+
client.headers = {
88+
**client.headers,
89+
"Authorization": f"Bearer {token}"
90+
}
7391

74-
new_user = res.json()
75-
new_user['password'] = user_data['password']
76-
return new_user
92+
return client
7793

7894

7995
@pytest.fixture
8096
def test_posts(test_user_exist, session, test_user_exist_not):
8197
posts_data = [{
8298
"title": "first title",
8399
"content": "first content",
84-
"owner_id": test_user_exist['id']
100+
"user_id": test_user_exist['id']
85101
}, {
86102
"title": "2nd title",
87103
"content": "2nd content",
88-
"owner_id": test_user_exist['id']
104+
"user_id": test_user_exist['id']
89105
},
90106
{
91107
"title": "3rd title",
92108
"content": "3rd content",
93-
"owner_id": test_user_exist['id']
109+
"user_id": test_user_exist['id']
94110
}, {
95111
"title": "3rd title",
96112
"content": "3rd content",
97-
"owner_id": test_user_exist2['id']
113+
"user_id": test_user_exist['id']
98114
}]
99115

100116
def create_post_model(post):

test/test_posts.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from app import schemas
3+
4+
5+
def test_get_all_posts(authorized_client, test_posts):
6+
res = authorized_client.get("/posts/")
7+
8+
def validate(post):
9+
return schemas.PostWithVotes(**post)
10+
11+
posts_map = map(validate, res.json())
12+
posts_list = list(posts_map)
13+
14+
assert len(res.json()) == len(test_posts)
15+
assert res.status_code == 200

0 commit comments

Comments
 (0)