diff --git a/class_social/db.py b/class_social/db.py index 1ef7ac8..b81242d 100644 --- a/class_social/db.py +++ b/class_social/db.py @@ -21,6 +21,11 @@ def load_users(): return [] +def save_posts(posts): + with open('posts.pickle', 'wb') as file: + pickle.dump(posts, file) + + def load_posts(): try: with open('posts.pickle', 'rb') as file: diff --git a/class_social/models.py b/class_social/models.py index 08711bb..a7904db 100644 --- a/class_social/models.py +++ b/class_social/models.py @@ -16,6 +16,7 @@ class User(BaseModel): address: str website: Optional[str] connections: Optional[List['User']] + # connections: Optional[List['User']] about: Optional[str] role: Optional[str] @@ -71,8 +72,8 @@ class Post(BaseModel): creator: User creation_date: datetime comments_on_this_post: Optional[List[str]] - likes_for_this_post: Optional[List[Union[Student, Teacher, Institution]]] - shares_for_this_post: Optional[List[Union[Student, Teacher, Institution]]] + likes_for_this_post: Optional[List[str]] + #likes_for_this_post: Optional[List[Union['Student', 'Teacher', 'Institution']]] + #shares_for_this_post: Optional[List[Union['Student', 'Teacher', 'Institution']]] + shares_for_this_post: Optional[List[str]] type_of_post: str - - diff --git a/class_social/posts.py b/class_social/posts.py index b450e1e..55f9c31 100644 --- a/class_social/posts.py +++ b/class_social/posts.py @@ -1,5 +1,7 @@ -from fastapi import APIRouter, HTTPException +from fastapi import HTTPException, APIRouter + from class_social import db +from class_social.models import Post from class_social.db import DBException @@ -8,33 +10,53 @@ class PostControllerError(Exception): class PostController: - def get_posts_by_creator_role(self, role): - result_post = [] - posts_list = db.load_posts() + def post_a_post(self, post): + try: + post_list = db.load_posts() + post_list.append(post) + db.save_posts(post_list) + except DBException: + raise PostControllerError('Wrong format') + def get_posts_by_username(self, username): + posts_list = db.load_posts() + user_posts = [] for post in posts_list: + if post.creator.username == username: + user_posts.append(post) + return user_posts + def get_posts_by_creator_role(self, role): + result_post = [] + posts_list = db.load_posts() + for post in posts_list: if post.creator.role == role: - result_post.append(post) - return result_post - # API Routes - posts_routes = APIRouter() post_controller = PostController() +@post_routes.post('/posts') +def insert_posts(post: Post) -> Post: + post = post_controller.post_a_post(post) + return post + +@post_routes.get('/posts/{username}') +def get_posts_by_username(username: str): + post = post_controller.get_posts_by_username(username) + if post is not None: + return post + + raise HTTPException(status_code=404) @posts_routes.get('/posts/creator/{role}') def get_posts_by_creator_role(role: str): post = post_controller.get_posts_by_creator_role(role) - if post is not None: return post raise HTTPException(status_code=404) - diff --git a/tests/test_post_routes.py b/tests/test_post_routes.py index 8ddc22f..7acf789 100644 --- a/tests/test_post_routes.py +++ b/tests/test_post_routes.py @@ -1,9 +1,7 @@ import datetime from unittest.mock import patch, Mock - import pytest from fastapi.testclient import TestClient - from class_social.main import app @@ -11,6 +9,32 @@ def http_test_client(): return TestClient(app) +post_user = { + "id": '1234', + "name": 'Wasi', + "username": 'wasi', + "password": 'somepass', + "email": 'wasi@mathias', + "created_on": "2023-03-27T00:00:00.000+00:00", + "is_active": True, + "address": 'some address 123' +} + # {"w1","wasi", "123456w","wasi@atwasi.c","2023-03-27T00:00:00.000+00:00",True,"Wasi","someaddress 123","wasi.co","con1","about me about me"}, # TODO a user dictionary here OR remove it completely!! + + +valid_post = { + "title": 'Our First Post', + "body": 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam' \ + ' nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, s' \ + 'ed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd g' \ + 'ubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ' \ + 'sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo' \ + ' duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.', + "creator": post_user, # TODO a user dictionary here OR remove it completely!! + "creation_date": '2023-03-27T00:00:00.000+00:00', + "type_of_post": 'some type' +} + valid_post_list = [{ 'title': 'Some_title', 'body': 'text_content', @@ -42,3 +66,15 @@ def test_given_a_nonexistent_creator_role_system_must_return_404_ok(http_test_cl controller_mock.get_posts_by_creator_role = Mock(return_value=None) response = http_test_client.get('/posts/creator/anything') assert response.status_code == 404 + + +def test_given_invalid_new_post_format_422(http_test_client): + response = http_test_client.post('/posts', json={}) + assert response.status_code == 422 + + +def test_given_valid_new_post_entry_the_system_must_submit_the_post_and_return_200_ok(http_test_client): + with patch('class_social.posts.post_controller') as controller_mock: + controller_mock.post_a_post = Mock(return_value=None) + response = http_test_client.post('/posts', json=valid_post) + assert response.status_code == 200