def find_by_id(self, id) -> Response: blogpost: Blogpost = Blogpost.find_by_id(id) if blogpost is None: raise NotFound(f"No blogpost found with id={id}") resp_dict = blogpost.to_dict() resp_dict.update(user=blogpost.user.to_dict()) return self.make_json_response([resp_dict])
def delete_blogpost(self, id) -> Response: post: Blogpost = Blogpost.find_by_id(id) if post is None: raise NotFound(f"No blogpost found with id={id}") post.delete() return self.make_json_response( dict(message="Blogpost successfully deleted") )
def test_post_blogposts_correct() -> None: with CustomTestClient() as c: user = User("jane", "asd123") user.save() user_id = user.id blogpost = dict( headline="This is my headline", body="This is my body") token = auth.create_jwt_token(user_id, user.username, ["ADMIN"]) c.set_cookie('localhost:5000', 'Authorization', token) res = c.post( '/api/v1/blogposts', data=json.dumps(blogpost), content_type="application/json" ) data = res.get_json() assert data["message"] == "Blogpost successfully created" assert res.status_code == 200 blogpost_2 = dict( headline="This is my other headline", body="This is my second body text different from the first!") res_2 = c.post( '/api/v1/blogposts', data=json.dumps(blogpost_2), content_type="application/json" ) data_2 = res_2.get_json() assert data_2["message"] == "Blogpost successfully created" assert res_2.status_code == 200 user = User.find_by_id(user_id) assert user.blogposts is not None assert user.blogposts[0].headline == "This is my headline" assert user.blogposts[0].body == "This is my body" assert user.blogposts[1].headline == "This is my other headline" assert user.blogposts[1].body == \ "This is my second body text different from the first!" blogpost_1 = Blogpost.find_by_id(user.blogposts[0].id) blogpost_2 = Blogpost.find_by_id(user.blogposts[1].id) assert blogpost_1.user is not None assert blogpost_2.user is not None assert blogpost_1.author_id == user_id assert blogpost_2.author_id == user_id