test_pagure_flask_api_issue_create.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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(
  15. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  16. )
  17. import pagure.lib.query # 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(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. )
  66. self.assertEqual(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 = {"title": "test issue"}
  82. # Create an issue on /test/ where pingou is the main admin
  83. output = self.app.post(
  84. "/api/0/test/new_issue", headers=headers, data=data
  85. )
  86. self.assertEqual(output.status_code, 400)
  87. data = json.loads(output.get_data(as_text=True))
  88. self.assertEqual(
  89. pagure.api.APIERROR.EINVALIDREQ.name, data["error_code"]
  90. )
  91. self.assertEqual(pagure.api.APIERROR.EINVALIDREQ.value, data["error"])
  92. self.assertEqual(
  93. data["errors"], {"issue_content": ["This field is required."]}
  94. )
  95. def test_create_issue_own_project(self):
  96. """ Test creating a new ticket on a project for which you're the
  97. main maintainer.
  98. """
  99. # pingou's token with all the ACLs
  100. headers = {"Authorization": "token aaabbbcccddd"}
  101. # complete data set
  102. data = {
  103. "title": "test issue",
  104. "issue_content": "This issue needs attention",
  105. }
  106. # Create an issue on /test/ where pingou is the main admin
  107. output = self.app.post(
  108. "/api/0/test/new_issue", headers=headers, data=data
  109. )
  110. self.assertEqual(output.status_code, 200)
  111. data = json.loads(output.get_data(as_text=True))
  112. data["issue"]["date_created"] = "1431414800"
  113. data["issue"]["last_updated"] = "1431414800"
  114. self.assertEqual(
  115. data,
  116. {
  117. "issue": {
  118. "assignee": None,
  119. "blocks": [],
  120. "close_status": None,
  121. "closed_at": None,
  122. "closed_by": None,
  123. "comments": [],
  124. "content": "This issue needs attention",
  125. "custom_fields": [],
  126. "date_created": "1431414800",
  127. "depends": [],
  128. "id": 1,
  129. "last_updated": "1431414800",
  130. "milestone": None,
  131. "priority": None,
  132. "private": False,
  133. "related_prs": [],
  134. "status": "Open",
  135. "tags": [],
  136. "title": "test issue",
  137. "user": {
  138. "fullname": "PY C",
  139. "name": "pingou",
  140. "url_path": "user/pingou",
  141. },
  142. },
  143. "message": "Issue created",
  144. },
  145. )
  146. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  147. def test_create_issue_someone_else_project_project_less_token(self):
  148. """ Test creating a new ticket on a project with which you have
  149. nothing to do.
  150. """
  151. # pingou's token with all the ACLs
  152. headers = {"Authorization": "token project-less-foo"}
  153. # complete data set
  154. data = {
  155. "title": "test issue",
  156. "issue_content": "This issue needs attention",
  157. }
  158. # Create an issue on /test/ where pingou is the main admin
  159. output = self.app.post(
  160. "/api/0/test/new_issue", headers=headers, data=data
  161. )
  162. self.assertEqual(output.status_code, 200)
  163. data = json.loads(output.get_data(as_text=True))
  164. data["issue"]["date_created"] = "1431414800"
  165. data["issue"]["last_updated"] = "1431414800"
  166. self.assertEqual(
  167. data,
  168. {
  169. "issue": {
  170. "assignee": None,
  171. "blocks": [],
  172. "close_status": None,
  173. "closed_at": None,
  174. "closed_by": None,
  175. "comments": [],
  176. "content": "This issue needs attention",
  177. "custom_fields": [],
  178. "date_created": "1431414800",
  179. "depends": [],
  180. "id": 1,
  181. "last_updated": "1431414800",
  182. "milestone": None,
  183. "priority": None,
  184. "private": False,
  185. "related_prs": [],
  186. "status": "Open",
  187. "tags": [],
  188. "title": "test issue",
  189. "user": {
  190. "fullname": "foo bar",
  191. "name": "foo",
  192. "url_path": "user/foo",
  193. },
  194. },
  195. "message": "Issue created",
  196. },
  197. )
  198. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  199. def test_create_issue_project_specific_token(self):
  200. """ Test creating a new ticket on a project with a regular
  201. project-specific token.
  202. """
  203. # pingou's token with all the ACLs
  204. headers = {"Authorization": "token project-specific-foo"}
  205. # complete data set
  206. data = {
  207. "title": "test issue",
  208. "issue_content": "This issue needs attention",
  209. }
  210. # Create an issue on /test/ where pingou is the main admin
  211. output = self.app.post(
  212. "/api/0/test/new_issue", headers=headers, data=data
  213. )
  214. self.assertEqual(output.status_code, 200)
  215. data = json.loads(output.get_data(as_text=True))
  216. data["issue"]["date_created"] = "1431414800"
  217. data["issue"]["last_updated"] = "1431414800"
  218. self.assertEqual(
  219. data,
  220. {
  221. "issue": {
  222. "assignee": None,
  223. "blocks": [],
  224. "close_status": None,
  225. "closed_at": None,
  226. "closed_by": None,
  227. "comments": [],
  228. "content": "This issue needs attention",
  229. "custom_fields": [],
  230. "date_created": "1431414800",
  231. "depends": [],
  232. "id": 1,
  233. "last_updated": "1431414800",
  234. "milestone": None,
  235. "priority": None,
  236. "private": False,
  237. "related_prs": [],
  238. "status": "Open",
  239. "tags": [],
  240. "title": "test issue",
  241. "user": {
  242. "fullname": "foo bar",
  243. "name": "foo",
  244. "url_path": "user/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", headers=headers, data=data
  265. )
  266. self.assertEqual(output.status_code, 401)
  267. data = json.loads(output.get_data(as_text=True))
  268. self.assertEqual(
  269. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  270. )
  271. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  272. if __name__ == "__main__":
  273. unittest.main(verbosity=2)