/solace/tests/link_check.py

https://bitbucket.org/muhuk/solace · Python · 71 lines · 45 code · 13 blank · 13 comment · 5 complexity · 3573e7e3527ffce4bbcf446e47b259f7 MD5 · raw file

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