test_pagure_flask_api_project_update_watch.py 9.4 KB

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