test_pagure_flask_api_project_git_alias.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2020 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import shutil
  10. import sys
  11. import os
  12. import json
  13. import pygit2
  14. from mock import patch, MagicMock
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.api
  19. import pagure.flask_app
  20. import pagure.lib.query
  21. import tests
  22. def set_projects_up(self):
  23. tests.create_projects(self.session)
  24. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  25. tests.add_content_git_repo(os.path.join(self.path, "repos", "test.git"))
  26. tests.create_tokens(self.session)
  27. tests.create_tokens_acl(self.session)
  28. self.session.commit()
  29. def set_up_board(self):
  30. headers = {
  31. "Authorization": "token aaabbbcccddd",
  32. "Content-Type": "application/json",
  33. }
  34. data = json.dumps({"dev": {"active": True, "tag": "dev"}})
  35. output = self.app.post("/api/0/test/boards", headers=headers, data=data)
  36. self.assertEqual(output.status_code, 200)
  37. data = json.loads(output.get_data(as_text=True))
  38. self.assertDictEqual(
  39. data,
  40. {
  41. "boards": [
  42. {
  43. "active": True,
  44. "full_url": "http://localhost.localdomain/test/boards/dev",
  45. "name": "dev",
  46. "status": [],
  47. "tag": {
  48. "tag": "dev",
  49. "tag_color": "DeepBlueSky",
  50. "tag_description": "",
  51. },
  52. }
  53. ]
  54. },
  55. )
  56. class PagureFlaskApiProjectGitAliastests(tests.SimplePagureTest):
  57. """Tests for flask API for branch alias in pagure"""
  58. maxDiff = None
  59. def setUp(self):
  60. super(PagureFlaskApiProjectGitAliastests, self).setUp()
  61. set_projects_up(self)
  62. self.repo_obj = pygit2.Repository(
  63. os.path.join(self.path, "repos", "test.git")
  64. )
  65. def test_api_git_alias_view_no_project(self):
  66. output = self.app.get("/api/0/invalid/git/alias")
  67. self.assertEqual(output.status_code, 404)
  68. data = json.loads(output.get_data(as_text=True))
  69. self.assertDictEqual(
  70. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  71. )
  72. def test_api_git_alias_view_empty(self):
  73. output = self.app.get("/api/0/test/git/alias")
  74. self.assertEqual(output.status_code, 200)
  75. data = json.loads(output.get_data(as_text=True))
  76. self.assertDictEqual(data, {})
  77. def test_api_new_git_alias_no_data(self):
  78. data = "{}"
  79. headers = {
  80. "Authorization": "token aaabbbcccddd",
  81. "Content-Type": "application/json",
  82. }
  83. output = self.app.post(
  84. "/api/0/test/git/alias/new", headers=headers, data=data
  85. )
  86. self.assertEqual(output.status_code, 400)
  87. data = json.loads(output.get_data(as_text=True))
  88. self.assertDictEqual(
  89. data,
  90. {
  91. "error": "Invalid or incomplete input submitted",
  92. "error_code": "EINVALIDREQ",
  93. },
  94. )
  95. def test_api_new_git_alias_invalid_data(self):
  96. data = json.dumps({"dev": "foobar"})
  97. headers = {
  98. "Authorization": "token aaabbbcccddd",
  99. "Content-Type": "application/json",
  100. }
  101. output = self.app.post(
  102. "/api/0/test/git/alias/new", headers=headers, data=data
  103. )
  104. self.assertEqual(output.status_code, 400)
  105. data = json.loads(output.get_data(as_text=True))
  106. self.assertDictEqual(
  107. data,
  108. {
  109. "error": "Invalid or incomplete input submitted",
  110. "error_code": "EINVALIDREQ",
  111. },
  112. )
  113. def test_api_new_git_alias_missing_data(self):
  114. data = json.dumps({"alias_from": "mster"})
  115. headers = {
  116. "Authorization": "token aaabbbcccddd",
  117. "Content-Type": "application/json",
  118. }
  119. output = self.app.post(
  120. "/api/0/test/git/alias/new", headers=headers, data=data
  121. )
  122. self.assertEqual(output.status_code, 400)
  123. data = json.loads(output.get_data(as_text=True))
  124. self.assertDictEqual(
  125. data,
  126. {
  127. "error": "Invalid or incomplete input submitted",
  128. "error_code": "EINVALIDREQ",
  129. },
  130. )
  131. def test_api_new_git_alias_no_existant_branch(self):
  132. data = json.dumps({"alias_from": "master", "alias_to": "main"})
  133. headers = {
  134. "Authorization": "token aaabbbcccddd",
  135. "Content-Type": "application/json",
  136. }
  137. output = self.app.post(
  138. "/api/0/test/git/alias/new", headers=headers, data=data
  139. )
  140. self.assertEqual(output.status_code, 400)
  141. data = json.loads(output.get_data(as_text=True))
  142. self.assertDictEqual(
  143. data,
  144. {
  145. "error": "Branch not found in this git repository",
  146. "error_code": "EBRANCHNOTFOUND",
  147. },
  148. )
  149. def test_api_new_git_alias(self):
  150. data = json.dumps({"alias_from": "main", "alias_to": "master"})
  151. headers = {
  152. "Authorization": "token aaabbbcccddd",
  153. "Content-Type": "application/json",
  154. }
  155. output = self.app.post(
  156. "/api/0/test/git/alias/new", headers=headers, data=data
  157. )
  158. self.assertEqual(output.status_code, 200)
  159. data = json.loads(output.get_data(as_text=True))
  160. self.assertDictEqual(data, {"refs/heads/main": "refs/heads/master"})
  161. def test_api_drop_git_alias_no_data(self):
  162. data = "{}"
  163. headers = {
  164. "Authorization": "token aaabbbcccddd",
  165. "Content-Type": "application/json",
  166. }
  167. output = self.app.post(
  168. "/api/0/test/git/alias/drop", headers=headers, data=data
  169. )
  170. self.assertEqual(output.status_code, 400)
  171. data = json.loads(output.get_data(as_text=True))
  172. self.assertDictEqual(
  173. data,
  174. {
  175. "error": "Invalid or incomplete input submitted",
  176. "error_code": "EINVALIDREQ",
  177. },
  178. )
  179. def test_api_drop_git_alias_invalid_data(self):
  180. data = json.dumps({"dev": "foobar"})
  181. headers = {
  182. "Authorization": "token aaabbbcccddd",
  183. "Content-Type": "application/json",
  184. }
  185. output = self.app.post(
  186. "/api/0/test/git/alias/drop", headers=headers, data=data
  187. )
  188. self.assertEqual(output.status_code, 400)
  189. data = json.loads(output.get_data(as_text=True))
  190. self.assertDictEqual(
  191. data,
  192. {
  193. "error": "Invalid or incomplete input submitted",
  194. "error_code": "EINVALIDREQ",
  195. },
  196. )
  197. def test_api_drop_git_alias_missing_data(self):
  198. data = json.dumps({"alias_from": "mster"})
  199. headers = {
  200. "Authorization": "token aaabbbcccddd",
  201. "Content-Type": "application/json",
  202. }
  203. output = self.app.post(
  204. "/api/0/test/git/alias/drop", headers=headers, data=data
  205. )
  206. self.assertEqual(output.status_code, 400)
  207. data = json.loads(output.get_data(as_text=True))
  208. self.assertDictEqual(
  209. data,
  210. {
  211. "error": "Invalid or incomplete input submitted",
  212. "error_code": "EINVALIDREQ",
  213. },
  214. )
  215. def test_api_drop_git_alias_no_existant_branch(self):
  216. data = json.dumps({"alias_from": "master", "alias_to": "main"})
  217. headers = {
  218. "Authorization": "token aaabbbcccddd",
  219. "Content-Type": "application/json",
  220. }
  221. output = self.app.post(
  222. "/api/0/test/git/alias/drop", headers=headers, data=data
  223. )
  224. self.assertEqual(output.status_code, 400)
  225. data = json.loads(output.get_data(as_text=True))
  226. self.assertDictEqual(
  227. data,
  228. {
  229. "error": "Branch not found in this git repository",
  230. "error_code": "EBRANCHNOTFOUND",
  231. },
  232. )
  233. def test_api_drop_git_alias(self):
  234. data = json.dumps({"alias_from": "main", "alias_to": "master"})
  235. headers = {
  236. "Authorization": "token aaabbbcccddd",
  237. "Content-Type": "application/json",
  238. }
  239. output = self.app.post(
  240. "/api/0/test/git/alias/drop", headers=headers, data=data
  241. )
  242. self.assertEqual(output.status_code, 200)
  243. data = json.loads(output.get_data(as_text=True))
  244. self.assertDictEqual(data, {})
  245. if __name__ == "__main__":
  246. unittest.main(verbosity=2)