test_pagure_flask_api_issue_create.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. import datetime
  8. import unittest
  9. import sys
  10. import os
  11. import json
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure # noqa: E402
  16. import pagure.lib # noqa: E402
  17. import tests # noqa: E402
  18. class PagureFlaskApiIssueCreatetests(tests.Modeltests):
  19. """ Tests for the flask API of pagure for creating an issue
  20. """
  21. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskApiIssueCreatetests, self).setUp()
  25. pagure.config.config['TICKETS_FOLDER'] = None
  26. tests.create_projects(self.session)
  27. tests.create_projects_git(os.path.join(self.path, 'tickets'))
  28. tests.create_tokens(self.session)
  29. tests.create_tokens_acl(self.session)
  30. # Create project-less token for user foo
  31. item = pagure.lib.model.Token(
  32. id='project-less-foo',
  33. user_id=2,
  34. project_id=None,
  35. expiration=datetime.datetime.utcnow()
  36. + datetime.timedelta(days=30)
  37. )
  38. self.session.add(item)
  39. self.session.commit()
  40. tests.create_tokens_acl(self.session, token_id='project-less-foo')
  41. # Create project-specific token for user foo
  42. item = pagure.lib.model.Token(
  43. id='project-specific-foo',
  44. user_id=2,
  45. project_id=1,
  46. expiration=datetime.datetime.utcnow()
  47. + datetime.timedelta(days=30)
  48. )
  49. self.session.add(item)
  50. self.session.commit()
  51. tests.create_tokens_acl(
  52. self.session, token_id='project-specific-foo')
  53. def test_create_issue_own_project_no_data(self):
  54. """ Test creating a new ticket on a project for which you're the
  55. main maintainer.
  56. """
  57. # pingou's token with all the ACLs
  58. headers = {'Authorization': 'token aaabbbcccddd'}
  59. # Create an issue on /test/ where pingou is the main admin
  60. output = self.app.post('/api/0/test/new_issue', headers=headers)
  61. self.assertEqual(output.status_code, 400)
  62. data = json.loads(output.data)
  63. self.assertEqual(
  64. pagure.api.APIERROR.EINVALIDREQ.name, data['error_code'])
  65. self.assertEqual(
  66. pagure.api.APIERROR.EINVALIDREQ.value, data['error'])
  67. self.assertEqual(
  68. data['errors'],
  69. {
  70. u'issue_content': [u'This field is required.'],
  71. u'title': [u'This field is required.']
  72. }
  73. )
  74. def test_create_issue_own_project_incomplete_data(self):
  75. """ Test creating a new ticket on a project for which you're the
  76. main maintainer.
  77. """
  78. # pingou's token with all the ACLs
  79. headers = {'Authorization': 'token aaabbbcccddd'}
  80. # complete data set
  81. data = {
  82. 'title': 'test issue',
  83. }
  84. # Create an issue on /test/ where pingou is the main admin
  85. output = self.app.post(
  86. '/api/0/test/new_issue',
  87. headers=headers,
  88. data=data)
  89. self.assertEqual(output.status_code, 400)
  90. data = json.loads(output.data)
  91. self.assertEqual(
  92. pagure.api.APIERROR.EINVALIDREQ.name, data['error_code'])
  93. self.assertEqual(
  94. pagure.api.APIERROR.EINVALIDREQ.value, data['error'])
  95. self.assertEqual(
  96. data['errors'],
  97. {
  98. u'issue_content': [u'This field is required.']
  99. }
  100. )
  101. def test_create_issue_own_project(self):
  102. """ Test creating a new ticket on a project for which you're the
  103. main maintainer.
  104. """
  105. # pingou's token with all the ACLs
  106. headers = {'Authorization': 'token aaabbbcccddd'}
  107. # complete data set
  108. data = {
  109. 'title': 'test issue',
  110. 'issue_content': 'This issue needs attention',
  111. }
  112. # Create an issue on /test/ where pingou is the main admin
  113. output = self.app.post(
  114. '/api/0/test/new_issue',
  115. headers=headers,
  116. data=data)
  117. self.assertEqual(output.status_code, 200)
  118. data = json.loads(output.data)
  119. data['issue']['date_created'] = '1431414800'
  120. data['issue']['last_updated'] = '1431414800'
  121. self.assertEqual(
  122. data,
  123. {
  124. "issue": {
  125. "assignee": None,
  126. "blocks": [],
  127. "close_status": None,
  128. "closed_at": None,
  129. "comments": [],
  130. "content": "This issue needs attention",
  131. "custom_fields": [],
  132. "date_created": "1431414800",
  133. "depends": [],
  134. "id": 1,
  135. "last_updated": "1431414800",
  136. "milestone": None,
  137. "priority": None,
  138. "private": False,
  139. "status": "Open",
  140. "tags": [],
  141. "title": "test issue",
  142. "user": {
  143. "fullname": "PY C",
  144. "name": "pingou"
  145. }
  146. },
  147. "message": "Issue created"
  148. }
  149. )
  150. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  151. def test_create_issue_someone_else_project_project_less_token(self):
  152. """ Test creating a new ticket on a project with which you have
  153. nothing to do.
  154. """
  155. # pingou's token with all the ACLs
  156. headers = {'Authorization': 'token project-less-foo'}
  157. # complete data set
  158. data = {
  159. 'title': 'test issue',
  160. 'issue_content': 'This issue needs attention',
  161. }
  162. # Create an issue on /test/ where pingou is the main admin
  163. output = self.app.post(
  164. '/api/0/test/new_issue',
  165. headers=headers,
  166. data=data)
  167. self.assertEqual(output.status_code, 200)
  168. data = json.loads(output.data)
  169. data['issue']['date_created'] = '1431414800'
  170. data['issue']['last_updated'] = '1431414800'
  171. self.assertEqual(
  172. data,
  173. {
  174. "issue": {
  175. "assignee": None,
  176. "blocks": [],
  177. "close_status": None,
  178. "closed_at": None,
  179. "comments": [],
  180. "content": "This issue needs attention",
  181. "custom_fields": [],
  182. "date_created": "1431414800",
  183. "depends": [],
  184. "id": 1,
  185. "last_updated": "1431414800",
  186. "milestone": None,
  187. "priority": None,
  188. "private": False,
  189. "status": "Open",
  190. "tags": [],
  191. "title": "test issue",
  192. "user": {
  193. "fullname": "foo bar",
  194. "name": "foo"
  195. }
  196. },
  197. "message": "Issue created"
  198. }
  199. )
  200. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  201. def test_create_issue_project_specific_token(self):
  202. """ Test creating a new ticket on a project with a regular
  203. project-specific token.
  204. """
  205. # pingou's token with all the ACLs
  206. headers = {'Authorization': 'token project-specific-foo'}
  207. # complete data set
  208. data = {
  209. 'title': 'test issue',
  210. 'issue_content': 'This issue needs attention',
  211. }
  212. # Create an issue on /test/ where pingou is the main admin
  213. output = self.app.post(
  214. '/api/0/test/new_issue',
  215. headers=headers,
  216. data=data)
  217. self.assertEqual(output.status_code, 200)
  218. data = json.loads(output.data)
  219. data['issue']['date_created'] = '1431414800'
  220. data['issue']['last_updated'] = '1431414800'
  221. self.assertEqual(
  222. data,
  223. {
  224. "issue": {
  225. "assignee": None,
  226. "blocks": [],
  227. "close_status": None,
  228. "closed_at": None,
  229. "comments": [],
  230. "content": "This issue needs attention",
  231. "custom_fields": [],
  232. "date_created": "1431414800",
  233. "depends": [],
  234. "id": 1,
  235. "last_updated": "1431414800",
  236. "milestone": None,
  237. "priority": None,
  238. "private": False,
  239. "status": "Open",
  240. "tags": [],
  241. "title": "test issue",
  242. "user": {
  243. "fullname": "foo bar",
  244. "name": "foo"
  245. }
  246. },
  247. "message": "Issue created"
  248. }
  249. )
  250. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  251. def test_create_issue_invalid_project_specific_token(self):
  252. """ Test creating a new ticket on a project with a regular
  253. project-specific token but for another project.
  254. """
  255. # pingou's token with all the ACLs
  256. headers = {'Authorization': 'token project-specific-foo'}
  257. # complete data set
  258. data = {
  259. 'title': 'test issue',
  260. 'issue_content': 'This issue needs attention',
  261. }
  262. # Create an issue on /test/ where pingou is the main admin
  263. output = self.app.post(
  264. '/api/0/test2/new_issue',
  265. headers=headers,
  266. data=data)
  267. self.assertEqual(output.status_code, 401)
  268. data = json.loads(output.data)
  269. self.assertEqual(
  270. pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
  271. self.assertEqual(
  272. pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  273. if __name__ == '__main__':
  274. unittest.main(verbosity=2)