MCPcopy
hub / github.com/django/django / test_add_view

Method test_add_view

tests/admin_views/tests.py:2689–2811  ·  view source on GitHub ↗

Test add view restricts access and actually adds items.

(self)

Source from the content-addressed store, hash-verified

2687 )
2688
2689 def test_add_view(self):
2690 """Test add view restricts access and actually adds items."""
2691 add_dict = {
2692 "title": "Døm ikke",
2693 "content": "<p>great article</p>",
2694 "date_0": "2008-03-18",
2695 "date_1": "10:54:39",
2696 "section": self.s1.pk,
2697 }
2698 # Change User should not have access to add articles
2699 self.client.force_login(self.changeuser)
2700 # make sure the view removes test cookie
2701 self.assertIs(self.client.session.test_cookie_worked(), False)
2702 response = self.client.get(reverse("admin:admin_views_article_add"))
2703 self.assertEqual(response.status_code, 403)
2704 # Try POST just to make sure
2705 post = self.client.post(reverse("admin:admin_views_article_add"), add_dict)
2706 self.assertEqual(post.status_code, 403)
2707 self.assertEqual(Article.objects.count(), 3)
2708 self.client.post(reverse("admin:logout"))
2709
2710 # View User should not have access to add articles
2711 self.client.force_login(self.viewuser)
2712 response = self.client.get(reverse("admin:admin_views_article_add"))
2713 self.assertEqual(response.status_code, 403)
2714 # Try POST just to make sure
2715 post = self.client.post(reverse("admin:admin_views_article_add"), add_dict)
2716 self.assertEqual(post.status_code, 403)
2717 self.assertEqual(Article.objects.count(), 3)
2718 # Now give the user permission to add but not change.
2719 self.viewuser.user_permissions.add(
2720 get_perm(Article, get_permission_codename("add", Article._meta))
2721 )
2722 response = self.client.get(reverse("admin:admin_views_article_add"))
2723 self.assertEqual(response.context["title"], "Add article")
2724 self.assertContains(response, "<title>Add article | Django site admin</title>")
2725 self.assertContains(
2726 response, '<input type="submit" value="Save and view" name="_continue">'
2727 )
2728 self.assertContains(
2729 response,
2730 '<h2 id="fieldset-0-0-heading" class="fieldset-heading">Some fields</h2>',
2731 )
2732 self.assertContains(
2733 response,
2734 '<h2 id="fieldset-0-1-heading" class="fieldset-heading">'
2735 "Some other fields</h2>",
2736 )
2737 self.assertContains(
2738 response,
2739 '<h2 id="fieldset-0-2-heading" class="fieldset-heading">이름</h2>',
2740 )
2741 post = self.client.post(
2742 reverse("admin:admin_views_article_add"), add_dict, follow=False
2743 )
2744 self.assertEqual(post.status_code, 302)
2745 self.assertEqual(Article.objects.count(), 4)
2746 article = Article.objects.latest("pk")

Callers

nothing calls this directly

Calls 15

reverseFunction · 0.90
get_permission_codenameFunction · 0.90
force_loginMethod · 0.80
test_cookie_workedMethod · 0.80
assertContainsMethod · 0.80
latestMethod · 0.80
assertNotContainsMethod · 0.80
assertRedirectsMethod · 0.80
lastMethod · 0.80
get_for_modelMethod · 0.80
get_change_messageMethod · 0.80
get_permFunction · 0.70

Tested by

no test coverage detected