Why does this test keep failing?

I'm trying to write a simple test for some endpoints but the second one keeps failing. Here's the test.py

from rest_framework.test import APITestCase, APIRequestFactory
from rest_framework import status
from django.urls import reverse
from .views import PostViewSet
from django.contrib.auth import get_user_model

User = get_user_model()


class PostListCreateTestCase(APITestCase):
    def setUp(self):
        self.factory = APIRequestFactory()
        self.view = PostViewSet.as_view({"get": "list", "post": "create"})
        self.url = reverse("post_list")
        self.user = User.objects.create(
            email="testuser@gmail.com", name="testuser", password="pass"
        )

    def test_list_posts(self):
        request = self.factory.get(self.url)
        response = self.view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_create_post(self):
        sample_post = {
            "title": "sample title",
            "body": "sample body",
        }
        request = self.factory.post(self.url, sample_post)
        request.user = self.user
        response = self.view(request)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
And here's the view:
from rest_framework import viewsets, status
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from .serializers import PostSerializer, LikeSerializer
from .models import Post, Like


class PostViewSet(viewsets.ModelViewSet):
    serializer_class = PostSerializer
    queryset = Post.objects.all()

    def get_queryset(self):
        posts = Post.objects.all()
        return posts

    def get_object(self):
        post = get_object_or_404(self.get_queryset(), pk=self.kwargs["pk"])
        self.check_object_permissions(self.request, post)
        return post

    def create(self, request, *args, **kwargs):
        try:
            post = Post.objects.create(
                title=request.data.get("title"),
                body=request.data.get("body"),
                author=request.user,
            )
            post = PostSerializer(post)
            return Response(post.data, status=status.HTTP_201_CREATED)
        except Exception as ex:
            return Response(str(ex), status=status.HTTP_400_BAD_REQUEST)

    def list(self, request, *args, **kwargs):
        posts = self.get_queryset()
        serializer = self.get_serializer(posts, many=True)
        return Response(
            data=dict(posts=serializer.data, total=len(serializer.data)),
            status=status.HTTP_200_OK,
        )

That's what I get:

Found 2 test(s). Creating test database for alias 'default'... System check identified no issues (0 silenced). F. ====================================================================== FAIL: test_create_post (posts.tests.PostListCreateTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/amr/Snakat/social_network/posts/tests.py", line 31, in test_create_post self.assertEqual(response.status_code, status.HTTP_201_CREATED) AssertionError: 400 != 201

---------------------------------------------------------------------- Ran 2 tests in 0.023s

FAILED (failures=1) Destroying test database for alias 'default'...

Back to Top