~ubuntu-branches/debian/squeeze/python-django/squeeze

« back to all changes in this revision

Viewing changes to tests/regressiontests/views/tests/generic/create_update.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb, Chris Lamb, David Spreen, Sandro Tosi
  • Date: 2008-11-19 21:31:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081119213100-gp0lqhxl1qxa6dgl
Tags: 1.0.2-1
[ Chris Lamb ]
* New upstream bugfix release. Closes: #505783
* Add myself to Uploaders with ACK from Brett.

[ David Spreen ]
* Remove python-pysqlite2 from Recommends because Python 2.5 includes
  sqlite library used by Django. Closes: 497886

[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import datetime
2
 
 
3
 
from django.test import TestCase
4
 
from django.core.exceptions import ImproperlyConfigured
5
 
from regressiontests.views.models import Article, UrlArticle
6
 
 
7
 
class CreateObjectTest(TestCase):
8
 
 
9
 
    fixtures = ['testdata.json']
10
 
 
11
 
    def test_login_required_view(self):
12
 
        """
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.
16
 
        """
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')
26
 
 
27
 
    def test_create_article_display_page(self):
28
 
        """
29
 
        Ensures the generic view returned the page and contains a form.
30
 
        """
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.')
37
 
 
38
 
    def test_create_article_with_errors(self):
39
 
        """
40
 
        POSTs a form that contains validation errors.
41
 
        """
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',
46
 
        })
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.")
51
 
 
52
 
    def test_create_custom_save_article(self):
53
 
        """
54
 
        Creates a new article using a custom form class with a save method
55
 
        that alters the slug entered.
56
 
        """
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',
61
 
            'author': 1,
62
 
            'date_created': datetime.datetime(2007, 6, 25),
63
 
        })
64
 
        self.assertRedirects(response,
65
 
            '/views/create_update/view/article/some-other-slug/',
66
 
            target_status_code=404)
67
 
 
68
 
class UpdateDeleteObjectTest(TestCase):
69
 
 
70
 
    fixtures = ['testdata.json']
71
 
 
72
 
    def test_update_object_form_display(self):
73
 
        """
74
 
        Verifies that the form was created properly and with initial values.
75
 
        """
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" />')
80
 
 
81
 
    def test_update_object(self):
82
 
        """
83
 
        Verifies the updating of an Article.
84
 
        """
85
 
        response = self.client.post('/views/create_update/update/article/old_article/', {
86
 
            'title': 'Another Article',
87
 
            'slug': 'another-article-slug',
88
 
            'author': 1,
89
 
            'date_created': datetime.datetime(2007, 6, 25),
90
 
        })
91
 
        article = Article.objects.get(pk=1)
92
 
        self.assertEquals(article.title, "Another Article")
93
 
 
94
 
    def test_delete_object_confirm(self):
95
 
        """
96
 
        Verifies the confirm deletion page is displayed using a GET.
97
 
        """
98
 
        response = self.client.get('/views/create_update/delete/article/old_article/')
99
 
        self.assertTemplateUsed(response, 'views/article_confirm_delete.html')
100
 
 
101
 
    def test_delete_object(self):
102
 
        """
103
 
        Verifies the object actually gets deleted on a POST.
104
 
        """
105
 
        view_url = '/views/create_update/delete/article/old_article/'
106
 
        response = self.client.post(view_url)
107
 
        try:
108
 
            Article.objects.get(slug='old_article')
109
 
        except Article.DoesNotExist:
110
 
            pass
111
 
        else:
112
 
            self.fail('Object was not deleted.')
113
 
 
114
 
class PostSaveRedirectTests(TestCase):
115
 
    """
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
118
 
    on the Model.
119
 
    """
120
 
 
121
 
    fixtures = ['testdata.json']
122
 
    article_model = Article
123
 
 
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/'
127
 
 
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/'
131
 
 
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',
137
 
            'author': '1',
138
 
            'date_created': datetime.datetime(2007, 6, 25),
139
 
        })
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.")
144
 
 
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',
150
 
            'author': 1,
151
 
            'date_created': datetime.datetime(2007, 6, 25),
152
 
        })
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.")
157
 
 
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.")
165
 
 
166
 
class NoPostSaveNoAbsoluteUrl(PostSaveRedirectTests):
167
 
    """
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
170
 
    error.
171
 
    """
172
 
 
173
 
    create_url = '/views/create_update/no_redirect/create/article/'
174
 
    update_url = '/views/create_update/no_redirect/update/article/old_article/'
175
 
 
176
 
    def test_create_article(self):
177
 
        self.assertRaises(ImproperlyConfigured,
178
 
            super(NoPostSaveNoAbsoluteUrl, self).test_create_article)
179
 
 
180
 
    def test_update_article(self):
181
 
        self.assertRaises(ImproperlyConfigured,
182
 
            super(NoPostSaveNoAbsoluteUrl, self).test_update_article)
183
 
 
184
 
    def test_delete_article(self):
185
 
        """
186
 
        The delete_object view requires a post_delete_redirect, so skip testing
187
 
        here.
188
 
        """
189
 
        pass
190
 
 
191
 
class AbsoluteUrlNoPostSave(PostSaveRedirectTests):
192
 
    """
193
 
    Tests that the views redirect to the Model's get_absolute_url when no
194
 
    post_save_redirect is passed.
195
 
    """
196
 
 
197
 
    # Article model with get_absolute_url method.
198
 
    article_model = UrlArticle
199
 
 
200
 
    create_url = '/views/create_update/no_url/create/article/'
201
 
    update_url = '/views/create_update/no_url/update/article/old_article/'
202
 
 
203
 
    create_redirect = '/urlarticles/my-first-article/'
204
 
    update_redirect = '/urlarticles/another-article-slug/'
205
 
 
206
 
    def test_delete_article(self):
207
 
        """
208
 
        The delete_object view requires a post_delete_redirect, so skip testing
209
 
        here.
210
 
        """
211
 
        pass