abdullah090809 commited on
Commit
f81faac
·
1 Parent(s): c8a050e

all test done test_votes.py also added

Browse files
Files changed (3) hide show
  1. tests/conftest.py +14 -1
  2. tests/test_posts.py +64 -1
  3. tests/test_votes.py +37 -0
tests/conftest.py CHANGED
@@ -46,6 +46,18 @@ def create_test_user(client):
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"]})
@@ -59,11 +71,12 @@ def authorize_client(client,token):
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)
 
46
  new_user["password"]="abdullah1234"
47
  return new_user
48
 
49
+ @pytest.fixture
50
+ def create_test_user2(client):
51
+ user_data = {
52
+ "email": "ali@gmail.com",
53
+ "password": "ali1234"
54
+ }
55
+ response = client.post("/users/", json=user_data)
56
+ assert response.status_code == 201
57
+ new_user = response.json()
58
+ new_user["password"] = "ali1234"
59
+ return new_user
60
+
61
  @pytest.fixture
62
  def token(create_test_user):
63
  return Create_Access_Token({"id": create_test_user["id"]})
 
71
  return client
72
 
73
  @pytest.fixture
74
+ def test_posts(create_test_user,create_test_user2,session):
75
  posts_data = [
76
  {"title": "first title", "content": "first content", "owner_id": create_test_user['id']},
77
  {"title": "2nd title", "content": "2nd content", "owner_id": create_test_user['id']},
78
  {"title": "3rd title", "content": "3rd content", "owner_id": create_test_user['id']},
79
+ {"title": "3rd title", "content": "3rd content", "owner_id": create_test_user2['id']}
80
  ]
81
  def create_post(post):
82
  return Post(**post)
tests/test_posts.py CHANGED
@@ -44,5 +44,68 @@ def test_create_post(authorize_client,title,content,published,create_test_user):
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
+
50
+ def test_unauthorized_delete_post(client,test_posts):
51
+ response = client.delete(f"/posts/{test_posts[0].id}")
52
+ assert response.status_code == 401
53
+
54
+ def test_delete_post(authorize_client,test_posts):
55
+ response = authorize_client.delete(f"/posts/{test_posts[0].id}")
56
+ assert response.status_code == 204
57
+
58
+ def test_delete_post_non_exit(authorize_client,test_posts):
59
+ response = authorize_client.delete("/posts/-9999")
60
+ assert response.status_code == 404
61
+
62
+ def test_delete_other_user_post(authorize_client,test_posts):
63
+ response = authorize_client.delete(f"/posts/{test_posts[3].id}")
64
+ assert response.status_code == 403
65
+
66
+ def test_update_post(authorize_client,test_posts):
67
+ data = {
68
+ "title": "MHA",
69
+ "content": "Anime of the Year",
70
+ "id": test_posts[0].id
71
+ }
72
+ response = authorize_client.put(f"/posts/{test_posts[0].id}", json=data)
73
+ updated_post = PostResponse(**response.json())
74
+ assert response.status_code == 200
75
+ assert updated_post.title == data["title"]
76
+
77
+ def test_other_user_post(authorize_client,test_posts):
78
+ data = {
79
+ "title": "MHA",
80
+ "content": "Anime of the Year",
81
+ "id": test_posts[3].id
82
+ }
83
+ response = authorize_client.put(f"/posts/{test_posts[3].id}", json=data)
84
+ assert response.status_code == 403
85
+
86
+ def test_patch_post(authorize_client,test_posts):
87
+ data = {
88
+ "title": "MHA",
89
+ }
90
+ response = authorize_client.patch(f"/posts/{test_posts[0].id}", json=data)
91
+ updated_post = PostResponse(**response.json())
92
+ assert response.status_code == 200
93
+ assert updated_post.title == data["title"]
94
+
95
+ def test_unauthorized_update_post(client,test_posts):
96
+ response = client.put(f"/posts/{test_posts[0].id}")
97
+ assert response.status_code == 401
98
+
99
+ def test_unauthorized_patch_update_post(client,test_posts):
100
+ response = client.patch(f"/posts/{test_posts[0].id}")
101
+ assert response.status_code == 401
102
+
103
+ def test_update_post_non_exit(authorize_client,test_posts):
104
+ data = {"title": "MHA", "content": "Anime of the Year"}
105
+ response = authorize_client.put("/posts/-9999", json=data)
106
+ assert response.status_code == 404
107
+
108
+ def test_patch_update_post_non_exit(authorize_client,test_posts):
109
+ data = {"title": "MHA"}
110
+ response = authorize_client.patch("/posts/-9999", json=data)
111
+ assert response.status_code == 404
tests/test_votes.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from app.models import Votes
3
+ from app.schemas.vote import VoteBase
4
+
5
+ @pytest.fixture
6
+ def test_vote(test_posts,session,create_test_user):
7
+ vote = {
8
+ "post_id": test_posts[3].id,
9
+ "user_id": create_test_user["id"]
10
+ }
11
+ new_vote=Votes(**vote)
12
+ session.add(new_vote)
13
+ session.commit()
14
+
15
+ def test_vote_on_post(authorize_client,test_posts):
16
+ response = authorize_client.post("/vote",json={"post_id": test_posts[3].id, "dir": 1})
17
+ response.status_code == 201
18
+
19
+ def test_vote_twice(authorize_client,test_posts,test_vote):
20
+ response = authorize_client.post("/vote/",json={"post_id": test_posts[3].id, "dir": 1})
21
+ assert response.status_code == 409
22
+
23
+ def test_unvote_on_post(authorize_client,test_posts,test_vote):
24
+ response = authorize_client.post("/vote",json={"post_id": test_posts[3].id, "dir": 0})
25
+ response.status_code == 201
26
+
27
+ def test_unvote_on_post_vote_does_not_exit(authorize_client,test_posts):
28
+ response = authorize_client.post("/vote",json={"post_id": test_posts[3].id, "dir": 0})
29
+ response.status_code == 404
30
+
31
+ def test_unvote_on_post_does_not_exit(authorize_client,test_posts):
32
+ response = authorize_client.post("/vote",json={"post_id": -9999, "dir": 0})
33
+ response.status_code == 404
34
+
35
+ def test_unauthorize_vote_on_post(client,test_posts):
36
+ response = client.post("/vote",json={"post_id": test_posts[3].id, "dir": 1})
37
+ response.status_code == 401