test_pagure_flask_api_plugins_install.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2019 - Copyright Red Hat Inc
  4. Authors:
  5. Michal Konecny <mkonecny@redhat.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import sys
  11. import os
  12. import json
  13. from mock import patch, MagicMock
  14. sys.path.insert(
  15. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  16. )
  17. import pagure.lib.query # noqa: E402
  18. import tests # noqa: E402
  19. class PagureFlaskApiPluginInstalltests(tests.Modeltests):
  20. """ Tests for the flask API of pagure for installing a plugin
  21. """
  22. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskApiPluginInstalltests, self).setUp()
  26. tests.create_projects(self.session)
  27. tests.create_tokens(self.session)
  28. tests.create_tokens_acl(self.session)
  29. # Create project-less token for user foo
  30. item = pagure.lib.model.Token(
  31. id="project-less-foo",
  32. user_id=2,
  33. project_id=None,
  34. expiration=datetime.datetime.utcnow()
  35. + datetime.timedelta(days=30),
  36. )
  37. self.session.add(item)
  38. self.session.commit()
  39. tests.create_tokens_acl(self.session, token_id="project-less-foo")
  40. # Create project-specific token for user foo
  41. item = pagure.lib.model.Token(
  42. id="project-specific-foo",
  43. user_id=2,
  44. project_id=1,
  45. expiration=datetime.datetime.utcnow()
  46. + datetime.timedelta(days=30),
  47. )
  48. self.session.add(item)
  49. self.session.commit()
  50. tests.create_tokens_acl(self.session, token_id="project-specific-foo")
  51. def test_install_plugin_own_project_no_data(self):
  52. """ Test installing a new plugin on a project for which you're the
  53. main maintainer.
  54. """
  55. # pingou's token with all the ACLs
  56. headers = {"Authorization": "token aaabbbcccddd"}
  57. # Install a plugin on /test/ where pingou is the main admin
  58. output = self.app.post(
  59. "/api/0/test/settings/Mail/install", headers=headers
  60. )
  61. self.assertEqual(output.status_code, 400)
  62. data = json.loads(output.get_data(as_text=True))
  63. self.assertEqual(
  64. pagure.api.APIERROR.EINVALIDREQ.name, data["error_code"]
  65. )
  66. self.assertEqual(pagure.api.APIERROR.EINVALIDREQ.value, data["error"])
  67. self.assertEqual(
  68. data["errors"], {"mail_to": ["This field is required."]}
  69. )
  70. def test_install_plugin_own_project(self):
  71. """ Test installing a new plugin on a project for which you're the
  72. main maintainer.
  73. """
  74. # pingou's token with all the ACLs
  75. headers = {"Authorization": "token aaabbbcccddd"}
  76. # complete data set
  77. data = {"mail_to": "serg@wh40k.com"}
  78. # Create an issue on /test/ where pingou is the main admin
  79. output = self.app.post(
  80. "/api/0/test/settings/Mail/install", headers=headers, data=data
  81. )
  82. self.assertEqual(output.status_code, 200)
  83. data = json.loads(output.get_data(as_text=True))
  84. self.assertEqual(
  85. data,
  86. {
  87. "plugin": {"mail_to": "serg@wh40k.com"},
  88. "message": "Hook 'Mail' activated",
  89. },
  90. )
  91. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  92. def test_install_plugin_someone_else_project_project_less_token(self):
  93. """ Test installing a new plugin on a project with which you have
  94. nothing to do.
  95. """
  96. # pingou's token with all the ACLs
  97. headers = {"Authorization": "token project-less-foo"}
  98. # Install a plugin on /test/ where pingou is the main admin
  99. output = self.app.post(
  100. "/api/0/test/settings/Prevent creating new branches by git push/"
  101. "install",
  102. headers=headers,
  103. )
  104. self.assertEqual(output.status_code, 200)
  105. data = json.loads(output.get_data(as_text=True))
  106. self.assertEqual(
  107. data,
  108. {
  109. "plugin": {},
  110. "message": "Hook 'Prevent creating new branches by git push' "
  111. "activated",
  112. },
  113. )
  114. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  115. def test_install_plugin_project_specific_token(self):
  116. """ Test installing a new plugin on a project with a regular
  117. project-specific token.
  118. """
  119. # pingou's token with all the ACLs
  120. headers = {"Authorization": "token project-specific-foo"}
  121. # complete data set
  122. data = {"mail_to": "serg@wh40k.com"}
  123. # Create an issue on /test/ where pingou is the main admin
  124. output = self.app.post(
  125. "/api/0/test/settings/Mail/install", headers=headers, data=data
  126. )
  127. self.assertEqual(output.status_code, 200)
  128. data = json.loads(output.get_data(as_text=True))
  129. self.assertEqual(
  130. data,
  131. {
  132. "plugin": {"mail_to": "serg@wh40k.com"},
  133. "message": "Hook 'Mail' activated",
  134. },
  135. )
  136. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  137. def test_install_plugin_invalid_project_specific_token(self):
  138. """ Test installing a new plugin on a project with a regular
  139. project-specific token but for another project.
  140. """
  141. # pingou's token with all the ACLs
  142. headers = {"Authorization": "token project-specific-foo"}
  143. # complete data set
  144. data = {"mail_to": "serg@wh40k.com"}
  145. # Create an issue on /test/ where pingou is the main admin
  146. output = self.app.post(
  147. "/api/0/test2/settings/Mail/install", headers=headers, data=data
  148. )
  149. self.assertEqual(output.status_code, 401)
  150. data = json.loads(output.get_data(as_text=True))
  151. self.assertEqual(
  152. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  153. )
  154. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  155. if __name__ == "__main__":
  156. unittest.main(verbosity=2)