File size: 4,331 Bytes
c8a050e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f81faac
c8a050e
f81faac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from urllib import response

import pytest
from typing import List
from pydantic import TypeAdapter
from app.schemas import PostResponse
from app.schemas.post import PostVote
from tests.conftest import authorize_client

def test_get_all_posts(authorize_client,test_posts):
    response = authorize_client.get("/posts/")
    posts = TypeAdapter(List[PostResponse]).validate_python(response.json())
    assert response.status_code == 200
    assert len(posts) == len(test_posts)

def test_unauthorized_get_all_post(client,test_posts):
    response=client.get("/posts/")
    assert response.status_code == 401

def test_unauthorized_get_one_post(client,test_posts):
    response=client.get(f"/posts/{test_posts[0].id}")
    assert response.status_code == 401

def test_get_one_post_not_exit(authorize_client):
    response=authorize_client.get(f"/posts/-9999")
    assert response.status_code == 404

def test_get_one_post(authorize_client,test_posts):
    response=authorize_client.get(f"/posts/{test_posts[0].id}")
    post=PostVote(**response.json())
    assert post.id == test_posts[0].id
    
@pytest.mark.parametrize("title, content, published", [
    ("MHA","Anime of the Year",True),
    ("To be Hero X","Can be Considerd to be Anime of the Year",True),
    ("Wind Breaker","Watched",False)])
def test_create_post(authorize_client,title,content,published,create_test_user):
    response =authorize_client.post("/posts/",json={"title": title, "content": content, "published": published})
    created_post = PostResponse(**response.json())
    assert response.status_code == 201
    assert created_post.title == title
    assert created_post.content == content
    assert created_post.published == published
    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})
    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 == 404