test_pagure_flask_api_issue_create.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. @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(self.session, token_id="project-specific-foo")
  52. def test_create_issue_own_project_no_data(self):
  53. """Test creating a new ticket on a project for which you're the
  54. main maintainer.
  55. """
  56. # pingou's token with all the ACLs
  57. headers = {"Authorization": "token aaabbbcccddd"}
  58. # Create an issue on /test/ where pingou is the main admin
  59. output = self.app.post("/api/0/test/new_issue", headers=headers)
  60. self.assertEqual(output.status_code, 400)
  61. data = json.loads(output.get_data(as_text=True))
  62. self.assertEqual(
  63. pagure.api.APIERROR.EINVALIDREQ.name, data["error_code"]
  64. )
  65. self.assertEqual(pagure.api.APIERROR.EINVALIDREQ.value, data["error"])
  66. self.assertEqual(
  67. data["errors"],
  68. {
  69. "issue_content": ["This field is required."],
  70. "title": ["This field is required."],
  71. },
  72. )
  73. def test_create_issue_own_project_incomplete_data(self):
  74. """Test creating a new ticket on a project for which you're the
  75. main maintainer.
  76. """
  77. # pingou's token with all the ACLs
  78. headers = {"Authorization": "token aaabbbcccddd"}
  79. # complete data set
  80. data = {"title": "test issue"}
  81. # Create an issue on /test/ where pingou is the main admin
  82. output = self.app.post(
  83. "/api/0/test/new_issue", headers=headers, data=data
  84. )
  85. self.assertEqual(output.status_code, 400)
  86. data = json.loads(output.get_data(as_text=True))
  87. self.assertEqual(
  88. pagure.api.APIERROR.EINVALIDREQ.name, data["error_code"]
  89. )
  90. self.assertEqual(pagure.api.APIERROR.EINVALIDREQ.value, data["error"])
  91. self.assertEqual(
  92. data["errors"], {"issue_content": ["This field is required."]}
  93. )
  94. def test_create_issue_own_project(self):
  95. """Test creating a new ticket on a project for which you're the
  96. main maintainer.
  97. """
  98. # pingou's token with all the ACLs
  99. headers = {"Authorization": "token aaabbbcccddd"}
  100. # complete data set
  101. data = {
  102. "title": "test issue",
  103. "issue_content": "This issue needs attention",
  104. }
  105. # Create an issue on /test/ where pingou is the main admin
  106. output = self.app.post(
  107. "/api/0/test/new_issue", headers=headers, data=data
  108. )
  109. self.assertEqual(output.status_code, 200)
  110. data = json.loads(output.get_data(as_text=True))
  111. data["issue"]["date_created"] = "1431414800"
  112. data["issue"]["last_updated"] = "1431414800"
  113. self.assertEqual(
  114. data,
  115. {
  116. "issue": {
  117. "assignee": None,
  118. "blocks": [],
  119. "close_status": None,
  120. "closed_at": None,
  121. "closed_by": None,
  122. "comments": [],
  123. "content": "This issue needs attention",
  124. "custom_fields": [],
  125. "full_url": "http://localhost.localdomain/test/issue/1",
  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. "full_url": "http://localhost.localdomain/user/pingou",
  140. "name": "pingou",
  141. "url_path": "user/pingou",
  142. },
  143. },
  144. "message": "Issue created",
  145. },
  146. )
  147. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  148. def test_create_issue_someone_else_project_project_less_token(self):
  149. """Test creating a new ticket on a project with which you have
  150. nothing to do.
  151. """
  152. # pingou's token with all the ACLs
  153. headers = {"Authorization": "token project-less-foo"}
  154. # complete data set
  155. data = {
  156. "title": "test issue",
  157. "issue_content": "This issue needs attention",
  158. }
  159. # Create an issue on /test/ where pingou is the main admin
  160. output = self.app.post(
  161. "/api/0/test/new_issue", headers=headers, data=data
  162. )
  163. self.assertEqual(output.status_code, 200)
  164. data = json.loads(output.get_data(as_text=True))
  165. data["issue"]["date_created"] = "1431414800"
  166. data["issue"]["last_updated"] = "1431414800"
  167. self.assertEqual(
  168. data,
  169. {
  170. "issue": {
  171. "assignee": None,
  172. "blocks": [],
  173. "close_status": None,
  174. "closed_at": None,
  175. "closed_by": None,
  176. "comments": [],
  177. "content": "This issue needs attention",
  178. "custom_fields": [],
  179. "full_url": "http://localhost.localdomain/test/issue/1",
  180. "date_created": "1431414800",
  181. "depends": [],
  182. "id": 1,
  183. "last_updated": "1431414800",
  184. "milestone": None,
  185. "priority": None,
  186. "private": False,
  187. "related_prs": [],
  188. "status": "Open",
  189. "tags": [],
  190. "title": "test issue",
  191. "user": {
  192. "fullname": "foo bar",
  193. "full_url": "http://localhost.localdomain/user/foo",
  194. "name": "foo",
  195. "url_path": "user/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", headers=headers, data=data
  216. )
  217. self.assertEqual(output.status_code, 200)
  218. data = json.loads(output.get_data(as_text=True))
  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. "closed_by": None,
  230. "comments": [],
  231. "content": "This issue needs attention",
  232. "custom_fields": [],
  233. "full_url": "http://localhost.localdomain/test/issue/1",
  234. "date_created": "1431414800",
  235. "depends": [],
  236. "id": 1,
  237. "last_updated": "1431414800",
  238. "milestone": None,
  239. "priority": None,
  240. "private": False,
  241. "related_prs": [],
  242. "status": "Open",
  243. "tags": [],
  244. "title": "test issue",
  245. "user": {
  246. "fullname": "foo bar",
  247. "full_url": "http://localhost.localdomain/user/foo",
  248. "name": "foo",
  249. "url_path": "user/foo",
  250. },
  251. },
  252. "message": "Issue created",
  253. },
  254. )
  255. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  256. def test_create_issue_invalid_project_specific_token(self):
  257. """Test creating a new ticket on a project with a regular
  258. project-specific token but for another project.
  259. """
  260. # pingou's token with all the ACLs
  261. headers = {"Authorization": "token project-specific-foo"}
  262. # complete data set
  263. data = {
  264. "title": "test issue",
  265. "issue_content": "This issue needs attention",
  266. }
  267. # Create an issue on /test/ where pingou is the main admin
  268. output = self.app.post(
  269. "/api/0/test2/new_issue", headers=headers, data=data
  270. )
  271. self.assertEqual(output.status_code, 401)
  272. data = json.loads(output.get_data(as_text=True))
  273. self.assertEqual(
  274. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  275. )
  276. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  277. if __name__ == "__main__":
  278. unittest.main(verbosity=2)