[PYTHON] How to test on a Django-authenticated page

Django is fairly easy to test, but I'm addicted to a simple access test on a page that requires authentication this time, so I'll write it down.

test.py


from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User

from .views import SomeGenericView


class TestSomeView(TestCase):
    def test_some_auth_page(self):
        user = User.objects.create_user(...)  #Create login user
        rf = RequestFactory()
        request = rf.get("/some/auth/page")
        request.user = user
        res = SomeGenericView.as_view()(request)
        
        self.assertEqual(res.status_code, 200)

It was a way to create a request object with RequestFactory () and poke the user who wants to authenticate to it.

Click here for documentation Advanced testing topics | Django documentation | Django

Is there a way that is a little easier to do ...

By the way, when you want to POST

test_post.py


class TestSomeView2(TestCase):
    def test_some_auth_page_post(self):
        user = ...  #Same as above
        
        rf = RequestFactory()
        req = rf.post("/some/post/url", data={"edit": "value"})  #Only here is different
        req.user = user  #Push the user
        res = SomeGenericView.as_view()(req)
        
        self.assertEqual(res.status_code, 302)

I think I can go there.

Recommended Posts

How to test on a Django-authenticated page
How to live a decent life on 2017 Windows
How to deploy a Django application on Alibaba Cloud
How to call a function
How to install Linux on a 32bit UEFI PC
A memorandum on how to use keras.preprocessing.image in Keras
How to register on pypi
How to hack a terminal
How to run Django on IIS on a Windows server
How to build a Python environment on amazon linux 2
How to use GitHub on a multi-person server without a password
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
A memo on how to easily prepare a Linux exercise environment
How to run a trained transformer model locally on CloudTPU
How to build a new python virtual environment on Ubuntu
How to make a multiplayer online action game on Slack
How to mount a Windows 10 directory on Ubuntu-Server 20.04 on VMware Workstation 15
A note on how to load a virtual environment in PyCharm
How to write a test for processing that uses BigQuery
How to register a package on PyPI (as of September 2017)
How to get a list of links from a page from wikipedia
How to make a Japanese-English translation
How to write a Python class
[Django] How to test Form [TDD]
How to put a symbolic link
How to install mysql-connector-python on mac
How to use Dataiku on Windows
Notes on how to use pywinauto
How to make a slack bot
How to create a Conda package
How to install graph-tool on macOS
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to make a crawler --Advanced
How to deploy django-compressor on Windows
Notes on how to use featuretools
How to make a recursive function
How to create a virtual bridge
How to run matplotlib on heroku
How to install PyPy on CentOS
How to use homebrew on Debian
Misunderstanding on how to connect cnn
How to install TensorFlow on CentOS 7
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to make a crawler --Basic
Notes on how to use doctest
How to install Maven on CentOS
Notes on how to write requirements.txt
How to install Go on Ubuntu
How to create a config file
How to install music 21 on windows
A note on how to check the connection to the license server port
How easy is it to synthesize a drug on the market?
How to customize U-Boot with OSD335X on a custom board (memo)
How to make a .dylib library from a .a library on OSX (El Capitan)
How to make a unit test Part.1 Design pattern for introduction
How to create a large amount of test data in MySQL? ??
How to deploy a Django app on heroku in just 5 minutes