test_pagure_flask_api_project_update_watch.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. tests.create_tokens(
  35. self.session, user_id=1, project_id=None, suffix="_project_less"
  36. )
  37. tests.create_tokens_acl(
  38. self.session,
  39. token_id="aaabbbcccddd_project_less",
  40. acl_name="modify_project",
  41. )
  42. # Create normal issue
  43. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  44. msg = pagure.lib.query.new_issue(
  45. session=self.session,
  46. repo=repo,
  47. title="Test issue #1",
  48. content="We should work on this",
  49. user="pingou",
  50. private=False,
  51. )
  52. self.session.commit()
  53. self.assertEqual(msg.title, "Test issue #1")
  54. # Create project-less token for user foo
  55. item = pagure.lib.model.Token(
  56. id="project-less-foo",
  57. user_id=1,
  58. project_id=None,
  59. expiration=datetime.datetime.utcnow()
  60. + datetime.timedelta(days=30),
  61. )
  62. self.session.add(item)
  63. self.session.commit()
  64. tests.create_tokens_acl(self.session, token_id="project-less-foo")
  65. def test_api_update_project_watchers_invalid_project(self):
  66. """ Test the api_update_project_watchers method of the flask api. """
  67. headers = {"Authorization": "token aaabbbcccddd"}
  68. # Invalid project
  69. output = self.app.post(
  70. "/api/0/foobar/watchers/update", headers=headers
  71. )
  72. self.assertEqual(output.status_code, 404)
  73. data = json.loads(output.get_data(as_text=True))
  74. self.assertDictEqual(
  75. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  76. )
  77. def test_api_change_status_issue_token_not_for_project(self):
  78. """ Test the api_update_project_watchers method of the flask api. """
  79. headers = {"Authorization": "token aaabbbcccddd"}
  80. # Valid token, wrong project
  81. output = self.app.post("/api/0/test2/watchers/update", headers=headers)
  82. self.assertEqual(output.status_code, 401)
  83. data = json.loads(output.get_data(as_text=True))
  84. self.assertEqual(
  85. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  86. )
  87. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  88. def test_api_update_project_watchers_no_user_watching(self):
  89. """ Test the api_update_project_watchers method of the flask api. """
  90. headers = {"Authorization": "token aaabbbcccddd"}
  91. data = {"status": "42"}
  92. output = self.app.post(
  93. "/api/0/test/watchers/update", headers=headers, data=data
  94. )
  95. self.assertEqual(output.status_code, 400)
  96. data = json.loads(output.get_data(as_text=True))
  97. self.assertDictEqual(
  98. data,
  99. {
  100. "error": "Invalid or incomplete input submitted",
  101. "error_code": "EINVALIDREQ",
  102. },
  103. )
  104. def test_api_update_project_watchers_no_watch_status(self):
  105. """ Test the api_update_project_watchers method of the flask api. """
  106. headers = {"Authorization": "token aaabbbcccddd"}
  107. data = {"watcher": "pingou"}
  108. output = self.app.post(
  109. "/api/0/test/watchers/update", headers=headers, data=data
  110. )
  111. self.assertEqual(output.status_code, 400)
  112. data = json.loads(output.get_data(as_text=True))
  113. self.assertDictEqual(
  114. data,
  115. {
  116. "error": 'The watch value of "None" is invalid',
  117. "error_code": "ENOCODE",
  118. },
  119. )
  120. def test_api_update_project_watchers_invalid_status(self):
  121. """ Test the api_update_project_watchers method of the flask api. """
  122. headers = {"Authorization": "token aaabbbcccddd"}
  123. data = {"watcher": "pingou", "status": "42"}
  124. output = self.app.post(
  125. "/api/0/test/watchers/update", headers=headers, data=data
  126. )
  127. self.assertEqual(output.status_code, 400)
  128. data = json.loads(output.get_data(as_text=True))
  129. self.assertDictEqual(
  130. data,
  131. {
  132. "error": 'The watch value of "42" is invalid',
  133. "error_code": "ENOCODE",
  134. },
  135. )
  136. def test_api_update_project_watchers_invalid_user(self):
  137. """ Test the api_update_project_watchers method of the flask api. """
  138. headers = {"Authorization": "token aaabbbcccddd"}
  139. data = {"watcher": "example", "status": "2"}
  140. output = self.app.post(
  141. "/api/0/test/watchers/update", headers=headers, data=data
  142. )
  143. self.assertEqual(output.status_code, 401)
  144. data = json.loads(output.get_data(as_text=True))
  145. self.assertDictEqual(
  146. data,
  147. {
  148. "error": "You are not allowed to modify this project",
  149. "error_code": "EMODIFYPROJECTNOTALLOWED",
  150. },
  151. )
  152. def test_api_update_project_watchers_other_user(self):
  153. """ Test the api_update_project_watchers method of the flask api. """
  154. headers = {"Authorization": "token aaabbbcccddd"}
  155. data = {"watcher": "foo", "status": "2"}
  156. output = self.app.post(
  157. "/api/0/test/watchers/update", headers=headers, data=data
  158. )
  159. self.assertEqual(output.status_code, 401)
  160. data = json.loads(output.get_data(as_text=True))
  161. self.assertDictEqual(
  162. data,
  163. {
  164. "error": "You are not allowed to modify this project",
  165. "error_code": "EMODIFYPROJECTNOTALLOWED",
  166. },
  167. )
  168. def test_api_update_project_watchers_all_good(self):
  169. """ Test the api_update_project_watchers method of the flask api. """
  170. headers = {"Authorization": "token aaabbbcccddd"}
  171. data = {"watcher": "pingou", "status": 1}
  172. output = self.app.post(
  173. "/api/0/test/watchers/update", headers=headers, data=data
  174. )
  175. self.assertEqual(output.status_code, 200)
  176. data = json.loads(output.get_data(as_text=True))
  177. self.assertDictEqual(
  178. data,
  179. {
  180. "message": "You are now watching issues and PRs on this project",
  181. "status": "ok",
  182. },
  183. )
  184. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  185. def test_api_update_project_watchers_other_user_admin(self):
  186. """ Test the api_update_project_watchers method of the flask api. """
  187. headers = {"Authorization": "token aaabbbcccddd"}
  188. data = {"watcher": "foo", "status": "2"}
  189. output = self.app.post(
  190. "/api/0/test/watchers/update", headers=headers, data=data
  191. )
  192. self.assertEqual(output.status_code, 200)
  193. data = json.loads(output.get_data(as_text=True))
  194. self.assertDictEqual(
  195. data,
  196. {
  197. "message": "You are now watching commits on this project",
  198. "status": "ok",
  199. },
  200. )
  201. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  202. def test_api_update_project_watchers_set_then_reset(self):
  203. """ Test the api_update_project_watchers method of the flask api. """
  204. headers = {"Authorization": "token aaabbbcccddd_project_less"}
  205. data = {"watcher": "foo", "status": "2"}
  206. output = self.app.post(
  207. "/api/0/test/watchers/update", headers=headers, data=data
  208. )
  209. self.assertEqual(output.status_code, 200)
  210. data = json.loads(output.get_data(as_text=True))
  211. self.assertDictEqual(
  212. data,
  213. {
  214. "message": "You are now watching commits on this project",
  215. "status": "ok",
  216. },
  217. )
  218. data = {"watcher": "foo", "status": "-1"}
  219. output = self.app.post(
  220. "/api/0/test/watchers/update", headers=headers, data=data
  221. )
  222. self.assertEqual(output.status_code, 200)
  223. data = json.loads(output.get_data(as_text=True))
  224. self.assertDictEqual(
  225. data, {"message": "Watch status reset", "status": "ok"},
  226. )
  227. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  228. def test_api_update_project_watchers_invalid_user_admin(self):
  229. """ Test the api_update_project_watchers method of the flask api. """
  230. headers = {"Authorization": "token aaabbbcccddd"}
  231. data = {"watcher": "example", "status": "2"}
  232. output = self.app.post(
  233. "/api/0/test/watchers/update", headers=headers, data=data
  234. )
  235. self.assertEqual(output.status_code, 400)
  236. data = json.loads(output.get_data(as_text=True))
  237. self.assertDictEqual(
  238. data,
  239. {
  240. "error": "Invalid or incomplete input submitted",
  241. "error_code": "EINVALIDREQ",
  242. },
  243. )
  244. @patch("pagure.utils.is_admin", MagicMock(return_value=True))
  245. def test_api_update_project_watchers_missing_user_admin(self):
  246. """ Test the api_update_project_watchers method of the flask api. """
  247. headers = {"Authorization": "token aaabbbcccddd"}
  248. data = {"status": "2"}
  249. output = self.app.post(
  250. "/api/0/test/watchers/update", headers=headers, data=data
  251. )
  252. self.assertEqual(output.status_code, 400)
  253. data = json.loads(output.get_data(as_text=True))
  254. self.assertDictEqual(
  255. data,
  256. {
  257. "error": "Invalid or incomplete input submitted",
  258. "error_code": "EINVALIDREQ",
  259. },
  260. )
  261. if __name__ == "__main__":
  262. unittest.main(verbosity=2)