[Web] fix broken test
[mygpo.git] / mygpo / web / tests.py
bloba2029ded0ce987447a6a8a5673c1af12dbc5224e
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 import unittest
19 import doctest
20 import uuid
22 from django.test import TestCase
23 from django.core.urlresolvers import reverse
24 from django.contrib.auth import get_user_model
26 from mygpo.podcasts.models import Podcast, Episode, Slug
27 import mygpo.web.utils
28 from mygpo.test import create_auth_string, anon_request
31 class SimpleWebTests(TestCase):
33 @classmethod
34 def setUpClass(self):
35 User = get_user_model()
36 self.user = User(username='web-test', email='web-test@example.com')
37 self.user.set_password('pwd')
38 self.user.save()
40 self.auth_string = create_auth_string('test', 'pwd')
42 @classmethod
43 def tearDownClass(self):
44 self.user.delete()
46 def test_access_parameterless_pages(self):
47 pages = [
48 'history',
49 'suggestions',
50 'tags',
51 'subscriptions',
52 'subscriptions-opml',
53 'favorites',
54 'account',
55 'privacy',
56 'delete-account',
57 'share',
58 'toplist',
59 'episode-toplist',
60 'devices',
61 'device-create',
62 'login',
63 'logout',
64 'home']
66 self.access_pages(pages, [], True)
68 def test_access_podcast_pages(self):
69 pages = ['podcast', ]
71 def access_pages(self, pages, args, login):
72 if login:
73 self.client.post('/login/', dict(
74 login_username=self.user.username, pwd='pwd'))
76 for page in pages:
77 response = self.client.get(reverse(page, args=args), follow=True)
78 self.assertEquals(response.status_code, 200)
81 class PodcastPageTests(TestCase):
82 """ Test the podcast page """
84 def setUp(self):
85 # create a podcast and some episodes
86 podcast = Podcast.objects.create(id=uuid.uuid1().hex)
87 for n in range(20):
88 episode = Episode.objects.create(id=uuid.uuid1().hex,
89 podcast=podcast,
91 # we only need (the last) one
92 self.episode_slug = Slug.objects.create(content_object=episode,
93 order=0,
94 scope=podcast.as_scope,
95 slug=str(n),
98 self.podcast_slug = Slug.objects.create(content_object=podcast,
99 order=n, scope=podcast.scope,
100 slug='podcast')
102 def test_podcast_queries(self):
103 """ Test that the expected number of queries is executed """
104 url = reverse('podcast-slug', args=(self.podcast_slug.slug, ))
105 # the number of queries must be independent of the number of episodes
107 with self.assertNumQueries(6):
108 anon_request(url)
110 def test_episode_queries(self):
111 """ Test that the expected number of queries is executed """
112 url = reverse('episode-slug', args=(self.podcast_slug.slug,
113 self.episode_slug.slug))
115 with self.assertNumQueries(5):
116 anon_request(url)
119 def suite():
120 suite = unittest.TestSuite()
121 suite.addTest(doctest.DocTestSuite(mygpo.web.utils))
122 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SimpleWebTests))
123 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(PodcastPageTests))
124 return suite