test_pagure_flask_api_project_update_watch.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import copy
  9. import datetime
  10. import unittest
  11. import shutil
  12. import sys
  13. import time
  14. import os
  15. import json
  16. from mock import patch, MagicMock
  17. sys.path.insert(
  18. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  19. )
  20. import pagure.lib.query
  21. import tests
  22. class PagureFlaskApiProjectUpdateWatchTests(tests.Modeltests):
  23. """ Tests for the flask API of pagure for changing the watch status on
  24. a project via the API
  25. """
  26. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  27. def setUp(self):
  28. """ Set up the environnment, ran before every tests. """
  29. super(PagureFlaskApiProjectUpdateWatchTests, self).setUp()
  30. tests.create_projects(self.session)
  31. tests.create_projects_git(os.path.join(self.path, "tickets"))
  32. tests.create_tokens(self.session)
  33. tests.create_tokens_acl(self.session)
  34. # Create normal issue
  35. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  36. msg = pagure.lib.query.new_issue(
  37. session=self.session,
  38. repo=repo,
  39. title="Test issue #1",
  40. content="We should work on this",
  41. user="pingou",
  42. private=False,
  43. )
  44. self.session.commit()
  45. self.assertEqual(msg.title, "Test issue #1")
  46. # Create project-less token for user foo
  47. item = pagure.lib.model.Token(
  48. id="project-less-foo",
  49. user_id=1,
  50. project_id=None,
  51. expiration=datetime.datetime.utcnow()
  52. + datetime.timedelta(days=30),
  53. )
  54. self.session.add(item)
  55. self.session.commit()
  56. tests.create_tokens_acl(self.session, token_id="project-less-foo")
  57. def test_api_update_project_watchers_invalid_project(self):
  58. """ Test the api_update_project_watchers method of the flask api. """
  59. headers = {"Authorization": "token aaabbbcccddd"}
  60. # Invalid project
  61. output = self.app.post(
  62. "/api/0/foobar/watchers/update", headers=headers
  63. )
  64. self.assertEqual(output.status_code, 404)
  65. data = json.loads(output.get_data(as_text=True))
  66. self.assertDictEqual(
  67. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  68. )
  69. def test_api_change_status_issue_token_not_for_project(self):
  70. """ Test the api_update_project_watchers method of the flask api. """
  71. headers = {"Authorization": "token aaabbbcccddd"}
  72. # Valid token, wrong project
  73. output = self.app.post("/api/0/test2/watchers/update", headers=headers)
  74. self.assertEqual(output.status_code, 401)
  75. data = json.loads(output.get_data(as_text=True))
  76. self.assertEqual(
  77. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  78. )
  79. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  80. def test_api_update_project_watchers_no_user_watching(self):
  81. """ Test the api_update_project_watchers method of the flask api. """
  82. headers = {"Authorization": "token aaabbbcccddd"}
  83. data = {"status": "42"}
  84. output = self.app.post(
  85. "/api/0/test/watchers/update", headers=headers, data=data
  86. )
  87. self.assertEqual(output.status_code, 400)
  88. data = json.loads(output.get_data(as_text=True))
  89. self.assertDictEqual(
  90. data,
  91. {
  92. "error": "Invalid or incomplete input submitted",
  93. "error_code": "EINVALIDREQ",
  94. },
  95. )
  96. def test_api_update_project_watchers_no_watch_status(self):
  97. """ Test the api_update_project_watchers method of the flask api. """
  98. headers = {"Authorization": "token aaabbbcccddd"}
  99. data = {"watcher": "pingou"}
  100. output = self.app.post(
  101. "/api/0/test/watchers/update", headers=headers, data=data
  102. )
  103. self.assertEqual(output.status_code, 400)
  104. data = json.loads(output.get_data(as_text=True))
  105. self.assertDictEqual(
  106. data,
  107. {
  108. "error": 'The watch value of "None" is invalid',
  109. "error_code": "ENOCODE",
  110. },
  111. )
  112. def test_api_update_project_watchers_invalid_status(self):
  113. """ Test the api_update_project_watchers method of the flask api. """
  114. headers = {"Authorization": "token aaabbbcccddd"}
  115. data = {"watcher": "pingou", "status": "42"}
  116. output = self.app.post(
  117. "/api/0/test/watchers/update", headers=headers, data=data
  118. )
  119. self.assertEqual(output.status_code, 400)
  120. data = json.loads(output.get_data(as_text=True))
  121. self.assertDictEqual(
  122. data,
  123. {
  124. "error": 'The watch value of "42" is invalid',
  125. "error_code": "ENOCODE",
  126. },
  127. )
  128. def test_api_update_project_watchers_invalid_user(self):
  129. """ Test the api_update_project_watchers method of the flask api. """
  130. headers = {"Authorization": "token aaabbbcccddd"}
  131. data = {"watcher": "example", "status": "2"}
  132. output = self.app.post(
  133. "/api/0/test/watchers/update", headers=headers, data=data
  134. )
  135. self.assertEqual(output.status_code, 401)
  136. data = json.loads(output.get_data(as_text=True))
  137. self.assertDictEqual(
  138. data,
  139. {
  140. "error": "You are not allowed to modify this project",
  141. "error_code": "EMODIFYPROJECTNOTALLOWED",
  142. },
  143. )
  144. def test_api_update_project_watchers_other_user(self):
  145. """ Test the api_update_project_watchers method of the flask api. """
  146. headers = {"Authorization": "token aaabbbcccddd"}
  147. data = {"watcher": "foo", "status": "2"}
  148. output = self.app.post(
  149. "/api/0/test/watchers/update", headers=headers, data=data
  150. )
  151. self.assertEqual(output.status_code, 401)
  152. data = json.loads(output.get_data(as_text=True))
  153. self.assertDictEqual(
  154. data,
  155. {
  156. "error": "You are not allowed to modify this project",
  157. "error_code": "EMODIFYPROJECTNOTALLOWED",
  158. },
  159. )
  160. def test_api_update_project_watchers_all_good(self):
  161. """ Test the api_update_project_watchers method of the flask api. """
  162. headers = {"Authorization": "token aaabbbcccddd"}
  163. data = {"watcher": "pingou", "status": 1}
  164. output = self.app.post(
  165. "/api/0/test/watchers/update", headers=headers, data=data
  166. )
  167. self.assertEqual(output.status_code, 200)
  168. data = json.loads(output.get_data(as_text=True))
  169. self.assertDictEqual(
  170. data,
  171. {
  172. "message": "You are now watching issues and PRs on this project",
  173. "status": "ok",
  174. },
  175. )
  176. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  177. def test_api_update_project_watchers_other_user_admin(self):
  178. """ Test the api_update_project_watchers method of the flask api. """
  179. headers = {"Authorization": "token aaabbbcccddd"}
  180. data = {"watcher": "foo", "status": "2"}
  181. output = self.app.post(
  182. "/api/0/test/watchers/update", headers=headers, data=data
  183. )
  184. self.assertEqual(output.status_code, 200)
  185. data = json.loads(output.get_data(as_text=True))
  186. self.assertDictEqual(
  187. data,
  188. {
  189. "message": "You are now watching commits on this project",
  190. "status": "ok",
  191. },
  192. )
  193. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  194. def test_api_update_project_watchers_invalid_user_admin(self):
  195. """ Test the api_update_project_watchers method of the flask api. """
  196. headers = {"Authorization": "token aaabbbcccddd"}
  197. data = {"watcher": "example", "status": "2"}
  198. output = self.app.post(
  199. "/api/0/test/watchers/update", headers=headers, data=data
  200. )
  201. self.assertEqual(output.status_code, 400)
  202. data = json.loads(output.get_data(as_text=True))
  203. self.assertDictEqual(
  204. data,
  205. {
  206. "error": "Invalid or incomplete input submitted",
  207. "error_code": "EINVALIDREQ",
  208. },
  209. )
  210. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  211. def test_api_update_project_watchers_missing_user_admin(self):
  212. """ Test the api_update_project_watchers method of the flask api. """
  213. headers = {"Authorization": "token aaabbbcccddd"}
  214. data = {"status": "2"}
  215. output = self.app.post(
  216. "/api/0/test/watchers/update", headers=headers, data=data
  217. )
  218. self.assertEqual(output.status_code, 400)
  219. data = json.loads(output.get_data(as_text=True))
  220. self.assertDictEqual(
  221. data,
  222. {
  223. "error": "Invalid or incomplete input submitted",
  224. "error_code": "EINVALIDREQ",
  225. },
  226. )
  227. if __name__ == "__main__":
  228. unittest.main(verbosity=2)