Django Rest Framework - How to test ViewSet?

前端 未结 5 1721
萌比男神i
萌比男神i 2021-01-31 09:24

I\'m having trouble testing a ViewSet:

class ViewSetTest(TestCase):
    def test_view_set(self):
        factory = APIRequestFactory()
        view = CatViewSet.         


        
5条回答
  •  余生分开走
    2021-01-31 10:07

    I needed to get this working with force authentication, and finally got it, here is what my test case looks like:

    from django.test import TestCase
    from rest_framework.test import APIRequestFactory
    from django.db.models.query import QuerySet
    from rest_framework.test import force_authenticate
    from django.contrib.auth.models import User
    
    from config_app.models import Config
    from config_app.apps import ConfigAppConfig
    from config_app.views import ConfigViewSet
    
    class ViewsTestCase(TestCase):
        def setUp(self):
            # Create a test instance
            self.config = Config.objects.create(
                ads='{"frequency": 1, "site_id": 1, "network_id": 1}',
                keys={}, methods={}, sections=[], web_app='{"image": 1, "label": 1, "url": 1}',
                subscriptions=[], name='test name', build='test build', version='1.0test', device='desktop',
                platform='android', client_id=None)
    
            # Create auth user for views using api request factory
            self.username = 'config_tester'
            self.password = 'goldenstandard'
            self.user = User.objects.create_superuser(self.username, 'test@example.com', self.password)
    
        def tearDown(self):
            pass
    
        @classmethod
        def setup_class(cls):
            """setup_class() before any methods in this class"""
            pass
    
        @classmethod
        def teardown_class(cls):
            """teardown_class() after any methods in this class"""
            pass
    
        def shortDescription(self):
            return None
    
    
        def test_view_set1(self):
            """
            No auth example
            """
            api_request = APIRequestFactory().get("")
            detail_view = ConfigViewSet.as_view({'get': 'retrieve'})
            response = detail_view(api_request, pk=self.config.pk)
            self.assertEqual(response.status_code, 401)
    
        def test_view_set2(self):
            """
            Auth using force_authenticate
            """
            factory = APIRequestFactory()
            user = User.objects.get(username=self.username)
            detail_view = ConfigViewSet.as_view({'get': 'retrieve'})
    
            # Make an authenticated request to the view...
            api_request = factory.get('')
            force_authenticate(api_request, user=user)
            response = detail_view(api_request, pk=self.config.pk)
            self.assertEqual(response.status_code, 200)
    

    I'm using this with the django-nose test runner and it seems to be working well. Hope it helps those that have auth enabled on their viewsets.

提交回复
热议问题