test_pagure_flask_ui_plugins_irc.py 5.4 KB

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