test_pagure_flask_api_plugins_remove.py 6.2 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.plugins as plugins # noqa: E402
  18. import pagure.lib.query # noqa: E402
  19. import tests # noqa: E402
  20. class PagureFlaskApiPluginRemovetests(tests.Modeltests):
  21. """ Tests for the flask API of pagure for removing a plugin
  22. """
  23. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureFlaskApiPluginRemovetests, self).setUp()
  27. tests.create_projects(self.session)
  28. tests.create_tokens(self.session)
  29. tests.create_tokens_acl(self.session)
  30. # Create project-less token for user foo
  31. item = pagure.lib.model.Token(
  32. id="project-less-foo",
  33. user_id=2,
  34. project_id=None,
  35. expiration=datetime.datetime.utcnow()
  36. + datetime.timedelta(days=30),
  37. )
  38. self.session.add(item)
  39. self.session.commit()
  40. tests.create_tokens_acl(self.session, token_id="project-less-foo")
  41. # Create project-specific token for user foo
  42. item = pagure.lib.model.Token(
  43. id="project-specific-foo",
  44. user_id=2,
  45. project_id=1,
  46. expiration=datetime.datetime.utcnow()
  47. + datetime.timedelta(days=30),
  48. )
  49. self.session.add(item)
  50. self.session.commit()
  51. # Install plugin
  52. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  53. plugin = plugins.get_plugin("Mail")
  54. plugin.set_up(repo)
  55. dbobj = plugin.db_object()
  56. dbobj.active = True
  57. dbobj.project_id = repo.id
  58. dbobj.mail_to = "serg@wh40k.com"
  59. plugin.install(repo, dbobj)
  60. self.session.add(dbobj)
  61. self.session.commit()
  62. tests.create_tokens_acl(self.session, token_id="project-specific-foo")
  63. def test_remove_plugin_own_project_plugin_not_installed(self):
  64. """ Test removing a plugin from a project for which you're the
  65. main maintainer and the plugin is not installed.
  66. """
  67. # pingou's token with all the ACLs
  68. headers = {"Authorization": "token aaabbbcccddd"}
  69. # Remove a plugin from /test/ where pingou is the main admin
  70. output = self.app.post(
  71. "/api/0/test/settings/IRC/remove", headers=headers
  72. )
  73. self.assertEqual(output.status_code, 400)
  74. data = json.loads(output.get_data(as_text=True))
  75. self.assertEqual(
  76. pagure.api.APIERROR.EPLUGINNOTINSTALLED.name, data["error_code"]
  77. )
  78. self.assertEqual(
  79. pagure.api.APIERROR.EPLUGINNOTINSTALLED.value, data["error"]
  80. )
  81. def test_remove_plugin_own_project(self):
  82. """ Test removing a plugin from a project for which you're the
  83. main maintainer.
  84. """
  85. # pingou's token with all the ACLs
  86. headers = {"Authorization": "token aaabbbcccddd"}
  87. # Remove a plugin from /test/ where pingou is the main admin
  88. output = self.app.post(
  89. "/api/0/test/settings/Mail/remove", headers=headers
  90. )
  91. self.assertEqual(output.status_code, 200)
  92. data = json.loads(output.get_data(as_text=True))
  93. self.assertEqual(
  94. data,
  95. {
  96. "plugin": {"mail_to": "serg@wh40k.com"},
  97. "message": "Hook 'Mail' deactivated",
  98. },
  99. )
  100. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  101. def test_remove_plugin_someone_else_project_project_less_token(self):
  102. """ Test removing a plugin from a project with which you have
  103. nothing to do.
  104. """
  105. # pingou's token with all the ACLs
  106. headers = {"Authorization": "token project-less-foo"}
  107. # Remove a plugin from /test/ where pingou is the main admin
  108. output = self.app.post(
  109. "/api/0/test/settings/Mail/" "remove", headers=headers
  110. )
  111. self.assertEqual(output.status_code, 200)
  112. data = json.loads(output.get_data(as_text=True))
  113. self.assertEqual(
  114. data,
  115. {
  116. "plugin": {"mail_to": "serg@wh40k.com"},
  117. "message": "Hook 'Mail' deactivated",
  118. },
  119. )
  120. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  121. def test_remove_plugin_project_specific_token(self):
  122. """ Test removing a plugin from a project with a regular
  123. project-specific token.
  124. """
  125. # pingou's token with all the ACLs
  126. headers = {"Authorization": "token project-specific-foo"}
  127. # Remove a plugin from /test/ where pingou is the main admin
  128. output = self.app.post(
  129. "/api/0/test/settings/Mail/remove", headers=headers
  130. )
  131. self.assertEqual(output.status_code, 200)
  132. data = json.loads(output.get_data(as_text=True))
  133. self.assertEqual(
  134. data,
  135. {
  136. "plugin": {"mail_to": "serg@wh40k.com"},
  137. "message": "Hook 'Mail' deactivated",
  138. },
  139. )
  140. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  141. def test_remove_plugin_invalid_project_specific_token(self):
  142. """ Test removing a plugin from a project with a regular
  143. project-specific token but for another project.
  144. """
  145. # pingou's token with all the ACLs
  146. headers = {"Authorization": "token project-specific-foo"}
  147. # Remove a plugin from /test2/
  148. output = self.app.post(
  149. "/api/0/test2/settings/Mail/remove", headers=headers
  150. )
  151. self.assertEqual(output.status_code, 401)
  152. data = json.loads(output.get_data(as_text=True))
  153. self.assertEqual(
  154. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  155. )
  156. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  157. if __name__ == "__main__":
  158. unittest.main(verbosity=2)