Skip to content

Commit ecd5e93

Browse files
committed
test_delete_owned_post
1 parent 0ac9bb4 commit ecd5e93

5 files changed

Lines changed: 81 additions & 46 deletions

File tree

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
unauthorized_client
44
)
55
from .fixtures.user import (
6-
add_user_db,
6+
add_user_db_id1,
77
user_creation_schema,
88
user_creation_json,
99
user_login_json,
@@ -14,8 +14,10 @@
1414

1515
from .fixtures.fakedata.posts import(
1616
fake_posts,
17-
fake_post_models_db,
18-
add_fake_posts_db,
17+
fake_post_models_db_userid1,
18+
add_fake_posts_db_userid1,
19+
fake_post_models_db_multiple_users,
20+
add_fake_posts_db_multiple_users
1921
)
2022

2123
from .fixtures.fakedata.users import(

tests/fixtures/fakedata/posts.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,36 @@ def fake_posts() -> list[post.PostCreate]:
1313
logger.debug('returning fake PostCreate list')
1414
return [
1515
post.PostCreate(title='1st title', content='1st content'),
16-
post.PostCreate(title='2st title', content='1st content'),
17-
post.PostCreate(title='3st title', content='1st content'),
16+
post.PostCreate(title='2nd title', content='2nd content'),
17+
post.PostCreate(title='3rd title', content='3rd content'),
18+
# post.PostCreate(title='4th title', content='4th content'),
1819
]
1920

2021
@pytest.fixture
21-
def fake_post_models_db(fake_posts, add_user_db) -> list[models.Post]:
22+
def fake_post_models_db_userid1(fake_posts, add_user_db_id1) -> list[models.Post]:
2223
logger.debug('returning fake Post model list')
23-
return [models.Post(**_post.model_dump(), owner_id=add_user_db.id)
24+
return [models.Post(**_post.model_dump(), owner_id=add_user_db_id1.id)
2425
for _post in fake_posts]
2526

2627
@pytest.fixture
27-
def add_fake_posts_db(session, fake_post_models_db) -> list[models.Post]:
28+
def add_fake_posts_db_userid1(session, fake_post_models_db_userid1) -> list[models.Post]:
2829
logger.debug('saving fake posts to db')
29-
session.add_all(fake_post_models_db)
30+
session.add_all(fake_post_models_db_userid1)
31+
session.commit()
32+
return session.query(models.Post).all()
33+
34+
@pytest.fixture
35+
def fake_post_models_db_multiple_users(fake_posts, add_users_db) -> list[models.Post]:
36+
logger.debug('returning fake Post model list')
37+
userids = [user.id for user in add_users_db]
38+
logger.warning(f'----- {userids = } -------')
39+
return [models.Post(**_post.model_dump(),
40+
owner_id=id)
41+
for _post, id in zip(fake_posts, userids)]
42+
43+
@pytest.fixture
44+
def add_fake_posts_db_multiple_users(session, fake_post_models_db_multiple_users) -> list[models.Post]:
45+
logger.debug('saving fake posts to db')
46+
session.add_all(fake_post_models_db_multiple_users)
3047
session.commit()
3148
return session.query(models.Post).all()

tests/fixtures/user.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
import logging
55

6+
from db.orm import models
67
import schemas, schemas.user
78
from app import oauth2
89
from .database import unauthorized_client
@@ -33,7 +34,7 @@ def user_login_json(user_creation_schema) -> dict[str, str]:
3334
}
3435

3536
@pytest.fixture
36-
def add_user_db(unauthorized_client, user_creation_schema, user_creation_json) -> schemas.user.UserDb:
37+
def add_user_db_id1(unauthorized_client, user_creation_schema, user_creation_json) -> schemas.user.UserDb:
3738
''' creates new user and return model of it from db '''
3839
logger.debug('creating 1st user')
3940
response = unauthorized_client.post(
@@ -48,9 +49,8 @@ def add_user_db(unauthorized_client, user_creation_schema, user_creation_json) -
4849
return userdb
4950

5051
@pytest.fixture
51-
def add_users_db(request, add_user_db, unauthorized_client, fake_users_creation_dict) -> schemas.user.UserDb:
52+
def add_users_db(request, session, add_user_db_id1, unauthorized_client, fake_users_creation_dict) -> schemas.user.UserDb:
5253
logger.debug('creating remaining user')
53-
new_users = []
5454
# if 'add_user_db' in request.fixturenames:
5555
# users = fake_users_creation_dict[1:]
5656
# else:
@@ -67,19 +67,20 @@ def add_users_db(request, add_user_db, unauthorized_client, fake_users_creation
6767
**response.json(),
6868
password=user_creds['password']
6969
)
70-
new_users.append(userdb)
71-
return new_users
70+
users = session.query(models.User).all()
71+
return users
7272

7373
@pytest.fixture
74-
def token(add_user_db) -> str:
74+
def token(add_user_db_id1) -> str:
7575
logger.debug('creating acces token')
76-
return oauth2.create_access_token({'user_id': add_user_db.id})
76+
return oauth2.create_access_token({'user_id': add_user_db_id1.id})
7777

7878
@pytest.fixture
7979
def authorized_client(unauthorized_client, token) -> Generator[TestClient, None, None]:
8080
logger.debug('creating authorized client')
81-
unauthorized_client.headers = {
81+
authorized = unauthorized_client
82+
authorized.headers = {
8283
**unauthorized_client.headers,
8384
'Authorization': f'Bearer {token}'
8485
}
85-
yield unauthorized_client
86+
yield authorized

tests/test_posts.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
import email
22
import json
3-
43
import pytest
4+
import logging
55

66
# from app.routers import posts
77
from app.routers import auth
8+
import app.oauth2
9+
from db.orm import models
810
import tests.debug
911
import schemas.post
1012

13+
logger = logging.getLogger(__name__)
1114

1215
def setup_module():
1316
tests.debug.DEBUG = False
1417

15-
def test_get_all_posts(authorized_client, add_fake_posts_db):
18+
def test_get_all_posts(authorized_client, add_fake_posts_db_userid1):
1619
response = authorized_client.get('/posts')
1720
assert response.status_code == 200
1821
# posts = [schemas.post.PostOut(**post) for post in response.json()]
1922
# [print(post) for post in posts]
2023
# posts = response.json()
2124
print(json.dumps(response.json(), indent=2))
2225
posts = [schemas.post.Post(**post.get('Post')) for post in response.json()]
23-
assert len(posts) == len(add_fake_posts_db)
24-
for post, dbpost in zip(posts, add_fake_posts_db):
26+
assert len(posts) == len(add_fake_posts_db_userid1)
27+
for post, dbpost in zip(posts, add_fake_posts_db_userid1):
2528
assert post.id == dbpost.id
2629
assert post.title == dbpost.title
2730
assert post.content == dbpost.content
2831
assert post.published == dbpost.published
2932
assert post.created_at == dbpost.created_at
3033
assert post.owner_id == dbpost.owner_id
3134

32-
def test_unauthorized_get_all_posts(unauthorized_client, add_fake_posts_db):
35+
def test_unauthorized_get_all_posts(unauthorized_client, add_fake_posts_db_userid1):
3336
response = unauthorized_client.get('/posts')
3437
assert response.status_code == 401
3538

36-
def test_unauthorized_get_one_post(unauthorized_client, add_fake_posts_db):
37-
response = unauthorized_client.get(f'/posts/{add_fake_posts_db[0].id}')
39+
def test_unauthorized_get_one_post(unauthorized_client, add_fake_posts_db_userid1):
40+
response = unauthorized_client.get(f'/posts/{add_fake_posts_db_userid1[0].id}')
3841
assert response.status_code == 401
3942

40-
def test_get_one_posts_not_exist(authorized_client, add_fake_posts_db):
43+
def test_get_one_posts_not_exist(authorized_client, add_fake_posts_db_userid1):
4144
response = authorized_client.get(
4245
f'/posts/-1')
4346
assert response.status_code == 404
4447

45-
def test_get_one_post(authorized_client, add_fake_posts_db):
46-
response = authorized_client.get(f'/posts/{add_fake_posts_db[0].id}')
48+
def test_get_one_post(authorized_client, add_fake_posts_db_userid1):
49+
response = authorized_client.get(f'/posts/{add_fake_posts_db_userid1[0].id}')
4750
assert response.status_code == 200
4851
post = schemas.post.Post(**response.json())
49-
assert post.id == add_fake_posts_db[0].id
52+
assert post.id == add_fake_posts_db_userid1[0].id
5053

5154
@pytest.mark.parametrize(
5255
'title, content, published',
@@ -58,8 +61,8 @@ def test_get_one_post(authorized_client, add_fake_posts_db):
5861
)
5962
def test_create_post(
6063
authorized_client,
61-
add_user_db,
62-
add_fake_posts_db,
64+
add_user_db_id1,
65+
add_fake_posts_db_userid1,
6366
title,
6467
content,
6568
published
@@ -77,8 +80,8 @@ def test_create_post(
7780
assert post.title == title
7881
assert post.content == content
7982
assert post.published == published
80-
assert post.owner_id == add_user_db.id
81-
assert post.owner.email == add_user_db.email
83+
assert post.owner_id == add_user_db_id1.id
84+
assert post.owner.email == add_user_db_id1.email
8285

8386
@pytest.mark.parametrize(
8487
'title, content',
@@ -90,8 +93,8 @@ def test_create_post(
9093
)
9194
def test_create_post_default_published_true(
9295
authorized_client,
93-
add_user_db,
94-
add_fake_posts_db,
96+
add_user_db_id1,
97+
add_fake_posts_db_userid1,
9598
title,
9699
content,
97100
):
@@ -107,8 +110,8 @@ def test_create_post_default_published_true(
107110
assert post.title == title
108111
assert post.content == content
109112
assert post.published == True
110-
assert post.owner_id == add_user_db.id
111-
assert post.owner.email == add_user_db.email
113+
assert post.owner_id == add_user_db_id1.id
114+
assert post.owner.email == add_user_db_id1.email
112115

113116
def test_unauthorized_create_post(unauthorized_client):
114117
response = unauthorized_client.post(
@@ -120,17 +123,29 @@ def test_unauthorized_create_post(unauthorized_client):
120123
)
121124
assert response.status_code == 401
122125

123-
def test_unauthorized_delete_post(unauthorized_client, add_fake_posts_db):
124-
response = unauthorized_client.delete(f'/posts/{add_fake_posts_db[0].id}')
126+
def test_unauthorized_delete_post(unauthorized_client, add_fake_posts_db_userid1):
127+
response = unauthorized_client.delete(f'/posts/{add_fake_posts_db_userid1[0].id}')
125128
assert response.status_code == 401
126129

127-
def test_delete_post(authorized_client, add_fake_posts_db):
128-
response = authorized_client.delete(f'/posts/{add_fake_posts_db[0].id}')
130+
def test_delete_post(authorized_client, add_fake_posts_db_userid1):
131+
response = authorized_client.delete(f'/posts/{add_fake_posts_db_userid1[0].id}')
129132
assert response.status_code == 204
130133

131-
def test_delete_post_not_exist(authorized_client, add_fake_posts_db):
134+
def test_delete_post_not_exist(authorized_client, add_fake_posts_db_userid1):
132135
response = authorized_client.delete(f'/posts/-1')
133136
assert response.status_code == 404
134137

135-
def test_delete_owned_post(authorized_client, add_users_db, add_fake_posts_db):
136-
pass
138+
def test_delete_owned_post(authorized_client, token, session, add_fake_posts_db_multiple_users):
139+
tokendata = app.oauth2.verify_access_token(token, app.oauth2.unhautorized_exception)
140+
post: models.Post = (
141+
session.query(models.Post)
142+
.filter(models.Post.owner_id==tokendata.id)
143+
.first()
144+
)
145+
logger.info(post.content)
146+
response = authorized_client.delete(f'posts/{post.id}')
147+
assert response.status_code == 204
148+
# posts = session.query(models.Post).all()
149+
# assert len(posts) == 2
150+
check = session.query(models.Post).filter(models.Post.id==post.id).first()
151+
assert check is None

tests/test_users.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_create_user(unauthorized_client, session, user_creation_json):
3232
assert user.email == 'testuser@gmail.com'
3333

3434
# session fixture is cached
35-
def test_login(unauthorized_client, session, user_login_json, add_user_db):
35+
def test_login(unauthorized_client, session, user_login_json, add_user_db_id1):
3636
debug(f'\nTEST_LOGIN_USER| {session.rand = }', force=False)
3737
debug()
3838
debug(f'TEST_LOGIN_USER| get_user')
@@ -50,5 +50,5 @@ def test_login(unauthorized_client, session, user_login_json, add_user_db):
5050
token.access_token,
5151
app.oauth2.unhautorized_exception
5252
)
53-
assert tokendata.id == add_user_db.id
53+
assert tokendata.id == add_user_db_id1.id
5454

0 commit comments

Comments
 (0)