test_pagure_flask_api_issue_create.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import sys
  11. import os
  12. import json
  13. from mock import patch, MagicMock
  14. sys.path.insert(0, os.path.join(os.path.dirname(
  15. os.path.abspath(__file__)), '..'))
  16. import pagure.lib.query # 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.get_data(as_text=True))
  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. 'issue_content': ['This field is required.'],
  71. 'title': ['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.get_data(as_text=True))
  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. 'issue_content': ['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.get_data(as_text=True))
  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. "closed_by": None,
  130. "comments": [],
  131. "content": "This issue needs attention",
  132. "custom_fields": [],
  133. "date_created": "1431414800",
  134. "depends": [],
  135. "id": 1,
  136. "last_updated": "1431414800",
  137. "milestone": None,
  138. "priority": None,
  139. "private": False,
  140. "status": "Open",
  141. "tags": [],
  142. "title": "test issue",
  143. "user": {
  144. "fullname": "PY C",
  145. "name": "pingou"
  146. }
  147. },
  148. "message": "Issue created"
  149. }
  150. )
  151. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  152. def test_create_issue_someone_else_project_project_less_token(self):
  153. """ Test creating a new ticket on a project with which you have
  154. nothing to do.
  155. """
  156. # pingou's token with all the ACLs
  157. headers = {'Authorization': 'token project-less-foo'}
  158. # complete data set
  159. data = {
  160. 'title': 'test issue',
  161. 'issue_content': 'This issue needs attention',
  162. }
  163. # Create an issue on /test/ where pingou is the main admin
  164. output = self.app.post(
  165. '/api/0/test/new_issue',
  166. headers=headers,
  167. data=data)
  168. self.assertEqual(output.status_code, 200)
  169. data = json.loads(output.get_data(as_text=True))
  170. data['issue']['date_created'] = '1431414800'
  171. data['issue']['last_updated'] = '1431414800'
  172. self.assertEqual(
  173. data,
  174. {
  175. "issue": {
  176. "assignee": None,
  177. "blocks": [],
  178. "close_status": None,
  179. "closed_at": None,
  180. "closed_by": None,
  181. "comments": [],
  182. "content": "This issue needs attention",
  183. "custom_fields": [],
  184. "date_created": "1431414800",
  185. "depends": [],
  186. "id": 1,
  187. "last_updated": "1431414800",
  188. "milestone": None,
  189. "priority": None,
  190. "private": False,
  191. "status": "Open",
  192. "tags": [],
  193. "title": "test issue",
  194. "user": {
  195. "fullname": "foo bar",
  196. "name": "foo"
  197. }
  198. },
  199. "message": "Issue created"
  200. }
  201. )
  202. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  203. def test_create_issue_project_specific_token(self):
  204. """ Test creating a new ticket on a project with a regular
  205. project-specific token.
  206. """
  207. # pingou's token with all the ACLs
  208. headers = {'Authorization': 'token project-specific-foo'}
  209. # complete data set
  210. data = {
  211. 'title': 'test issue',
  212. 'issue_content': 'This issue needs attention',
  213. }
  214. # Create an issue on /test/ where pingou is the main admin
  215. output = self.app.post(
  216. '/api/0/test/new_issue',
  217. headers=headers,
  218. data=data)
  219. self.assertEqual(output.status_code, 200)
  220. data = json.loads(output.get_data(as_text=True))
  221. data['issue']['date_created'] = '1431414800'
  222. data['issue']['last_updated'] = '1431414800'
  223. self.assertEqual(
  224. data,
  225. {
  226. "issue": {
  227. "assignee": None,
  228. "blocks": [],
  229. "close_status": None,
  230. "closed_at": None,
  231. "closed_by": None,
  232. "comments": [],
  233. "content": "This issue needs attention",
  234. "custom_fields": [],
  235. "date_created": "1431414800",
  236. "depends": [],
  237. "id": 1,
  238. "last_updated": "1431414800",
  239. "milestone": None,
  240. "priority": None,
  241. "private": False,
  242. "status": "Open",
  243. "tags": [],
  244. "title": "test issue",
  245. "user": {
  246. "fullname": "foo bar",
  247. "name": "foo"
  248. }
  249. },
  250. "message": "Issue created"
  251. }
  252. )
  253. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  254. def test_create_issue_invalid_project_specific_token(self):
  255. """ Test creating a new ticket on a project with a regular
  256. project-specific token but for another project.
  257. """
  258. # pingou's token with all the ACLs
  259. headers = {'Authorization': 'token project-specific-foo'}
  260. # complete data set
  261. data = {
  262. 'title': 'test issue',
  263. 'issue_content': 'This issue needs attention',
  264. }
  265. # Create an issue on /test/ where pingou is the main admin
  266. output = self.app.post(
  267. '/api/0/test2/new_issue',
  268. headers=headers,
  269. data=data)
  270. self.assertEqual(output.status_code, 401)
  271. data = json.loads(output.get_data(as_text=True))
  272. self.assertEqual(
  273. pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
  274. self.assertEqual(
  275. pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  276. if __name__ == '__main__':
  277. unittest.main(verbosity=2)