test_libravatar.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Unit testing suite for the Libravatar Plugin"""
  2. ## Copyright (C) 2015 Rafael Laboissiere <rafael@laboissiere.net>
  3. ##
  4. ## This program is free software: you can redistribute it and/or modify it
  5. ## under the terms of the GNU General Affero Public License as published by
  6. ## the Free Software Foundation, either version 3 of the License, or (at
  7. ## your option) any later version.
  8. ##
  9. ## This program is distributed in the hope that it will be useful, but
  10. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ## Affero General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU Affero General Public License
  15. ## along with this program. If not, see http://www.gnu.org/licenses/.
  16. import os
  17. import re
  18. import unittest
  19. import hashlib
  20. from tempfile import mkdtemp
  21. from shutil import rmtree
  22. from . import libravatar
  23. from pelican import Pelican
  24. from pelican.settings import read_settings
  25. AUTHOR_EMAIL = 'bart.simpson@example.com'
  26. MD5_HASH = hashlib.md5 (AUTHOR_EMAIL).hexdigest ()
  27. LIBRAVATAR_BASE_URL = 'http://cdn.libravatar.org/avatar/'
  28. class TestLibravatarURL (unittest.TestCase):
  29. """Class for testing the URL output of the Libravatar plugin"""
  30. def setUp (self, override = None):
  31. self.output_path = mkdtemp (prefix = 'pelicantests.')
  32. self.content_path = mkdtemp (prefix = 'pelicantests.')
  33. theme_path = os.path.join (os.path.dirname (os.path.abspath (__file__)),
  34. 'test_data', 'theme')
  35. settings = {
  36. 'PATH': self.content_path,
  37. 'THEME': theme_path,
  38. 'OUTPUT_PATH': self.output_path,
  39. 'PLUGINS': [libravatar],
  40. 'CACHE_CONTENT': False
  41. }
  42. if override:
  43. settings.update (override)
  44. fid = open (os.path.join (self.content_path, 'test.md'), 'w')
  45. fid.write ('Title: Test\nDate:\nEmail: ' + AUTHOR_EMAIL + '\n\n')
  46. fid.close ()
  47. self.settings = read_settings (override = settings)
  48. pelican = Pelican (settings = self.settings)
  49. pelican.run ()
  50. def tearDown (self):
  51. rmtree (self.output_path)
  52. rmtree (self.content_path)
  53. def test_url (self, options = ''):
  54. fid = open (os.path.join (self.output_path, 'test.html'), 'r')
  55. found = False
  56. for line in fid.readlines ():
  57. print line
  58. if re.search (LIBRAVATAR_BASE_URL + MD5_HASH + options, line):
  59. found = True
  60. break
  61. assert found
  62. class TestLibravatarMissing (TestLibravatarURL):
  63. """Class for testing the Libravatar "missing picture" option"""
  64. def setUp (self, override = None):
  65. self.library = 'wavatar'
  66. TestLibravatarURL.setUp (self,
  67. override = {'LIBRAVATAR_MISSING':
  68. self.library})
  69. def test_url (self):
  70. TestLibravatarURL.test_url (self, '\?d=' + self.library)
  71. class TestLibravatarSize (TestLibravatarURL):
  72. """Class for testing the Libravatar size option"""
  73. def setUp (self, override = None):
  74. self.size = 100
  75. TestLibravatarURL.setUp (self,
  76. override = {'LIBRAVATAR_SIZE': self.size})
  77. def test_url (self):
  78. TestLibravatarURL.test_url (self, '\?s=' + str (self.size))