tests.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. """
  3. Copyright (C) 2014, Paul Munday.
  4. PO Box 28228, Portland, OR, USA 97228
  5. paul at paulmunday.net
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. There should also be a copy of the GPL in src/license.md that should be accessib
  17. le by going to <a href ="/license">/license<a> on this site.
  18. As originally distributed this program will be able to display its own source co
  19. de, which may count as conveying under the terms of the GPL v3. You should there
  20. fore make sure the copy of the GPL (i.e. src/license.md) is left in place.
  21. You are also free to remove this section from the code as long as any modified c
  22. opy you distribute (including a copy that is unchanged except for removal of thi
  23. s feature) is also licensed under the GPL version 3 (or later versions).
  24. None of this means you have to license your own content this way, only the origi
  25. nal source code and any modifications, or any subsequent additions that have bee
  26. n explicitly licensed under the GPL version 3 or later.
  27. You are therefore free to add templates and style sheets under your own terms th
  28. ough I would be happy if you chose to license them in the same way.
  29. """
  30. import monomotapa
  31. import unittest
  32. import tempfile
  33. import os
  34. import os.path
  35. class TestCase(unittest.TestCase):
  36. def setUp(self):
  37. self.app = monomotapa.app.test_client()
  38. path = 'monomotapa/src/'
  39. self.tmpfile = tempfile.NamedTemporaryFile(suffix=".md",
  40. dir=path, delete=False)
  41. self.filename = os.path.basename(self.tmpfile.name)
  42. self.route = os.path.splitext(self.filename)[0]
  43. with open(self.tmpfile.name ,'w') as f:
  44. f.write("&aleph; test")
  45. def tearDown(self):
  46. os.unlink(self.tmpfile.name)
  47. # Test Page class
  48. # generate_page is not tested directlu here as it is reliant on
  49. # flask's context, testing is implicit in test_static_page etc)
  50. def test_Page(self):
  51. staticpage = monomotapa.views.Page(self.route)
  52. self.assertEquals(staticpage.name, self.route)
  53. self.assertIn(self.route.lower(), staticpage.title)
  54. self.assertEquals(staticpage.heading, self.route.capitalize())
  55. self.assertFalse(staticpage.trusted)
  56. # test generate page via call to home page
  57. def test_generate_page_with_template(self):
  58. index_page = self.app.get('/')
  59. # makes assumptions about templating/content
  60. self.assertIn('<div id="home">', index_page.data)
  61. def test_get_page_src(self):
  62. page = monomotapa.views.Page(self.route)
  63. result = page.get_page_src(self.filename, 'src')
  64. self.assertEquals(result, 'monomotapa/src/' + self.filename)
  65. def test_get_page_src_with_extension(self):
  66. page = monomotapa.views.Page(self.route)
  67. result = page.get_page_src(self.route, 'src', 'md')
  68. self.assertEquals(result, 'monomotapa/src/' + self.filename)
  69. def test_get_page_src_with_lookup(self):
  70. page = monomotapa.views.Page(self.route)
  71. result = page.get_page_src('index', 'src')
  72. self.assertEquals(result, 'monomotapa/src/home.md')
  73. def test_get_page_src_nonexistant_source(self):
  74. page = monomotapa.views.Page(self.route)
  75. result = page.get_page_src('non_existant')
  76. self.assertIsNone(result)
  77. def test_get_template(self):
  78. page = monomotapa.views.Page(self.route)
  79. result = page.get_template(self.route)
  80. self.assertEquals(result, 'static.html')
  81. def test_get_template_with_template(self):
  82. page = monomotapa.views.Page(self.route)
  83. result = page.get_template('index')
  84. self.assertEquals(result, 'home.html')
  85. # Test helper functions
  86. def test_get_page_attribute(self):
  87. pages = { 'test' : {'src' : 'test.md'}}
  88. result = monomotapa.views.get_page_attribute(pages, 'test', 'src')
  89. self.assertEquals(result, 'test.md')
  90. def test_get_page_attribute_no_page(self):
  91. pages = { 'test' : {'src' : 'test.md'}}
  92. result = monomotapa.views.get_page_attribute(pages, 'notest', 'src')
  93. self.assertIsNone(result)
  94. def test_get_page_attribute_no_attribute(self):
  95. pages = { 'test' : {'src' : 'test.md'}}
  96. result = monomotapa.views.get_page_attribute(pages, 'test', 'nosrc')
  97. self.assertIsNone(result)
  98. def test_src_file(self):
  99. result = monomotapa.views.src_file('test')
  100. self.assertEquals(result, 'monomotapa/test')
  101. def test_src_file_with_dir(self):
  102. result = monomotapa.views.src_file('test', 'src')
  103. self.assertEquals(result, 'monomotapa/src/test')
  104. def test_get_extension_no_period(self):
  105. result = monomotapa.views.get_extension('md')
  106. self.assertEquals(result, '.md')
  107. def test_get_extension_with_period(self):
  108. result = monomotapa.views.get_extension('.md')
  109. self.assertEquals(result, '.md')
  110. def test_get_extension_with_None(self):
  111. result = monomotapa.views.get_extension(None)
  112. self.assertEquals(result, '')
  113. def test_render_markdown(self):
  114. markdown = monomotapa.views.render_markdown(self.tmpfile.name)
  115. self.assertIn( 'test', markdown)
  116. def test_render_markdown_untrusted(self):
  117. markdown = monomotapa.views.render_markdown(self.tmpfile.name)
  118. self.assertNotIn( '&aleph;', markdown)
  119. def test_render_markdown_trusted(self):
  120. markdown = monomotapa.views.render_markdown(self.tmpfile.name,
  121. trusted=True)
  122. self.assertIn('&aleph;', markdown)
  123. def test_pygments_renderer(self):
  124. results = monomotapa.views.render_pygments(self.tmpfile.name,
  125. 'markdown')
  126. self.assertIn('<pre>', results)
  127. self.assertIn('test', results)
  128. def test_get_pygments_css(self):
  129. css = monomotapa.views.get_pygments_css()
  130. self.assertIn('.highlight', css)
  131. def test_heading(self):
  132. expected = '\n<h1>test</h1>\n'
  133. result = monomotapa.views.heading('test', 1)
  134. self.assertEquals(result, expected)
  135. # Test page display/routes
  136. # Some of these are dependent on templates and contents supplied,
  137. # but we want to test our end points
  138. def test_index(self):
  139. index_page = self.app.get('/')
  140. # makes assumptions about templating/content
  141. self.assertIn('Monomotapa', index_page.data)
  142. # will fail if md not rendered
  143. self.assertNotIn('Not in this page', index_page.data)
  144. title = '%s</title>' % 'home'
  145. self.assertIn( title, index_page.data)
  146. def test_static_page(self):
  147. static_page = self.app.get('/' + self.route)
  148. # tests for content
  149. self.assertIn('test', static_page.data)
  150. # will fail if md not rendered
  151. self.assertNotIn('Not in page', static_page.data)
  152. def test_static_page_heading(self):
  153. static_page = self.app.get('/' + self.route)
  154. # note dependent of static page template
  155. heading = '<h1>%s</h1>' % self.route.capitalize()
  156. self.assertIn( heading, static_page.data)
  157. def test_static_page_title(self):
  158. # note dependent of static page template
  159. static_page = self.app.get('/' + self.route)
  160. title = '%s</title>' % self.route.lower()
  161. self.assertIn( title, static_page.data)
  162. def test_static_page_404(self):
  163. static_page = self.app.get('/non_existant')
  164. self.assertEquals(static_page.status_code, 404)
  165. def test_static_page_200(self):
  166. static_page = self.app.get('/' + self.route)
  167. self.assertEquals(static_page.status_code, 200)
  168. def test_source_page(self):
  169. source_page = self.app.get('/source?page=%s' % self.route)
  170. self.assertIn(self.filename, source_page.data)
  171. def test_source_page_200(self):
  172. static_page = self.app.get('/source?page=%s' % self.route)
  173. self.assertEquals(static_page.status_code, 200)
  174. def test_source_page_404(self):
  175. static_page = self.app.get('/source?page=non_existant')
  176. self.assertEquals(static_page.status_code, 404)
  177. def test_source_page_for_source(self):
  178. source_page = self.app.get('/source?page=source')
  179. # N.B. this makes assumptions about page layout
  180. # i.e. page names rendered as h2 heading
  181. self.assertIn('<h2>views.py', source_page.data)
  182. self.assertNotIn('<h2>tests.py', source_page.data)
  183. def test_source_page_for_rendered_source(self):
  184. source_page = self.app.get('/source?page=source')
  185. self.assertIn('Page', source_page.data)
  186. def test_source_page_for_unittests(self):
  187. source_page = self.app.get('/source?page=unit-tests')
  188. # N.B. this makes assumptions about page layout
  189. # i.e. page names rendered as h2 heading
  190. self.assertIn('<h2>tests.py', source_page.data)
  191. def test_source_page_for_rendered_unittests(self):
  192. source_page = self.app.get('/source?page=unit-tests')
  193. # always true if this test is rendered
  194. self.assertIn('def test_source_page_for_unittests', source_page.data)
  195. def test_source_page_for_rendered_template(self):
  196. source_page = self.app.get('/source?page=%s' % self.route)
  197. # always true if this test is rendered
  198. self.assertIn('static.html', source_page.data)
  199. def test_source_page_for_rendered_template_set(self):
  200. source_page = self.app.get('/source?page=%s' % 'index')
  201. # always true if this test is rendered
  202. self.assertIn('home.html', source_page.data)
  203. if __name__ == '__main__':
  204. unittest.main()