Add test for users.form_valid

Ensures that the message is added to the request when a form is submitted
This commit is contained in:
Thorrak 2020-11-26 10:17:20 -05:00
parent d2ec1eca12
commit 894dbce645

View File

@ -1,8 +1,12 @@
import pytest
from django.contrib import messages
from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.http.response import Http404
from django.test import RequestFactory
from {{ cookiecutter.project_slug }}.users.forms import UserChangeForm
from {{ cookiecutter.project_slug }}.users.models import User
from {{ cookiecutter.project_slug }}.users.tests.factories import UserFactory
from {{ cookiecutter.project_slug }}.users.views import (
@ -41,6 +45,24 @@ class TestUserUpdateView:
assert view.get_object() == user
def test_form_valid(self, user: User, rf: RequestFactory):
view = UserUpdateView()
request = rf.get("/fake-url/")
# Add the session/message middleware to the request
SessionMiddleware().process_request(request)
MessageMiddleware().process_request(request)
request.user = user
view.request = request
# Initialize the form
form = UserChangeForm()
form.cleaned_data = []
view.form_valid(form)
assert messages.get_messages(request)._queued_messages[0].message == "Information successfully updated"
class TestUserRedirectView:
def test_get_redirect_url(self, user: User, rf: RequestFactory):