mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2024-11-14 05:36:58 +03:00
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:
parent
d2ec1eca12
commit
894dbce645
|
@ -1,8 +1,12 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
from django.contrib import messages
|
||||||
from django.contrib.auth.models import AnonymousUser
|
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.http.response import Http404
|
||||||
from django.test import RequestFactory
|
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.models import User
|
||||||
from {{ cookiecutter.project_slug }}.users.tests.factories import UserFactory
|
from {{ cookiecutter.project_slug }}.users.tests.factories import UserFactory
|
||||||
from {{ cookiecutter.project_slug }}.users.views import (
|
from {{ cookiecutter.project_slug }}.users.views import (
|
||||||
|
@ -41,6 +45,24 @@ class TestUserUpdateView:
|
||||||
|
|
||||||
assert view.get_object() == user
|
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:
|
class TestUserRedirectView:
|
||||||
def test_get_redirect_url(self, user: User, rf: RequestFactory):
|
def test_get_redirect_url(self, user: User, rf: RequestFactory):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user