Test using django-nose
[mygpo.git] / mygpo / web / tests.py
blobec8049eacd1b16cb41454ce02584dd46f4233a36
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.assertEqual(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(),
87 title='My Podcast',
88 max_episode_order=1,
90 for n in range(20):
91 episode = Episode.objects.get_or_create_for_url(
92 podcast,
93 'http://www.example.com/episode%d.mp3' % (n, ),
96 # we only need (the last) one
97 self.episode_slug = Slug.objects.create(content_object=episode,
98 order=0,
99 scope=podcast.as_scope,
100 slug=str(n),
103 self.podcast_slug = Slug.objects.create(content_object=podcast,
104 order=n, scope=podcast.scope,
105 slug='podcast')
107 def test_podcast_queries(self):
108 """ Test that the expected number of queries is executed """
109 url = reverse('podcast-slug', args=(self.podcast_slug.slug, ))
110 # the number of queries must be independent of the number of episodes
112 with self.assertNumQueries(5):
113 anon_request(url)
115 def test_episode_queries(self):
116 """ Test that the expected number of queries is executed """
117 url = reverse('episode-slug', args=(self.podcast_slug.slug,
118 self.episode_slug.slug))
120 with self.assertNumQueries(5):
121 anon_request(url)