test_pagure_flask_api_plugins_install.py 6.1 KB

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