/solace/tests/link_check.py
Python | 71 lines | 45 code | 13 blank | 13 comment | 5 complexity | 3573e7e3527ffce4bbcf446e47b259f7 MD5 | raw file
Possible License(s): BSD-3-Clause
1# -*- coding: utf-8 -*-
2"""
3 solace.tests.link_check
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 A test that finds 404 links in the default templates.
7
8 :copyright: (c) 2009 by Plurk Inc., see AUTHORS for more details.
9 :license: BSD, see LICENSE for more details.
10"""
11import re
12import unittest
13from urlparse import urljoin
14from solace.tests import SolaceTestCase
15
16from solace import models, settings
17from solace.database import session
18
19
20BASE_URL = 'http://localhost/'
21MIN_VISITED = 12
22
23
24class LinkCheckTestCase(SolaceTestCase):
25
26 def test_only_valid_links(self):
27 """Make sure that all links are valid"""
28 settings.LANGUAGE_SECTIONS = ['en']
29 user = models.User('user1', 'user1@example.com', 'default')
30 user.is_admin = True
31 banned_user = models.User('user2', 'user2@example.com', 'default')
32 banned_user.is_banned = True
33 topic = models.Topic('en', 'This is a test topic', 'Foobar', user)
34 post1 = models.Post(topic, user, 'meh1')
35 post2 = models.Post(topic, user, 'meh2')
36 topic.accept_answer(post1)
37 session.commit()
38
39 visited_links = set()
40 def visit(url):
41 url = urljoin(BASE_URL, url).split('#', 1)[0]
42 if not url.startswith(BASE_URL) or url in visited_links:
43 return
44 visited_links.add(url)
45 path = '/' + url.split('/', 3)[-1]
46 if path.startswith('/logout?'):
47 return
48 response = self.client.get(path, follow_redirects=True)
49 self.assertEqual(response.status_code, 200)
50 for link in response.html.xpath('//a[@href]'):
51 visit(link.attrib['href'])
52
53 # logged out
54 visit('/')
55 self.assert_(len(visited_links) > MIN_VISITED)
56
57 # logged in
58 visited_links.clear()
59 self.login('user1', 'default')
60 visit('/')
61 self.assert_(len(visited_links) > MIN_VISITED)
62
63
64def suite():
65 suite = unittest.TestSuite()
66 suite.addTest(unittest.makeSuite(LinkCheckTestCase))
67 return suite
68
69
70if __name__ == '__main__':
71 unittest.main(defaultTest='suite')