Beispiel #1
0
    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])
Beispiel #2
0
    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)
Beispiel #3
0
    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)