1+ import email
12import json
23
4+ import pytest
5+
36# from app.routers import posts
7+ from app .routers import auth
48import tests .debug
59import schemas .post
610
711
812def setup_module ():
9- tests .debug .DEBUG = True
13+ tests .debug .DEBUG = False
1014
1115def test_get_all_posts (authorized_client , add_fake_posts ):
1216 response = authorized_client .get ('/posts' )
@@ -29,7 +33,79 @@ def test_unauthorized_get_all_posts(client, add_fake_posts):
2933 response = client .get ('/posts' )
3034 assert response .status_code == 401
3135
32- def test_unauthorized_get_one_posts (client , add_fake_posts ):
36+ def test_unauthorized_get_one_post (client , add_fake_posts ):
3337 response = client .get (f'/posts/{ add_fake_posts [0 ].id } ' )
3438 assert response .status_code == 401
3539
40+ def test_get_one_posts_not_exist (authorized_client , add_fake_posts ):
41+ response = authorized_client .get (
42+ f'/posts/-1' )
43+ assert response .status_code == 404
44+
45+ def test_get_one_post (authorized_client , add_fake_posts ):
46+ response = authorized_client .get (f'/posts/{ add_fake_posts [0 ].id } ' )
47+ assert response .status_code == 200
48+ post = schemas .post .Post (** response .json ())
49+ assert post .id == add_fake_posts [0 ].id
50+
51+ @pytest .mark .parametrize (
52+ 'title, content, published' ,
53+ [
54+ ('new title' , 'new content' , True ),
55+ ('new second title' , 'new second content' , False ),
56+ ('new third title' , 'new third content' , True ),
57+ ]
58+ )
59+ def test_create_post (
60+ authorized_client ,
61+ new_user ,
62+ add_fake_posts ,
63+ title ,
64+ content ,
65+ published
66+ ):
67+ response = authorized_client .post (
68+ 'posts' ,
69+ json = dict (
70+ title = title ,
71+ content = content ,
72+ published = published
73+ )
74+ )
75+ assert response .status_code == 201
76+ post = schemas .post .Post (** response .json ())
77+ assert post .title == title
78+ assert post .content == content
79+ assert post .published == published
80+ assert post .owner_id == new_user .id
81+ assert post .owner .email == new_user .email
82+
83+ @pytest .mark .parametrize (
84+ 'title, content' ,
85+ [
86+ ('new title' , 'new content' ),
87+ ('new second title' , 'new second content' ),
88+ ('new third title' , 'new third content' ),
89+ ]
90+ )
91+ def test_create_post_default_published_true (
92+ authorized_client ,
93+ new_user ,
94+ add_fake_posts ,
95+ title ,
96+ content ,
97+ ):
98+ response = authorized_client .post (
99+ 'posts' ,
100+ json = dict (
101+ title = title ,
102+ content = content ,
103+ )
104+ )
105+ assert response .status_code == 201
106+ post = schemas .post .Post (** response .json ())
107+ assert post .title == title
108+ assert post .content == content
109+ assert post .published == True
110+ assert post .owner_id == new_user .id
111+ assert post .owner .email == new_user .email
0 commit comments