test_pagure_flask_api_issue_create.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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
  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 # noqa: E402
  17. import pagure.lib # noqa: E402
  18. import tests # noqa: E402
  19. class PagureFlaskApiIssueCreatetests(tests.Modeltests):
  20. """ Tests for the flask API of pagure for creating an issue
  21. """
  22. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskApiIssueCreatetests, self).setUp()
  26. pagure.config.config['TICKETS_FOLDER'] = None
  27. tests.create_projects(self.session)
  28. tests.create_projects_git(os.path.join(self.path, 'tickets'))
  29. tests.create_tokens(self.session)
  30. tests.create_tokens_acl(self.session)
  31. # Create project-less token for user foo
  32. item = pagure.lib.model.Token(
  33. id='project-less-foo',
  34. user_id=2,
  35. project_id=None,
  36. expiration=datetime.datetime.utcnow()
  37. + datetime.timedelta(days=30)
  38. )
  39. self.session.add(item)
  40. self.session.commit()
  41. tests.create_tokens_acl(self.session, token_id='project-less-foo')
  42. # Create project-specific token for user foo
  43. item = pagure.lib.model.Token(
  44. id='project-specific-foo',
  45. user_id=2,
  46. project_id=1,
  47. expiration=datetime.datetime.utcnow()
  48. + datetime.timedelta(days=30)
  49. )
  50. self.session.add(item)
  51. self.session.commit()
  52. tests.create_tokens_acl(
  53. self.session, token_id='project-specific-foo')
  54. def test_create_issue_own_project_no_data(self):
  55. """ Test creating a new ticket on a project for which you're the
  56. main maintainer.
  57. """
  58. # pingou's token with all the ACLs
  59. headers = {'Authorization': 'token aaabbbcccddd'}
  60. # Create an issue on /test/ where pingou is the main admin
  61. output = self.app.post('/api/0/test/new_issue', headers=headers)
  62. self.assertEqual(output.status_code, 400)
  63. data = json.loads(output.get_data(as_text=True))
  64. self.assertEqual(
  65. pagure.api.APIERROR.EINVALIDREQ.name, data['error_code'])
  66. self.assertEqual(
  67. pagure.api.APIERROR.EINVALIDREQ.value, data['error'])
  68. self.assertEqual(
  69. data['errors'],
  70. {
  71. 'issue_content': ['This field is required.'],
  72. 'title': ['This field is required.']
  73. }
  74. )
  75. def test_create_issue_own_project_incomplete_data(self):
  76. """ Test creating a new ticket on a project for which you're the
  77. main maintainer.
  78. """
  79. # pingou's token with all the ACLs
  80. headers = {'Authorization': 'token aaabbbcccddd'}
  81. # complete data set
  82. data = {
  83. 'title': 'test issue',
  84. }
  85. # Create an issue on /test/ where pingou is the main admin
  86. output = self.app.post(
  87. '/api/0/test/new_issue',
  88. headers=headers,
  89. data=data)
  90. self.assertEqual(output.status_code, 400)
  91. data = json.loads(output.get_data(as_text=True))
  92. self.assertEqual(
  93. pagure.api.APIERROR.EINVALIDREQ.name, data['error_code'])
  94. self.assertEqual(
  95. pagure.api.APIERROR.EINVALIDREQ.value, data['error'])
  96. self.assertEqual(
  97. data['errors'],
  98. {
  99. 'issue_content': ['This field is required.']
  100. }
  101. )
  102. def test_create_issue_own_project(self):
  103. """ Test creating a new ticket on a project for which you're the
  104. main maintainer.
  105. """
  106. # pingou's token with all the ACLs
  107. headers = {'Authorization': 'token aaabbbcccddd'}
  108. # complete data set
  109. data = {
  110. 'title': 'test issue',
  111. 'issue_content': 'This issue needs attention',
  112. }
  113. # Create an issue on /test/ where pingou is the main admin
  114. output = self.app.post(
  115. '/api/0/test/new_issue',
  116. headers=headers,
  117. data=data)
  118. self.assertEqual(output.status_code, 200)
  119. data = json.loads(output.get_data(as_text=True))
  120. data['issue']['date_created'] = '1431414800'
  121. data['issue']['last_updated'] = '1431414800'
  122. self.assertEqual(
  123. data,
  124. {
  125. "issue": {
  126. "assignee": None,
  127. "blocks": [],
  128. "close_status": None,
  129. "closed_at": 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. "comments": [],
  181. "content": "This issue needs attention",
  182. "custom_fields": [],
  183. "date_created": "1431414800",
  184. "depends": [],
  185. "id": 1,
  186. "last_updated": "1431414800",
  187. "milestone": None,
  188. "priority": None,
  189. "private": False,
  190. "status": "Open",
  191. "tags": [],
  192. "title": "test issue",
  193. "user": {
  194. "fullname": "foo bar",
  195. "name": "foo"
  196. }
  197. },
  198. "message": "Issue created"
  199. }
  200. )
  201. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  202. def test_create_issue_project_specific_token(self):
  203. """ Test creating a new ticket on a project with a regular
  204. project-specific token.
  205. """
  206. # pingou's token with all the ACLs
  207. headers = {'Authorization': 'token project-specific-foo'}
  208. # complete data set
  209. data = {
  210. 'title': 'test issue',
  211. 'issue_content': 'This issue needs attention',
  212. }
  213. # Create an issue on /test/ where pingou is the main admin
  214. output = self.app.post(
  215. '/api/0/test/new_issue',
  216. headers=headers,
  217. data=data)
  218. self.assertEqual(output.status_code, 200)
  219. data = json.loads(output.get_data(as_text=True))
  220. data['issue']['date_created'] = '1431414800'
  221. data['issue']['last_updated'] = '1431414800'
  222. self.assertEqual(
  223. data,
  224. {
  225. "issue": {
  226. "assignee": None,
  227. "blocks": [],
  228. "close_status": None,
  229. "closed_at": None,
  230. "comments": [],
  231. "content": "This issue needs attention",
  232. "custom_fields": [],
  233. "date_created": "1431414800",
  234. "depends": [],
  235. "id": 1,
  236. "last_updated": "1431414800",
  237. "milestone": None,
  238. "priority": None,
  239. "private": False,
  240. "status": "Open",
  241. "tags": [],
  242. "title": "test issue",
  243. "user": {
  244. "fullname": "foo bar",
  245. "name": "foo"
  246. }
  247. },
  248. "message": "Issue created"
  249. }
  250. )
  251. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  252. def test_create_issue_invalid_project_specific_token(self):
  253. """ Test creating a new ticket on a project with a regular
  254. project-specific token but for another project.
  255. """
  256. # pingou's token with all the ACLs
  257. headers = {'Authorization': 'token project-specific-foo'}
  258. # complete data set
  259. data = {
  260. 'title': 'test issue',
  261. 'issue_content': 'This issue needs attention',
  262. }
  263. # Create an issue on /test/ where pingou is the main admin
  264. output = self.app.post(
  265. '/api/0/test2/new_issue',
  266. headers=headers,
  267. data=data)
  268. self.assertEqual(output.status_code, 401)
  269. data = json.loads(output.get_data(as_text=True))
  270. self.assertEqual(
  271. pagure.api.APIERROR.EINVALIDTOK.name, data['error_code'])
  272. self.assertEqual(
  273. pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  274. if __name__ == '__main__':
  275. unittest.main(verbosity=2)