test_pagure_flask_api_plugins_remove.py 6.2 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.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. @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(PagureFlaskApiPluginRemovetests, 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. # Install plugin
  51. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  52. plugin = plugins.get_plugin("Mail")
  53. plugin.set_up(repo)
  54. dbobj = plugin.db_object()
  55. dbobj.active = True
  56. dbobj.project_id = repo.id
  57. dbobj.mail_to = "serg@wh40k.com"
  58. plugin.install(repo, dbobj)
  59. self.session.add(dbobj)
  60. self.session.commit()
  61. tests.create_tokens_acl(self.session, token_id="project-specific-foo")
  62. def test_remove_plugin_own_project_plugin_not_installed(self):
  63. """Test removing a plugin from a project for which you're the
  64. main maintainer and the plugin is not installed.
  65. """
  66. # pingou's token with all the ACLs
  67. headers = {"Authorization": "token aaabbbcccddd"}
  68. # Remove a plugin from /test/ where pingou is the main admin
  69. output = self.app.post(
  70. "/api/0/test/settings/IRC/remove", headers=headers
  71. )
  72. self.assertEqual(output.status_code, 400)
  73. data = json.loads(output.get_data(as_text=True))
  74. self.assertEqual(
  75. pagure.api.APIERROR.EPLUGINNOTINSTALLED.name, data["error_code"]
  76. )
  77. self.assertEqual(
  78. pagure.api.APIERROR.EPLUGINNOTINSTALLED.value, data["error"]
  79. )
  80. def test_remove_plugin_own_project(self):
  81. """Test removing a plugin from a project for which you're the
  82. main maintainer.
  83. """
  84. # pingou's token with all the ACLs
  85. headers = {"Authorization": "token aaabbbcccddd"}
  86. # Remove a plugin from /test/ where pingou is the main admin
  87. output = self.app.post(
  88. "/api/0/test/settings/Mail/remove", headers=headers
  89. )
  90. self.assertEqual(output.status_code, 200)
  91. data = json.loads(output.get_data(as_text=True))
  92. self.assertEqual(
  93. data,
  94. {
  95. "plugin": {"mail_to": "serg@wh40k.com"},
  96. "message": "Hook 'Mail' deactivated",
  97. },
  98. )
  99. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  100. def test_remove_plugin_someone_else_project_project_less_token(self):
  101. """Test removing a plugin from a project with which you have
  102. nothing to do.
  103. """
  104. # pingou's token with all the ACLs
  105. headers = {"Authorization": "token project-less-foo"}
  106. # Remove a plugin from /test/ where pingou is the main admin
  107. output = self.app.post(
  108. "/api/0/test/settings/Mail/" "remove", headers=headers
  109. )
  110. self.assertEqual(output.status_code, 200)
  111. data = json.loads(output.get_data(as_text=True))
  112. self.assertEqual(
  113. data,
  114. {
  115. "plugin": {"mail_to": "serg@wh40k.com"},
  116. "message": "Hook 'Mail' deactivated",
  117. },
  118. )
  119. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  120. def test_remove_plugin_project_specific_token(self):
  121. """Test removing a plugin from a project with a regular
  122. project-specific token.
  123. """
  124. # pingou's token with all the ACLs
  125. headers = {"Authorization": "token project-specific-foo"}
  126. # Remove a plugin from /test/ where pingou is the main admin
  127. output = self.app.post(
  128. "/api/0/test/settings/Mail/remove", headers=headers
  129. )
  130. self.assertEqual(output.status_code, 200)
  131. data = json.loads(output.get_data(as_text=True))
  132. self.assertEqual(
  133. data,
  134. {
  135. "plugin": {"mail_to": "serg@wh40k.com"},
  136. "message": "Hook 'Mail' deactivated",
  137. },
  138. )
  139. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  140. def test_remove_plugin_invalid_project_specific_token(self):
  141. """Test removing a plugin from a project with a regular
  142. project-specific token but for another project.
  143. """
  144. # pingou's token with all the ACLs
  145. headers = {"Authorization": "token project-specific-foo"}
  146. # Remove a plugin from /test2/
  147. output = self.app.post(
  148. "/api/0/test2/settings/Mail/remove", headers=headers
  149. )
  150. self.assertEqual(output.status_code, 401)
  151. data = json.loads(output.get_data(as_text=True))
  152. self.assertEqual(
  153. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  154. )
  155. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  156. if __name__ == "__main__":
  157. unittest.main(verbosity=2)