3
from django.test import TestCase
4
from django.core.exceptions import ImproperlyConfigured
5
from regressiontests.views.models import Article, UrlArticle
7
class CreateObjectTest(TestCase):
9
fixtures = ['testdata.json']
11
def test_login_required_view(self):
13
Verifies that an unauthenticated user attempting to access a
14
login_required view gets redirected to the login page and that
15
an authenticated user is let through.
17
view_url = '/views/create_update/member/create/article/'
18
response = self.client.get(view_url)
19
self.assertRedirects(response, '/accounts/login/?next=%s' % view_url)
20
# Now login and try again.
21
login = self.client.login(username='testclient', password='password')
22
self.failUnless(login, 'Could not log in')
23
response = self.client.get(view_url)
24
self.assertEqual(response.status_code, 200)
25
self.assertTemplateUsed(response, 'views/article_form.html')
27
def test_create_article_display_page(self):
29
Ensures the generic view returned the page and contains a form.
31
view_url = '/views/create_update/create/article/'
32
response = self.client.get(view_url)
33
self.assertEqual(response.status_code, 200)
34
self.assertTemplateUsed(response, 'views/article_form.html')
35
if not response.context.get('form'):
36
self.fail('No form found in the response.')
38
def test_create_article_with_errors(self):
40
POSTs a form that contains validation errors.
42
view_url = '/views/create_update/create/article/'
43
num_articles = Article.objects.count()
44
response = self.client.post(view_url, {
45
'title': 'My First Article',
47
self.assertFormError(response, 'form', 'slug', [u'This field is required.'])
48
self.assertTemplateUsed(response, 'views/article_form.html')
49
self.assertEqual(num_articles, Article.objects.count(),
50
"Number of Articles should not have changed.")
52
def test_create_custom_save_article(self):
54
Creates a new article using a custom form class with a save method
55
that alters the slug entered.
57
view_url = '/views/create_update/create_custom/article/'
58
response = self.client.post(view_url, {
59
'title': 'Test Article',
60
'slug': 'this-should-get-replaced',
62
'date_created': datetime.datetime(2007, 6, 25),
64
self.assertRedirects(response,
65
'/views/create_update/view/article/some-other-slug/',
66
target_status_code=404)
68
class UpdateDeleteObjectTest(TestCase):
70
fixtures = ['testdata.json']
72
def test_update_object_form_display(self):
74
Verifies that the form was created properly and with initial values.
76
response = self.client.get('/views/create_update/update/article/old_article/')
77
self.assertTemplateUsed(response, 'views/article_form.html')
78
self.assertEquals(unicode(response.context['form']['title']),
79
u'<input id="id_title" type="text" name="title" value="Old Article" maxlength="100" />')
81
def test_update_object(self):
83
Verifies the updating of an Article.
85
response = self.client.post('/views/create_update/update/article/old_article/', {
86
'title': 'Another Article',
87
'slug': 'another-article-slug',
89
'date_created': datetime.datetime(2007, 6, 25),
91
article = Article.objects.get(pk=1)
92
self.assertEquals(article.title, "Another Article")
94
def test_delete_object_confirm(self):
96
Verifies the confirm deletion page is displayed using a GET.
98
response = self.client.get('/views/create_update/delete/article/old_article/')
99
self.assertTemplateUsed(response, 'views/article_confirm_delete.html')
101
def test_delete_object(self):
103
Verifies the object actually gets deleted on a POST.
105
view_url = '/views/create_update/delete/article/old_article/'
106
response = self.client.post(view_url)
108
Article.objects.get(slug='old_article')
109
except Article.DoesNotExist:
112
self.fail('Object was not deleted.')
114
class PostSaveRedirectTests(TestCase):
116
Verifies that the views redirect to the correct locations depending on
117
if a post_save_redirect was passed and a get_absolute_url method exists
121
fixtures = ['testdata.json']
122
article_model = Article
124
create_url = '/views/create_update/create/article/'
125
update_url = '/views/create_update/update/article/old_article/'
126
delete_url = '/views/create_update/delete/article/old_article/'
128
create_redirect = '/views/create_update/view/article/my-first-article/'
129
update_redirect = '/views/create_update/view/article/another-article-slug/'
130
delete_redirect = '/views/create_update/'
132
def test_create_article(self):
133
num_articles = self.article_model.objects.count()
134
response = self.client.post(self.create_url, {
135
'title': 'My First Article',
136
'slug': 'my-first-article',
138
'date_created': datetime.datetime(2007, 6, 25),
140
self.assertRedirects(response, self.create_redirect,
141
target_status_code=404)
142
self.assertEqual(num_articles + 1, self.article_model.objects.count(),
143
"A new Article should have been created.")
145
def test_update_article(self):
146
num_articles = self.article_model.objects.count()
147
response = self.client.post(self.update_url, {
148
'title': 'Another Article',
149
'slug': 'another-article-slug',
151
'date_created': datetime.datetime(2007, 6, 25),
153
self.assertRedirects(response, self.update_redirect,
154
target_status_code=404)
155
self.assertEqual(num_articles, self.article_model.objects.count(),
156
"A new Article should not have been created.")
158
def test_delete_article(self):
159
num_articles = self.article_model.objects.count()
160
response = self.client.post(self.delete_url)
161
self.assertRedirects(response, self.delete_redirect,
162
target_status_code=404)
163
self.assertEqual(num_articles - 1, self.article_model.objects.count(),
164
"An Article should have been deleted.")
166
class NoPostSaveNoAbsoluteUrl(PostSaveRedirectTests):
168
Tests that when no post_save_redirect is passed and no get_absolute_url
169
method exists on the Model that the view raises an ImproperlyConfigured
173
create_url = '/views/create_update/no_redirect/create/article/'
174
update_url = '/views/create_update/no_redirect/update/article/old_article/'
176
def test_create_article(self):
177
self.assertRaises(ImproperlyConfigured,
178
super(NoPostSaveNoAbsoluteUrl, self).test_create_article)
180
def test_update_article(self):
181
self.assertRaises(ImproperlyConfigured,
182
super(NoPostSaveNoAbsoluteUrl, self).test_update_article)
184
def test_delete_article(self):
186
The delete_object view requires a post_delete_redirect, so skip testing
191
class AbsoluteUrlNoPostSave(PostSaveRedirectTests):
193
Tests that the views redirect to the Model's get_absolute_url when no
194
post_save_redirect is passed.
197
# Article model with get_absolute_url method.
198
article_model = UrlArticle
200
create_url = '/views/create_update/no_url/create/article/'
201
update_url = '/views/create_update/no_url/update/article/old_article/'
203
create_redirect = '/urlarticles/my-first-article/'
204
update_redirect = '/urlarticles/another-article-slug/'
206
def test_delete_article(self):
208
The delete_object view requires a post_delete_redirect, so skip testing