Spaces:
Runtime error
Runtime error
Commit ·
f81faac
1
Parent(s): c8a050e
all test done test_votes.py also added
Browse filesFiles changed (3)
- tests/conftest.py+14-1
- tests/test_posts.py+64-1
- tests/test_votes.pyAdded+37
45 unmodified lines4647484950515253545556575859606162637 unmodified lines7172736274757677787980818245 unmodified lines new_user["password"]="abdullah1234" return new_user
@pytest.fixturedef create_test_user2(client): user_data = { "email": "ali@gmail.com", "password": "ali1234" } response = client.post("/users/", json=user_data) assert response.status_code == 201 new_user = response.json() new_user["password"] = "ali1234" return new_user
@pytest.fixturedef token(create_test_user): return Create_Access_Token({"id": create_test_user["id"]})7 unmodified lines return client
@pytest.fixturedef test_posts(create_test_user,session):def test_posts(create_test_user,create_test_user2,session): posts_data = [ {"title": "first title", "content": "first content", "owner_id": create_test_user['id']}, {"title": "2nd title", "content": "2nd content", "owner_id": create_test_user['id']}, {"title": "3rd title", "content": "3rd content", "owner_id": create_test_user['id']}, {"title": "3rd title", "content": "3rd content", "owner_id": create_test_user2['id']} ] def create_post(post): return Post(**post)43 unmodified lines44454647474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011143 unmodified lines assert created_post.owner_id == create_test_user['id'] def test_unauthorized_user_create_post(client,test_posts): response =client.post("/posts/",json={"title": "Anime", "content": "Worth Watching", "published": True}) response = client.post("/posts/",json={"title": "Anime", "content": "Worth Watching", "published": True}) assert response.status_code == 401
def test_unauthorized_delete_post(client,test_posts): response = client.delete(f"/posts/{test_posts[0].id}") assert response.status_code == 401
def test_delete_post(authorize_client,test_posts): response = authorize_client.delete(f"/posts/{test_posts[0].id}") assert response.status_code == 204
def test_delete_post_non_exit(authorize_client,test_posts): response = authorize_client.delete("/posts/-9999") assert response.status_code == 404
def test_delete_other_user_post(authorize_client,test_posts): response = authorize_client.delete(f"/posts/{test_posts[3].id}") assert response.status_code == 403
def test_update_post(authorize_client,test_posts): data = { "title": "MHA", "content": "Anime of the Year", "id": test_posts[0].id } response = authorize_client.put(f"/posts/{test_posts[0].id}", json=data) updated_post = PostResponse(**response.json()) assert response.status_code == 200 assert updated_post.title == data["title"]
def test_other_user_post(authorize_client,test_posts): data = { "title": "MHA", "content": "Anime of the Year", "id": test_posts[3].id } response = authorize_client.put(f"/posts/{test_posts[3].id}", json=data) assert response.status_code == 403
def test_patch_post(authorize_client,test_posts): data = { "title": "MHA", } response = authorize_client.patch(f"/posts/{test_posts[0].id}", json=data) updated_post = PostResponse(**response.json()) assert response.status_code == 200 assert updated_post.title == data["title"]
def test_unauthorized_update_post(client,test_posts): response = client.put(f"/posts/{test_posts[0].id}") assert response.status_code == 401
def test_unauthorized_patch_update_post(client,test_posts): response = client.patch(f"/posts/{test_posts[0].id}") assert response.status_code == 401
def test_update_post_non_exit(authorize_client,test_posts): data = {"title": "MHA", "content": "Anime of the Year"} response = authorize_client.put("/posts/-9999", json=data) assert response.status_code == 404
def test_patch_update_post_non_exit(authorize_client,test_posts): data = {"title": "MHA"} response = authorize_client.patch("/posts/-9999", json=data) assert response.status_code == 40412345678910111213141516171819202122232425262728293031323334353637import pytestfrom app.models import Votesfrom app.schemas.vote import VoteBase
@pytest.fixturedef test_vote(test_posts,session,create_test_user): vote = { "post_id": test_posts[3].id, "user_id": create_test_user["id"] } new_vote=Votes(**vote) session.add(new_vote) session.commit()
def test_vote_on_post(authorize_client,test_posts): response = authorize_client.post("/vote",json={"post_id": test_posts[3].id, "dir": 1}) response.status_code == 201
def test_vote_twice(authorize_client,test_posts,test_vote): response = authorize_client.post("/vote/",json={"post_id": test_posts[3].id, "dir": 1}) assert response.status_code == 409
def test_unvote_on_post(authorize_client,test_posts,test_vote): response = authorize_client.post("/vote",json={"post_id": test_posts[3].id, "dir": 0}) response.status_code == 201
def test_unvote_on_post_vote_does_not_exit(authorize_client,test_posts): response = authorize_client.post("/vote",json={"post_id": test_posts[3].id, "dir": 0}) response.status_code == 404
def test_unvote_on_post_does_not_exit(authorize_client,test_posts): response = authorize_client.post("/vote",json={"post_id": -9999, "dir": 0}) response.status_code == 404
def test_unauthorize_vote_on_post(client,test_posts): response = client.post("/vote",json={"post_id": test_posts[3].id, "dir": 1}) response.status_code == 401tests/conftest.py+14-1
tests/test_posts.py+64-1
tests/test_votes.pyAdded+37