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/>.
22 from django
.test
import TestCase
23 from django
.core
.urlresolvers
import reverse
, resolve
24 from django
.test
.client
import RequestFactory
25 from django
.contrib
.auth
.models
import AnonymousUser
27 from mygpo
.podcasts
.models
import Podcast
, Episode
, Slug
28 import mygpo
.web
.utils
29 from mygpo
.users
.models
import User
30 from mygpo
.test
import create_auth_string
33 class SimpleWebTests(TestCase
):
37 self
.user
= User(username
='web-test', email
='web-test@example.com')
38 self
.user
.set_password('pwd')
41 self
.auth_string
= create_auth_string('test', 'pwd')
44 def tearDownClass(self
):
47 def test_access_parameterless_pages(self
):
54 'subscriptions-download',
68 self
.access_pages(pages
, [], True)
70 def test_access_podcast_pages(self
):
73 def access_pages(self
, pages
, args
, login
):
75 self
.client
.post('/login/', dict(
76 login_username
=self
.user
.username
, pwd
='pwd'))
79 response
= self
.client
.get(reverse(page
, args
=args
), follow
=True)
80 self
.assertEquals(response
.status_code
, 200)
83 class PodcastPageTests(TestCase
):
84 """ Test the podcast page """
87 self
.factory
= RequestFactory()
89 # create a podcast and some episodes
90 podcast
= Podcast
.objects
.create(id=uuid
.uuid1().hex)
92 episode
= Episode
.objects
.create(id=uuid
.uuid1().hex,
95 # we only need (the last) one
96 self
.episode_slug
= Slug
.objects
.create(content_object
=episode
,
102 self
.podcast_slug
= Slug
.objects
.create(content_object
=podcast
,
103 order
=n
, scope
=podcast
.scope
,
106 def test_podcast_queries(self
):
107 """ Test that the expected number of queries is executed """
108 url
= reverse('podcast-slug', args
=(self
.podcast_slug
.slug
, ))
109 # the number of queries must be independent of the number of episodes
110 self
._check
_queries
_anon
_page
(url
, 5)
113 def test_episode_queries(self
):
114 """ Test that the expected number of queries is executed """
115 url
= reverse('episode-slug', args
=(self
.podcast_slug
.slug
,
116 self
.episode_slug
.slug
))
117 self
._check
_queries
_anon
_page
(url
, 5)
119 def _check_queries_anon_page(self
, url
, num_queries
):
120 request
= self
.factory
.get(url
)
121 request
.user
= AnonymousUser()
124 with self
.assertNumQueries(num_queries
):
125 response
= view
.func(request
, *view
.args
, **view
.kwargs
)
129 suite
= unittest
.TestSuite()
130 suite
.addTest(doctest
.DocTestSuite(mygpo
.web
.utils
))
131 suite
.addTest(unittest
.TestLoader().loadTestsFromTestCase(SimpleWebTests
))
132 suite
.addTest(unittest
.TestLoader().loadTestsFromTestCase(PodcastPageTests
))