test_pagure_flask_api_issue_create.py 10.0 KB

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