Kuracha
Kuracha

Reputation: 321

Django API test: expected status code 200, instead received 301

I'm trying to create tests for my API views but I'm receiving status code 301 which mean redirection instead status 200 and 201. In first test help adding follow=True, but If I'm right "follow" just force my url response so this is pointless. This is my code:

test_api.py

from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile

from rest_framework.test import APITestCase, APIClient

from ..models import Category, Product, Comment


class CreateCommentAPI(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_user(username='test', password='test123')
        self.user.save()

    @classmethod
    def setUpTestData(cls):
        Category.objects.create(name='PC', slug='pc')
        Product.objects.create(
            category=Category.objects.get(id=1),
            name='Laptop', slug='laptop',
            description='here is description',
            photo=SimpleUploadedFile("file.jpeg", b"file_content", content_type="image/jpeg"),
            price=1999, available='available'
        )

    def test_logged_in_access_to_view(self):
        product = Product.objects.get(id=1)
        login = self.client.login(username='test', password='test123')
        response = self.client.get(f'/api/add_comments/{product.id}')
        self.assertTrue(login)
        self.assertEqual(response.status_code, 200, f'expected Response code 200, instead get {response.status_code}')

    def test_post_logged_in(self):
        product = Product.objects.get(id=1)
        self.client.login(username='test', password='test123')
        comment = {
            'nick': self.user.id,
            'rate': '1/5',
            'content': 'here is comment',
            'product': product.id
        }
        response = self.client.post(f'/api/add_comments/{product.id}/', comment, format='json')
        self.assertEqual(response.data, comment)
        self.assertEqual(response.status_code, 201, f'expected status code 201, instead get{response.status_code}')

views.py

class CreateComment(APIView):

    def get_object(self, id):
        try:
            return Product.objects.get(id=id)
        except Product.DoesNotExist:
            raise Http404

    def get(self,request,  id):
        product = self.get_object(id)
        serializer = ProductSerializer(product)
        return Response(serializer.data)

    def post(self, request,id):
        serializer = CommentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(nick=request.user, product=self.get_object(id))
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Upvotes: 2

Views: 1663

Answers (1)

Michał Jabłoński
Michał Jabłoński

Reputation: 1277

You should put a slash at the end of your api endpoint url. For example, instead of response = self.client.get(f'/api/add_comments/{product.id}') you should have used response = self.client.get(f'/api/add_comments/{product.id}/').

Upvotes: 3

Related Questions