test_pagure_flask_ui_plugins_irc.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskPluginIRCtests(tests.Modeltests):
  21. """ Tests for pagure_hook plugin of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginIRCtests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.plugins.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. tests.HERE, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. tests.HERE, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, 'docs')
  39. self.app = pagure.APP.test_client()
  40. def test_plugin_mail(self):
  41. """ Test the irc plugin on/off endpoint. """
  42. tests.create_projects(self.session)
  43. user = tests.FakeUser(username='pingou')
  44. with tests.user_set(pagure.APP, user):
  45. output = self.app.get('/test/settings/IRC')
  46. self.assertEqual(output.status_code, 200)
  47. self.assertIn(
  48. '<div class="projectinfo m-t-1 m-b-1">\n'
  49. 'test project #1 </div>', output.data)
  50. self.assertTrue('<h3>IRC settings</h3>' in output.data)
  51. self.assertTrue(
  52. '<input id="active" name="active" type="checkbox" value="y">'
  53. in output.data)
  54. csrf_token = output.data.split(
  55. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  56. data = {}
  57. output = self.app.post('/test/settings/IRC', data=data)
  58. self.assertEqual(output.status_code, 200)
  59. self.assertIn(
  60. '<div class="projectinfo m-t-1 m-b-1">\n'
  61. 'test project #1 </div>', output.data)
  62. self.assertTrue('<h3>IRC settings</h3>' in output.data)
  63. self.assertTrue(
  64. '<input id="active" name="active" type="checkbox" value="y">'
  65. in output.data)
  66. self.assertFalse(os.path.exists(os.path.join(
  67. tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))
  68. data['csrf_token'] = csrf_token
  69. # No git found
  70. output = self.app.post('/test/settings/IRC', data=data)
  71. self.assertEqual(output.status_code, 404)
  72. tests.create_projects_git(tests.HERE)
  73. # With the git repo
  74. output = self.app.post(
  75. '/test/settings/IRC', data=data, follow_redirects=True)
  76. self.assertEqual(output.status_code, 200)
  77. self.assertIn(
  78. '<section class="settings">\n <h3>Settings for test</h3>',
  79. output.data)
  80. self.assertTrue(
  81. '</button>\n Hook IRC inactived' in output.data)
  82. output = self.app.get('/test/settings/IRC')
  83. self.assertEqual(output.status_code, 200)
  84. self.assertIn(
  85. '<div class="projectinfo m-t-1 m-b-1">\n'
  86. 'test project #1 </div>', output.data)
  87. self.assertTrue('<h3>IRC settings</h3>' in output.data)
  88. self.assertTrue(
  89. '<input id="active" name="active" type="checkbox" value="y">'
  90. in output.data)
  91. self.assertFalse(os.path.exists(os.path.join(
  92. tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))
  93. # Activate hook
  94. data = {
  95. 'csrf_token': csrf_token,
  96. 'active': 'y',
  97. 'server': 'irc.freenode.net',
  98. 'port': 6667,
  99. 'room': '#fedora-apps',
  100. }
  101. output = self.app.post(
  102. '/test/settings/IRC', data=data, follow_redirects=True)
  103. self.assertEqual(output.status_code, 200)
  104. self.assertIn(
  105. '<section class="settings">\n <h3>Settings for test</h3>',
  106. output.data)
  107. self.assertTrue(
  108. '</button>\n Hook IRC activated' in output.data)
  109. output = self.app.get('/test/settings/IRC')
  110. self.assertEqual(output.status_code, 200)
  111. self.assertIn(
  112. '<div class="projectinfo m-t-1 m-b-1">\n'
  113. 'test project #1 </div>', output.data)
  114. self.assertTrue('<h3>IRC settings</h3>' in output.data)
  115. self.assertTrue(
  116. '<input checked id="active" name="active" type="checkbox" '
  117. 'value="y">' in output.data)
  118. # TODO: Fix this
  119. #self.assertTrue(os.path.exists(os.path.join(
  120. #tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))
  121. # De-Activate hook
  122. data = {'csrf_token': csrf_token}
  123. output = self.app.post('/test/settings/IRC', data=data, follow_redirects=True)
  124. self.assertEqual(output.status_code, 200)
  125. self.assertIn(
  126. '<section class="settings">\n <h3>Settings for test</h3>',
  127. output.data)
  128. self.assertTrue(
  129. '</button>\n Hook IRC inactived' in output.data)
  130. output = self.app.get('/test/settings/IRC')
  131. self.assertEqual(output.status_code, 200)
  132. self.assertIn(
  133. '<div class="projectinfo m-t-1 m-b-1">\n'
  134. 'test project #1 </div>', output.data)
  135. self.assertTrue('<h3>IRC settings</h3>' in output.data)
  136. self.assertTrue(
  137. '<input id="active" name="active" type="checkbox" '
  138. 'value="y">' in output.data)
  139. self.assertFalse(os.path.exists(os.path.join(
  140. tests.HERE, 'test.git', 'hooks', 'post-receive.irc')))
  141. if __name__ == '__main__':
  142. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  143. PagureFlaskPluginIRCtests)
  144. unittest.TextTestRunner(verbosity=2).run(SUITE)