Spaces:
Runtime error
Runtime error
Commit ·
c8a050e
1
Parent(s): bb4b25f
test_post.py added
Browse files- app/routers/post.py +1 -1
- tests/{database.py → conftest.py} +44 -1
- tests/test_posts.py +48 -0
- tests/test_users.py +13 -13
app/routers/post.py
CHANGED
|
@@ -25,7 +25,7 @@ def get_posts(db: Session = Depends(get_db),current_user: User= Depends(Get_Curr
|
|
| 25 |
post.votes = vote_count
|
| 26 |
posts.append(post)
|
| 27 |
return posts
|
| 28 |
-
@router.post("/", response_model=PostResponse)
|
| 29 |
def create_post(post: PostCreate, db: Session = Depends(get_db), current_user: User= Depends(Get_Current_User)):
|
| 30 |
new_post = Post(owner_id=current_user.id,**post.model_dump())
|
| 31 |
db.add(new_post)
|
|
|
|
| 25 |
post.votes = vote_count
|
| 26 |
posts.append(post)
|
| 27 |
return posts
|
| 28 |
+
@router.post("/", response_model=PostResponse,status_code=status.HTTP_201_CREATED)
|
| 29 |
def create_post(post: PostCreate, db: Session = Depends(get_db), current_user: User= Depends(Get_Current_User)):
|
| 30 |
new_post = Post(owner_id=current_user.id,**post.model_dump())
|
| 31 |
db.add(new_post)
|
tests/{database.py → conftest.py}
RENAMED
|
@@ -5,6 +5,8 @@ from app.main import app
|
|
| 5 |
from sqlalchemy import create_engine
|
| 6 |
from sqlalchemy.orm import sessionmaker
|
| 7 |
from app.config import setting
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
SQLALCHEMY_DATABASE_URL = f"postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}_Test"
|
|
@@ -30,4 +32,45 @@ def client(session):
|
|
| 30 |
finally:
|
| 31 |
session.close()
|
| 32 |
app.dependency_overrides[get_db] = override_get_db
|
| 33 |
-
yield TestClient(app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from sqlalchemy import create_engine
|
| 6 |
from sqlalchemy.orm import sessionmaker
|
| 7 |
from app.config import setting
|
| 8 |
+
from app.oauth2 import Create_Access_Token
|
| 9 |
+
from app.models import Post
|
| 10 |
|
| 11 |
|
| 12 |
SQLALCHEMY_DATABASE_URL = f"postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}_Test"
|
|
|
|
| 32 |
finally:
|
| 33 |
session.close()
|
| 34 |
app.dependency_overrides[get_db] = override_get_db
|
| 35 |
+
yield TestClient(app)
|
| 36 |
+
|
| 37 |
+
@pytest.fixture
|
| 38 |
+
def create_test_user(client):
|
| 39 |
+
user_data={
|
| 40 |
+
"email": "abdullah@gmail.com",
|
| 41 |
+
"password": "abdullah1234"
|
| 42 |
+
}
|
| 43 |
+
response = client.post("/users/",json=user_data)
|
| 44 |
+
assert response.status_code == 201
|
| 45 |
+
new_user=response.json()
|
| 46 |
+
new_user["password"]="abdullah1234"
|
| 47 |
+
return new_user
|
| 48 |
+
|
| 49 |
+
@pytest.fixture
|
| 50 |
+
def token(create_test_user):
|
| 51 |
+
return Create_Access_Token({"id": create_test_user["id"]})
|
| 52 |
+
|
| 53 |
+
@pytest.fixture
|
| 54 |
+
def authorize_client(client,token):
|
| 55 |
+
client.headers = {
|
| 56 |
+
**client.headers,
|
| 57 |
+
"Authorization": f"Bearer {token}"
|
| 58 |
+
}
|
| 59 |
+
return client
|
| 60 |
+
|
| 61 |
+
@pytest.fixture
|
| 62 |
+
def test_posts(create_test_user,session):
|
| 63 |
+
posts_data = [
|
| 64 |
+
{"title": "first title", "content": "first content", "owner_id": create_test_user['id']},
|
| 65 |
+
{"title": "2nd title", "content": "2nd content", "owner_id": create_test_user['id']},
|
| 66 |
+
{"title": "3rd title", "content": "3rd content", "owner_id": create_test_user['id']},
|
| 67 |
+
]
|
| 68 |
+
def create_post(post):
|
| 69 |
+
return Post(**post)
|
| 70 |
+
post=map(create_post,posts_data)
|
| 71 |
+
post=list(post)
|
| 72 |
+
session.add_all(post)
|
| 73 |
+
session.commit()
|
| 74 |
+
posts=session.query(Post).all()
|
| 75 |
+
return posts
|
| 76 |
+
|
tests/test_posts.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from urllib import response
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
from typing import List
|
| 5 |
+
from pydantic import TypeAdapter
|
| 6 |
+
from app.schemas import PostResponse
|
| 7 |
+
from app.schemas.post import PostVote
|
| 8 |
+
from tests.conftest import authorize_client
|
| 9 |
+
|
| 10 |
+
def test_get_all_posts(authorize_client,test_posts):
|
| 11 |
+
response = authorize_client.get("/posts/")
|
| 12 |
+
posts = TypeAdapter(List[PostResponse]).validate_python(response.json())
|
| 13 |
+
assert response.status_code == 200
|
| 14 |
+
assert len(posts) == len(test_posts)
|
| 15 |
+
|
| 16 |
+
def test_unauthorized_get_all_post(client,test_posts):
|
| 17 |
+
response=client.get("/posts/")
|
| 18 |
+
assert response.status_code == 401
|
| 19 |
+
|
| 20 |
+
def test_unauthorized_get_one_post(client,test_posts):
|
| 21 |
+
response=client.get(f"/posts/{test_posts[0].id}")
|
| 22 |
+
assert response.status_code == 401
|
| 23 |
+
|
| 24 |
+
def test_get_one_post_not_exit(authorize_client):
|
| 25 |
+
response=authorize_client.get(f"/posts/-9999")
|
| 26 |
+
assert response.status_code == 404
|
| 27 |
+
|
| 28 |
+
def test_get_one_post(authorize_client,test_posts):
|
| 29 |
+
response=authorize_client.get(f"/posts/{test_posts[0].id}")
|
| 30 |
+
post=PostVote(**response.json())
|
| 31 |
+
assert post.id == test_posts[0].id
|
| 32 |
+
|
| 33 |
+
@pytest.mark.parametrize("title, content, published", [
|
| 34 |
+
("MHA","Anime of the Year",True),
|
| 35 |
+
("To be Hero X","Can be Considerd to be Anime of the Year",True),
|
| 36 |
+
("Wind Breaker","Watched",False)])
|
| 37 |
+
def test_create_post(authorize_client,title,content,published,create_test_user):
|
| 38 |
+
response =authorize_client.post("/posts/",json={"title": title, "content": content, "published": published})
|
| 39 |
+
created_post = PostResponse(**response.json())
|
| 40 |
+
assert response.status_code == 201
|
| 41 |
+
assert created_post.title == title
|
| 42 |
+
assert created_post.content == content
|
| 43 |
+
assert created_post.published == published
|
| 44 |
+
assert created_post.owner_id == create_test_user['id']
|
| 45 |
+
|
| 46 |
+
def test_unauthorized_user_create_post(client,test_posts):
|
| 47 |
+
response =client.post("/posts/",json={"title": "Anime", "content": "Worth Watching", "published": True})
|
| 48 |
+
assert response.status_code == 401
|
tests/test_users.py
CHANGED
|
@@ -2,19 +2,6 @@ import pytest
|
|
| 2 |
from app.config import setting
|
| 3 |
from app.schemas import user, token
|
| 4 |
from jose import jwt
|
| 5 |
-
from tests.database import client, session
|
| 6 |
-
|
| 7 |
-
@pytest.fixture
|
| 8 |
-
def create_test_user(client):
|
| 9 |
-
user_data={
|
| 10 |
-
"email": "abdullah@gmail.com",
|
| 11 |
-
"password": "abdullah1234"
|
| 12 |
-
}
|
| 13 |
-
response = client.post("/users/",json=user_data)
|
| 14 |
-
assert response.status_code == 201
|
| 15 |
-
new_user=response.json()
|
| 16 |
-
new_user["password"]="abdullah1234"
|
| 17 |
-
return new_user
|
| 18 |
|
| 19 |
def test_create_user(client):
|
| 20 |
response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})
|
|
@@ -30,3 +17,16 @@ def test_login_user(client,create_test_user):
|
|
| 30 |
assert id == create_test_user["id"]
|
| 31 |
assert login_response.token_type == "bearer"
|
| 32 |
assert response.status_code==200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from app.config import setting
|
| 3 |
from app.schemas import user, token
|
| 4 |
from jose import jwt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def test_create_user(client):
|
| 7 |
response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})
|
|
|
|
| 17 |
assert id == create_test_user["id"]
|
| 18 |
assert login_response.token_type == "bearer"
|
| 19 |
assert response.status_code==200
|
| 20 |
+
|
| 21 |
+
@pytest.mark.parametrize("email, password, status_code", [
|
| 22 |
+
("abdullah@gmail.com", "wrongpassword", 403),
|
| 23 |
+
("wrong@gmail.com", "abdullah1234", 403),
|
| 24 |
+
("wrong@gmail.com", "wrongpassword", 403),
|
| 25 |
+
(None, None, 422),
|
| 26 |
+
("abdullah@gmail.com", None, 422),
|
| 27 |
+
(None, "abdullah1234", 422),
|
| 28 |
+
("abdullah@gmail.com", "abdullah1234", 200),
|
| 29 |
+
])
|
| 30 |
+
def test_login_parametrized(client, create_test_user, email, password, status_code):
|
| 31 |
+
response = client.post("/login", data={"username": email, "password": password})
|
| 32 |
+
assert response.status_code == status_code
|