test_pagure_flask_ui_plugins_irc.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. sys.path.insert(0, os.path.join(os.path.dirname(
  12. os.path.abspath(__file__)), '..'))
  13. import tests
  14. class PagureFlaskPluginIRCtests(tests.SimplePagureTest):
  15. """ Tests for pagure_hook plugin of pagure """
  16. def test_plugin_mail(self):
  17. """ Test the irc plugin on/off endpoint. """
  18. tests.create_projects(self.session)
  19. tests.create_projects_git(os.path.join(self.path, 'repos'))
  20. user = tests.FakeUser(username='pingou')
  21. with tests.user_set(self.app.application, user):
  22. output = self.app.get('/test/settings/IRC')
  23. self.assertEqual(output.status_code, 200)
  24. output_text = output.get_data(as_text=True)
  25. self.assertIn(
  26. '<title>Settings IRC - test - Pagure</title>', output_text)
  27. self.assertIn(
  28. '<input class="form-check-input mt-2" id="active" name="active" '
  29. 'type="checkbox" value="y">', output_text)
  30. csrf_token = output_text.split(
  31. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  32. data = {}
  33. output = self.app.post('/test/settings/IRC', data=data)
  34. self.assertEqual(output.status_code, 200)
  35. output_text = output.get_data(as_text=True)
  36. self.assertIn(
  37. '<title>Settings IRC - test - Pagure</title>', output_text)
  38. self.assertIn(
  39. '<input class="form-check-input mt-2" id="active" name="active" '
  40. 'type="checkbox" value="y">', output_text)
  41. self.assertFalse(os.path.exists(os.path.join(
  42. self.path, 'repos', 'test.git', 'hooks', 'post-receive.irc')))
  43. data['csrf_token'] = csrf_token
  44. # With the git repo
  45. output = self.app.post(
  46. '/test/settings/IRC', data=data, follow_redirects=True)
  47. self.assertEqual(output.status_code, 200)
  48. output_text = output.get_data(as_text=True)
  49. self.assertIn(
  50. '<h5 class="pl-2 font-weight-bold text-muted">'
  51. 'Project Settings</h5>\n', output_text)
  52. self.assertIn(
  53. 'Hook IRC deactivated', output_text)
  54. output = self.app.get('/test/settings/IRC')
  55. self.assertEqual(output.status_code, 200)
  56. output_text = output.get_data(as_text=True)
  57. self.assertIn(
  58. '<title>Settings IRC - test - Pagure</title>', output_text)
  59. self.assertIn(
  60. '<input class="form-check-input mt-2" id="active" name="active" '
  61. 'type="checkbox" value="y">', output_text)
  62. self.assertFalse(os.path.exists(os.path.join(
  63. self.path, 'repos', 'test.git', 'hooks', 'post-receive.irc')))
  64. # Activate hook
  65. data = {
  66. 'csrf_token': csrf_token,
  67. 'active': 'y',
  68. 'server': 'irc.freenode.net',
  69. 'port': 6667,
  70. 'room': '#fedora-apps',
  71. }
  72. output = self.app.post(
  73. '/test/settings/IRC', data=data, follow_redirects=True)
  74. self.assertEqual(output.status_code, 200)
  75. output_text = output.get_data(as_text=True)
  76. self.assertIn(
  77. '<h5 class="pl-2 font-weight-bold text-muted">'
  78. 'Project Settings</h5>\n', output_text)
  79. self.assertIn(
  80. 'Hook IRC activated', output_text)
  81. output = self.app.get('/test/settings/IRC')
  82. self.assertEqual(output.status_code, 200)
  83. output_text = output.get_data(as_text=True)
  84. self.assertIn(
  85. '<title>Settings IRC - test - Pagure</title>', output_text)
  86. self.assertIn(
  87. '<input checked class="form-check-input mt-2" id="active" name="active" '
  88. 'type="checkbox" value="y">', output_text)
  89. # TODO: Fix this
  90. #self.assertTrue(os.path.exists(os.path.join(
  91. #self.path, 'repos', 'test.git', 'hooks', 'post-receive.irc')))
  92. # De-Activate hook
  93. data = {'csrf_token': csrf_token}
  94. output = self.app.post('/test/settings/IRC', data=data, follow_redirects=True)
  95. self.assertEqual(output.status_code, 200)
  96. output_text = output.get_data(as_text=True)
  97. self.assertIn(
  98. '<h5 class="pl-2 font-weight-bold text-muted">'
  99. 'Project Settings</h5>\n', output_text)
  100. self.assertIn(
  101. 'Hook IRC deactivated', output_text)
  102. output = self.app.get('/test/settings/IRC')
  103. self.assertEqual(output.status_code, 200)
  104. output_text = output.get_data(as_text=True)
  105. self.assertIn(
  106. '<title>Settings IRC - test - Pagure</title>', output_text)
  107. self.assertIn(
  108. '<input class="form-check-input mt-2" id="active" name="active" '
  109. 'type="checkbox" value="y">', output_text)
  110. self.assertFalse(os.path.exists(os.path.join(
  111. self.path, 'repos', 'test.git', 'hooks', 'post-receive.irc')))
  112. if __name__ == '__main__':
  113. unittest.main(verbosity=2)