test_pagure_flask_api_project_update_watch.py 9.4 KB

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