2015-01-28 20:35:27 +08:00
|
|
|
from django import forms
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-09-03 06:36:09 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, override_settings
|
2013-06-22 19:12:43 +08:00
|
|
|
from django.test.client import RequestFactory
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2012-08-18 21:46:17 +08:00
|
|
|
from django.views.generic.base import View
|
2023-01-13 16:55:34 +08:00
|
|
|
from django.views.generic.edit import CreateView, FormMixin, ModelFormMixin
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
from . import views
|
2015-11-07 23:47:36 +08:00
|
|
|
from .forms import AuthorForm
|
2011-10-14 05:34:56 +08:00
|
|
|
from .models import Artist, Author
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class FormMixinTests(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
|
|
|
|
2013-02-11 15:39:14 +08:00
|
|
|
def test_initial_data(self):
|
|
|
|
"""Test instance independence of initial data dict (see #16138)"""
|
|
|
|
initial_1 = FormMixin().get_initial()
|
|
|
|
initial_1["foo"] = "bar"
|
|
|
|
initial_2 = FormMixin().get_initial()
|
|
|
|
self.assertNotEqual(initial_1, initial_2)
|
2012-03-18 06:31:03 +08:00
|
|
|
|
2013-06-22 19:12:43 +08:00
|
|
|
def test_get_prefix(self):
|
|
|
|
"""Test prefix can be set (see #18872)"""
|
|
|
|
test_string = "test"
|
|
|
|
|
2018-11-27 03:01:27 +08:00
|
|
|
get_request = self.request_factory.get("/")
|
2013-06-22 19:12:43 +08:00
|
|
|
|
|
|
|
class TestFormMixin(FormMixin):
|
|
|
|
request = get_request
|
|
|
|
|
|
|
|
default_kwargs = TestFormMixin().get_form_kwargs()
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertIsNone(default_kwargs.get("prefix"))
|
2013-06-22 19:12:43 +08:00
|
|
|
|
|
|
|
set_mixin = TestFormMixin()
|
|
|
|
set_mixin.prefix = test_string
|
|
|
|
set_kwargs = set_mixin.get_form_kwargs()
|
|
|
|
self.assertEqual(test_string, set_kwargs.get("prefix"))
|
|
|
|
|
2014-10-15 02:56:39 +08:00
|
|
|
def test_get_form(self):
|
|
|
|
class TestFormMixin(FormMixin):
|
2018-11-27 03:01:27 +08:00
|
|
|
request = self.request_factory.get("/")
|
2014-10-15 02:56:39 +08:00
|
|
|
|
|
|
|
self.assertIsInstance(
|
|
|
|
TestFormMixin().get_form(forms.Form),
|
|
|
|
forms.Form,
|
|
|
|
"get_form() should use provided form class.",
|
|
|
|
)
|
|
|
|
|
|
|
|
class FormClassTestFormMixin(TestFormMixin):
|
|
|
|
form_class = forms.Form
|
|
|
|
|
|
|
|
self.assertIsInstance(
|
|
|
|
FormClassTestFormMixin().get_form(),
|
|
|
|
forms.Form,
|
|
|
|
"get_form() should fallback to get_form_class() if none is provided.",
|
|
|
|
)
|
|
|
|
|
2015-04-15 04:50:36 +08:00
|
|
|
def test_get_context_data(self):
|
|
|
|
class FormContext(FormMixin):
|
2018-11-27 03:01:27 +08:00
|
|
|
request = self.request_factory.get("/")
|
2015-04-15 04:50:36 +08:00
|
|
|
form_class = forms.Form
|
|
|
|
|
|
|
|
self.assertIsInstance(FormContext().get_context_data()["form"], forms.Form)
|
|
|
|
|
2012-12-04 20:18:57 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="generic_views.urls")
|
2012-12-04 20:18:57 +08:00
|
|
|
class BasicFormTests(TestCase):
|
|
|
|
def test_post_data(self):
|
|
|
|
res = self.client.post("/contact/", {"name": "Me", "message": "Hello"})
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2012-12-04 20:18:57 +08:00
|
|
|
|
2015-11-10 21:06:59 +08:00
|
|
|
def test_late_form_validation(self):
|
|
|
|
"""
|
|
|
|
A form can be marked invalid in the form_valid() method (#25548).
|
|
|
|
"""
|
|
|
|
res = self.client.post("/late-validation/", {"name": "Me", "message": "Hello"})
|
|
|
|
self.assertFalse(res.context["form"].is_valid())
|
|
|
|
|
2012-12-04 20:18:57 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class ModelFormMixinTests(SimpleTestCase):
|
2010-12-04 19:20:52 +08:00
|
|
|
def test_get_form(self):
|
|
|
|
form_class = views.AuthorGetQuerySetFormView().get_form_class()
|
2011-08-12 22:14:58 +08:00
|
|
|
self.assertEqual(form_class._meta.model, Author)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2014-02-09 18:53:23 +08:00
|
|
|
def test_get_form_checks_for_object(self):
|
|
|
|
mixin = ModelFormMixin()
|
|
|
|
mixin.request = RequestFactory().get("/")
|
|
|
|
self.assertEqual({"initial": {}, "prefix": None}, mixin.get_form_kwargs())
|
|
|
|
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="generic_views.urls")
|
2010-10-18 21:34:47 +08:00
|
|
|
class CreateViewTests(TestCase):
|
|
|
|
def test_create(self):
|
|
|
|
res = self.client.get("/edit/authors/create/")
|
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res.context["form"], forms.ModelForm)
|
|
|
|
self.assertIsInstance(res.context["view"], View)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn("object", res.context)
|
|
|
|
self.assertNotIn("author", res.context)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/author_form.html")
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/",
|
|
|
|
{"name": "Randall Munroe", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True), ["Randall Munroe"]
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_create_invalid(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/", {"name": "A" * 101, "slug": "randall-munroe"}
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertTemplateUsed(res, "generic_views/author_form.html")
|
|
|
|
self.assertEqual(len(res.context["form"].errors), 1)
|
|
|
|
self.assertEqual(Author.objects.count(), 0)
|
|
|
|
|
|
|
|
def test_create_with_object_url(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post("/edit/artists/create/", {"name": "Rene Magritte"})
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2011-01-04 13:57:32 +08:00
|
|
|
artist = Artist.objects.get(name="Rene Magritte")
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/detail/artist/%d/" % artist.pk)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Artist.objects.all(), [artist])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_create_with_redirect(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/redirect/",
|
|
|
|
{"name": "Randall Munroe", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/edit/authors/create/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True), ["Randall Munroe"]
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2010-12-04 19:20:30 +08:00
|
|
|
def test_create_with_interpolated_redirect(self):
|
2015-01-12 19:00:27 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/interpolate_redirect/",
|
|
|
|
{"name": "Randall Munroe", "slug": "randall-munroe"},
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True), ["Randall Munroe"]
|
2015-01-12 19:00:27 +08:00
|
|
|
)
|
2010-12-04 19:20:30 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-01-12 19:00:27 +08:00
|
|
|
pk = Author.objects.first().pk
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/edit/author/%d/update/" % pk)
|
2015-01-12 19:00:27 +08:00
|
|
|
# Also test with escaped chars in URL
|
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/interpolate_redirect_nonascii/",
|
|
|
|
{"name": "John Doe", "slug": "john-doe"},
|
|
|
|
)
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
pk = Author.objects.get(name="John Doe").pk
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/%C3%A9dit/author/{}/update/".format(pk))
|
2010-12-04 19:20:30 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_create_with_special_properties(self):
|
|
|
|
res = self.client.get("/edit/authors/create/special/")
|
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res.context["form"], views.AuthorForm)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn("object", res.context)
|
|
|
|
self.assertNotIn("author", res.context)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/form.html")
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/special/",
|
|
|
|
{"name": "Randall Munroe", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2010-10-18 23:53:08 +08:00
|
|
|
obj = Author.objects.get(slug="randall-munroe")
|
|
|
|
self.assertRedirects(res, reverse("author_detail", kwargs={"pk": obj.pk}))
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [obj])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_create_without_redirect(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"No URL to redirect to. Either provide a url or define a "
|
|
|
|
"get_absolute_url method on the Model."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-04-08 10:04:45 +08:00
|
|
|
self.client.post(
|
|
|
|
"/edit/authors/create/naive/",
|
|
|
|
{"name": "Randall Munroe", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_create_restricted(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/authors/create/restricted/",
|
|
|
|
{"name": "Randall Munroe", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(
|
|
|
|
res, "/accounts/login/?next=/edit/authors/create/restricted/"
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-02-22 05:56:55 +08:00
|
|
|
def test_create_view_with_restricted_fields(self):
|
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
|
|
|
fields = ["name"]
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(list(MyCreateView().get_form_class().base_fields), ["name"])
|
2013-02-22 05:56:55 +08:00
|
|
|
|
|
|
|
def test_create_view_all_fields(self):
|
2014-03-22 08:44:34 +08:00
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
|
|
|
fields = "__all__"
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(
|
|
|
|
list(MyCreateView().get_form_class().base_fields), ["name", "slug"]
|
|
|
|
)
|
2013-02-22 05:56:55 +08:00
|
|
|
|
|
|
|
def test_create_view_without_explicit_fields(self):
|
2014-03-22 08:44:34 +08:00
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2014-03-22 08:44:34 +08:00
|
|
|
message = (
|
|
|
|
"Using ModelFormMixin (base class of MyCreateView) without the "
|
|
|
|
"'fields' attribute is prohibited."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, message):
|
|
|
|
MyCreateView().get_form_class()
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2014-11-15 19:17:55 +08:00
|
|
|
def test_define_both_fields_and_form_class(self):
|
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
|
|
|
form_class = AuthorForm
|
|
|
|
fields = ["name"]
|
|
|
|
|
|
|
|
message = "Specifying both 'fields' and 'form_class' is not permitted."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, message):
|
|
|
|
MyCreateView().get_form_class()
|
|
|
|
|
2012-08-18 21:46:17 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="generic_views.urls")
|
2010-10-18 21:34:47 +08:00
|
|
|
class UpdateViewTests(TestCase):
|
2018-11-09 22:26:52 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.author = Author.objects.create(
|
|
|
|
pk=1, # Required for OneAuthorUpdate.
|
2010-10-18 21:34:47 +08:00
|
|
|
name="Randall Munroe",
|
|
|
|
slug="randall-munroe",
|
|
|
|
)
|
2018-11-09 22:26:52 +08:00
|
|
|
|
|
|
|
def test_update_post(self):
|
|
|
|
res = self.client.get("/edit/author/%d/update/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res.context["form"], forms.ModelForm)
|
2018-11-09 22:26:52 +08:00
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["author"], self.author)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/author_form.html")
|
2015-12-31 05:22:58 +08:00
|
|
|
self.assertEqual(res.context["view"].get_form_called_count, 1)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
# Modification with both POST and PUT (browser compatible)
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
2018-11-09 22:26:52 +08:00
|
|
|
"/edit/author/%d/update/" % self.author.pk,
|
2016-04-08 10:04:45 +08:00
|
|
|
{"name": "Randall Munroe (xkcd)", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True), ["Randall Munroe (xkcd)"]
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_update_invalid(self):
|
2018-11-09 22:26:52 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/author/%d/update/" % self.author.pk,
|
|
|
|
{"name": "A" * 101, "slug": "randall-munroe"},
|
2010-10-18 21:34:47 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertTemplateUsed(res, "generic_views/author_form.html")
|
|
|
|
self.assertEqual(len(res.context["form"].errors), 1)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [self.author])
|
2015-12-31 05:22:58 +08:00
|
|
|
self.assertEqual(res.context["view"].get_form_called_count, 1)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_update_with_object_url(self):
|
|
|
|
a = Artist.objects.create(name="Rene Magritte")
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/artists/%d/update/" % a.pk, {"name": "Rene Magritte"}
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/detail/artist/%d/" % a.pk)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Artist.objects.all(), [a])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_update_with_redirect(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
2018-11-09 22:26:52 +08:00
|
|
|
"/edit/author/%d/update/redirect/" % self.author.pk,
|
2016-04-08 10:04:45 +08:00
|
|
|
{"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/edit/authors/create/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True),
|
|
|
|
["Randall Munroe (author of xkcd)"],
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2010-12-04 19:20:30 +08:00
|
|
|
def test_update_with_interpolated_redirect(self):
|
2015-01-12 19:00:27 +08:00
|
|
|
res = self.client.post(
|
2018-11-09 22:26:52 +08:00
|
|
|
"/edit/author/%d/update/interpolate_redirect/" % self.author.pk,
|
2015-01-12 19:00:27 +08:00
|
|
|
{"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"},
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True),
|
|
|
|
["Randall Munroe (author of xkcd)"],
|
2015-01-12 19:00:27 +08:00
|
|
|
)
|
2010-12-04 19:20:30 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-01-12 19:00:27 +08:00
|
|
|
pk = Author.objects.first().pk
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/edit/author/%d/update/" % pk)
|
2015-01-12 19:00:27 +08:00
|
|
|
# Also test with escaped chars in URL
|
|
|
|
res = self.client.post(
|
2018-11-09 22:26:52 +08:00
|
|
|
"/edit/author/%d/update/interpolate_redirect_nonascii/" % self.author.pk,
|
2015-01-12 19:00:27 +08:00
|
|
|
{"name": "John Doe", "slug": "john-doe"},
|
|
|
|
)
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
pk = Author.objects.get(name="John Doe").pk
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/%C3%A9dit/author/{}/update/".format(pk))
|
2010-12-04 19:20:30 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_update_with_special_properties(self):
|
2018-11-09 22:26:52 +08:00
|
|
|
res = self.client.get("/edit/author/%d/update/special/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res.context["form"], views.AuthorForm)
|
2018-11-09 22:26:52 +08:00
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["thingy"], self.author)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn("author", res.context)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/form.html")
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
2018-11-09 22:26:52 +08:00
|
|
|
"/edit/author/%d/update/special/" % self.author.pk,
|
2016-04-08 10:04:45 +08:00
|
|
|
{"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2018-11-09 22:26:52 +08:00
|
|
|
self.assertRedirects(res, "/detail/author/%d/" % self.author.pk)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True),
|
|
|
|
["Randall Munroe (author of xkcd)"],
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_update_without_redirect(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"No URL to redirect to. Either provide a url or define a "
|
|
|
|
"get_absolute_url method on the Model."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-04-08 10:04:45 +08:00
|
|
|
self.client.post(
|
2018-11-09 22:26:52 +08:00
|
|
|
"/edit/author/%d/update/naive/" % self.author.pk,
|
2016-04-08 10:04:45 +08:00
|
|
|
{"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"},
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-02-15 16:12:29 +08:00
|
|
|
def test_update_get_object(self):
|
|
|
|
res = self.client.get("/edit/author/update/")
|
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res.context["form"], forms.ModelForm)
|
|
|
|
self.assertIsInstance(res.context["view"], View)
|
2018-11-09 22:26:52 +08:00
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["author"], self.author)
|
2011-02-15 16:12:29 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/author_form.html")
|
|
|
|
|
|
|
|
# Modification with both POST and PUT (browser compatible)
|
2016-04-08 10:04:45 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/author/update/",
|
|
|
|
{"name": "Randall Munroe (xkcd)", "slug": "randall-munroe"},
|
|
|
|
)
|
2011-02-15 16:12:29 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2020-10-19 00:29:52 +08:00
|
|
|
Author.objects.values_list("name", flat=True), ["Randall Munroe (xkcd)"]
|
|
|
|
)
|
2011-02-15 16:12:29 +08:00
|
|
|
|
2012-08-18 21:46:17 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="generic_views.urls")
|
2010-10-18 21:34:47 +08:00
|
|
|
class DeleteViewTests(TestCase):
|
2018-11-14 06:27:24 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.author = Author.objects.create(
|
|
|
|
name="Randall Munroe",
|
|
|
|
slug="randall-munroe",
|
|
|
|
)
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_delete_by_post(self):
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.get("/edit/author/%d/delete/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2018-11-14 06:27:24 +08:00
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["author"], self.author)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/author_confirm_delete.html")
|
|
|
|
|
|
|
|
# Deletion with POST
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.post("/edit/author/%d/delete/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_delete_by_delete(self):
|
|
|
|
# Deletion with browser compatible DELETE method
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.delete("/edit/author/%d/delete/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_delete_with_redirect(self):
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.post("/edit/author/%d/delete/redirect/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/edit/authors/create/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-02-11 15:39:14 +08:00
|
|
|
def test_delete_with_interpolated_redirect(self):
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.post(
|
|
|
|
"/edit/author/%d/delete/interpolate_redirect/" % self.author.pk
|
|
|
|
)
|
2013-02-11 15:39:14 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2018-11-14 06:27:24 +08:00
|
|
|
self.assertRedirects(res, "/edit/authors/create/?deleted=%d" % self.author.pk)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [])
|
2015-01-12 19:00:27 +08:00
|
|
|
# Also test with escaped chars in URL
|
|
|
|
a = Author.objects.create(
|
|
|
|
**{"name": "Randall Munroe", "slug": "randall-munroe"}
|
|
|
|
)
|
|
|
|
res = self.client.post(
|
|
|
|
"/edit/author/{}/delete/interpolate_redirect_nonascii/".format(a.pk)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-01-12 19:00:27 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/%C3%A9dit/authors/create/?deleted={}".format(a.pk))
|
2013-02-11 15:39:14 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_delete_with_special_properties(self):
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.get("/edit/author/%d/delete/special/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2018-11-14 06:27:24 +08:00
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["thingy"], self.author)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn("author", res.context)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertTemplateUsed(res, "generic_views/confirm_delete.html")
|
|
|
|
|
2018-11-14 06:27:24 +08:00
|
|
|
res = self.client.post("/edit/author/%d/delete/special/" % self.author.pk)
|
2010-10-18 21:34:47 +08:00
|
|
|
self.assertEqual(res.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertRedirects(res, "/list/authors/")
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(Author.objects.all(), [])
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_delete_without_redirect(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "No URL to redirect to. Provide a success_url."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2018-11-14 06:27:24 +08:00
|
|
|
self.client.post("/edit/author/%d/delete/naive/" % self.author.pk)
|
2021-07-13 22:06:12 +08:00
|
|
|
|
|
|
|
def test_delete_with_form_as_post(self):
|
|
|
|
res = self.client.get("/edit/author/%d/delete/form/" % self.author.pk)
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["author"], self.author)
|
|
|
|
self.assertTemplateUsed(res, "generic_views/author_confirm_delete.html")
|
|
|
|
res = self.client.post(
|
|
|
|
"/edit/author/%d/delete/form/" % self.author.pk, data={"confirm": True}
|
|
|
|
)
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, "/list/authors/")
|
|
|
|
self.assertSequenceEqual(Author.objects.all(), [])
|
|
|
|
|
|
|
|
def test_delete_with_form_as_post_with_validation_error(self):
|
|
|
|
res = self.client.get("/edit/author/%d/delete/form/" % self.author.pk)
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(res.context["object"], self.author)
|
|
|
|
self.assertEqual(res.context["author"], self.author)
|
|
|
|
self.assertTemplateUsed(res, "generic_views/author_confirm_delete.html")
|
|
|
|
|
|
|
|
res = self.client.post("/edit/author/%d/delete/form/" % self.author.pk)
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertEqual(len(res.context_data["form"].errors), 2)
|
|
|
|
self.assertEqual(
|
|
|
|
res.context_data["form"].errors["__all__"],
|
|
|
|
["You must confirm the delete."],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
res.context_data["form"].errors["confirm"],
|
|
|
|
["This field is required."],
|
|
|
|
)
|