-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathtests.py
More file actions
188 lines (154 loc) · 6.93 KB
/
Copy pathtests.py
File metadata and controls
188 lines (154 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from __future__ import absolute_import
import random
from django.core.exceptions import ValidationError
from django.http.request import HttpRequest
from django.urls import reverse
from django.utils.text import slugify
from test_plus import TestCase
from ..context_processors import scoped
from ..models import Blog, Post, Section
ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"
def randomword(length):
return "".join(random.choice(ascii_lowercase) for i in range(length))
class TestBlog(TestCase):
def setUp(self):
"""
Create default Sections and Posts.
"""
self.blog = Blog.objects.first()
self.apples = Section.objects.create(name="Apples", slug="apples")
self.oranges = Section.objects.create(name="Oranges", slug="oranges")
self.user = self.make_user("patrick")
self.markup = "markdown"
# Create two published Posts, one in each section.
self.orange_title = "Orange You Wonderful"
self.orange_subtitle = "Subtitle for you orange"
self.orange_slug = slugify(self.orange_title)
self.orange_post = Post.objects.create(blog=self.blog,
section=self.oranges,
title=self.orange_title,
subtitle=self.orange_subtitle,
slug=self.orange_slug,
author=self.user,
markup=self.markup,
state=Post.STATE_CHOICES[-1][0])
self.apple_title = "Apple of My Eye"
self.apple_subtitle = "Subtitle for you apple"
self.apple_slug = slugify(self.apple_title)
self.apple_post = Post.objects.create(blog=self.blog,
section=self.apples,
title=self.apple_title,
subtitle=self.apple_subtitle,
slug=self.apple_slug,
author=self.user,
markup=self.markup,
state=Post.STATE_CHOICES[-1][0])
class TestViewGetSection(TestBlog):
def test_invalid_section_slug(self):
"""
Ensure invalid section slugs do not cause site crash.
"""
invalid_slug = "bananas"
url = reverse("pinax_blog:blog_section", kwargs={"section": invalid_slug})
try:
response = self.get(url)
except Section.DoesNotExist:
self.fail("section '{}' does not exist".format(invalid_slug))
self.assertEqual(response.status_code, 404)
def test_valid_section_slug(self):
"""
Verify that existing section slug works fine
"""
valid_slug = "oranges"
url = reverse("pinax_blog:blog_section", kwargs={"section": valid_slug})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
class TestViewGetPosts(TestBlog):
def test_section_posts(self):
"""
Verify only the expected Post is in context for section "orange".
"""
url = reverse("pinax_blog:blog_section", kwargs={"section": "oranges"})
response = self.client.get(url)
self.assertIn(self.orange_post, response.context_data["post_list"])
self.assertNotIn(self.apple_post, response.context_data["post_list"])
def test_all_posts(self):
"""
Verify all Posts are in context for All.
"""
url = reverse("pinax_blog:blog")
response = self.client.get(url)
self.assertEqual(response.context_data["post_list"].count(), 2)
self.assertIn(self.orange_post, response.context_data["post_list"])
self.assertIn(self.apple_post, response.context_data["post_list"])
class TestModelFieldValidation(TestBlog):
def test_overlong_slug(self):
title_len = Post._meta.get_field("title").max_length
title = randomword(title_len)
subtitle_len = Post._meta.get_field("subtitle").max_length
subtitle = randomword(subtitle_len)
slug_len = Post._meta.get_field("slug").max_length
slug = randomword(slug_len + 1)
slug_post = Post(blog=self.blog,
section=self.apples,
title=title,
subtitle=subtitle,
slug=slug,
author=self.user,
state=Post.STATE_CHOICES[-1][0])
with self.assertRaises(ValidationError) as context_manager:
slug_post.save()
the_exception = context_manager.exception
self.assertIn(
"Ensure this value has at most {} characters (it has {})."
.format(slug_len, len(slug)),
the_exception.messages
)
class TestContextProcessors(TestBlog):
def test_no_resolver_match(self):
"""
Ensure no problem when `request.resolver_match` is None
"""
request = HttpRequest()
self.assertEqual(request.resolver_match, None)
result = scoped(request)
self.assertEqual(result, {"scoper_lookup": ""})
class TestViews(TestBlog):
def test_manage_post_create_get(self):
"""
Ensure template with external URL references renders properly
for user with proper credentials.
"""
with self.login(self.user):
response = self.client.get("pinax_blog:manage_post_create")
self.assertEqual(response.status_code, 404)
self.user.is_staff = True
self.user.save()
with self.login(self.user):
self.get("pinax_blog:manage_post_create")
self.response_200()
self.assertTemplateUsed("pinax/blog/manage_post_create")
pinax_images_upload_url = reverse("pinax_images:imageset_new_upload")
self.assertResponseContains(pinax_images_upload_url, html=False)
def test_manage_post_create_post(self):
"""
Ensure template with external URL references renders properly
for user with proper credentials.
"""
self.user.is_staff = True
self.user.save()
post_title = "You'll never believe what happened next!"
post_subtitle = "never believe what happened subtitle!"
post_data = dict(
section=self.apples.pk,
title=post_title,
subtitle=post_subtitle,
teaser="teaser",
content="content",
description="description",
state=Post.STATE_CHOICES[-1][0],
)
with self.login(self.user):
self.post("pinax_blog:manage_post_create", data=post_data)
self.assertRedirects(self.last_response, reverse("pinax_blog:manage_post_list"))
self.assertTrue(Post.objects.get(title=post_title))