test_pagure_flask_api_issue_create.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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": {"fullname": "PY C", "name": "pingou"},
  138. },
  139. "message": "Issue created",
  140. },
  141. )
  142. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  143. def test_create_issue_someone_else_project_project_less_token(self):
  144. """ Test creating a new ticket on a project with which you have
  145. nothing to do.
  146. """
  147. # pingou's token with all the ACLs
  148. headers = {"Authorization": "token project-less-foo"}
  149. # complete data set
  150. data = {
  151. "title": "test issue",
  152. "issue_content": "This issue needs attention",
  153. }
  154. # Create an issue on /test/ where pingou is the main admin
  155. output = self.app.post(
  156. "/api/0/test/new_issue", headers=headers, data=data
  157. )
  158. self.assertEqual(output.status_code, 200)
  159. data = json.loads(output.get_data(as_text=True))
  160. data["issue"]["date_created"] = "1431414800"
  161. data["issue"]["last_updated"] = "1431414800"
  162. self.assertEqual(
  163. data,
  164. {
  165. "issue": {
  166. "assignee": None,
  167. "blocks": [],
  168. "close_status": None,
  169. "closed_at": None,
  170. "closed_by": None,
  171. "comments": [],
  172. "content": "This issue needs attention",
  173. "custom_fields": [],
  174. "date_created": "1431414800",
  175. "depends": [],
  176. "id": 1,
  177. "last_updated": "1431414800",
  178. "milestone": None,
  179. "priority": None,
  180. "private": False,
  181. "related_prs": [],
  182. "status": "Open",
  183. "tags": [],
  184. "title": "test issue",
  185. "user": {"fullname": "foo bar", "name": "foo"},
  186. },
  187. "message": "Issue created",
  188. },
  189. )
  190. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  191. def test_create_issue_project_specific_token(self):
  192. """ Test creating a new ticket on a project with a regular
  193. project-specific token.
  194. """
  195. # pingou's token with all the ACLs
  196. headers = {"Authorization": "token project-specific-foo"}
  197. # complete data set
  198. data = {
  199. "title": "test issue",
  200. "issue_content": "This issue needs attention",
  201. }
  202. # Create an issue on /test/ where pingou is the main admin
  203. output = self.app.post(
  204. "/api/0/test/new_issue", headers=headers, data=data
  205. )
  206. self.assertEqual(output.status_code, 200)
  207. data = json.loads(output.get_data(as_text=True))
  208. data["issue"]["date_created"] = "1431414800"
  209. data["issue"]["last_updated"] = "1431414800"
  210. self.assertEqual(
  211. data,
  212. {
  213. "issue": {
  214. "assignee": None,
  215. "blocks": [],
  216. "close_status": None,
  217. "closed_at": None,
  218. "closed_by": None,
  219. "comments": [],
  220. "content": "This issue needs attention",
  221. "custom_fields": [],
  222. "date_created": "1431414800",
  223. "depends": [],
  224. "id": 1,
  225. "last_updated": "1431414800",
  226. "milestone": None,
  227. "priority": None,
  228. "private": False,
  229. "related_prs": [],
  230. "status": "Open",
  231. "tags": [],
  232. "title": "test issue",
  233. "user": {"fullname": "foo bar", "name": "foo"},
  234. },
  235. "message": "Issue created",
  236. },
  237. )
  238. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  239. def test_create_issue_invalid_project_specific_token(self):
  240. """ Test creating a new ticket on a project with a regular
  241. project-specific token but for another project.
  242. """
  243. # pingou's token with all the ACLs
  244. headers = {"Authorization": "token project-specific-foo"}
  245. # complete data set
  246. data = {
  247. "title": "test issue",
  248. "issue_content": "This issue needs attention",
  249. }
  250. # Create an issue on /test/ where pingou is the main admin
  251. output = self.app.post(
  252. "/api/0/test2/new_issue", headers=headers, data=data
  253. )
  254. self.assertEqual(output.status_code, 401)
  255. data = json.loads(output.get_data(as_text=True))
  256. self.assertEqual(
  257. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  258. )
  259. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  260. if __name__ == "__main__":
  261. unittest.main(verbosity=2)