def test_post_created(self): user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id, tags=['java', 'python']) post.save() post = Post(**self.post, owner_id=user.id, tags=['ruby', 'python']) post.save() post = Post(**self.post, owner_id=user.id, tags=['aws', 'azure']) post.save() post = Post(**self.post, owner_id=user.id, tags=['python', 'aws']) post.save() post = Post(**self.post, owner_id=user.id, tags=['java', 'ml']) post.save() res = self.client.get('/tags') self.assertEqual(res.json, [ 'python', 'aws', 'java', 'azure', 'ml', 'ruby', ])
def test_post_deleted(self): user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id) post.save() res = self.authorized_delete(f"/posts/{post.id}", user) self.assertEqual(res.status_code, 200) self.assertEqual(res.json['deleted'], post.id)
def test_post_favorited(self): user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id) post.save() res = self.authorized_post(f"/posts/{post.id}/favorite", user) self.assertEqual(res.status_code, 200) self.assertEqual(res.json['favorites_count'], 1) self.assertEqual(res.json['is_favorited'], True)
def test_post_unfavorited(self): user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id) post.favorited_by = [user] post.save() res = self.authorized_delete(f"/posts/{post.id}/favorite", user) self.assertEqual(res.status_code, 200) self.assertEqual(res.json['favorites_count'], 0) self.assertEqual(res.json['is_favorited'], False)
def test_post_unauthorized_delete(self): user = User(**self.user) user.save() user2 = User('Another', 'another', '*****@*****.**', 'secret') user2.save() post = Post(**self.post, owner_id=user.id) post.save() res = self.authorized_delete(f"/posts/{post.id}", user2) self.assertEqual(res.status_code, 403) self.assertEqual(res.json['message'], 'Permission denied')
def test_comments_retrieved(self): COMMENTS_NUM = 5 user = User(**self.user) user.save() post = Post(owner_id=user.id, **self.post) post.save() [Comment(**self.comment, post_id=post.id, author_id=user.id).save() for _ in range(COMMENTS_NUM)] res = self.client.get(f"/posts/{post.id}/comments") self.assertEqual(res.status_code, 200) self.assertEqual(len(res.json), COMMENTS_NUM)
def test_user_get_me_with_posts(self): user = User(**self.user) user.save() post = {'title': 'A', 'description': 'B', 'contents': 'C'} post = Post(**post, owner_id=user.id) post.save() res = self.authorized_get('/me?include=posts', user) self.assertIn('posts', res.json) posts = res.json['posts'] self.assertEqual(posts[0]['title'], post.title) self.assertEqual(posts[0]['description'], post.description) self.assertEqual(posts[0]['contents'], post.contents)
def test_favorites_retrieved(self): user = User(**self.user) user.save() user2 = User('Another', 'another', '*****@*****.**', 'secret') user2.save() post1 = Post(**self.post, owner_id=user.id) post1.favorited_by = [user, user2] post1.save() post2 = Post(**self.post, owner_id=user.id) post2.favorited_by = [user2] post2.save() post3 = Post(**self.post, owner_id=user.id) post3.favorited_by = [user] post3.save() post5 = Post(**self.post, owner_id=user.id) post5.save() res = self.client.get(f'/@{user.username}/favorites') data = res.json authors = [post['author']['id'] for post in data] posts_ids = [post['id'] for post in data] self.assertEqual(res.status_code, 200) self.assertEqual(len(data), 2) self.assertEqual(authors, [user.id] * 2) self.assertEqual(posts_ids, [post3.id, post1.id])
def test_post_unauthorized_update(self): new_data = { 'title': 'New title', 'description': 'New description', 'contents': 'New contents' } user = User(**self.user) user.save() user2 = User('Another', 'another', '*****@*****.**', 'secret') user2.save() post = Post(**self.post, owner_id=user.id) post.save() res = self.authorized_put(f"/posts/{post.id}", user2, json=new_data) self.assertEqual(res.status_code, 403) self.assertEqual(res.json['message'], 'Permission denied')
def test_comment_created(self): user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id) post.save() res = self.authorized_post( f"/posts/{post.id}/comments", user, json=self.comment, ) data = res.json self.assertEqual(res.status_code, 201) self.assertEqual(data['contents'], self.comment['contents']) self.assertEqual(data['author']['id'], user.id) self.assertIn('created_at', data)
def test_post_retrieved(self): user = User(**self.user) user.save() user2 = User('Another', 'another', '*****@*****.**', 'secret') user2.save() post = Post(**self.post, owner_id=user.id) post.favorited_by = [user, user2] post.save() res = self.client.get(f"/posts/{post.id}") data = res.json self.assertEqual(res.status_code, 200) self.assertEqual(data['title'], self.post['title']) self.assertEqual(data['description'], self.post['description']) self.assertEqual(data['contents'], self.post['contents']) self.assertEqual(data['author']['id'], user.id) self.assertEqual(data['favorites_count'], 2) self.assertEqual(data['is_favorited'], False)
def test_post_updated(self): new_data = { 'title': 'New title', # 'description': 'Not updated' 'contents': 'New contents' } user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id) post.save() res = self.authorized_put(f"/posts/{post.id}", user, json=new_data) data = res.json self.assertEqual(res.status_code, 200) self.assertEqual(data['title'], new_data['title']) self.assertEqual(data['contents'], new_data['contents']) self.assertEqual(data['author']['id'], user.id) # Assert that description remains unchanged self.assertEqual(data['description'], self.post['description'])
def test_comment_updated(self): new_data = { 'contents': 'New contents' } user = User(**self.user) user.save() post = Post(owner_id=user.id, **self.post) post.save() comment = Comment(**self.comment, post_id=post.id, author_id=user.id) comment.save() res = self.authorized_put( f"/posts/{post.id}/comments/{comment.id}", user, json=new_data ) data = res.json self.assertEqual(res.status_code, 200) self.assertEqual(data['contents'], new_data['contents']) self.assertEqual(data['author']['id'], user.id)
def test_tagged_posts_retrieved(self): user = User(**self.user) user.save() post = Post(**self.post, owner_id=user.id) post.tags = ['aws'] post.save() post = Post(**self.post, owner_id=user.id) post.tags = ['aws', 'python'] post.save() post = Post(**self.post, owner_id=user.id) post.tags = ['azure'] post.save() post = Post(**self.post, owner_id=user.id) post.tags = ['js', 'ruby'] post.save() res = self.client.get('/posts?tag=aws') data = res.json self.assertEqual(res.status_code, 200) self.assertEqual(len(data['data']), 2)
def post(self, data): post = Post(**data, owner_id=current_user.id) post.save() return post