test_pagure_flask_api_project_update_watch.py 9.4 KB

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